<?php
    
    require "payment-api/lib/Twocheckout.php";
    require "../registro_pagos_visita.php";
    
    
    $seller_id = '251206854467';
    $publishable_key = '6965F831-0BFC-4545-A657-1EFA995FAE42';
    $private_key = '3BEFBF06-BF47-4939-9300-82CB343C8E97';
    
    //4242424242424242
    $localhost = "directoriofitness.com.mx";
    $user = "direct18";
    $passwd = "L4LL4v3M@3str@";
    
    /*$token = $_POST['token'];
    $importe = $_POST['importe'];
    $nombre = $_POST['nombre'];
    $apellido = $_POST['apellido'];
    $tel = $_POST['tel'];
    $domi = $_POST['domi'];
    $col = $_POST['colonia'];
    $cp = $_POST['cp'];*/
    $token = $_POST['token'];
    $bd = str_replace('_', ' ', $_POST['bd']);
    $id_cliente = str_replace('_', ' ', $_POST['id_cliente']);
    $id_servicio = str_replace('_', ' ', $_POST['plan']);
    $plan = str_replace('_', ' ', $_POST['plan']);
    $importe = str_replace('_', ' ', $_POST['importe']);
    $nombre = str_replace('_', ' ', $_POST['nombre']);
    $apellido = str_replace('_', ' ', $_POST['apellido']);
    $fechaN = str_replace('_', ' ', $_POST['fechaN']);
    $email = str_replace('_', ' ', $_POST['email']);
    $tel = str_replace('_', ' ', $_POST['tel']);
    $domi = str_replace('_', ' ', $_POST['domi']);
    $col = str_replace('_', ' ', $_POST['colonia']);
    $cp = str_replace('_', ' ', $_POST['cp']);
    $genero = str_replace('_', ' ', $_POST['genero']);
    $fechaR = str_replace('_', ' ', $_POST['fechaR']);
    $sucursal = str_replace('_', ' ', $_POST['sucursal']);
    $qr = $_POST['qr'];
    
    $conn = new mysqli($localhost,$user,$passwd, 'direct18_'.$bd);
    mysqli_set_charset($conn, 'utf8');
    $query = "SELECT pagos.idPago as 'folio' FROM pagos ORDER BY pagos.idPago DESC LIMIT 1";
    $stmt = $conn->prepare($query);
    //$stmt->bind_param('s', $gym);
    $stmt->execute();
    $rs = $stmt->get_result();
    $row = mysqli_fetch_array($rs, MYSQLI_ASSOC);
    $folio = $row["folio"] + 1;
    $conn->close();
    
    echo $folio;
    
    // Your sellerId(account number) and privateKey are required to make the Payment API Authorization call.
    Twocheckout::privateKey($private_key);
    Twocheckout::sellerId($seller_id);

    // To use your sandbox account set sandbox to true
    Twocheckout::sandbox(false);
    try {
        $array_charge = array(
                "sellerId" => $seller_id,
                "merchantOrderId" => $folio,
                "token" => $token,
                "currency" => 'MXN',
                "total" => $importe,
                "billingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => 'Aguascalientes',
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => '20660',
                    "country" => 'MEX',
                    "email" => $email,
                    "phoneNumber" => $tel
                ),
                "shippingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => 'Aguascalientes',
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => '20660',
                    "country" => 'MEX',
                    "email" => $email,
                    "phoneNumber" => $tel
                )
            );
        //4242424242424242
        $charge_a = array(
                "merchantOrderId" => $folio,
                "token"      => $token,
                "currency"   => 'MXN',
                "total"      => $importe,
                "billingAddr" => array(
                    "name" => $nombre.' '.$apellido,
                    "addrLine1" => $domi,
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => $cp,
                    "country" => 'MEX',
                    "email" => $email,
                    "phoneNumber" => $tel
                )
            );
        
        /*try{
            
        }catch(Exception $e){
            echo 'error => '.$e."\n";
            echo json_encode(array(
                "merchantOrderId" => $folio,
                "token"      => $token,
                "currency"   => 'MXN',
                "total"      => $importe,
                "billingAddr" => array(
                    "name" => $nombre.' '.$apellido,
                    "addrLine1" => $domi,
                    "city" => 'Aguascalientes',
                    "state" => 'Aguascalientes',
                    "zipCode" => $cp,
                    "country" => 'MEX',
                    "email" => $email,
                    "phoneNumber" => $tel
                )
            ));
        }*/
        
        //echo json_encode($array_charge);
        //$charge = Twocheckout_Charge::auth($charge_a);
        //echo json_encode($charge['response']);
        $charge = Twocheckout_Charge::auth($array_charge);
        if ($charge['response']['responseCode'] == 'APPROVED') {
            echo "Thanks for your Order!";
            $obj = new PagoVisitaReg();
            $t = $obj -> registrarPago($bd,$id_cliente,$id_servicio,$plan,$importe,$nombre,$apellido,$fechaN,$email,$tel,$domi,$col,$cp,$genero,$fechaR,$sucursal,$qr,"2Chk");
        }
    } catch (Twocheckout_Error $e) {
        echo $e->getMessage(), " ",$charge;
    }

?>