<?php

session_start();


include "connection.php";

//$APP_ID = $_SESSION['id'];



$strJsonFileContents = file_get_contents("config.json");
$array = json_decode($strJsonFileContents, true);
// print_r($array);exit;
$amount = $_GET['amount'];
// $invoice = "INVAP228ID "; // must be unique
$invoice = "INV_APP_ID_".$_GET['APP_ID']; // must be unique
$intent = "sale";
$proxy = $array["proxy"];
    $createpaybody=array('amount'=>$amount, 'currency'=>'BDT', 'merchantInvoiceNumber'=>$invoice,'intent'=>$intent);   
    $url = curl_init($array["createURL"]);

    $createpaybodyx = json_encode($createpaybody);

    $header=array(
        'Content-Type:application/json',
        'authorization:'.$_SESSION['token'],
        'x-app-key:'.$array["app_key"]
    );

    curl_setopt($url,CURLOPT_HTTPHEADER, $header);
	curl_setopt($url,CURLOPT_CUSTOMREQUEST, "POST");
	curl_setopt($url,CURLOPT_RETURNTRANSFER, true);
	curl_setopt($url,CURLOPT_POSTFIELDS, $createpaybodyx);
    curl_setopt($url,CURLOPT_FOLLOWLOCATION, 1);
    //curl_setopt($url, CURLOPT_PROXY, $proxy);
    
    $resultdata = curl_exec($url);
    curl_close($url);
//     echo '{
//     "paymentID": "F7Z7HUR1645071215849",
//     "createTime": "2022-02-17T04:13:35:937 GMT+0000",
//     "orgLogo": "https://s3-ap-southeast-1.amazonaws.com/merchantlogo.sandbox.bka.sh/pgw-default.png",
//     "orgName": "TestCheckoutDemoMerchant1",
//     "transactionStatus": "Initiated",
//     "amount": "5",
//     "currency": "BDT",
//     "intent": "authorization",
//     "merchantInvoiceNumber": "Inv0124"
// }';
    echo $resultdata;

    $logData = [
        "API Title" => "Create Payment API",
        "API URL" => stripslashes($array["createURL"]),
        "Request Timestamp" => date('Y-m-d\TH:i:s.Z')."Z",
        "Header" => $header,
        "Body" => json_decode($createpaybodyx, true),
        "API Response" => json_decode($resultdata, true)
    ];

    writeLog("bKash", $logData);

    function writeLog($logName, $logData){
        file_put_contents("Logs/".$logName."_".date("Y-m-d_H").".json","\n============== ".date("Y-m-d H:i:s")." ==============\n".json_encode($logData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), FILE_APPEND);
    }
    

?>
