<?php include 'security.php';

ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');
?>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Secure Acceptance - Payment Form Example</title>
    <link rel="stylesheet" type="text/css" href="payment.css"/>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<style>
	.form_cntr{width: 100%;margin: auto;position: fixed;left: 0;right: 0;top: 0;bottom: 0;background-color:#fbfbfb;font-family: 'Poppins', sans-serif;}
	.header_cntr{width: 100%;height:60px;margin: auto;float: left;box-shadow:1px 1px 10px #ccc;background-color: #fff;text-align: center;}
	.header_cntr img{width:150px;height: auto;margin: auto;float: none;padding-top: 10px;}
	.total_cntr{width: 100%;height:50;margin: auto;float: left;background-color: #000;font-weight: 600;}
	.total_cntr1{width: 100%;height:20;margin: auto;float: left;background-color: #000;font-weight: 600;}
	.total_cntr h2{padding: 0;margin: 12px;color: #fff;font-weight:500;font-size: 20px;}
	.total_cntr .lft_cnt{width: 70%;height: auto;margin: auto;float: left;}
	.right_cnt{width: 30%;height: auto;margin: auto;float: left;text-align: right;}
	.prodct_cntr{width: 100%;height: auto;margin: auto;float: left;overflow-y: auto;}
	.prodct_inr{width:100%;height: auto;margin: auto;float:none;overflow: scroll;}
	.pr_row{width: 98%;height:80px;margin: auto;float: left;border-bottom: 1px solid #f2f2f2;background-color: #fff;padding: 1%;}
	.pr_img{width:20%;height: auto;margin: auto;float: left;background-color: #fff;height:80px;border-radius: 8px;}
	.pr_img img{width: 100%;height:auto;max-height: 100%;margin: auto;float: left;}
	.pr_desc{width:55%;height: auto;margin: auto;float: left;height:auto;}
	.pr_desc h3{font-size: 14px;font-weight: 500;width: 100%;margin: auto;float: left;text-align: left;line-height: 16px;}
	.pr_cnt{padding: 6px;}
	.spn_qp{width: 100%;height: auto;margin: auto;float: left;margin-top: 10px;}
	.spn_qp span{width: 50%;height: auto;margin: auto;float: left;text-align:left;font-size: 12px;}

	.pr_amt{width:25%;height: auto;margin: auto;float: left;height:80px;border-radius: 8px;text-align: right;font-size: 12px;font-weight: 800;color: #000;}
	.pr_amt h3{width:auto;padding: 10px;margin: auto;float:right;text-align: right;font-size: 12px;font-weight: 800;color: #000;line-height: 30px;}
	.footer_cntr{width: 100%;height: auto;margin: auto;float: left;position:absolute;bottom: 0;background-color: #fff;}
	.footr_cnt{width: 90%;height: auto;margin: auto;float:none;text-align: center;padding: 20px;}
	.footr_cnt #submit{width: 100%;height: 40px;border-radius: 8px;font-size: 14px;color: #fff;background-color: #000;color: #fff;font-weight: 400;}



</style>
</head>
<body>
<form id="payment_confirmation" action="https://testsecureacceptance.cybersource.com/pay" method="post">
<?php
    foreach($_REQUEST as $name => $value) {
        $params[$name] = $value;
    }
?>
<fieldset id="confirmation">
    <legend>Review Payment Details</legend>
    <div>
        <?php
        $prod =  $_POST['cart'];
$shipping_amount = $_POST['shipping_amount'];

$prod = str_replace(PHP_EOL, '', $prod);

$prod = json_decode($prod, true);
           if($prod){
          
          $grand_total = 0;
          foreach($prod as $product ){
              $grand_total = $grand_total + ((float) $product['price'] * (float) $product['qty']);
              
          }
          $grand_total=$grand_total+$shipping_amount;
    $uuid = uniqid();
    $refer = floor(microtime(true) * 1000);
    $myData = [];
    $myData["access_key"]="d58430d0cf86321db1fa5d59e36d5e75";
    $myData["profile_id"]="C3687FDA-4A0D-43FC-982D-F8D21BA31876";
    $myData["transaction_uuid"]=$uuid;
    $myData["signed_date_time"]=gmdate("Y-m-d\TH:i:s\Z");
    $myData["local"]= "en";
    $myData["transaction_type"]="sale";
    $myData["reference_number"]= floor(microtime(true) * 1000);;
    $myData["amount"]=$grand_total;
    $myData["currency"]="QAR";
    $myData["locale"]="en";
    $myData["signed_field_names"]="access_key,profile_id,transaction_uuid,signed_field_names,unsigned_field_names,signed_date_time,locale,transaction_type,reference_number,amount,currency";
    $myData["unsigned_field_names"]="";
    $myData["submit"]="Submit";
    $myData["merchant_defined_data3"]="YES";

        ?>
    </div>
</fieldset>
    <?php
        foreach($myData as $name => $value) {
            echo "<input type=\"hidden\" id=\"" . $name . "\" name=\"" . $name . "\" value=\"" . $value . "\"/>\n";
        }
        
        // $cartId = (isset($_POST['quoteId']))? $_POST['quoteId']: 0; // replace with actual quote ID
// $token = '2dhdhimvhb2eg5pczxquwhmh1e1v9x70'; // replace with actual token

// $baseUrl = 'https://www.ahmarket.com';
// $requestUrl = $baseUrl . '/rest/V1/carts/' . $quoteId;

// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $requestUrl);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
//     'Authorization: Bearer ' . $token,
//     'Content-Type: application/json'
// ]);

// $response = curl_exec($ch);

// if (curl_errno($ch)) {
//     echo 'Error: ' . curl_error($ch);
// } else {
//     $cartInfo = json_decode($response, true);
//     // process cart information as needed
//     print_r($cartInfo);
// }

// curl_close($ch);



// $isGuestCart = true; // set to false if the cart is a customer cart

// $requestUrl = $baseUrl . '/rest/V1/';
// if ($isGuestCart) {
//     $requestUrl .= 'guest-carts/';
// } else {
//     $requestUrl .= 'carts/';
// }
// $requestUrl .= $cartId;

// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $requestUrl);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
//     'Authorization: Bearer ' . $token,
//     'Content-Type: application/json'
// ]);

// $response = curl_exec($ch);

// if (curl_errno($ch)) {
//     echo 'Error: ' . curl_error($ch);
// } else {
//     $cartInfo = json_decode($response, true);
//     // process cart information as needed
//      //print_r($cartInfo);
// }

// // curl_close($ch);
//  $pData= $_POST['cart'];
// //var_dump(json_decode(json_decode("".$pData."", true), true));
//         $product_data = json_decode(file_get_contents('php://input'));; //json_decode($pData,true); //: [];
//       print_r($product_data);
//         echo json_decode("".$pData."");
        // Get the raw POST data
//$post_data = file_get_contents("php://input");

// Decode the JSON data into an array
// $data = json_decode( $_POST['cart'], true);

// // Access the data from the array
// echo $value = $data['cart'];
      if($prod){
          
          $grand_total = 0;
          foreach($prod as $product ){
              $grand_total = $grand_total + ((float) $product['price'] * (float) $product['qty']);
              echo $grand_total;
          }
        ?>
        
<div class="form_cntr">
	<div class="header_cntr">
		<img src="https://www.ahmarket.com/pub/media/wysiwyg/new_images/logo.png">
	</div>
	<div class="total_cntr">
		<div class="lft_cnt">
			<h2>Order Total:</h2>
		</div>
		<div class="right_cnt">
			<h2><?php echo $grand_total; ?></h2>
		</div>
	</div>
	<div class="total_cntr1">
		<div class="lft_cnt">
			<h6>Shipping Fees:</h6>
		</div>
		<div class="right_cnt">
			<h6><?php echo $shipping_amount == 0 ? "Free" : $shipping_amount; ?></h6>
		</div>
	</div>
	<div class="prodct_cntr">
		<div class="prodct_inr">

			
			<?php foreach($prod as $product ){   ?> 
			<!----product-row---->
			<div class="pr_row">
				<div class="pr_img">
					<img src="<?php echo "https://www.ahmarket.com/pub/media/catalog/product/".$product['image']; ?>">
				</div>
				<div class="pr_desc">
					<div class="pr_cnt">
						<h3><?php echo $product['title']; ?></h3>
						<span class="spn_qp">
							<span>Qty: <b><?php echo $product['qty']; ?></b></span>
							<span>Price: <b><?php echo $product['price']; ?></b></span>
						</span>
					</div>
				</div>
				<div class="pr_amt"><h3><?php echo (float) $product['price'] * (float) $product['qty']; ?></h3></div>
			</div>
				<!----product-row end---->
			<?php } ?>
		

		</div>
	</div>
	<div class="footer_cntr">
		<div class="footr_cnt">
			<button type="submit">
				Confirm Order
			</button>
		</div>
	</div>
</div>

<?php 
} // close of if $product_data  count
?>
      <?php  echo "<input type=\"hidden\" id=\"signature\" name=\"signature\" value=\"" . sign($myData) . "\"/>\n";
    ?>
    
	<div class="footer_cntr">
		<div class="footr_cnt">
            <input type="submit" id="submit" value="Confirm Order"/>
		</div>
	</div>
</form>
</body>
</html>
