<?php
session_start();
session_regenerate_id();
require_once('../system/function.php');	
//if($_SERVER["REQUEST_METHOD"]=="POST"){
$arr = array("status"=>'PENDING', "message"=>'Payment Processing');	
//print_r($_SESSION);
if(isset($_SESSION['txn_ref_id']) && 
!empty($_SESSION['upi_id']) && 
!empty($_SESSION['client_orderid']) && 
!empty($_SESSION['cust_Mobile']) && 
!empty($_SESSION['cust_Email']) && 
!empty($_SESSION['muid']) && 
!empty($_SESSION['auth_token'])){
	
$txn_ref_id = $_SESSION['txn_ref_id'];
$upi_id = $_SESSION['upi_id'];	
$client_orderid = $_SESSION['client_orderid'];	
$cust_Mobile = $_SESSION['cust_Mobile'];	
$cust_Email = $_SESSION['cust_Email'];		
$txnNote = $_SESSION['txnNote'];
$muid = $_SESSION['muid'];	
$auth_token = $_SESSION['auth_token'];	
$a = sha1($upi_id);
$skey = substr($a, 10, 5)."-".substr($a, 5, 5)."-".substr($a, 20, 5)."-".substr($a, 15, 5);
	
$JsonData = json_encode(array("MID"=>$muid,"ORDERID"=>$txn_ref_id));
$r = curl_get("https://securegw.paytm.in/order/status?JsonData=$JsonData","");	
$res = json_decode($r,true);	

if($res['STATUS']=="TXN_SUCCESS" && $res['MID']==$muid && $res['ORDERID']==$txn_ref_id){
           
$json_string = json_encode(array(
"txnStatus"=>'TXN_SUCCESS', 
"resultInfo"=>$res['RESPMSG'],
"orderId"=>$client_orderid,
"txnAmount"=>$res['TXNAMOUNT'],
"bankTxnId"=>$res['TXNID'],
"paymentMode"=>$res['PAYMENTMODE'],
"txnDate"=>$res['TXNDATE'],
"utr"=>$res['BANKTXNID'],
"sender_vpa"=>$cust_Mobile,
"sender_note"=>$txnNote,
));	

    
$hash = hash_encrypt($json_string, $skey); 
$arr = array("status"=>'SUCCESS', "message"=>'Transactions Successfully', "hash"=>$hash);	
//session_destroy();

unset($_SESSION['muid']);
unset($_SESSION['auth_token']);
unset($_SESSION['txn_ref_id']);
unset($_SESSION['upi_id']);
unset($_SESSION['client_orderid']);	
unset($_SESSION['txnAmount']);	
}    
}
echo json_encode($arr);
?>