<?php
// === CONFIG ===
$gateway_url = "https://www.lg-pay.com/api/order/create";
$merchant_id = "PKR3051"; // Your LG Pay merchant ID
$trade_type = "PKRPH"; // Provided by LG Pay support
$notify_url = "https://warix.codexm.site/payment_callback.php";
$secret_key = "DD3UsTt84Wsf6pQAw9SH5dgbzbVd8xYX"; // Provided by LG Pay


// === SIGNATURE FUNCTION ===
function md5_sign($data, $key) {
    ksort($data);
    $string = http_build_query($data);
    $string = urldecode($string);
    $string = trim($string) . "&key=" . $key;
    return strtoupper(md5($string));
}

// === ORDER INPUT ===
$amount = 100.00; // Amount in INR
$order_sn = "P" . time(); // Unique Order ID
$client_ip = $_SERVER['REMOTE_ADDR'] ?? "0.0.0.0";

// === BUILD REQUEST ===
$data = [
    "app_id"     => $merchant_id,
    "trade_type" => $trade_type,
    "order_sn"   => $order_sn,
    "money"      => intval($amount * 100), // Convert ₹ to paisa
    "notify_url" => $notify_url,
    "ip"         => $client_ip,
    "remark"     => "UserID-123", // Optional - user ID, email, anything traceable
"return_url" => "https://warix.codexm.site/payment_success.php",

];

// Add signature
$data['sign'] = md5_sign($data, $secret_key);

// === SEND TO GATEWAY ===
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $gateway_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/x-www-form-urlencoded"
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch);
} else {
    $result = json_decode($response, true);
    
    if ($result['status'] == 1) {
        // Redirect user to pay_url
        $pay_url = $result['data']['pay_url'];
        header("Location: " . $pay_url);
        exit;
    } else {
        echo "<strong>Gateway Error:</strong> " . $result['msg'];
        echo "<pre>" . print_r($result, true) . "</pre>";
    }
}

curl_close($ch);
?>
