<?php include("../Header/Connection.php"); ?>

<?php
    $user_ref_code = $_GET['user_ref_code'];
	$trans_code  = $_GET['trans_code'];
	$amount  = $_GET['amount'];
	$package_ref_code  = $_GET['package_ref_code'];
	
	
	$sql_currency = "SELECT *
	FROM currency_rate_setting 
	WHERE status='1'";
	$result_currency = $mysqli->query($sql_currency);
	$row_currency = $result_currency->fetch_assoc();
	$deposit_rm = $row_currency['deposit_rm'];	
	
	/*
	$amount_usd = $amount;
	$amount1 = $amount * $deposit_rm;
	$amount = $amount1.'.00';
	$amount_Pay = $amount1 * 100;
	$status = '100';
	*/

	$amount_usd = $amount/$deposit_rm;
	$amount_Pay = $amount * 100;
	$status = '100';

	$sql = "SELECT * 
	FROM user
	WHERE ref_code = '$user_ref_code'";
	$result = $mysqli-> query($sql);
	if ($result -> num_rows > 0)
	{
		$row = $result ->fetch_assoc();
		$name = $row['name'];
		$phone_number = $row['phone_number'];
		$email = $row['email'];
	}
	
	date_default_timezone_set("Asia/Kuala_Lumpur");
	$created_date = date('Y-m-d H:i:s');

	//Get User Info
	$user_name = $name;
	$user_phone_number = $phone_number;
	$user_email = $email;

	$userSecretKey = '9qtw0jfp-qeiq-sxqa-gjki-7ce1jc0kyzbg';
	$categoryCode = 'cwrx4ecx';
	$billName = 'UHM Package Payment';
	$billDescription = 'UHM Payment RM' .$amount. ' ';
	$billPriceSetting = '1';
	$billPayorInfo = '1';
	$billAmount = $amount_Pay ;
	$billReturnUrl = 'https://system.mygoldsdnbhd.com/System/Page/U/checkout_proceed_B_after_registration_package_toyyibpay.php';
	$billCallbackUrl = 'https://system.mygoldsdnbhd.com/System/Page/U/checkout_proceed_B_after_registration_package_toyyibpay.php';
	$billExternalReferenceNo = 'ft5yvgnv';
	$billTo = $user_name;
	$billEmail = $user_email;
	$billPhone = $user_phone_number;
	$billSplitPayment = '0';
	$billSplitPaymentArgs = '';
	$billPaymentChannel = '0';
	$billDisplayMerchant = '1';
	$billContentEmail = 'Thank You For Your Payment';
	$billAdditionalField = $json;
	$billChargeToCustomer = '1';


	$array2 = array("" => "");
	$json = json_encode($array2);

	//$array2 = array("add_input_1" => "Alamat", "add_input_2" => "Poskod", "add_input_3" => "Jalan");
	//$json = json_encode($array2);

	//Refer Create Bill athttps://toyyibpay.com/apireference/

	$some_data = array(
	'userSecretKey'=> $userSecretKey,
	'categoryCode'=> $categoryCode,
	'billName'=> $billName,
	'billDescription'=> $billDescription,
	'billPriceSetting'=> $billPriceSetting,
	'billPayorInfo'=> $billPayorInfo,
	'billAmount'=> $billAmount,
	'billReturnUrl'=> $billReturnUrl,
	'billCallbackUrl'=> $billCallbackUrl,
	'billExternalReferenceNo' => $billExternalReferenceNo,
	'billTo'=> $user_name,
	'billEmail'=> $user_email,
	'billPhone'=> $user_phone_number,
	'billSplitPayment'=> $billSplitPayment,
	'billSplitPaymentArgs'=> $billSplitPaymentArgs,
	'billPaymentChannel'=> $billPaymentChannel,
	'billDisplayMerchant'=> $billDisplayMerchant,
	'billContentEmail'=> $billContentEmail,
	//'billAdditionalField' => $json,
	'billChargeToCustomer'=> $billChargeToCustomer
	);  

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_URL, 'https://toyyibpay.com/index.php/api/createBill');  
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);

	$result = curl_exec($curl);
	$info = curl_getinfo($curl);  
	curl_close($curl);
	$obj = json_decode($result);
	echo $result;

	$replace1 = str_replace('[', '', $result);
	$replace = str_replace(']', '', $replace1);

	//$json = '{"BillCode":"hzcyugwg"}';

	$obj = json_decode($replace);
	$BillCode =  $obj->{'BillCode'}; 

	/*
	echo "<script type='text/javascript'>alert('Billcode : $BillCode');</script>";
	echo "<script type='text/javascript'>alert('User_ID : $User_ID');</script>";
	echo "<script type='text/javascript'>alert('name : $name');</script>";
	echo "<script type='text/javascript'>alert('email : $email');</script>";
	echo "<script type='text/javascript'>alert('phone_number : $phone_number');</script>";
	echo "<script type='text/javascript'>alert('amount : $amount');</script>";
	echo "<script type='text/javascript'>alert('status : $status');</script>";
	echo "<script type='text/javascript'>alert('created_date : $created_date');</script>";
	*/

	$query_payment  = " INSERT INTO payment
	(
	payment_code,
	trans_code,
	ref_code,
    reference,
	name,
	email,
	phone_number,
	amount,
	amount_myr,
	status,
	created_date
	)
	VALUES
	(
	'$BillCode',
	'$user_ref_code',
	'$user_ref_code',
	'$package_ref_code',
	'$name',
	'$email',
	'$phone_number',
	'$amount_usd',	
	'$amount',
	'$status',
	'$created_date'
	)";
	$res_payment = $mysqli->query($query_payment);

	if ($res_payment)
	{
		echo "<script type='text/javascript'> document.location='https://toyyibpay.com/$BillCode'; </script>";
	}
	else
	{
		echo "<script type='text/javascript'>alert('Fail');</script>";
		echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/pendaftaran/'; </script>";
		exit();
	}

  /*
  $some_data = array(
    'userSecretKey' => '2pd11az1-x0xr-tq1q-4u4k-7ivtwdb7q8eh',
    'billCode' => 'eqohom1q',
    'billpaymentAmount' => '2.00',
    'billpaymentPayorName' => 'Sumayyah',
    'billpaymentPayorPhone'=>'60197789876',
    'billpaymentPayorEmail'=>'sumayyah@gmail.com',
    'billBankID'=>'TEST0021'

  );  

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_URL, 'https://toyyibpay.com/index.php/api/runBill');  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);

  $result = curl_exec($curl);
  $info = curl_getinfo($curl);  
  curl_close($curl);
  $obj = json_decode($result);
  echo $result;
  */		

  ?>