<?php 
    /* coded by Rahul Barui ( https://github.com/Rahul-Barui ) */
    ob_start();
    session_start();
    include "config.php";
    
    $session_id = session_id();

	/*print "<pre>";
	print_r($_POST);
	var_dump($_POST);
	die;*/

	$payment_id = $statusMsg = ''; 
	$ordStatus = 'error';
	$id = '';

	// Check whether stripe token is not empty

	if(!empty($_POST['stripeToken'])){

		// Get Token, Card and User Info from Form
		$token = $_POST['stripeToken'];
		$name = $_POST['holdername'];
		$email = $_POST['email'];
		$card_no = $_POST['card_number'];
		$card_cvc = $_POST['card_cvc'];
		$card_exp_month = $_POST['card_exp_month'];
		$card_exp_year = $_POST['card_exp_year'];

		// Get Product ID From - Form
		//$productId = $_POST['productId'];

		// Get Product Details By Using Product-Id
		//$SQL_getPr = "SELECT * FROM `products` WHERE `id`='$productId'";
	    //$res_getPr = mysqli_query($con,$SQL_getPr) or die("MySql Query Error".mysqli_error($con));
	    //$row_getPr = mysqli_fetch_assoc($res_getPr);
	    //$price = $row_getPr['price'];
	    //$pr_desc = $row_getPr['name'];
	    
	    $arrMyOrder = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `og_orders` WHERE `unique_id` = '".$session_id."'"));
		
		$grand_total = $arrMyOrder['grand_total'];

		// Include STRIPE PHP Library
		require_once('stripe-php/init.php');

		// set API Key
		$stripe = array(
		"SecretKey"=>"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",
		"PublishableKey"=>"pk_live_51PzytaKWxdfw0CwsnYIqJrMeuzoj1bHdSx85jhgnlTFpOaWAwOYN9DlD14MvKBXGtP5OuknTvgOa2vQriZl1eGpZ00rpqpw7vS"
		);
		/*$stripe = array(
		"SecretKey"=>"sk_test_cEjXShRlZO1YrzP8Txn6Ulya00EhUj1dRS",
		"PublishableKey"=>"pk_test_18lgnnPV3SZZn36tyAFO131T00P2pCl90m"
		);*/

		// Set your secret key: remember to change this to your live secret key in production
		// See your keys here: https://dashboard.stripe.com/account/apikeys
		\Stripe\Stripe::setApiKey($stripe['SecretKey']);

		// Add customer to stripe 
	    $customer = \Stripe\Customer::create(array( 
	        'email' => $email, 
	        'source'  => $token,
	        'name' => $name,
	        'description'=>"Product"
	    ));

	    // Generate Unique order ID 
	    $orderID = strtoupper(str_replace('.','',uniqid('', true)));
	     
	    // Convert price to cents 
	    $itemPrice = ($grand_total*100);
	    $currency = "gbp";
	    $itemName = "Product";

	    // Charge a credit or a debit card 
	    $charge = \Stripe\Charge::create(array( 
	        'customer' => $customer->id, 
	        'amount'   => $itemPrice, 
	        'currency' => $currency, 
	        'description' => $itemName, 
	        'metadata' => array( 
	            'order_id' => $orderID 
	        ) 
	    ));

	    // Retrieve charge details 
    	$chargeJson = $charge->jsonSerialize();

    	// Check whether the charge is successful 
    	if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1){ 

	        // Order details 
	        $transactionID = $chargeJson['balance_transaction']; 
	        $paidAmount = $chargeJson['amount']; 
	        $paidCurrency = $chargeJson['currency']; 
	        $payment_status = $chargeJson['status'];
	        $payment_date = date("Y-m-d H:i:s");
	        $dt_tm = date('Y-m-d H:i:s');

	        // Insert tansaction data into the database

	        //$sql = "INSERT INTO `orders`(`name`,`email`,`card_number`,`card_exp_month`,`card_exp_year`,`item_name`,`item_number`,`item_price`,`item_price_currency`,`paid_amount`,`paid_amount_currency`,`txn_id`,`payment_status`,`created`,`modified`) VALUES ('$name','$email','$card_no','$card_exp_month','$card_exp_year','$itemName','','$itemPrice','$currency','$paidAmount','$paidCurrency','$transactionID','$payment_status','$dt_tm','$dt_tm')";
	        //mysqli_query($con,$sql) or die("Mysql Error Stripe-Charge(SQL)".mysqli_error($con));

    		//Get Last Id
    		/*$sql_g = "SELECT * FROM `orders`";
    		$res_g = mysqli_query($con,$sql_g) or die("Mysql Error Stripe-Charge(SQL2)".mysqli_error($con));
    		while($row_g=mysqli_fetch_assoc($res_g)){
    			$id = $row_g['id'];
    		}*/
    		$_SESSION['TRANSACTION_ID'] = $transactionID;

	        // If the order is successful 
	        if($payment_status == 'succeeded'){ 
	            $ordStatus = 'success'; 
	            //$statusMsg = 'Your Payment has been Successful!';
	            
	            $sqlUpdate = "UPDATE `og_orders` SET `transaction_id` = '".$transactionID."',`transaction_completed` = 'Yes',`payment_status` = 'Paid' WHERE `unique_id` = '".$session_id."'";
                $exeUpdate = mysqli_query($connection,$sqlUpdate);
	            
	            echo '<script>window.location.href="payment-success.php";</script>';
	    	} else{ 
	            //$statusMsg = "Your Payment has Failed!"; 
	            echo '<script>window.location.href="payment-failed.php";</script>';
	        } 
	    } else { 
	        //print '<pre>';print_r($chargeJson); 
	        //$statusMsg = "Transaction has been failed!";
	        echo '<script>window.location.href="payment-failed.php";</script>';
	    } 
	} else{ 
	    //$statusMsg = "Error on form submission."; 
	    echo '<script>window.location.href="payment-error.php";</script>';
	} 
	
?>