<?php
// Simple PHP script to call Zwitch UPI Intent API and print the response

$apiUrl = "https://api.zwitch.io/icp/upi/intent";

// Headers
$accessKey = "{{Your Access Key}}";
$authToken = "{{Access_key:Secret_key}}";

// Request payload
$mtx = 'mtx_' . time() . '_' . bin2hex(random_bytes(4));

$payload = [
    "amount" => "5.00", // amount to be collected
    "contact_number" => "8900223344", // merchant's mobile number
    "email_id" => "john.doe@gmail.com", // merchant's email
    "currency" => "INR", //Currency which is always INR
    "mtx" => $mtx // Unique reference number
];

// Initialize cURL
$ch = curl_init($apiUrl);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Access-Key: $accessKey",
    "Authorization: Bearer $authToken",
    "Accept: application/json",
    "Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));

// Execute
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Error handling
if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch);
    curl_close($ch);
    exit;
}

curl_close($ch);

// Output response

echo "HTTP Status Code: $httpCode\n";
echo "Response: \n$response";
