<?php
// getway/sketpay/payemnt_create.php

require_once __DIR__ . '/config.php';

$cus_name = $_REQUEST['cus_name'] ?? 'John Doe';
$cus_email = $_REQUEST['cus_email'] ?? 'john@gmail.com';
$amount = $_REQUEST['amount'] ?? '10';
$phone = $_REQUEST['phone'] ?? '01700000000';

$amount = (float)$amount;
$amount = floor($amount) == $amount ? (int)$amount : $amount;

$payload = [
    "cus_name" => $cus_name,
    "cus_email" => $cus_email,
    "amount" => (string)$amount,
    "success_url" => SUCCESS_URL,
    "cancel_url" => CANCEL_URL,
    "webhook_url" => WEBHOOK_URL,
    "meta_data" => ["phone" => $phone]
];

$baseUrl = rtrim(NP_BASE_URL, '/');
$baseUrl = preg_replace('/\/api\/payment$/i', '', $baseUrl);
$apiUrl = $baseUrl . '/api/payment/create';

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => $apiUrl,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => json_encode($payload),
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_HTTPHEADER => [
        'API-KEY: ' . NP_API_KEY,
        'Content-Type: application/json'
    ],
]);

$response = curl_exec($curl);
$curl_err = curl_error($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

$responseData = json_decode($response, true);

if ($http_code == 200 && isset($responseData['payment_url'])) {
    redirect($responseData['payment_url']);
} elseif (isset($responseData['url'])) {
    redirect($responseData['url']);
} else {
    $errorMessage = "An unknown error occurred during payment initialization.";
    
    if ($curl_err) {
        $errorMessage = "cURL Error: " . $curl_err;
    } elseif (isset($responseData['message'])) {
        $errorMessage = $responseData['message'];
    } elseif (isset($responseData['error'])) {
        $errorMessage = is_array($responseData['error']) ? json_encode($responseData['error']) : $responseData['error'];
    }

    echo '<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; border: 1px solid #f5c6cb; border-radius: 6px; background-color: #f8d7da; color: #721c24; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">';
    echo '<h2 style="margin-top: 0; display: flex; align-items: center; font-size: 20px;">';
    echo '<svg style="width: 24px; height: 24px; margin-right: 10px;" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>';
    echo 'Payment Initialization Failed</h2>';
    
    echo '<p style="font-size: 15px; line-height: 1.5;"><strong>Error Detail:</strong> ' . htmlspecialchars($errorMessage) . '</p>';
    echo '<p style="font-size: 15px; line-height: 1.5;"><strong>HTTP Status Code:</strong> ' . htmlspecialchars($http_code) . '</p>';
    
    echo '<hr style="border-top: 1px solid #f5c6cb; margin: 20px 0;">';
    
    echo '<p style="margin-bottom: 8px; font-size: 14px;"><strong>Debug Info:</strong></p>';
    echo '<ul style="font-size: 13px; color: #555; background: #fff; padding: 12px 12px 12px 30px; border: 1px solid #f5c6cb; border-radius: 4px; margin: 0 0 15px 0;">';
    echo '<li><strong>API URL:</strong> ' . htmlspecialchars($apiUrl) . '</li>';
    echo '<li><strong>API KEY:</strong> ' . htmlspecialchars(substr(NP_API_KEY, 0, 5) . '****') . '</li>';
    echo '<li><strong>Payload Amount:</strong> ' . htmlspecialchars($amount) . '</li>';
    echo '</ul>';

    echo '<p style="margin-bottom: 8px; font-size: 14px;"><strong>Raw API Response:</strong></p>';
    echo '<pre style="background: #fff; padding: 12px; border: 1px solid #f5c6cb; border-radius: 4px; overflow-x: auto; font-size: 13px; color: #333; margin: 0;">' . htmlspecialchars($response ?: 'No response received from the payment server.') . '</pre>';
    
    echo '<div style="margin-top: 25px; text-align: right;">';
    echo '<a href="javascript:history.back()" style="display: inline-block; padding: 10px 20px; background-color: #dc3545; color: #fff; text-decoration: none; border-radius: 4px; font-weight: bold; font-size: 14px; transition: background-color 0.2s;">Go Back to Checkout</a>';
    echo '</div>';
    echo '</div>';
}
?>