Simple PHP Class for Integrating Monnify Payment Gateway
Just Like Paystack, Monnify is another solution for online payments.
You should be aware of some API terminologies like endpoint, authorization header, and request types which I will be using in this post.
Our Logic
The Monnify class I created is easy to understand and use if your App’s payment logic follows the structure below.
Authorization ==> Initialize Transaction ==> Redirect to Checkout URL ==> (After payment is completed) ==> Redirect to Callback URL ==> Verify the Transaction ==> Update your Database.
Before you can initialize a transaction, you must have an Authorization Header present in your request following your data.
Authorization
According to their docs, you must login to get an access token by providing an authorization header containing your api key, a colon, and your secret key encoded in base64.
base64_encode(apiKey:secretKey).
You can find your apiKey, secretKey and contract code in the develoer section of your dashboard.
Now once you have logged in using the credentials, an access token will be generated and can be used to access their endpoints.
Here’s a Sample response
For any endpoint that specifies that it is protected by Oauth 2.0, your authorization header to access that endpoint should look like this:
Authorization: Bearer Access_Token
But if the endpoint is protected by a basic Authentication, your authorization header should look like this:
Authorization: Basic base64_encode(apiKey:secretKey)
Good news is that I included this in our Monnify PHP class, so you don’t need to worry about authorization.
Initialize Transaction
Now in their Docs which is available here, this endpoint allows you to initialize a transaction then redirect customers to the checkout url included in the response data.
This endpoint as they specified, is protected with OAuth 2.0 Bearer token.
When I tried to use this Authorization to initialize a transaction, it didn’t work but the basic authentication worked just fine.
Before you call the endpoints, you need to provide the transaction data to be initialized as JSON. But you don’t have to worry about this.
In the Monnify Class, you may provide your data as PHP array then when you call the initialize transaction method it will be converted to JSON using json_encode.
You should also generate unique payment References for every new transation.
You may combine series of PHP inbuilt function for uniqueness if you want to acheive a reference key that is soo unique.
For Example, you can add the First 3 letters of the customer’s name + uniqid() + time().
You should also provide a Callback URL for customers to be redirected to after payment for you to verify the status of their transaction.
Now you should note that the main reponse data you’re looking for, when accessing any of their endpoints, including the Initialize transaction endpoint is located in responseBody index.
So if you’re looking for a particular data, say paymentReference, you have to access it like so.
response["data"]["responseBody"]["paymentReference"]
You don’t have to worry about this, I have configured the Class to return the data from the reponseBody index.
Don’t forget to store the transactionReference and paymentReference to your DB to check the status of completed transactions
Redirect to Checkout URL
Once the transaction has been initialized, redirect customers to the checkout URL for payment to be made.
Just like the Image Above, the checkout url is located in
"responseBody": { "checkoutUrl" : "https://a.com" }
Using the Monnify class, you just need to pass in checkoutUrl since our response contains data from responseBody.
A typical checkout page will look like
Now depending on the payment methods you enabled, your checkout page will contain those methods and you can add more or remove some payment methods in your transaction initialization data.
Using the Monnify Class, to initialize a transaction, you need to pass in the data like so
$init = $monnify->initTrans($data)
Verify Transaction
The verification endpoint is protected using OAuth 2.0 and this means that you’ll need an access token to access it.
Remember that we have talked about generating an Access Token.
So to access the verification endpoint, our Authorization header should look like this
Authorization: Bearer Access_Token
Good News is that this works just fine!
Once your customers are redirected to your callback Url, the query parameter provided by default is the paymentReference that you had generated.
Now you need to query the transactionReference using that paymentReference you had stored in the Database, then call the verifcation endpoint and the reponse data will look like this.
Looking at the Data Above, you can check how much the customer paid, when the payment was made, payment status, method of payment used etc
Using the Monnify Class, to verify a transaction, you need to pass in the data like so
$init = $monnify->verifyTrans($transactionReference)
Once you are satisfied with the data, you may proceed to updating your Database and sending a mail to the customer that you have received their payment.
NOTES
- The
{{base url}}
for test ishttps://sandbox.monnify.com
but when you go live, it changes to the live url. - If for any reason, you wish to use the deprecated transaction verification endpoint, you need to provide Basic Authorization as the Authorization header. More details here
The Monnify PHP class is available at my Github Here.
Thank You!