Php

How to integrate a payment gateway to your website

Intermediate 2 Hrs
view more posts

Payment Gateway has become a mandate these days if u need to make payments through websites. And if you are a freelancer it is necessary for you to know how to integrate one. When I began no one was there to guide me, so I had the wonderful opportunity to make a great number of mistakes and learn. Now that I have done lots of E-commerce and payment integration many people have asked me about how to do it so I thought i’ll write an article about it.

These days there are ‘n’ number of payment gateways offered by third party merchants, and then 100s of banks offering their own gateways. With so many variations it will be totally absurd to even count the number of APIs, leave alone explaining them individually. But the basic bread and butter of every API remains the same. If you know them then integrating any payment Gateway is very easy.

We assume that you have chosen your favorite Payment Gateway and have registered with them. On registering you are given your starter kit which has details like your Instance ID, Merchat ID, Hash Tag, Currency Code. Some of these details may vary according to your merchant but otherwise it is almost the same.

Payment Gateway integration is not as tough as we think. It is a series of form submitted using the ‘POST’ method. If you follow the details given in the API document provided by your merchant it will be as easy as slicing a piece of butter.

The 4 Steps

There are 4 main steps in any Payment gateway integration.

  1. Collection and generation of data.
  2. Confirmation of details.
  3. Submitting those details to your payment gateway.
  4. Returning the message from Payment Gateway after payment is made and storing them in a database.

Let me explain to you each step in details and the process involved. It’s actually not as complex as you think.

Step 1: Collection and generation of data.

Through out this tutorial we will imagine we are integrating payment gateway to a shopping website.

So now the user has chosen the article he/she wants to buy and have added it to the cart. When the click ‘Checkout’ the process of ‘Collection and generation of data’ starts. First you collect all the items added to the cart and their quantities. Then you ask for the billing address and other posting details. This is the ‘Collection’ process where you collect in the data to keep a record of the transactions.

Next comes generation of data. Based on the format followed by the company you generate a Reference Number or an Reference ID. This step is very important. Right from the start to finish this number will help you track the transaction process. Imagine a situation when the user comes to the page of confirming their details but then escapes from the page or because of poor internet connectivity gets disconnected. In such situation Reference ID helps you to track back and find at what stage the payment process was stopped.

Imagine a situation when the user comes to the page of confirming their details but then escapes from the page or because of poor internet connectivity gets disconnected. In such situation Reference ID helps you to track back and find at what stage the payment process was stopped.

Reference ID are also important as payment gateway too require an unique ID sent from our side so that they too can keep a record. So it is utmost important that you generate one as soon as the customer clicks on ‘Check Out’.

Using ‘POST’ method in HTML form you pass all the collected and generated information to the Confirmation Page. Till this step you can give any ‘name’ in your form elements like this.

<input type="text" name="some_fullName" />

But from the next step on you will have to adhere to the payment gateway terminologies given in their API document.

Step 2: Confirmation of details.

After collecting all the information on the product to be purchased and the billing address it is mandate to confirm them. We will again use a form with ‘readonly’ input fields to confirm the information.

It is advisable to let the user see the article they are going to pay for and the amount to be paid. Also confirming the address of delivery will avoid any hassles later. Though this step looks completely customer oriented and the one that can be avoided its actually not.

This step lays the base for us the developers to prepare the form to be submitted to the Payment Gateway. The Payment Gateway API lists down few specific required fields for them to make a successful payment. This includes the starter kit information given to you from the Payment Gateway merchant. Along with these they require few other details like specified in the below list. This is the reason we use forms and ‘readonly’ input fields to confirm message. Because while the data is sent via ‘POST’ the name attribute of the input fields can to be set to the ones specified in API of the Payment Gateway. An example is given below.

  • R: Required
  • C: Conditional
  • O: Optional
Html Field Name Field Requirement: R/C/O Comments
instance_id R The Instance ID given to you with the starter kit
mer_id R The Merchant ID given to you with the starter kit
transaction_type R The type of transaction to be made.
1001 = Sale, 1009 = Pre-Authentication
reference_id R The Reference ID created by you.

Here is an example of the ‘readonly’ input fields:

<input type="hidden" name="instance_id" />
<input type="text" name="reference_id" readonly="readonly" />
<!-- and so on... -->

Step 3: Submitting those details to your payment gateway.

As said before we use ‘POST’ method to pass all these information to the URL given to you by the Payment Gateway merchant in the starter kit. Make sure you pass in all the R: Required information, only then you the Payment Gateway will display the form with the fields to initiate the payment transfer. After this till the payment successfully completes all the process is taken care by the payment gateway. On successful completion or if an error occurs the payment gateway returns with a set of message codes again in the ‘POST’ method. We should make sure that we capture them and this where the next steps comes in.

Step 4: Returning the message from Payment Gateway after payment is made and storing them in a database.

Be it a successful or an unsuccessful transaction the payment gateway returns with ‘POST’ variable that carry information about the transaction. Mostly it is numbers which can interpreted. This is done again with the starter kit provided by the Payment Gateway. Each number returned by the Payment Gateway corresponds to a message. An example is shown below.

Status Code Description
2001 Initiated
2011 Capture Aborted by User
2012 3DS Start
2013 3DS Failed
2020 SUCCESS
2019 Failed
2053 Test Transaction

Along with this the Payment Gateway also give you a Transaction ID from their end. This is for you to match up the Reference ID you created with the Transcation ID provided by them and get the details. Make sure we capture all these information and store it up in the database for future reference. Without this is will be practically impossible to track any bug or issues with the payment process.

Make sure we capture all these information and store it up in the database for future reference. Without this is will be practically impossible to track any bug or issues with the payment process.

Conclusion

Payment Gateway integration is not as tough as we think. It is a series of form submitted using the ‘POST’ method. If you follow the details given in the API document provided by your merchant it will be as easy as slicing a piece of butter. So have fun a become a pro in integration Payment Gateway. Do let me know your success stories or your queries. Happy to help you.

Latest Comments

Leave a Reply

comment on this article