Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: instamojo

Web Development and Design | Tutorial for Java, PHP, HTML, Javascript: instamojo
Showing posts with label instamojo. Show all posts
Showing posts with label instamojo. Show all posts

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.
0