<?php

	require_once("../connection.php");
	$key=mysqli_real_escape_string($connect, $_POST['key']);
	$bank=mysqli_real_escape_string($connect, $_POST['bank']);


	$content 	= trim(file_get_contents("php://input"));
	$decoded 	= json_decode($content, true);

	$tanggal	= gmdate("Y-m-d", time()+60*60*7);

	
	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$dt=mysqli_fetch_array($datatoko);

	if ($jumlah > 0){
	
	$date		= gmdate("dis");
	
	$nama = $dt['name_store'];
	$arr = explode(' ', $nama);
	$singkatan = "";
	foreach($arr as $kata)
	{
	$singkatan .= substr($kata, 0, 1);
	}
	
		$kode= strtoupper($singkatan);

	
	$no_upgrade	= "$kode-$dt[id_store]$date";
	$payment_amount = $m['nominal_upgrade'];

	

    $merchantCode = 'D5712'; // from duitku
    $merchantKey = '624955ab73915c5755560e2f5a1a3208'; // from duitku
    $paymentAmount = $payment_amount;
    $paymentMethod = "$bank"; // VC = Credit Card
    $merchantOrderId = time() . ''; // from merchant, unique
    $productDetails = 'Upgrade Kasir Toko';
     $email = $m['email']; // your customer email
    $phoneNumber = $m['no_telp']; // your customer phone number (optional)
    $additionalParam = ''; // optional
    $merchantUserInfo = ''; // optional
    $customerVaName = $m['nama_lengkap']; // display name on bank confirmation display
    $callbackUrl = 'https://kasirtoko.online/callback.php'; // url for callback
    $returnUrl = 'https://kasirtoko.online/api/pages/terimakasih.php'; // url for redirect
    $expiryPeriod = 10; // set the expired time in minutes
    $signature = md5($merchantCode . $merchantOrderId . $paymentAmount . $merchantKey);

    

    $item1 = array(
        'name' => 'Test Item 1',
        'price' => $payment_amount,
        'quantity' => 1);
		
    $itemDetails = array(
        $item1
    );

   $params = array(
        'merchantCode' => $merchantCode,
        'paymentAmount' => $paymentAmount,
        'paymentMethod' => $paymentMethod,
        'merchantOrderId' => $merchantOrderId,
        'productDetails' => $productDetails,
        'additionalParam' => $additionalParam,
        'merchantUserInfo' => $merchantUserInfo,
		'customerVaName' => $customerVaName,
        'email' => $email,
        'phoneNumber' => $phoneNumber,
        'itemDetails' => $itemDetails,
        'callbackUrl' => $callbackUrl,
        'returnUrl' => $returnUrl,
        'signature' => $signature,
	'expiryPeriod' => $expiryPeriod
    );
    $params_string = json_encode($params);
    //echo $params_string;
    //$url = 'https://sandbox.duitku.com/webapi/api/merchant/v2/inquiry'; // Sandbox
    $url = 'https://passport.duitku.com/webapi/api/merchant/v2/inquiry'; // Production
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',                                                                                
        'Content-Length: ' . strlen($params_string))                                                                       
    );   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    //execute post
    $request = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if($httpCode == 200)
    {
        $result = json_decode($request, true);
		//header('location: '. $result['paymentUrl']);
		//echo "paymentUrl :". $result['paymentUrl'] . "<br />";
	mysqli_query($connect,"UPDATE users SET code = '$result[reference]' WHERE id_session='$key'");
		
        ?>
        <script>alert('Anda akan diarahkan kehalaman pembayaran');
		 var seconds = 1
setTimeout(() => {
   window.location ="<?php echo $result['paymentUrl'];?>";
}, seconds * 1000)</script>

   <?php }
    else
        echo $httpCode;

	}else{	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
	);
    echo json_encode($json);	
	}

?>