<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
validate_admin();
if(isset($_POST['payment']) && $_POST['payment']=='Make Payment'){
	//print_r($_POST); die;
	$error='';
	$paymentmsg='';
	$shopperid=$_SESSION['sess_admin_id'];
	
if($_POST['orderid']!='' && $_POST['orderamount']!='' && $_POST['orderamount']>0){
	$ordercheck=getFieldWhere('id','insta_transacttion','orderid',$_POST['orderid']);

	if(empty($ordercheck)){
	$orderid=mysqli_real_escape_string($GLOBALS['conn'],$_POST['orderid']);
	$orderamount=mysqli_real_escape_string($GLOBALS['conn'],number_format($_POST['orderamount'],2,'.',''));
	$shopper_remarks=mysqli_real_escape_string($GLOBALS['conn'],$_POST['remarks']);
	
	if(SITE_URL==MAINSITE_URL){
	include("include/bt-production.php");
	$mastermerchntid="MyValue365Inc_marketplace";
	}else{
	include("include/bt-sandbox.php");
	$mastermerchntid="myvalue365";
	}
	$vaultuserid='QS'.$_SESSION['sess_admin_id'];
	
	$result = Braintree_Transaction::sale(array(
		'orderId' => $orderid,
		'amount' => $orderamount,
		'merchantAccountId' => $mastermerchntid,
		'customerId' => $vaultuserid,
		'options' => array(
		'submitForSettlement' => true
		),
		//'serviceFeeAmount' => round($servicefee, 2)
		));
		$request_array = array(
		'orderId' => $orderid,
		'amount' => $orderamount,
		'merchantAccountId' => $mastermerchntid,
		//'paymentMethodNonce' => $nonce,
		'customerId' => $vaultuserid,
		'options' => array(
		'submitForSettlement' => true
		),
		//'serviceFeeAmount' => round($servicefee, 2)
		);
		
		debug_logs(json_encode($request_array));
		debug_logs(json_encode($result), 'response');
	
	if ($result->success) {
		$txn = $result->transaction;
		$transactionid=$txn->id;
		
		$obj->query("insert into insta_transacttion set orderid='$orderid', transaction_id='$transactionid', transaction_date=now(),transaction_status='1', shopper_id='$shopperid',order_amount='$orderamount', shopper_remarks='$shopper_remarks'");	
		
		
		//$obj->query("update insta_order_itmes set shopper_payment_status=1 where order_id='$orderid'");
		$paymentmsg="Payment Completed!";
		$orderid=$orderamount=$shopper_remarks='';
		
		} else {
		$obj->query("insert into insta_transacttion set orderid='$orderid', transaction_id='$transactionid', transaction_date=now(),transaction_status='0', shopper_id='$shopperid',order_amount='$orderamount', shopper_remarks='$shopper_remarks'");
		
		//$obj->query("update insta_order_itmes set shopper_payment_status=0 where order_id='$orderid'");
		$paymentmsg="Payment Failed! Try Again!";
		
		} 
	}else{
	$error = "Already paid for this order.";
}
}else{
	$error = "One or more fields are empty!";
}

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
	<div class="row">
		<div class="col-md-12 mt-5">
		<h3 style="text-align: center;background: green;padding: 10px;color: #fff;border-radius: 3px;"> Instacart Order Payment</h3>
		
		<div align="center" style="font-weight: 600; font-size: 16px;">
		<?php if(!empty($error)){ echo "<div class='text-danger'><br>Error : ".$error."</div>"; } ?>
		<?php if(!empty($paymentmsg)){ echo "<div class='text-danger'><br>".$paymentmsg."</div>"; } ?>
	</div>
			<form name="order-payment" action="" enctype="multipart/form-data" method="post">
			<div class="form-group"><br>
				<label>Order ID : </label>
				<input type="text" class="form-control" name="orderid" value="<?php echo $orderid; ?>" required>
				</div>
				<div class="form-group">
				<label>Order Amount ($) : </label>
				<input type="text" class="form-control" name="orderamount" value="<?php echo $orderamount; ?>" required>
				</div>
				<div class="form-group">
				<label>Remarks : </label>
					<textarea class="form-control" name="remarks" rows="5"><?php echo $shopper_remarks; ?></textarea>
				</div>
				<div class="form-group" align="center">
				<input type="submit" class="btn btn-success" name="payment" value="Make Payment">
				</div>
			</form>
		</div>
	</div>
</div>
<?php
	$error='';
	$paymentmsg='';
function debug_logs($string, $type='request')
	{
		$fp = fopen(__DIR__."/logs_instacart/". date("Ymd").".txt", "a+");
		fwrite($fp, date("Y-m-d H:i:s") . "--------------------" . $type . " start ---------------------\n");
		fwrite($fp, date("Y-m-d H:i:s") . " " . $string . "\n");
		fwrite($fp, date("Y-m-d H:i:s") . "--------------------" . $type . " end ---------------------\n");
		fclose($fp);
	}
	?>
</body>
</html>
