Instamojo Payment Gateway Integration in PHP

Hello,

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.

<?php

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, 'https://www.instamojo.com/api/1.1/payment-requests/');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,
 array("X-Api-Key:yourApiKeyHere",
 "X-Auth-Token:yourAuthTokenHere"));
$payload = Array(
 'purpose' => $purpose,
 'amount' => $amount,
 'phone' => $phone,
 'buyer_name' => $buyername,
 'redirect_url' => 'https://www.yourwebsitename.com/webhook.php',
 'webhook' => 'https://www.yourwebsitename.com/webhook.php',
 '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);
curl_close($ch); 


$data = json_decode($response,true);
var_dump($data);
$site=$data["payment_request"]["longurl"];
header('HTTP/1.1 301 Moved Permanently');
header('Location:'.$site); 

} else {
 header("Location:payment.php");
}

?>

In webhook.php, pass the Salt.

<?php
/*
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);
}
else{
 uksort($data, 'strcasecmp');
}
// You can get the 'salt' from Instamojo's developers page(make sure to log in first): https://www.instamojo.com/developers
// 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. 
 }
 else{

 // Payment was unsuccessful, mark it as failed in your database.
 // You can acess payment_request_id, purpose etc here.
 }
}
else{
 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 https://www.instamojo.com/developers/

Thanks for reading. Happy Coding.