<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

    <title>FirstData</title>
  </head>
  <style>
    body{
        padding:25px;
    }
    form{
        padding: 15px;
    }
  </style>
  <body>
    
    <form>
      <div class="mb-3">
        <label for="nt" class="form-label">Nombre Titular</label>
        <input type="text" class="form-control" id="nt" name="nt">
      </div>
      <div class="mb-3">
        <label for="numt" class="form-label">Numero de Tarjeta</label>
        <input type="number" class="form-control" id="numt" name="numt">
      </div>
      <div class="mb-3">
        <label for="numt" class="form-label">Fecha Expiracion</label>
        <input type="text" class="form-control" id="numE" name="numE">
      </div>
       <div class="mb-3">
        <label for="cvvnumber" class="form-label">CVV</label>
        <input type="number" class="form-control" id="cvvnumber" name="cvvnumbre">
      </div>
      <button type="submit" class="btn btn-primary">Submit</button>
    </form>
    
    
    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
  </body>
</html>

<?php
    require 'vendor/autoload.php'; //composer packages (guzzle)

    use GuzzleHttp\Client;
    use GuzzleHttp\Psr7\Request;
    
    if(!isset($_POST["nt"]) && !isset($_POST["numt"]) && !isset($_POST["numE"]) && !isset($_POST["cvvnumber"])){
        
        $API_KEY = "npfhBnGhAAlExcxkG4PWmt6Dok6kPjjz";
        $API_SECRET = "JPVzTCmmocqGDTGT"; 
        $send_ruta = 'payment-schedules';
        $costo = 14;
        $mes = 12;
        $unit = 'MONTH';
        $transaction = [
             "purchaseOrderNumber"=> "84256400037",
            "requestType"=> "PaymentMethodPaymentSchedulesRequest",
            "transactionAmount"=> [
                "total"=> "25.00",
                "currency"=> "MXN"
            ],
            "startDate"=> "2022-12-30",
            "frequency"=> [
                "every"=> 1,
                "unit"=> "MONTH"
            ],
            "paymentMethod"=> [
                "paymentCard"=> [
                    "number"=> "4035874000424977",
                    "securityCode"=> "123",
                    "expiryDate"=> [
                        "month"=> "02",
                        "year"=> "25"
                    ]
                ]
            ],
            "numberOfPayments"=> 3
            
            
        ];
        
        $data_string = json_encode($transaction);
        
        $uuid = sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0xffff)
        );
    $timestamp = round(microtime(true) * 1000);
    
    $msg = $API_KEY. $uuid . $timestamp . $data_string;
        
    $hmac = hash_hmac('sha256', $msg, $API_SECRET);
    $messageSignature = base64_encode($hmac);
    $msg_signature = $messageSignature;
    //echo "uuid ".$uuid;
    //echo "msg_signature ".$msg_signature;
    //API URL
    $url = "https://cert.api.firstdata.com/gateway/v2/";

    //PREPARE REQUEST
    $client = new Client([
        'base_uri' => $url,
        'timeout' => $timestamp,
        'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'],
        'http_errors' => false
    ]);
    $headers = [
        'Api-Key' => $API_KEY,
        'Client-Request-Id' => $uuid,
        'Timestamp' => $timestamp,
        'Message-Signature' => $msg_signature
    ];

    try {

        //SEND REQUEST
        $request = new Request('POST', $send_ruta, $headers, $data_string);
        $response = $client->send($request);
        http_response_code($response->getStatusCode());
        $result = json_decode($response->getBody()->getContents());
        echo json_encode($result);
        //$transationid = $result;
   
    } catch (Exception $e) {
        http_response_code(503);
        echo json_encode($data_string);
        //HANDLE GUZZLE EXCEPTION 
        //FOR EXAMPLE: SERVER IMPOSSIBLE TO REACH DUE CONNECTION ERRORS
    }
    }

?>
