<?php
// ====== PHP Part: Generate Signature & Call API ======

// Replace with your credentials
$merchantId     = "SB2506000051";
$applicationKey = "cal0xZbZ8HyGv6q-ebb7Mw5k2yc_gx9AvBuxMen67MY";
$domain         = "hexdive.com";
$secretKey      = "fcf9a8155cef644002fbf50663f257444895de77742f43f1f7dacbcb2d00d083"; // used for HMAC

// Hardcoded order details
$orderId  = "TEST001"; 
$amount   = 120.00;
$currency = "LKR"; 

$payload = [
    "merchantId"     => $merchantId,
    "applicationKey" => $applicationKey,
    "domain"         => $domain,
    "platformType"   => "WEB", // or MOBILE
    "amount"         => $amount,
    "paymentType"    => "ONE_TIME",
    "orderId"        => $orderId,
    "currency"       => $currency,
    "pluginVersion"  => "1.0.0",
    "notifyUrl"      => "https://hexdive.com/noti.php", 
    "redirectUrl"    => "https://hexdive.com/red.php",
    "source"         => "PHP",
    "description"    => "Test payment of 120 t01",
    "doInitialPayment" => false,
    "customerInfo"   => [
        "firstname"   => "John",
        "lastname"    => "Doe",
        "email"       => "john@example.com",
        "phoneNumber" => "123456789",
        "dialCode"    => "+94"
    ]
];

// Encode payload
$dataString = base64_encode(json_encode($payload));

// Generate HMAC signature
$signature = hash_hmac('sha256', $dataString, $secretKey);

// API Endpoint (Sandbox or Live)
$apiUrl = "https://gateway-sandbox-api.payplus.lk/api/payment-session/create";

$headers = [
    "Content-Type: application/json",
    "Authorization: hmac " . $signature
];

// Send request
$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dataString));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if(curl_errno($ch)){
    echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);

// Decode and show response
$j = json_decode($response, true);
echo $j['data']['link'];
?>
