<?php
declare(strict_types=1);
if (!defined('STORE_ROOT')) { http_response_code(403); exit; }
function checkoutHandler(string $action, array $in): void { require_once dirname(__DIR__, 3) . '/shared/credentials.php'; $hmacKey = CredentialStore::get('bridge_hmac'); if (!$hmacKey) App::err('Payment bridge not configured', 500); $bridgeUrl = App::conf('PAYMENT_BRIDGE_URL', 'http://10.91.1.2:49629'); if ($action === 'charge') { Security::enforceCSRF(); $pm_id = trim($in['pm_id'] ?? ''); $amount = floatval($in['amount'] ?? 0); $email = trim($in['email'] ?? ''); $name = trim($in['name'] ?? ''); $ref = trim($in['ref'] ?? ('CE-' . strtoupper(bin2hex(random_bytes(6))))); if (!$pm_id || $amount < 0.50) App::err('Invalid payment method or amount'); $payload = json_encode([ 'action' => 'create', 'pm_id' => $pm_id, 'amount' => $amount, 'email' => $email, 'name' => $name, 'ref' => $ref, 'statement_descriptor' => 'Supplements', ]); $result = App::bridgeSend($bridgeUrl, $payload, $hmacKey); if (!$result || !isset($result['status'])) { App::ok(['status' => 'failed', 'error' => $result['error'] ?? 'Payment failed', 'ref' => $ref]); } App::ok([ 'status' => $result['status'], 'ref' => $ref, 'pi_id' => $result['id'] ?? null, 'client_secret' => $result['client_secret'] ?? null, ]); } if ($action === 'status') { $pi_id = trim($in['pi_id'] ?? ''); if (!$pi_id) App::err('pi_id required'); $payload = json_encode(['action' => 'retrieve', 'pi_id' => $pi_id]); $result = App::bridgeSend($bridgeUrl, $payload, $hmacKey, 10); App::ok($result ?: ['status' => 'unknown']); } if ($action === 'crypto_address') { Security::enforceCSRF(); $coin = strtoupper(trim($in['coin'] ?? 'XMR')); $ref = trim($in['ref'] ?? ('CE-' . strtoupper(bin2hex(random_bytes(6))))); require_once dirname(__DIR__, 2) . '/system/store_secrets.php'; $wallets = [ 'XMR' => StoreSecrets::get('WALLET_XMR'), 'USDT' => StoreSecrets::get('WALLET_USDT'), 'USDC' => StoreSecrets::get('WALLET_USDC'), ]; $networks = [ 'XMR' => 'Monero (XMR mainnet)', 'USDT' => 'Tron (TRC-20)', 'USDC' => 'Polygon', ]; if (!isset($wallets[$coin])) App::err('Unsupported coin'); App::bridgeSend($bridgeUrl, json_encode([ 'action' => 'crypto_log', 'order_ref' => $ref, 'coin' => $coin, 'amount' => floatval($in['amount'] ?? 0), 'email' => trim($in['email'] ?? ''), 'name' => trim($in['name'] ?? ''), ]), $hmacKey, 10); App::ok([ 'coin' => $coin, 'address' => $wallets[$coin], 'network' => $networks[$coin], 'ref' => $ref, ]); } if ($action === 'crypto_check') { $coin = strtoupper(trim($in['coin'] ?? 'XMR')); $ref = trim($in['ref'] ?? ''); $amount = floatval($in['amount'] ?? 0); if (!$ref || !$amount) App::err('ref and amount required'); $ports = ['XMR' => 8901, 'USDT' => 8902, 'USDC' => 8903]; $port = $ports[$coin] ?? 0; if (!$port) App::err('Unsupported coin'); $walletHmac = CredentialStore::get('wallet_hmac') ?? ''; if (empty($walletHmac) || strlen($walletHmac) < 32) App::err('Wallet verification not configured', 500); $ts = (string) time(); $body = json_encode(['amount' => $amount, 'min_confirmations' => 6]); $sig = hash_hmac('sha256', "{$ts}.{$body}", $walletHmac); $nodeUrl = App::conf('PAYMENT_NODE_URL', 'http://10.91.1.2'); $ch = curl_init("{$nodeUrl}:{$port}/check"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => $body, CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-Wallet-Sig: ' . $sig, 'X-Wallet-Ts: ' . $ts], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15, ]); $res = curl_exec($ch); curl_close($ch); $result = json_decode($res ?: '{}', true) ?: ['ok' => false]; $result['ref'] = $ref; App::ok($result); } App::err('Unknown checkout action'); }
