Paystack Subscription with PHP

Jeremy Ikwuje
5 min readAug 2, 2020

In this guide, you’ll learn how to implement Paystack Subscription with PHP.

Partner content: If you want to know the dollar to naira rate of popular virtual cards, you can subscribe for free on monierate.com.

Let’s get started.

Paystack is a modern payment system for charging customers in Africa.

PHP is currently the most popular programming language used to developed dynamic website applications.

A subscription allows a customer to pay a certain (mostly fixed) amount of money on a regular interval. This interval can be daily, weekly, monthly, quarterly, and even annually.

When you combine these three, you create power.

This is fantastic.

Step 1: Create A Plan

The first step in setting up a subscription using Paystack is to create a plan.

A Plan is where you set the amount, interval and currency in which the subscription will be paid. Every subscription from a customer has to be on a Plan.

And there are two ways you can create a Plan:

1. From your Paystack Dashboard
2. or with PHP using the Create Plan endpoint

If you’ve already created a plan, just skip to step 2: Adding Plan code to subscription or Create A Subscription.

For most applications, it is 5X easier to create a plan on the dashboard.

However, this guide is based on PHP. So we will be creating plan programmatically.

Create a Plan with PHP

This can be done with cURL on PHP. cURL is a library that lets you make HTTP requests in PHP.

To do this, you will have to send a request to the Create Plan endpoint:

https://api.paystack.co/plan

The URL (endpoint) above requires we send some $_POST data alongside.

{    "name" : "Pro Hosting",    "interval" : "monthly",    "amount" : 100000}

The name is the name of the Plan you want the user to subscribe to, the interval is the period of their subscription, and the amount they pay per subscription.

Simple, right?

Now, here is a code that will do the above without error:

You can copy that code and run it.

Just replace the SECRET_KEY with your paystack secret key. You can find it on your Dashboard.

An important thing you shouldn’t miss, when you create a subscription Plan under TEST mode on your dashboard or with TEST SECRET KEY and you go live, you won’t have access to that plan and Vice Versa.

So it is best you create plan same plan on both live and test mode so you interchange depending on which mode you’re currently on.

What Response did you get after making the above request

When you run the code above and a plan is created, the response — a JSON contains a plan code that will be used to create a subscription for customers:

Successful Paystack Response from Create Plan endpoint

As you can see above, when a plan is created, the response contains a plan code, and this is what you will use to create a subscription as you will see later.

The Plan interval we have are daily, weekly, monthly, quarterly and annually.

Setting Invoice Limit

You can also set invoice limits when creating a subscription plan.

Invoice limit determines how many times you want to charge a user for the Plan you created.

You do that by adding invoice_limit => 6in the cURL request.

invoice_limt is set to 6

By setting the invoice_limit to 6 in the request above, your customer will be charged only 6 times. This is ideal if you want customers to pay in instalments.

Paystack is a blast!

Step 2: Creating a Subscription

Now that you’ve created a Paystack subscription Plan, the next step is to implement a subscription.

There are two ways you can easily create a subscription per user.

  1. Adding the Plan code when a user wants to make a transaction
  2. Using the Create Subscription API endpoint

Adding Plan Code to a transaction

Simply add the Plan code when Initializing a transaction on your application.

To initialize a transaction, we need the amount , email , and the plan .

Endpoint:

https://api.paystack.co/transaction/initialize

Request body:

{    "email" : "ikwuje24@gmail.com",    "plan" : "PLN_ijhi17fybuszv8f",    "amount" : 10000}

The initialization will be authorized before a user is taken to Paystack checkout to make a payment through the authorization_url.

Paystack Initialize response when authorization url has been created

Here is a PHP implementation of the above:

The amount should be the same amount you set when creating the plan because Paystack will override this amount with what you set on the plan.

Please: Since you set plan during initialization, the customer will be charged based on the amount you set when creating the Plan.

However, there are special cases where you want to charge users of your app a different amount during registration — may be to test their card, but the amount set on the Plan should be charge later.

… that’s where the Create Subscription Endpoint is ideal.

So here we go.

Create Subscription Endpoint

For this, you only need a customer code and the plan code. A customer code can be obtained after they made a first successful transaction[LINK].

Endpoint:

https://api.paystack.co/subscription

Request body:

{    "customer" : "CUS_vb704ng2ubir0kf",    "plan" : "PLN_ijhi17fybuszv8f"}

When you make the request, this will result in a similar response below:

A Paystack successful subscription response

From the response above, it clearly indicates that the customer has subscribed to the Plan. N100 (10000 Kobo) will be deducted from their account monthly.

Here is a PHP implementation using cURL:

Ah hah!

You have successfully seen how you can implement Paystack Subscription with PHP.

Go ahead and continue building great products with Paystack.

Recommended Reading

How to Integrate Paystack Payment with PHP

Paystack Subscription API

Jeremy Ikwuje
Jeremy Ikwuje

Written by Jeremy Ikwuje

Software Engineer. I write on technology. Follow on Twitter @jeremyikwuje

Responses (1)

Write a response