
<center><h2><strong>Ubuntu</strong></h2>
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php session_start();?>
<!doctype html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <!-- Google Tag Manager -->
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
                new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
            j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
            'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','GTM-KP74Q3L');</script>
    <!-- End Google Tag Manager -->
    <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 data-rsssl=1>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KP74Q3L"  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<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>
<?php
    error_reporting(E_ALL);
    ini_set("display_errors", 0);

// Valide depuis client.js
// FIXME : je retire && $_REQUEST['noc']
    if( isset($_REQUEST['ok']) ) {
        $NewPaiementCB = true;
    } else {
        $NewPaiementCB = false;
    }
//
    if($_REQUEST['FTTH_contact']) {
        $H1 ="Demande enregistrée.";
        $Confim = "";
    } else {
        $H1 ="Paiement";
        $Confim = "STRIPE";
    }
?>

    <div style='text-align:center; color:#245ba5; margin-bottom:50px;'><h1><?=$H1?></h1></div>
    <br>
    <div class="container">
    <div class="row">
        <?php
        if($Confim=="STRIPE"){?>
            <div class="col-md-12">
                <?php
                include_once ("./lib/gestion.lib.php");
                $MyCde       = new ClassCdeV2();
                $sendMail    = false;
                $email       = $_POST['email'];
                $name        = $_POST['name'];
                $amount      = $_POST['amount'];
                $description = $_POST['description'];

                /*
                 * --------------------------
                 * MODE CB
                 * --------------------------
                 * */
                /*
                if($_REQUEST['mode']=="CB") {
                    $token  = $_REQUEST['stripeToken'];

                    $Customer = $MyCde->APIStripe(['action' => 'customers',
                                                   'data'   => ['source' => $token, 'name' => $name, 'description' => $description, 'email' => $email]]);

                    $idu      = $_SESSION['AIRCMDV2']['idunique'];
                }
                */
                /*
                 * --------------------------
                 * MODE SEPA
                 * --------------------------
                 * */
                if($_REQUEST['mode']=="SEPA") {
                    // R2cupère le id du client
                    $token  = $_REQUEST['stripeSource'];
                    $Customer = $MyCde->APIStripe(['action' => 'customers',
                                                   'data'   => ['source' => $token, 'name' => $name, 'description' => $description, 'email' => $email]]);

                    $idu      = $_SESSION['AIRCMDV2']['idunique'];

                    // Array
                    //    echo "<pre style='background: #00ff00'>"; print_r($Customer); echo "</pre>";
                }

                /*
                 * --------------------------
                 * RETOUR STRIPE
                 * --------------------------
                 * */


                //  [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  ) {
                    $nobdc = $MyCde->NoBDC();
                    /*
                    if($_REQUEST['mode']=="CB"  ) {
                        $CodeMode   = "Paiement";
                        $data       = ['amount' => $amount,'currency'=>'eur','description'=>$email, 'customer'=>$Customer->id,'metadata'=>['NoBC'=>$nobdc,'code_promo'=>$description]];
                        $MsgValide  = "Nous avons bien enregistré votre règlement et vous remercions pour votre commande.";
                    }
                    */
                    if($_REQUEST['mode']=="SEPA") {
                        $CodeMode   = "SEPA";
                        $data       = ['amount' => $amount,'currency'=>'eur','description'=>$email, 'customer'=>$Customer->id,'source'=>$token,'metadata'=>['NoBC'=>$nobdc,'code_promo'=>$description]];
                        $MsgValide  = "Nous avons bien enregistré votre opération et vous remercions pour votre commande.";
                    }

                    // Paiement
                    $Paiement = $MyCde->APIStripe([ 'action' => 'charges',
                                                    'data'   => $data
                    ]);

                    // Array
                    //   echo "<pre style='background: #ff0000'>"; print_r($Paiement); echo "</pre>";

                    // paid = poru CB
                    // mandate->reference (ref du mandat J41JQ3EQUTKH0SC4)
                    if ( ($Paiement->paid==1 || $Paiement->source->mandate->reference) && !$Paiement->error) {

                        $Color  = "#5cb85c"	;
                        $Msg    = $CodeMode." accepté";
                        $sendMail = true;
                        //$MyCde->SendMail_Client();
                    } else {
                        $Color      = "#d9534f"	;
                        $Msg        = $CodeMode." echec";
                        /*
                        $CodeRefus   = $Paiement->error->code;
                        $RaisonRefus = $Paiement->error->message."<br>".$Paiement->error->type;*/
                        $RaisonRefus = "Merci de vérifier la validité de vos informations et recommencer l'opération...";
                    }
                    //echo "<h1>=>Pas d enreg commande !</h1>";
                    $MyCde->PaiementCmd($Msg,$Customer,$nobdc);
                }
                // [KO] echec, deja passé, reload etc...
                //
                elseif($NewPaiementCB) {
                    $MsgValide  = "Nous avons bien enregistré votre règlement et vous remercions pour votre commande.";
                    $Color  = "#5cb85c"	;
                    $Msg    = "Paiement enregistré";
                     $nobdc = $_REQUEST['noc']; // Le Numero de BC ne peut pas etre connu car c est le webhook qui le fait pour le CB
                //  Confirmation par Webhook
                //   $MyCde->PaiementCmd($Msg,$Customer,$nobdc);
                //   $sendMail = true;

                }
            //
                else {
                    $Color      = "#d9534f"	;
                    $Msg        = $CodeMode." echec..";
                    $CodeRefus  = "Impossible de continuer !";
                }



                //  Email de confirmation vers AIRMOB uniquement poru le SEPA
                if ($_REQUEST['mode']=="SEPA" && $sendMail) {
                    if ($idu) {
                        include "etatcmdv2.cron.php";
                        //echo "<h1>=>MODE TEST!</h1>";
                    }
                }
                error_reporting(E_ALL);
                ini_set("display_errors", 1);
                ?>
                <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"><?=$MsgValide?><br>
                                Le service client AIRMOB va revenir vers vous pour préparer l’expédition de votre commande.
                                <?php $MyCde->RecapituleCmd($nobdc); ?>


                            </p>
                        <?php } ?>
                        <br>
                        <div style="text-align:center;"><a href="#" class="btn btn-suivant" Onclick="javascript:location=('https://airmob.eu/')">Accueil</a></div>
                    </div>
                </div>
                <?php

                //die('DEBUG : initialisation stop !');
                unset($_SESSION['AIRCMDV2']);
                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>
        <?php } else { ?>
            <div class="col-md-12" style="text-align:center;">
                <h4>Votre éligibilité nécessite une étude complémentaire.<br>Nous vous remercions et allons vous contacter très rapidement. </h4>
                <br>
                <h4><a href="https://airmob.net">https://airmob.net</a></h4>
            </div>

        <?php

            unset($_SESSION['AIRCMDV2']);
        } ?>
    </div>
    </div>

</body>
</html>