<?php

$cert_file  = $_SERVER['DOCUMENT_ROOT'].'/giftmandu/nabil_bank/giftmandu.com.crt';
$key_file   = $_SERVER['DOCUMENT_ROOT'].'/giftmandu/nabil_bank/private_key_for_csr.key';

if($currency=="NPR")
{
    $order_currency=524;
    
}
else
{
    $order_currency=840;
    
}

    $order_amount=$amount*100;

	$xml ='<?xml version="1.0" encoding="UTF-8"?>
	<TKKPG>
	<Request><Operation>CreateOrder</Operation>
	<Language>EN</Language>
	<Order>
	<OrderType>Purchase</OrderType>
	<Merchant>'.NABIL_MERCHANT.'</Merchant>  
	<Amount>'.$order_amount.'</Amount> -- for Rs 1, you need to pass 100
	<Currency>'.$order_currency.'</Currency>  -- 524 for NPR  or 840 USD
	<Description>Order no '.$api_response_id.'</Description> -- this is the unique id from your system
	<ApproveURL>'.NABIL_SUCCESS_URL.'</ApproveURL> 
	<CancelURL>'.NABIL_CANCEL_URL.'</CancelURL> 
	<DeclineURL>'.NABIL_DECLINE_URL.'</DeclineURL> 
	<Fee>0</Fee>
	</Order>
	</Request>
	</TKKPG>';

/*
echo "payload:",$xml;*/
$headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($xml),
    "Connection: close"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,NABIL_API_ENDPOINT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  /for disabling curl certificate issue
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // disabling curl certificate issue
curl_setopt($ch, CURLOPT_SSLCERT, $cert_file);
curl_setopt($ch, CURLOPT_SSLKEY, $key_file);
//curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $cert_password);


$data = curl_exec($ch);
//curl_close($ch);

/*echo $data;*/

if (!$data) {
    echo "Curl Error: " . curl_error($ch);
} else {
    $xmlResp = simplexml_load_string($data);
   /* echo "<pre>";
    print_r($xmlResp);
    echo "</pre>";*/

    $payment_status=$xmlResp->Response->Status;

    if($payment_status==00)
    {
        // payment successs

        $order_id=$xmlResp->Response->Order->OrderID;
        $order_session_id=$xmlResp->Response->Order->SessionID;

        $payment_url=$xmlResp->Response->Order->URL;

        echo '<script> window.location="'.$payment_url.'?OrderID='.$order_id.'&SessionID='.$order_session_id.'"</script>';
    }
    else{
       // echo "payment page url is not generated";
        //echo $data;
    }
}
?>