<?php
require 'includes/db_connect.php';
require 'includes/paystack_config.php';

$ref = $_GET['reference'];

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.paystack.co/transaction/verify/$ref",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer ".PAYSTACK_SECRET_KEY
    ],
]);

$response = json_decode(curl_exec($curl));
curl_close($curl);

if ($response->data->status == "success") {

    $cert_no = "LGID-" . strtoupper(substr(md5(time()),0,8));

    $conn->query("
        UPDATE lgid 
        SET payment_status='paid',
            certificate_no='$cert_no'
        WHERE reference='$ref'
    ");

    header("Location: generate_lgid_certificate.php?ref=$ref");
}
