<?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'};
$redirect_url = $obj->{'redirect_url'};
$cancel_url = $obj->{'cancel_url'};
$webhook = $obj->{'webhook'};
$order_id = $obj->{'order_id'};


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

    $orderArray = array(
        "vendor"=>$SELCOM_VENDOR,
        "order_id"=>$order_id,
        "buyer_email"=> $email,
        "buyer_name"=> $fullname,
        "buyer_phone"=> "255763038743",
        "gateway_buyer_uuid"=> "",
        "amount"=> $amount,
        "currency"=>$currency,
        "buyer_remarks"=> "None",
        "merchant_remarks"=> "None",
        "no_of_items"=> 1,
        "payment_methods"=>"CARD",
        "redirect_url"=>$redirect_url,
        "cancel_url"=>$cancel_url ,
        "webhook"=>$webhook,
        "billing.firstname" => $fullname,
        "billing.lastname" => $fullname,
        "billing.address_1" => "Office No.1105, 11th floor, Samora Tower Buildings",
        "billing.address_2" => "",
        "billing.city" => "Dar es Salaam",
        "billing.state_or_region" => "Tanzania",
        "billing.postcode_or_pobox" => "63",
        "billing.country" => "TZ",
        "billing.phone" => "255763038743"
        );
    
    // path relatiive to base url
    $orderPath = "/v1/checkout/create-order";
    
    // create order
    $response = $client->postFunc($orderPath,$orderArray);

    // 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'];

        $response=array(
            "status"=>"200",
            "statusDesc"=>$result_code,        
            "paymentGatewayUrl"=>$paymentGatewayUrl, 
            "reference"=>$reference, 
        );
    }
    else
    {
        $response=array(
            "status"=>"201",
            "statusDesc"=>$result_code      
        );
    }
    
    $response=json_encode($response);

}
else
{
    $response=array(
        "status"=>"201",
        "statusDesc"=>"Invalid API KEY"        
    );
    $response=json_encode($response);
    //error_log("\n" . date('Y-m-d H:i:s') . " Response :\n $response ", 3, $destination);
    
}


print_r($response);
die(); 
