Ajoft is a software development company which develops software’s for small to mid sized business to increase their professionalism, productivity and profitability. Recently our team has added support for instamojo payment gateway in our hotel reservation software. You can download the Instamojo payment gateway patch for Ajoft Hotelare from download section of the members area.
Instamojo is one of the best payment gateway of India helping more than 40,000+ business to accept payments online. Payment gateways are one of the most important ingredient when you develop eCommerce portal. Instamojo provides both live mode as well as test mode to test your payment gateway integration before it goes live. Its always advised to do test integration first and then change it to live mode. This tutorial caters to the needs of PHP developers, however, integration in other programming languages are more or less the same.
For perfectly completing the integration of Instamojo payment gateway you will need below details:
Card Number – 4242 4242 4242 4242 Expiry Date – 01/20 CVV – 111 Second Factor Authentication Code – 1221
Instamojo Payment gateway has a very straightforward way of accepting payments and getting paid. Its one of the easiest payment gateway available out there.
Payment on Instamojo is a 3 step process
After payment, customer is redirected back to your app/website.
Integrating Instamojo is a simple 3 step process.
1: Create A payment Request
Instamojo is extremely simple. Once you are registered & approved on Instamojo live website, you can receive payments right away. Instamojo provides you a personalized url which looks something like https://www.instamojo.com/@username/ (username is your instamojo username), you can use this url to send it to your clients and they can fill in the purpose and amount which they want to pay and they can pay right away.
Now, incase your application needs a customized payment url with prefilled amount and purpose you would need to integrate Instamojo API. Instamojo payments work by creating payment request URLs which can accept payments. Creating a request is as simple as passing an amount
and a purpose
. This step generates a Payment Request ID.Amount and Purpose are mandatory parameters needed to generate payment request ID.
Once a request is created, you can use the request URL to accept a payment in your mobile app, website or application.
Using the Payment Request ID you can display a payment form to your customers. Your customer can choose a payment method of their choice and make a payment. Once a payment is made, you will receive a Payment ID.
Get the final amount from your cart total and call the Create a Request endpoint of instamojo API. The values of fields like amount
, buyer_name
, email
, and phone
are straight-forward an they are optional. The values of send_email
and send_sms
should be false
if you don’t want users to get email and sms from instamojo.
The purpose
field can be used to pass the custom data (unique identifier) created on your end. This is going to be useful later on to identify the order using this custom data (unique identifier) for which a particular payment was made. You can send invoice id, transaction id or any unique id which determines your order. This variable can be manipulated later on success page.
Also it must be remembered that there can be multiple payments ID for a single payment request ID, if customer has made payments multiple times.
How to generate Instamojo Payment Request ID & URL?
Generating payment request url is pretty simple. PHP code example of generating payment request url is given below:
include 'config.php'; include 'src/instamojo.php'; //These variable can be fetched from post data or from database or from session, totally depends on your requirements $purpose = $_POST["purpose"]; $amount = $_POST["amount"]; $name = $_POST["name"]; $phone = $_POST["phone"]; $email = $_POST["email"]; $api = new Instamojo\Instamojo($private_key, $private_auth_token,$api_url); try { $response = $api->paymentRequestCreate(array( "purpose" => $purpose, "amount" => $amount, "buyer_name" => $name, "phone" => $phone, "send_email" => true, "send_sms" => true, "email" => $email, 'allow_repeated_payments' => false, "redirect_url" => "http://your_website.com/success.php", //success page where gateway should redirect after payment,should always be an absolute url "webhook" => "http://your_website.com/webhook.php" //notification page where instamojo sends payment gateway response, should always be an absolute url )); //print_r($response); $pay_ulr = $response['longurl']; header("Location: $pay_ulr"); exit(); } catch (Exception $e) { print('Error: ' . $e->getMessage()); }
In the code above, you see few parameters are passed when creating a payment request, we explain the Payment Request Creation Parameters below:
Required
Optional
How to show the payment response on success page?
Once the payment has been been made, instamojo redirects you to the redirect url. Now you have to show the success or failure message to the user so that they can know the current status of their payment and order.
include 'config.php'; include 'src/instamojo.php'; $api = new Instamojo\Instamojo($private_key, $private_auth_token,$api_url); $payid = $_GET["payment_request_id"]; try { $response = $api->paymentRequestStatus($payid); echo "Payment ID: " . $response['payments'][0]['payment_id'] . "
" ; echo "Payment Name: " . $response['payments'][0]['buyer_name'] . "
" ; echo "Payment Email: " . $response['payments'][0]['buyer_email'] . "
" ; print_r($response); } catch (Exception $e) { print('Error: ' . $e->getMessage()); }
How to manage & utilize webhook url?
Webhook url is one of the best way to receive payment notifications to the website admins or log payment responses from payment gateway.
include 'config.php'; $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'); } //if(LOG_MODE){ // if(LOG_TO_FILE){ $myFile = 'payment_instamojo.log'; $fh = fopen($myFile, 'a') or die('can\'t open file'); /// } $log_data .= $nl.$nl.'=== ['.date('Y-m-d H:i:s').'] ==================='.$nl; $log_data .= '
---------------
'.$nl; $log_data .= '
POST
'.$nl; foreach($_POST as $key=>$value) { $log_data .= $key.'='.$value.'
'.$nl; } $log_data .= '
---------------
'.$nl; $log_data .= '
GET
'.$nl; foreach($_GET as $key=>$value) { $log_data .= $key.'='.$value.'
'.$nl; } // } // 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 the <>. $mac_calculated = hash_hmac("sha1", implode("|", $data), $private_salt); if($mac_provided == $mac_calculated){ echo "MAC is fine"; // Do something here if($data['status'] == "Credit"){ // Payment was successful, mark it as completed in your database $to = $admin_email; $subject = 'Website Payment Request ' .$data['buyer_name'].''; $message = "Payment Details
"; $message .= "
"; $message .= 'ID: '.$data['payment_id'].'
'; $message .= 'Amount: '.$data['amount'].'
'; $message .= "
"; $message .= 'Name: '.$data['buyer_name'].'
'; $message .= 'Email: '.$data['buyer'].'
'; $message .= 'Phone: '.$data['buyer_phone'].'
'; $message .= "
"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; // send email mail($to, $subject, $message, $headers); } else{ // Payment was unsuccessful, mark it as failed in your database } } else{ echo "Invalid MAC passed"; } // if(LOG_MODE){ $log_data .= '
'.$nl.$message.'
'.$nl; // if(LOG_TO_FILE){ fwrite($fh, strip_tags($log_data)); fclose($fh); // } //}
You can download the entire code for this tutorial using the link below.
[indeed-social-locker sm_list=’fb,tw,li,’ sm_template=’ism_template_1′ sm_list_align=’horizontal’ sm_display_counts=’false’ sm_display_full_name=’true’ unlock_type=2 locker_template=2 sm_d_text=’
Share This Page To Unlock The Content!
‘ ism_overlock=’default’ ] https://www.ajoft.com/development/integration-of-instamojo-payment-gateway-php-web-applications.jsp [/indeed-social-locker]
Incase you want us to integrate Instamojo payment gateway on your website as per your requirement, you can click this link to buy instamjo payment gateway integration service.