<!DOCTYPE html>
<html>
<?php session_start();?>
<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
    <link rel = "stylesheet" href = "css/airmob.css?<?=time();?>">
    <link rel = "stylesheet" href = "css/step.css?<?=time();?>">
    <link rel = "stylesheet" href = "css/switch.css?<?=time();?>">
    <title>Airmob</title>
</head>
<body>

<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
<br>
<div style='text-align:center; color:#245ba5; margin-bottom:50px;'><h1>Paiement par Carte</h1></div>

    <br>
    <div class="container">
    <div class="row">


    <div class="col-md-12">
    <?php
    error_reporting(E_ALL);
    ini_set("display_errors", 0);

    include_once ("./lib/gestion.lib.php");
    $MyCde  = new ClassCdeV2();

    $token  = $_POST['stripeToken'];
    $email  = $_POST['email'];
    $name   = $_POST['name'];
    $amount = $_POST['amount'];
    $description = $_POST['description'];

    $Customer = $MyCde->APIStripe([ 'action' => 'customers',
        'data'   => ['source' => $token,'name'=>$name,'description'=>$description,'email'=>$email]]);

    //  DEBUG : affichage
    //    echo "<pre>"; print_r($Customer); echo "</pre>";


    //  Email de confirmation vers AIRMOB
    $sendMail = false;
    $idu      = $_SESSION['AIRCMDV2']['idunique'];
    //  [KO] erreurs de la banque
    if( $Customer->error->code ) {
        $Color       = "#d9534f"	;
        $Msg         = "Paiement refusé";
        $CodeRefus   = $Customer->error->code;
        $RaisonRefus = $Customer->error->decline_code;
    }

    //  [OK] Création de customer
    elseif( $Customer->id  ) {
        // Paiement
        $Paiement = $MyCde->APIStripe([ 'action' => 'charges',
                                        'data'   => ['amount' => $amount,'currency'=>'eur','description'=>$email, 'customer'=>$Customer->id,'metadata'=>['BC###']]]);

        if ($Paiement->paid==1 && !$Paiement->error) {
            $Color  = "#5cb85c"	;
            $Msg    = "Paiement accepté";
            $sendMail = true;
        } else {
            $Color      = "#d9534f"	;
            $Msg        = "Paiement echec";
            $CodeRefus   = $Paiement->error->code;
            $RaisonRefus = $Paiement->error->message."<br>".$Paiement->error->type;
        }

    }
    // [KO] echec, deja passé, reload etc...
    else {
        $Color      = "#d9534f"	;
        $Msg        = "Paiement echec";
        $CodeRefus  = "Impossible de continuer !";
    }


    $MyCde->PaiementCmd($Msg,$Customer);
//  Email de confirmation vers AIRMOB
    if ($sendMail) {
        if ($idu) include "etatcmdv2.cron.php";
    }
    ?>
        <div class="card">
            <div class="card-header">
                Airmob : Votre paiement.
            </div>
            <div class="card-body">
                <h4 class="card-title" style="color:<?=$Color?>;"><b><?=$Msg?></b></h4>
                 <?php if ($CodeRefus) {?>
                    <p class="card-text">Raison du refus :<br>[<?=$CodeRefus."] ".$RaisonRefus?></p>

                <?php } else { ?>
                     <p class="card-text">Nous avons bien enregistré votre réglement...</p>
                <?php } ?>
                <br>
                <div style="text-align:center;"><a href="#" class="btn btn-suivant" Onclick="javascript:location=('http://pro.airmob.eu/')">Accueil</a></div>
            </div>
        </div>

    <?php

    //die('DEBUG : initialisation stop !');

    unset($_SESSION['airmob']);
    //die('Reset commande !');
    //echo "paiement interrompu !"; exit;
    //  DEBUG : affichage
    //echo "<pre>"; print_r($_SESSION['airmob']); echo "</pre>";
    //echo "<h1>==> ".$Customer->id."</h1>";
    //echo "<pre>"; print_r($Paiement); echo "</pre>";
    exit;

    ?>
    </div>

    </div>
    </div>

</body>
</html>