<!DOCTYPE html>
<html>
<?php
/*
 * https://airmob.digital/lesptitsfayots/log/log.txt
 * https://airmob.digital/lesptitsfayots/webh.php?
 * */

    session_start();
    error_reporting(E_ALL);
    ini_set("display_errors", 0);
    include_once ("./lib/gestion.lib.php");
    $MyCde       = new ClassCdeV2();

//
    if($_REQUEST['force']) {
        $REQ = ['nocmd'=>$_REQUEST['force'],'etat'=>'paiement_ok','force'=>'ok'];
    } else {

    // Webhook STRIPE
        $json_str     = file_get_contents('php://input');
        $json_obj     = json_decode($json_str);
        $Arr          = json_decode($json_str, TRUE);
        $idu          = $Arr['data']['object']['metadata']['NoBDC']; // idunique
        $codepromo    = $Arr['data']['object']['metadata']['code_promo'];
        $Type         = $Arr['type'];

        if ($Type <> 'payment_intent.created' && $idu) {

            if      ($Type == "charge.succeeded")           $Paye = "paiement_ok";
            elseif  ($Type == "payment_intent.succeeded")   $Paye = "paiement_ok";
            else                                            $Paye = $Arr['type'];

            /*
            $fileJson = "./log/wh.txt";
            $fp = fopen($fileJson, 'a+'); // a+ ajoute / w ecrase
            fwrite($fp, "-".$idu."-".$Paye."-".$nobdc."\n");
            fclose($fp);
            */

        // Envoi un email au client + airmob
            $traite ="non";
            if ($Type == "charge.succeeded") { // Fait 1 fois l envoi
                $nobdc = $MyCde->NoBDC();
                $MyCde->PaiementCmdCB($idu,$Paye, $codepromo ,$nobdc);
                if($Paye) include "etatcmdv2.cron.php";
                $traite ="oui";
            }

            $fileJson = "./log/wh1.txt";
            $fp = fopen($fileJson, 'a+'); // a+ ajoute / w ecrase
            fwrite($fp, date('Y-m-d H:i')." :: ".$traite." | BC : ". $nobdc." | IDU ".$idu." | TYPE : ".$Type." | PAYE : ".$Paye."\n");
            fclose($fp);

        }
    }

    $fileJson2 = "./log/wh2.txt";
    $fp2 = fopen($fileJson2, 'a+'); // a+ ajoute / w ecrase
    fwrite($fp2, date('Y-m-d H:i')." :: ".$traite." | BC : ". $nobdc." | IDU ".$idu." | TYPE : ".$Type." | PAYE : ".$Paye."\n");
    fclose($fp2);



?>fin