<?php
if (!defined('ADDFUNDS')) {
    http_response_code(404);
    die();
}

$merchant_id = $methodExtras['merchantId'];
$secured_key = $methodExtras['merchantKey'];
$basket_id = rand(9,1882882827872);



$paymentAmount = from_to($currencies_array, $methodCurrency, $settings["site_base_currency"], $paymentAmount);

      
//errorExit($paymentAmount);
$tokenApiUrl = 'https://ipg1.apps.net.pk/Ecommerce/api/Transaction/GetAccessToken';

 $urlPostParams = sprintf('MERCHANT_ID=%s&SECURED_KEY=%s&TXNAMT=%s&BASKET_ID=%s&CURRENCY_CODE=%s', $merchant_id, $secured_key, $paymentAmount, $basket_id,"PKR");

 $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $tokenApiUrl);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $urlPostParams);
                curl_setopt($ch, CURLOPT_USERAGENT, 'CURL/PHP PayFast Example');
                $response = curl_exec($ch);
            
          //   errorExit($response);
             
                curl_close($ch);
                $payload = json_decode($response);
               
                $token = isset($payload->ACCESS_TOKEN) ? $payload->ACCESS_TOKEN : '';
                
               
  if ($token){
      
      
      $insert = $conn->prepare(
    "INSERT INTO payments SET
client_id=:client_id,
payment_amount=:amount,
payment_method=:method,
payment_mode=:mode,
payment_create_date=:date,
payment_ip=:ip,
payment_extra=:extra,
t_id=:t_id
",

);
$extras = array('access_token'=>$token);
$insert->execute([
    "client_id" => $user["client_id"],
    "amount" => $paymentAmount,
    "method" => $methodId,
    "mode" => "Automatic",
    "date" => date("Y.m.d H:i:s"),
    "ip" => GetIP(),
    "extra" => json_encode($extras),
    "t_id" => $basket_id,
]);


      $transaction_date = date('Y-m-d H:i:s', time());
      $data = array(
          'CURRENCY_CODE' => "PKR",
          'MERCHANT_ID' => $merchant_id,
          'MERCHANT_NAME' => 'Superior SMM',
          'TOKEN' => $token,
          'SUCCESS_URL' => site_url("payfast_callback") ,
          'FAILURE_URL' => site_url("payfast_callback") ,
          'CHECKOUT_URL' => site_url("payfast_callback") ,
          'CUSTOMER_EMAIL_ADDRESS' => $user['email'],
          'CUSTOMER_MOBILE_NO' => $user['telephone'],
          'TXNAMT' => $paymentAmount,
          'BASKET_ID' => $basket_id,
          'ORDER_DATE' => $transaction_date,
          'SIGNATURE' => 'fbsisin',
          'VERSION' => 'MERCHANT-CART-0.1',
          'TXNDESC' => 'Account Topup',
          'PROCCODE' => '00',
          'TRAN_TYPE' => 'ECOMM_PURCHASE',
          );
     
        
      $_SESSION['basket_id']= $basket_id;
      $checkoutURL = 'https://ipg1.apps.net.pk/Ecommerce/api/Transaction/PostTransaction';
      
                $redirectForm .= '<form method="POST" action="'.$checkoutURL.'" name="payfastCheckout">';
    foreach($data as $name => $value) {
        $redirectForm .= '<input type="hidden" name="'.$name.'" value="'.$value.'">';
    }
    $redirectForm .= '</form>
    <script type="text/javascript">
    document.payfastCheckout.submit();
        </script>';
					
                 

		errorExit('Your payment has been initiated you will be redirected soon'.$redirectForm);
			
	 $response["success"] = true;
    $response["message"] = "Your payment has been initiated and you will now be redirected to the payment gateway.";
    $response["content"] = $redirectForm;
				}else{
				    errorExit('Something went wrong');
				    
				}
?>