<?php
// session_start();
// $ref = $_GET['reference'];
$ref = $_SESSION['ref'];
// $ref = isset($_GET['reference']) ? $_GET['reference'] : $_GET['reference'] ;

// $data = isset($_GET['data']) ? $_GET['data'] : null;

if ($ref == "") {
    echo '<script type="text/javascript">history.go(-1);</script>';
    exit();
}

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.paystack.co/transaction/verify/" . rawurlencode($ref),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer sk_live_f04d91e51a3c4b39cb57666975e4361a395911a4",
        "Cache-Control: no-cache",
    ),
));  

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo '<script>alert("cURL Error #:" . $err");</script>';
} else {
    $result = json_decode($response, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        //echo "JSON decode error." . '' . json_last_error();
    } else {
        if ($result["data"]['status'] == 'success') {
            
            $_SESSION['amountPaid'] = $result["data"]['amount'];
            $_SESSION['status'] = "Payment Successful";
            $_SESSION['userId'] = $_SESSION['user_id'];

            echo '<script>window.location.href = "success";</script>';
        } else {
            echo "Transaction verification failed.";
        }
    }
}
?>