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

Metro Railway Project in JSP Free Download

Metro Railway Project in JSP Free Download

jsp

 

As a Student Life we All Know That How our College and School Project is for Us. So here i share My One Project to All Us. It is Complete project of Metro Rail Created using JSP language. 

Hope You like it.

 

0

JavaScript Alternatives Method SetInterval and ClearInterval in Android

JavaScript Alternatives Method SetInterval and ClearInterval in Android

 
Javascript Setinterval

Hello Friends as We know Switching from Web Developers to Android we Stuck in Some Situation of Methods and How we use Such Methods.

Here i Give a simple Method of JavaScript Which is.

SetInterval = for Creating Task Multiple Times Until we Not Stop it.
Clearinterval = For Stoping SetInterval Task.

Lets Create a Demo page and see How it Works?


<!DOCTYPE html>
<html>
<head>
 <title>Set Interval and Clear interval in JavaScript</title>
</head>
<body>
<p>Example of Set interval and Clear interval</p>
<div id="data">
 
</div>
<script type="text/javascript">
 var myHtmlDiv=document.getElementById("data");
 var loop=0;
 var s=setInterval(function(){
  myHtmlDiv.innerHTML+='A Text After 1 seconds <br>';
 
  if(loop==10){
   //stop my setinterval function after 10 times if you want infinte times executing then remove the if condition means no clearinterval method
   clearInterval(s);
  }
  loop++;
 },1000);
</script>
</body>
</html>


Result :

Javascript Setinterval



Now Lets How We Use this Methods in Android?

So We Can use This Method Simply in Android by Using this Methods

 
      final Timer timer= new Timer();
        timer.scheduleAtFixedRate(new TimerTask(){
            @Override
            public void run(){
                testi++;
                if(testi==5){
                    Log.i("Complete","TaskComplete");
                    timer.cancel();
                }
                Log.i("tag", "A Kiss every 5 seconds");
            }
        },0,5000);
0