<?php

include("config.php");



if ($_SERVER["REQUEST_METHOD"] === "POST") {  
    if (empty($postData)) {
        //echo "No POST data received!\n";
    } else {
        
        $postData = $_POST;
        $order_id = $postData["orderId"];
        $order_id_encrpyted = $postData["OrderIDEncrypted"];
        $order_session_id = $postData["SessionId"];
        $xmlString = $postData["xmlmsg"];
        

        // Convert XML string to object
        $xml = simplexml_load_string($xmlString);
        
        if ($xml === false) {
            //echo "Error parsing XML!";
            exit();
        }

        // Extract OrderStatus from XML
        $orderStatus = (string) $xml->OrderStatus;
        $OrderDescription=(string) $xml->OrderDescription;

        if ($orderStatus === "APPROVED") {
            
          $db_id=trim(str_replace("Order no","",$OrderDescription));
           $status="COMPLETE";
           
           $response=json_encode($postData);
        
        mysqli_query($Conn_db,"UPDATE `api_response` SET `paymentmethod_response`='".mysqli_real_escape_string($Conn_db,$response)."', `transaction_id`='".$order_id."', `transaction_status`='".$status."' WHERE `id`='".$db_id."'");
        
			echo "<script>window.location.href='".BC_URL."checkout'</script>";
			exit();
        }
    }
}

function verify_order($session_id,$order_id)
{
    $xml = '<?xml version="1.0" encoding="UTF-8"?>
        <TKKPG>
            <Request>
                <Operation>GetOrderInformation</Operation>
                <Language></Language>
                <Order>
                    <Merchant>NABIL106511</Merchant>
                    <OrderID>' . $order_id . '</OrderID>
                </Order>
                <SessionID>' . $session_id . '</SessionID>
                <ShowParams>true</ShowParams>
                <ShowOperations>true</ShowOperations>
                <ClassicView>true</ClassicView>
            </Request>
        </TKKPG>';

    
    //echo strlen($xml);
    
    $headers = array(
        "Content-type: text/xml",
        "Content-length: " . strlen($xml),
        "Connection: close",
    
    );
    
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    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_SSLCERT, $cert_file);
    curl_setopt($ch, CURLOPT_SSLKEY, $key_file);*/
    //curl_setopt($ch, CURLOPT_CERTINFO, true);
     // If the private key is password-protected
    
    
    $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>";*/
    
        $order_status=$xmlResp->Response->Status;
    
        if($order_status==00)
        {
            // payment successs
    
            $order_id=$xmlResp->Response->Order->row->OrderID;
            $Orderstatus=$xmlResp->Response->Order->row->Orderstatus;
    
            if(trim($Orderstatus)=="APPROVED")
            {
                echo "APPROVED";
                
    
            }
            else if(trim($Orderstatus)=="CREATED")
            {
                 echo "CREATED";
    
            }
            
            echo '<script>window.location = "'.BC_URL.'";</script>'; 
    
        }
        else{
            echo '<script>window.location = "'.BC_URL.'";</script>'; 
        }   
    
    }
}

?>
