<?php
// ============================================================
// StroWallet RAW Debug Script
// Place at: admin/strowallet-debug.php
// Visit: yourdomain.com/admin/strowallet-debug.php
// DELETE AFTER USE!
// ============================================================

$config_path = dirname(__DIR__) . '/include/config.php';
if (file_exists($config_path)) {
    include($config_path);
    $PUB     = isset($site_data['strowallet_public_key'])  ? $site_data['strowallet_public_key']  : '';
    $SEC     = isset($site_data['strowallet_secret_key'])  ? $site_data['strowallet_secret_key']  : '';
    $SANDBOX = isset($site_data['strowallet_sandbox'])     ? (int)$site_data['strowallet_sandbox'] : 1;
} else {
    // Hardcoded fallback if config path fails
    $PUB     = 'pub_oFSy6K4nnTk133nlVCF9gCDOhRrG5VVH8Shhmmtf';
    $SEC     = 'sec_R59YPSagslzP5a61rA3LAInPVy8Y0YDkZTr71CYx';
    $SANDBOX = 1;
}
$MODE = $SANDBOX ? 'sandbox' : 'live';
$BASE = 'https://strowallet.com/api/bitvcard';

function do_post($url, $body) {
    $json = json_encode($body);
    $ch   = curl_init($url);
    curl_setopt_array($ch, array(
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => $json,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER     => array(
            'Content-Type: application/json',
            'Accept: application/json',
        ),
    ));
    $resp = curl_exec($ch);
    $err  = curl_error($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $eff  = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    return array('json'=>$json,'resp'=>$resp,'code'=>$code,'err'=>$err,'eff'=>$eff,'dec'=>json_decode($resp,true));
}

function do_form($url, $body) {
    $ch = curl_init($url);
    curl_setopt_array($ch, array(
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => http_build_query($body),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 30,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER     => array(
            'Content-Type: application/x-www-form-urlencoded',
            'Accept: application/json',
        ),
    ));
    $resp = curl_exec($ch);
    $err  = curl_error($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $eff  = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    curl_close($ch);
    return array('form'=>http_build_query($body),'resp'=>$resp,'code'=>$code,'err'=>$err,'eff'=>$eff,'dec'=>json_decode($resp,true));
}

$T = array();

// A: Exact copy from their official docs example
$T['A – Exact docs payload (JSON, sandbox)'] = do_post($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => 'sandbox',
));

// B: Same with mode=live
$T['B – Same, mode=live'] = do_post($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => 'live',
));

// C: Add secret_key
$T['C – With secret_key'] = do_post($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'secret_key'    => $SEC,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => $MODE,
));

// D: Form-encoded instead of JSON
$T['D – Form-encoded body'] = do_form($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => $MODE,
));

// E: No trailing slash
$T['E – No trailing slash'] = do_post($BASE.'/create-card', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => $MODE,
));

// F: No mode field
$T['F – No mode field'] = do_post($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => '5',
    'customerEmail' => 'mydemo@gmail.com',
));

// G: check-balance (simplest auth test, no sandbox/live needed)
$T['G – check-balance (auth test)'] = do_post('https://strowallet.com/api/check-balance', array(
    'public_key' => $PUB,
));

// H: check-balance with secret_key
$T['H – check-balance + secret_key'] = do_post('https://strowallet.com/api/check-balance', array(
    'public_key' => $PUB,
    'secret_key' => $SEC,
));

// I: fetch-card-detail with dummy card_id (tests auth on a different endpoint)
$T['I – fetch-card-detail (auth check)'] = do_post($BASE.'/fetch-card-detail/', array(
    'public_key' => $PUB,
    'card_id'    => 'test_dummy_id',
    'mode'       => $MODE,
));

// J: Amount as integer not string
$T['J – amount as integer (not string)'] = do_post($BASE.'/create-card/', array(
    'name_on_card'  => 'My Name',
    'card_type'     => 'visa',
    'public_key'    => $PUB,
    'amount'        => 5,
    'customerEmail' => 'mydemo@gmail.com',
    'mode'          => $MODE,
));

