<?php

		$paymenta = $db->prepare("SELECT ad,deger FROM payment");

    $paymenta->execute(array());

    $paymenta = $paymenta->fetchAll(PDO::FETCH_ASSOC);



$a=0;

while(isset($paymenta[$a])){

	$payment[$paymenta[$a]["ad"]]=$paymenta[$a]["deger"];

	$a++;

}
	$balance=htmlentities($_GET["mik"], ENT_QUOTES, "UTF-8");
	$pay=htmlentities($_GET["pay"], ENT_QUOTES, "UTF-8");

		$productName = "Payment";  
$productID = $pay;  
$productPrice = round($balance,2); 
$currency = "try"; 


define('STRIPE_API_KEY', $payment["stripe_api"]); 
define('STRIPE_PUBLISHABLE_KEY', $payment["stripe_publishable"]); 

define('STRIPE_SUCCESS_URL', $site."?callback=Stripe"); //Payment success URL 
define('STRIPE_CANCEL_URL', $site."?sip=0");

    require_once __ROOT__.'/system/class/stripe/vendor/stripe/stripe-php/init.php'; 
 
$key=md5($productID.date("Y-m-d")."basariliixsgam");

// Set API key 
\Stripe\Stripe::setApiKey(STRIPE_API_KEY); 
 
$response = array( 
    'status' => 0, 
    'error' => array( 
        'message' => 'Invalid Request!'    
    ) 
); 
 
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $input = file_get_contents('php://input'); 
    $request = json_decode($input);     
} 
 
if (json_last_error() !== JSON_ERROR_NONE) { 
    http_response_code(400); 
    echo json_encode($response); 
    exit; 
} 

if(!empty($request->createCheckoutSession)){ 
    // Convert product price to cent 
    $stripeAmount = round($productPrice*100, 2); 
/*	
[ 
            'line_items' => [ 
              'price_data' => [ 
                    'product_data' => [ 
                        'name' => $productName, 
                        'metadata' => [ 
                            'pro_id' => $pay 
                        ] 
                    ], 
                    'unit_amount' => $stripeAmount, 
                    'currency' => $currency, 
                ], 
                'quantity' => 1, 
                'description' => $productName, 
            ],
            'mode' => 'payment', 
            'success_url' => STRIPE_SUCCESS_URL.'&is='.$key."&key=".$productID, 
            'cancel_url' => STRIPE_CANCEL_URL, 
        ] */

    // Create new Checkout Session for the order 
    try { 
        $checkout_session = \Stripe\Checkout\Session::create([ 
  'line_items' => [[
    'price_data' => [
      'currency' => $currency,
      'unit_amount' => $stripeAmount,
      'product_data' => [
        'name' => $productName,
        'description' => $productName,
                        'metadata' => [ 
                            'pro_id' => $pay 
                        ] 
      ],
    ],
    'quantity' => 1,
  ]],
            'mode' => 'payment', 
            'success_url' => STRIPE_SUCCESS_URL.'&is='.$key."&key=".$productID, 
            'cancel_url' => STRIPE_CANCEL_URL, 
        ]); 
    } catch(Exception $e) {  
        $api_error = $e->getMessage();  
    } 
  
    if(empty($api_error) && $checkout_session){ 

$tarih=date("Y-m-d H:i:s");
$pkey=$checkout_session->payment_intent;
 $settings = $db->prepare("UPDATE siparis SET pkey=? where id=?");
$settings->execute(array($pkey,$pay));
        $response = array( 
            'status' => 1, 
            'message' => 'Checkout Session created successfully!', 
            'sessionId' => $checkout_session->id 
        ); 
    }else{ 
        $response = array( 
            'status' => 0, 
            'error' => array( 
                'message' => 'Checkout Session creation failed! '.$api_error    
            ) 
        ); 
    } 
} 

echo json_encode($response); 
?>