Instamojo Payment Gateway Integration in PHP


Instamojo is one of the easiest payment gateway to integrate in PHP. It gives your user option to pay with Cards, NetBanking and even Wallets.

After you set up an account with Instamojo, you will get an Api-Key, Auth-Token and Salt.

First Step is to pass the required data to the Instamojo API through POST method.

These are the data to be passed:

  • amount – The amount to be paid by the customer.
  • phone – Phone number of the customer.
  • buyer_name – Customer name.
  • purpose – Purpose of the payment.
  • email – Email address of the customer.

Here’s is the sample code:

Pass the Api-Key and Auth-Token here.


if((isset($_POST['amount'])) && (!empty($_POST['amount']))){
 $amount = $_POST['amount'];
 $purpose = $_POST['amount'];
 $phone = $_POST['phone'];
 $buyername = $_POST['buyername'];
 $email = $_POST['email'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
$payload = Array(
 'purpose' => $purpose,
 'amount' => $amount,
 'phone' => $phone,
 'buyer_name' => $buyername,
 'redirect_url' => '',
 'webhook' => '',
 'send_email' => true,
 'send_sms' => true,
 'email' => $email,
 'allow_repeated_payments' => false

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
$response = curl_exec($ch);

$data = json_decode($response,true);
header('HTTP/1.1 301 Moved Permanently');

} else {


In webhook.php, pass the Salt.

Basic PHP script to handle Instamojo RAP webhook.

$data = $_POST;
$mac_provided = $data['mac']; // Get the MAC from the POST data
unset($data['mac']); // Remove the MAC key from the data.
$ver = explode('.', phpversion());
$major = (int) $ver[0];
$minor = (int) $ver[1];
if($major >= 5 and $minor >= 4){
 ksort($data, SORT_STRING | SORT_FLAG_CASE);
 uksort($data, 'strcasecmp');
// You can get the 'salt' from Instamojo's developers page(make sure to log in first):
// Pass the 'salt' without <>
$mac_calculated = hash_hmac("sha1", implode("|", $data), "<yourSaltHere>");
if($mac_provided == $mac_calculated){
 $payment_id = $data['payment_id'];
 $status = $data['status'];
 $amount = $data['amount'];
 $purpose = $data['purpose'];
 if($data['status'] == "Credit"){

 // Payment was successful, mark it as successful in your database.
 // You can acess payment_request_id, purpose etc here. 

 // Payment was unsuccessful, mark it as failed in your database.
 // You can acess payment_request_id, purpose etc here.
 echo "MAC mismatch";

Here based on the data you can update status in database and redirect the user to corresponding urls.

All the instruction will be available in

Thanks for reading. Happy Coding.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s