<?php
require_once __DIR__ .'/vendors/autoload.php';

// use selcom client module
use Selcom\ApigwClient\Client;


@set_time_limit(0);

$destination = "logs/faic_mail" . date('Y-m-d') . ".log";

$apiKey='SHERIA-8Plj0rPnRhVUkUSa';
$apiSecret='87567523-6J21-4J13-aT85-a78c1b152161';
$SELCOM_VENDOR='TILL60452976';
$baseUrl='https://apigw.selcommobile.com';

$json = file_get_contents('php://input');
$obj = json_decode($json);

//error_log("\n" . date('Y-m-d H:i:s') . " Incoming Request :\n $json ", 3, $destination);

$api_key = $obj->{'api_key'};
$fullname = $obj->{'fullname'};
$email = $obj->{'email'};
$phonenumber = $obj->{'phonenumber'};
$amount = $obj->{'amount'};
$currency = $obj->{'currency'};
$webhook = $obj->{'webhook'};
$order_id = $obj->{'order_id'};

if($api_key=='YTc4YWY3ZWVlZWViODU3YTY5ODUzNTA4ZGU3YzVhYzM1NTdjNjM1MWEyYzA1ODU1ZDhlqA12AjkP0qNTc4ZjU3N2QwMDVmMw==')
{
    $client = new Client($baseUrl, $apiKey, $apiSecret);

  
        $orderMinArray = array(
            "vendor"=>$SELCOM_VENDOR,
            "order_id"=>$order_id,
            "buyer_email"=> $email,
            "buyer_name"=> $fullname,
            "buyer_phone"=> $phonenumber,
            "amount"=>  $amount,
            "currency"=>"TZS",
            "webhook"=>$webhook,
            "buyer_remarks"=>"None",
            "merchant_remarks"=>"None",
            "no_of_items"=>1
            );

            $orderMinPath = "/v1/checkout/create-order-minimal";

              
            // create order
            $response = $client->postFunc($orderMinPath,$orderMinArray);

            // Convert the array to a JSON string
            $jsonString = json_encode($response);

            $responseArray = json_decode($jsonString, true);
            $result_code = $responseArray['result'];
            if($result_code==="SUCCESS")
            {
                //Extract payment_gateway_url and result
                $paymentGatewayUrl = $responseArray['data'][0]['payment_gateway_url'];
                
                $reference = $responseArray['reference'];

                $client2 = new Client($baseUrl, $apiKey, $apiSecret);

                // data
                $walletPaymentArray = array(
                    "transid"=>$reference,
                    "order_id"=>$order_id,
                    "msisdn"=>$phonenumber,
                    );
                
                // path relatiive to base url
                $walletPaymentPath = "/v1/checkout/wallet-payment";
                
                // wallet payment
                $response2 = $client2->postFunc($walletPaymentPath,$walletPaymentArray);
                //print_r($response2);
                //die();
                
                $response=array(
                    "status"=>"200",
                    "statusDesc"=>$result_code,
                    "reference"=>$reference,
                    "paymentGatewayUrl"=>"none",
                    "order_id"=>$order_id
                );
            }
            else
            {
                $response=array(
                    "status"=>"201",
                    "statusDesc"=>$result_code      
                );
            }
            
            $response=json_encode($response);
}
else
{
    $response=array(
        "status"=>"201",
        "statusDesc"=>"Wrong Data"     
    );

    $response=json_encode($response);
}