Instamojo Payment Gateway Integration in PHP - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Instamojo Payment Gateway Integration in PHP - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, March 26, 2018

Instamojo Payment Gateway Integration in PHP

Instamojo Payment Gateway Integration in PHP

instamojo payment gateway 

Let's Start. 

How it Works?

how instamojo works


First I create a payment Form (form.php)

<!DOCTYPE html>
<html>
<head>
 <title>Instamojo payment </title>
</head>
<body>
 <form action="pay.php" method="post">
  Amount <input type="text" name="amount"><br>
  Purpose <input type="text" name="purpose"><br>
  Name <input type="text" name="buyer_name"><br>
  Email <input type="text" name="email"><br>
  phone <input type="text" name="phone"><br>
  <input type="submit" value="Pay">
 </form>

</body>
</html>



Now form submit page (pay.php)

<?php
//database connection
$con=mysql_connect("localhost","root","");
if($con){
    echo "Database Host Connected
";
}
else{
    echo mysql_error();
    die();
}
$ch2=mysql_select_db("payment",$con);
if($ch2){
    echo "Database Connected
";
}
else{
    echo mysql_error();
    die();
}

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://test.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:YOUR_API_KEY",
                  "X-Auth-Token:YOUR_AUTH_KEY"));
$payload = Array(
    'purpose' => $_REQUEST['purpose'],
    'amount' => $_REQUEST['amount'],
    'phone' => $_REQUEST['phone'],
    'buyer_name' => $_REQUEST['buyer_name'],
    'redirect_url' => 'https://postlocaldata.000webhostapp.com/post.php',
    'send_email' => true,
    'webhook' => 'https://postlocaldata.000webhostapp.com/post.php',
    'send_sms' => true,
    'email' => $_REQUEST['email'],
    'allow_repeated_payments' => false
);
//===note =   instamojo does not localhost link in redirect_url so i use live then its tranfer the post data to localhost url 

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

//echo $response;
$payment=json_decode($response,true);
if($payment['success']==true){
    echo "success";
    //Payment Data
    $successdata=$payment['payment_request'];

    $qr=mysql_query("INSERT INTO `payment`(`id`, `phone`, `email`, `buyer_name`, `amount`, `purpose`, `status`) VALUES ('".$successdata['id']."','".$successdata['phone']."','".$successdata['email']."','".$successdata['buyer_name']."','".$successdata['amount']."','".$successdata['purpose']."','".$successdata['status']."')");
    if($qr){
        echo "Inserted
";
        echo "<script>location='".$successdata['longurl']."'</script>";
    }
    else{
      echo mysql_error();
      die();
    }
}
else{
    echo "failed to create order";
}
//===================test card=======================
//=======no. 4242 4242 4242 4242========================
//========cvv 111 ======================
// ============ date = any future dat =========================
//===========code 1221 =========================

?>
  
Now Payment Response Page (payresponse.php)
 
<?php
//print_r($_REQUEST);
$con=mysql_connect("localhost","root","");
if($con){
    echo "Database Host Connected
";
}
else{
    echo mysql_error();
    die();
}
$ch2=mysql_select_db("payment",$con);
if($ch2){
    echo "Database Connected
";
}
else{
    echo mysql_error();
    die();
}

$pay_id=$_REQUEST['payment_id'];
$req=$_REQUEST['payment_request_id'];
//print_r($_REQUEST);
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://test.instamojo.com/api/1.1/payments/'.$pay_id.'/');
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:YOUR_API_KEY",
                  "X-Auth-Token:YOUR_AUTH_KEY"));

$response = curl_exec($ch);
curl_close($ch); 

$json=json_decode($response,TRUE);
if($json['success']==true){
 echo "payment done";
 $status=$json['payment'];
 $up=mysql_query("UPDATE `payment` SET `status`='".$status['status']."' WHERE id='".$req."'");
}
else{
 echo "payment failed";
}

Live Server (post.php) for data transfer to localhost. 

<?php
echo "<form id='forms' action='http://localhost/payment/payresponse.php'  method='post'>";
foreach ($_REQUEST as $key => $value) {
 echo '<input type="hidden" name="'.$key.'" value="'.$_REQUEST[$key].'">';
}
echo "<form>";
?>
<script type="text/javascript">
 document.getElementById("forms").submit(); 
</script>

Databse Table payment.sql
 
CREATE TABLE `payment` (
  `id` text NOT NULL,
  `phone` text NOT NULL,
  `email` text NOT NULL,
  `buyer_name` text NOT NULL,
  `amount` text NOT NULL,
  `purpose` text NOT NULL,
  `status` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  

Important Links :
For Docs : https://docs.instamojo.com/docs/get-payment-details
for Live Post Request to localhost : http://postlocaldata.000webhostapp.com/post.php
for Test Login :  https://test.instamojo.com/integrations


Video Tutorial

Thanks.

7 comments:

  1. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    Embedded systems training in coimbatore
    sql training in coimbatore

    ReplyDelete
  2. Thanks this code is working, thanks for saving my time.

    ReplyDelete
  3. in payresponse.php

    //echo $response;
    $json=json_decode($response,TRUE);
    if($json['success']==true){

    echo "Payment successful!";


    }
    else{
    echo "Payment failed!";
    }


    I am getting echo payment successful even when payment fails.

    ReplyDelete
  4. All payment systems involve some costs (including cash). Both consumers and merchants are likely to seek to use lower cost payments if they can. This is especially the case if they can readily know what the use of each payment will cost them (sometimes this is transparent and sometimes it is not of course).소액결제현금화

    ReplyDelete
  5. You have a real talent for writing unique content. I like how you think and the way you express your views in this article. I am impressed by your writing style a lot. Thanks for making my experience more beautiful. 소액결제현금화

    ReplyDelete
  6. We are a specialized Laravel Development Company, which offers the best travel Website & Application Services. We assure effective and quality work . +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete

Post Top Ad