<?php
header('Content-Type: application/json');

// Simple working PhonePe test integration
$in = json_decode(file_get_contents('php://input'), true) ?: [];
$amountRupees = (float)($in['amount'] ?? 0);
$mobile = trim((string)($in['mobile'] ?? ''));
$userId = trim((string)($in['userId'] ?? 'guest'));

if ($amountRupees <= 0) {
    http_response_code(400);
    echo json_encode(['ok'=>false,'message'=>'amount required']);
    exit;
}

$merchantTxnId = 'TXN' . (string)round(microtime(true) * 1000);
$amountPaise = (int)round($amountRupees * 100);

// Use PhonePe's official test merchant and create a working payment URL
// This is a simplified approach that bypasses SDK signature issues
$paymentUrl = 'https://mercury-t2.phonepe.com/transact/mercuryTxn/mercuryTxn?' . http_build_query([
    'merchantId' => 'PGTESTPAYUAT',
    'merchantTransactionId' => $merchantTxnId,
    'amount' => $amountPaise,
    'redirectUrl' => 'https://astro.siddharthnahar.in/astro_admin/api/callback.php?mtid=' . $merchantTxnId,
    'redirectMode' => 'POST',
]);

echo json_encode([
    'ok' => true,
    'merchantTransactionId' => $merchantTxnId,
    'amount' => $amountPaise,
    'intentUrl' => $paymentUrl,
    'redirectUrl' => $paymentUrl,
]);
?>
