<?php
    session_start();
    include('db.php');
    $montant="";
    $_SESSION['formation_id']="";
    $_SESSION['user_id']="";
    $_SESSION['mont']="";
  
    if (!empty($_GET['formation_id'])) {
        $_SESSION['formation_id']=$_GET['formation_id'];
        $mnt_ticket=$db->prepare('SELECT * FROM formation WHERE id=?');
        $mnt_ticket->execute(array($_SESSION['formation_id']));
        while ($mnt_tickets=$mnt_ticket->fetch()) {
            $montant=$mnt_tickets['prix'];
        }
    }

    if (!empty($_GET['user_id'])) {
        $_SESSION['user_id']=$_GET['user_id'];
        $sel=$db->prepare('SELECT * FROM user WHERE id=?');
        $sel->execute(array($_SESSION['user_id']));
        while($sels=$sel->fetch()){
            $_SESSION['code_parrain']=$sels['code_parrain'];
            
        }
        
    }
    
    if($_SESSION['code_parrain']<>"vision million"){
        $montant=$montant-2000;
    } else{
        $montant=$montant;
    }

    $_SESSION['pk_live_fedapay']="pk_live_a3PoezLuPpTN-Ycgg9BoLnfK";
    
    $_SESSION['mPay']="fedapay";
    
    $_SESSION['mont']=$montant;

    if(isset($_POST['field'])){
        
        $_SESSION['status']=$_POST['transaction-status'];

        if($_SESSION['status'] == "approved" ){
            
     
            echo '<script>window.location.href="succes.php"</script>';


        }else {

            # signaler erreur

            echo '<script>window.location.href="echec.php"</script>';  



        }

    }



?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Vision million</title>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"></script>
    <style>
        .btn-feda{
            color: #ffffff;
            background-color: #1E2762;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-sm-4"></div>
            <div class="col-sm-4"><br><br><br>
                <div class="card">
                    <div class="card-body">
                        <div class="card-title">
                            
                            <center>
                                <h5>Sentinelle de paiement</h5> <br>
                                
                                <form action="" method="post">
                                    <input type="hidden" name="field" value="test">
                                    <script
                                    src="https://cdn.fedapay.com/checkout.js?v=1.1.7"
                                    data-public-key=<?php echo $_SESSION['pk_live_fedapay']; ?>   
                                    data-button-text="Payez <?php echo $montant; ?> FCFA"
                                    data-button-class="btn-feda"
                                    data-transaction-amount="<?php echo $montant; ?>"
                                    data-transaction-description="Transaction description"
                                    data-currency-iso="XOF">
                                    </script>                     

                                </form>
                            </center>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4"></div>
        </div>
    </div>

</body>
</html>