function badge($ok, $txt) {
    $c = $ok ? '#3fb950' : '#f85149';
    $b = $ok ? 'rgba(63,185,80,.12)' : 'rgba(248,81,73,.12)';
    return "<span style='display:inline-block;padding:1px 8px;border-radius:20px;font-size:11px;font-weight:700;color:{$c};background:{$b};border:1px solid {$c};margin-right:5px'>{$txt}</span>";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>StroWallet Debug</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:monospace;background:#0d1117;color:#c9d1d9;padding:20px;font-size:13px}
h1{color:#58a6ff;padding:0 0 12px;font-size:17px;border-bottom:1px solid #21262d;margin-bottom:20px}
.cred{background:#161b22;border:1px solid #30363d;border-radius:6px;padding:14px;margin-bottom:20px}
table{border-collapse:collapse;width:100%}
td,th{border:1px solid #21262d;padding:6px 10px;vertical-align:top}
th{background:#21262d;color:#8b949e;font-size:10px;text-transform:uppercase}
.test{background:#161b22;border:1px solid #30363d;border-radius:6px;padding:14px;margin-bottom:14px}
.ok{border-left:4px solid #3fb950}.fail{border-left:4px solid #f85149}.warn{border-left:4px solid #d29922}
.title{font-size:13px;font-weight:bold;margin-bottom:8px}
.grid{display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-top:10px}
.box{background:#0d1117;border:1px solid #21262d;border-radius:4px;padding:10px}
.lbl{font-size:10px;color:#8b949e;text-transform:uppercase;letter-spacing:.06em;margin-bottom:5px}
pre{white-space:pre-wrap;word-break:break-all;font-size:11.5px;line-height:1.5}
.msg{color:#e3b341;font-size:12px;margin-top:4px}
</style>
</head>
<body>

<h1>&#128300; StroWallet Raw API Debug</h1>

<div class="cred">
<table>
<tr><th>Item</th><th>Value</th></tr>
<tr><td>Public Key</td><td><?php echo $PUB ? htmlspecialchars(substr($PUB,0,20)).'…' : '<span style="color:#f85149">EMPTY — not loaded from DB</span>'; ?></td></tr>
<tr><td>Secret Key</td><td><?php echo $SEC ? htmlspecialchars(substr($SEC,0,10)).'…' : '<span style="color:#f85149">EMPTY</span>'; ?></td></tr>
<tr><td>strowallet_sandbox (DB)</td><td><?php echo $SANDBOX ? '<span style="color:#d29922">1 = sandbox</span>' : '<span style="color:#3fb950">0 = live</span>'; ?></td></tr>
<tr><td>mode being sent</td><td><?php echo htmlspecialchars($MODE); ?></td></tr>
<tr><td>PHP</td><td><?php echo PHP_VERSION; ?></td></tr>
<tr><td>cURL</td><td><?php $cv=curl_version(); echo $cv['version']; ?></td></tr>
<tr><td>config.php</td><td><?php echo file_exists($config_path) ? '<span style="color:#3fb950">Found &#10003;</span>' : '<span style="color:#f85149">NOT FOUND — using hardcoded keys above</span>'; ?></td></tr>
</table>
</div>

<?php foreach ($T as $label => $r):
    $dec     = $r['dec'];
    $code    = $r['code'];
    $status  = isset($dec['status']) ? $dec['status'] : null;
    $isOk    = ($code >= 200 && $code < 300);
    $isSucc  = ($status===true||$status==='success'||$status==='Success'||$status===1||$status==='1');
    $css     = $isSucc ? 'ok' : ($isOk ? 'warn' : 'fail');
    $msg     = isset($dec['message']) ? $dec['message'] : (isset($dec['error']) ? $dec['error'] : (isset($dec['msg']) ? $dec['msg'] : ''));
    // Pretty-print payload (hide secret)
    $sent_arr = json_decode(isset($r['json']) ? $r['json'] : '{}', true);
    if ($sent_arr && isset($sent_arr['secret_key'])) $sent_arr['secret_key'] = '***';
    $sent_pretty = $sent_arr ? json_encode($sent_arr, JSON_PRETTY_PRINT) : (isset($r['form']) ? $r['form'] : '');
?>
<div class="test <?php echo $css; ?>">
    <div class="title"><?php echo htmlspecialchars($label); ?></div>
    <div>
        <?php echo badge($isSucc, 'HTTP '.$code); ?>
        <?php if ($status !== null) echo badge($isSucc, 'status: '.var_export($status,true)); ?>
        <?php if ($r['err']) echo badge(false, 'cURL: '.htmlspecialchars($r['err'])); ?>
    </div>
    <?php if ($msg): ?><div class="msg">&#8594; <?php echo htmlspecialchars($msg); ?></div><?php endif; ?>
    <div class="grid">
        <div class="box">
            <div class="lbl">Effective URL</div>
            <pre style="color:#79c0ff;margin-bottom:8px"><?php echo htmlspecialchars($r['eff']); ?></pre>
            <div class="lbl">Payload Sent</div>
            <pre><?php echo htmlspecialchars($sent_pretty); ?></pre>
        </div>
        <div class="box">
            <div class="lbl">Raw Response</div>
            <pre style="color:<?php echo $isSucc ? '#3fb950' : '#f85149'; ?>"><?php echo $r['resp'] ? htmlspecialchars($r['resp']) : '<em style="color:#555">(empty)</em>'; ?></pre>
        </div>
    </div>
</div>
<?php endforeach; ?>

<div class="test fail" style="margin-top:20px;padding:12px">
    <strong style="color:#f85149">DELETE THIS FILE: admin/strowallet-debug.php</strong>
</div>
</body>
</html>