Payumoney Transaction Status API

After integrating the Payumoney or Payu, we need to integrate the transaction status API as well so that we can check the user payment status from time to time with Curl.

This API will help us to update the status of those orders whose status is “Cancelled” at our end but “Payment Captured” at Payumoney end. Such situation sometimes arise when user closed the browser or his internet stopped working suddenly after making payment.

We can run the API every half an hour in Cron Job to check the payment status of last 20-30 orders depends upon the frequency of the orders and updates their status in case.

Below PHP code will help to integrate Transaction API :-


$key = 'gtKFFx';
$salt = 'eCwWELxi';

$command = "verify_payment";
$var1 =1000451451;
$hash_str = $key . '|' . $command . '|' . $var1 . '|' . $salt ;
$hash = strtolower(hash('sha512', $hash_str));
$r = array('key' => $key , 'hash' =>$hash , 'var1' => $var1, 'command' => $command);


$qs= http_build_query($r);
$wsUrl = "https://test.payu.in/merchant/postservice?form=2";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $wsUrl);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $qs);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$o = curl_exec($c);
if (curl_errno($c)) {
$sad = curl_error($c);
throw new Exception($sad);
}
curl_close($c);


$valueSerialized = @unserialize($o);
if($o === 'b:0;' || $valueSerialized !== false) {
print_r($valueSerialized);
}
$o = json_decode($o);

In above code, we are checking the status of one order but we will check the status of last few orders so we will add foreach loop to get last few orders and check their status and update at our end if status vary.


// Get last few orders (Example of Laravel code)
$orders = Order::where('payment_method','Payumoney')->take(30)->orderBy('id','DESC')->get();
$orders = json_decode(json_encode($orders));
foreach($orders as $order){
$key = 'gtKFFx';
$salt = 'eCwWELxi';
$command = "verify_payment";
$var1 = $order->id;
$hash_str = $key . '|' . $command . '|' . $var1 . '|' . $salt ;
$hash = strtolower(hash('sha512', $hash_str));
$r = array('key' => $key , 'hash' =>$hash , 'var1' => $var1, 'command' => $command);

$qs= http_build_query($r);
$wsUrl = "https://test.payu.in/merchant/postservice?form=2";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $wsUrl);
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $qs);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
$o = curl_exec($c);
if (curl_errno($c)) {
$sad = curl_error($c);
throw new Exception($sad);
}
curl_close($c);


$valueSerialized = @unserialize($o);
if($o === 'b:0;' || $valueSerialized !== false) {
print_r($valueSerialized);
}
$o = json_decode($o);
foreach($o->transaction_details as $key => $val){
if(($val->status=="success")&&($val->unmappedstatus=="captured")){
if($order->order_status == "Payment Cancelled"){
Order::where(['id' => $order->id])->update(['order_status' => 'Payment Captured']);
} else if($order->order_status == "Payment Fail"){
Order::where(['id' => $order->id])->update(['order_status' => 'Payment Captured']);
} else if($order->order_status == "New"){
Order::where(['id' => $order->id])->update(['order_status' => 'Payment Captured']);
}
}else{
if($order->order_status == "Payment Captured"){
Order::where(['id' => $order->id])->update(['order_status' => 'Payment Cancelled']);
} else if($order->order_status == "New"){
Order::where(['id' => $order->id])->update(['order_status' => 'Payment Cancelled']);
}
}
}
}
echo "cron job run successfully"; die;
}