<!DOCTYPE html>
<html>
<?php
/* ex STRIPE = create.php */
require 'vendor/autoload.php';
// This is a sample test API key. Sign in to see examples pre-filled with your key.

// CODE:STRIPE
\Stripe\Stripe::setApiKey('sk_test_LYUyWDDA0kqNYa2p8sn3eKpH00f7x40bgb');
//\Stripe\Stripe::setApiKey('sk_live_ip8gbLpdBR6wdSxNBoOhBkY800rMBGob94');

function calculateOrderAmount(array $items): int {
    // Replace this constant with a calculation of the order's amount
    // Calculate the order total on the server to prevent
    // customers from directly manipulating the amount on the client
    return $items[0]['montant'];
}

header('Content-Type: application/json');
try {
    // retrieve JSON from POST body
    $json_str = file_get_contents('php://input');
    $json_obj = json_decode($json_str);
    $Arr      = json_decode($json_str,TRUE);

    /*
    $customer = \Stripe\Customer::create([
        'name'=>'luc001',
        'email'=>'luc001@test.fr'
    ]);

    */
    $customer = \Stripe\Customer::create(['description'=>$Arr['items']['0']['nobdc']]);
    /*

    */
    $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => ($Arr['items']['0']['montant']),
        'currency' => 'eur',
        'description'=>$Arr['items']['0']['nobdc']." ".$Arr['items']['0']['nom'],
        'customer' => $customer->id,
        'setup_future_usage' => 'off_session',
        'metadata' => ['NoBDC' => $Arr['items']['0']['nobdc'],'code_promo'=>'C0001'],
    ]);

    $output = [
        'clientSecret' => $paymentIntent->client_secret,
        'idc'=>$customer->id,
    ];

    // $customer->id permet de connaitre le cus_

    echo json_encode($output);



} catch (Error $e) {
    http_response_code(500);
    echo json_encode(['error' => $e->getMessage()]);
}