<?php
$ref = $_GET['ref'] ?? '';
$orderId = $_GET['order_id'] ?? '';
$captureId = $_GET['capture_id'] ?? '';
$captureStatus = $_GET['capture_status'] ?? 'DECLINED';
$processor = $_GET['processor'] ?? '';
$amount = $_GET['amount'] ?? '';
$currency = $_GET['currency'] ?? 'EUR';
$date = date("d M Y, H:i:s");

function getDeclineReason($processor) {
    if (strpos($processor, 'PPAX') !== false) {
        return 'The card issuer declined the authorization. Possible reasons include insufficient available balance, card spending limit, online or international payment restriction, or bank security decline.';
    }
    if (strpos($processor, 'INSUFFICIENT_FUNDS') !== false) return 'The transaction was declined because the card does not have sufficient available balance.';
    if (strpos($processor, 'EXPIRED_CARD') !== false) return 'This card has expired. Please use another valid card.';
    if (strpos($processor, 'INVALID_CVV') !== false) return 'The security code (CVV) entered is incorrect.';
    return 'The card issuer declined the transaction authorization.';
}
$reason = getDeclineReason($processor);
?>

<?php
function safeProcessorCode($raw) {

    $raw = strtoupper((string)$raw);

    $codes = [
        'PAYER_CANNOT_PAY','UNPROCESSABLE_ENTITY','INSUFFICIENT_FUNDS',
        'TRANSACTION_NOT_ALLOWED','INVALID_CVV','EXPIRED_CARD',
        'PPAX','PPBG','PPC2','PPCE','PPCO','PPCR','PPCT','PPCU',
        'PPD3','PPDC','PPDI','PPDT','PPEF','PPEL','PPER','PPEX',
        'PPFI','PPFR','PPFV','PPGR','PPIF','PPII','PPIM','PPIT',
        'PPLR','PPLS','PPMC','PPNC','PPNM','PPNT','PPPI','PPPM',
        'PPQC','PPRF','PPRR','PPS4','PPS5','PPS6','PPSE','PPTE',
        'PPTF','PPTI','PPTR','PPTT','PPTV','PPUA','PPUC','PPUE',
        'PPUI','PPUP','PPUR','PPVC','PPVE','PPVT'
    ];

    foreach ($codes as $code) {
        if (strpos($raw, $code) !== false) {
            return $code;
        }
    }

    if (preg_match('/([0-9]{4})/', $raw, $m)) {
        return $m[1];
    }

    return 'DECLINED';
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Failed</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0;font-family:Arial,Helvetica,sans-serif}
body{background:#fff7f7;padding:18px;color:#0f172a}
.container{max-width:460px;margin:0 auto}
.card{background:#fff;border-radius:28px;padding:24px;box-shadow:0 10px 35px rgba(0,0,0,.08)}
.title{font-size:36px;font-weight:900;text-align:center;margin-bottom:10px}
.sub{text-align:center;color:#64748b;line-height:1.5;margin-bottom:24px}
.box{background:#fef2f2;border:1px solid #fecaca;border-radius:22px;padding:14px}
.row{display:flex;justify-content:space-between;gap:14px;padding:14px 0;border-bottom:1px solid #f5c2c7}
.row:last-child{border-bottom:none}
.label{color:#7f1d1d;font-size:15px}
.value{font-weight:800;text-align:right;max-width:62%;word-break:break-word}
.reason{background:#fff;border:1px solid #fecaca;border-radius:14px;padding:12px;font-weight:800;line-height:1.5;color:#7f1d1d;margin-top:8px}
.btn{width:100%;border:none;padding:17px;border-radius:18px;font-size:18px;font-weight:900;color:#fff;margin-top:18px;cursor:pointer}
.blue{background:#0077c8}.green{background:#16a34a}
.footer{text-align:center;color:#94a3b8;font-size:13px;margin-top:18px;line-height:1.5}
</style>
</head>
<body>

<div class="container">
<div class="card">
<div class="title">Payment Failed</div>
<div class="sub">Your payment could not be completed.<br>No successful charge was captured.</div>

<div class="box">
<div class="row"><div class="label">Reference</div><div class="value"><?php echo htmlspecialchars($ref); ?></div></div>
<div class="row"><div class="label">Amount</div><div class="value"><?php echo htmlspecialchars($amount . ' ' . $currency); ?></div></div>

<div style="padding:14px 0;border-bottom:1px solid #f5c2c7;">
<div class="label">Reason</div>
<div class="reason"><?php echo htmlspecialchars($reason); ?></div>
</div>

<div class="row"><div class="label">Capture Status</div><div class="value"><?php echo htmlspecialchars($captureStatus); ?></div></div>
<div class="row"><div class="label">Order ID</div><div class="value"><?php echo htmlspecialchars($orderId); ?></div></div>
<div class="row"><div class="label">Capture ID</div><div class="value"><?php echo htmlspecialchars($captureId); ?></div></div>
<div class="row"><div class="label">Processor Code</div><div class="value"><?php echo htmlspecialchars(safeProcessorCode($processor)); ?></div></div>
<div class="row"><div class="label">Date & Time</div><div class="value"><?php echo htmlspecialchars($date); ?></div></div>
</div>

<button class="btn blue" onclick="returnToBot()">Return to Bot</button>
<button class="btn green" onclick="tryAgain()">Try Again</button>

<div class="footer">You may close this page and create a new payment from Telegram bot.</div>
</div>
</div>

<script>
try {
    if (window.Telegram && Telegram.WebApp) {
        Telegram.WebApp.ready();
        Telegram.WebApp.expand();
    }
} catch(e) {}

function returnToBot(){
    try {
        if (window.Telegram && Telegram.WebApp) {
            Telegram.WebApp.close();
            return;
        }
    } catch(e) {}

    window.location.href = "https://t.me/rumbofacil_bot";
}

function tryAgain(){
    window.location.href = "checkout.php?ref=<?php echo urlencode($ref); ?>&amount=<?php echo urlencode($amount); ?>&v=" + Date.now();
}
</script>

</body>
</html>
