1023 lines
32 KiB
HTML
1023 lines
32 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Test Checkout (Admin) - Token Top-ups - Plugin Compass</title>
|
|
<link rel="icon" type="image/png" href="/assets/Plugin.png">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;600;700&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
|
|
<!-- Dodo Payments Inline Checkout SDK -->
|
|
<script src="https://cdn.jsdelivr.net/npm/dodopayments-checkout@latest/dist/index.js"></script>
|
|
|
|
<!-- PostHog Analytics -->
|
|
<script src="/posthog.js"></script>
|
|
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
<script>
|
|
tailwind.config = {
|
|
theme: {
|
|
extend: {
|
|
fontFamily: {
|
|
sans: ['Inter', 'sans-serif'],
|
|
display: ['Space Grotesk', 'sans-serif'],
|
|
},
|
|
colors: {
|
|
brand: {
|
|
50: '#f0fdf4',
|
|
100: '#dcfce7',
|
|
200: '#bbf7d0',
|
|
300: '#86efac',
|
|
400: '#4ade80',
|
|
500: '#22c55e',
|
|
600: '#16a34a',
|
|
700: '#15803d',
|
|
800: '#166534',
|
|
900: '#008060', // Shopify Green
|
|
950: '#004c3f',
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style>
|
|
:root {
|
|
--shopify-green: #008060;
|
|
--shopify-green-dark: #004c3f;
|
|
--shopify-green-light: #e3f5ef;
|
|
--border: #e5e7eb;
|
|
--muted: #6b7280;
|
|
--bg: #f8fafc;
|
|
}
|
|
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
margin: 0;
|
|
font-family: 'Space Grotesk', 'Inter', system-ui, -apple-system, sans-serif;
|
|
background: radial-gradient(circle at 10% 20%, rgba(0, 128, 96, 0.06), transparent 25%),
|
|
radial-gradient(circle at 90% 10%, rgba(0, 76, 63, 0.05), transparent 28%),
|
|
linear-gradient(135deg, #f7f9fb 0%, #edf1f5 100%);
|
|
color: #0f172a;
|
|
min-height: 100vh;
|
|
}
|
|
|
|
a {
|
|
color: inherit;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.shell {
|
|
max-width: 1180px;
|
|
margin: 0 auto;
|
|
padding: 32px 20px 48px;
|
|
}
|
|
|
|
.topbar {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
gap: 12px;
|
|
margin-bottom: 24px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.title {
|
|
font-size: 28px;
|
|
font-weight: 700;
|
|
margin: 0;
|
|
letter-spacing: -0.01em;
|
|
}
|
|
|
|
.crumb {
|
|
color: var(--muted);
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.subtitle {
|
|
color: var(--muted);
|
|
font-size: 15px;
|
|
margin-top: 8px;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
.actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.currency-section {
|
|
margin-bottom: 24px;
|
|
}
|
|
|
|
.currency-label {
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
color: var(--muted);
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.025em;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.currency-select {
|
|
width: 200px;
|
|
padding: 10px 14px;
|
|
border-radius: 8px;
|
|
border: 1px solid var(--border);
|
|
font-size: 14px;
|
|
background: #fff;
|
|
color: #0f172a;
|
|
font-weight: 600;
|
|
transition: all 0.2s;
|
|
appearance: none;
|
|
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23008060'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
|
|
background-repeat: no-repeat;
|
|
background-position: right 12px center;
|
|
background-size: 16px;
|
|
padding-right: 40px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.currency-select:focus {
|
|
outline: none;
|
|
border-color: var(--shopify-green);
|
|
box-shadow: 0 0 0 3px rgba(0, 128, 96, 0.1);
|
|
}
|
|
|
|
.currency-select:hover {
|
|
border-color: rgba(0, 128, 96, 0.3);
|
|
}
|
|
|
|
/* Currency dropdown styles */
|
|
.currency-dropdown.open #currency-btn {
|
|
border-color: var(--shopify-green);
|
|
box-shadow: 0 0 0 3px rgba(0, 128, 96, 0.1);
|
|
}
|
|
|
|
.currency-option {
|
|
transition: all 0.15s ease;
|
|
}
|
|
|
|
.currency-option:hover {
|
|
background-color: #d1fae5;
|
|
color: #065f46;
|
|
}
|
|
|
|
.btn {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 8px;
|
|
padding: 10px 18px;
|
|
border-radius: 10px;
|
|
border: 1px solid var(--border);
|
|
background: #fff;
|
|
font-weight: 600;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
|
color: #1e293b;
|
|
}
|
|
|
|
.btn:hover {
|
|
background: #f8fafc;
|
|
border-color: #cbd5e1;
|
|
transform: translateY(-1px);
|
|
}
|
|
|
|
.btn.primary {
|
|
background: var(--shopify-green);
|
|
color: #fff;
|
|
border: none;
|
|
box-shadow: 0 4px 12px rgba(0, 128, 96, 0.15);
|
|
}
|
|
|
|
.btn.primary:hover {
|
|
background: var(--shopify-green-dark);
|
|
box-shadow: 0 6px 16px rgba(0, 128, 96, 0.25);
|
|
transform: translateY(-1px);
|
|
}
|
|
|
|
.btn.ghost {
|
|
background: transparent;
|
|
border: 1px solid transparent;
|
|
}
|
|
|
|
.btn.ghost:hover {
|
|
background: rgba(0, 0, 0, 0.04);
|
|
border-color: rgba(0, 0, 0, 0.05);
|
|
}
|
|
|
|
.grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
|
gap: 16px;
|
|
}
|
|
|
|
.card {
|
|
background: #fff;
|
|
border: 1px solid var(--border);
|
|
border-radius: 14px;
|
|
padding: 24px;
|
|
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.05);
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 16px;
|
|
position: relative;
|
|
transition: all 0.2s ease;
|
|
}
|
|
|
|
.card:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.08);
|
|
border-color: rgba(0, 128, 96, 0.2);
|
|
}
|
|
|
|
.pill {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
padding: 6px 12px;
|
|
border-radius: 999px;
|
|
font-size: 12px;
|
|
font-weight: 700;
|
|
letter-spacing: 0.02em;
|
|
background: var(--shopify-green-light);
|
|
color: var(--shopify-green);
|
|
border: 1px solid rgba(0, 128, 96, 0.15);
|
|
}
|
|
|
|
.muted {
|
|
color: var(--muted);
|
|
font-size: 14px;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
.price {
|
|
font-size: 36px;
|
|
font-weight: 700;
|
|
letter-spacing: -0.01em;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.old-price {
|
|
color: var(--muted);
|
|
text-decoration: line-through;
|
|
font-weight: 500;
|
|
font-size: 20px;
|
|
}
|
|
|
|
.status {
|
|
padding: 14px 18px;
|
|
border-radius: 12px;
|
|
border: 1px solid var(--border);
|
|
background: #fff;
|
|
margin-bottom: 20px;
|
|
display: none;
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.status.show {
|
|
display: block;
|
|
}
|
|
|
|
.status.success {
|
|
border-color: #22c55e33;
|
|
background: #f0fdf4;
|
|
color: #166534;
|
|
}
|
|
|
|
.status.error {
|
|
border-color: #fca5a5;
|
|
background: #fef2f2;
|
|
color: #b91c1c;
|
|
}
|
|
|
|
.status.pending {
|
|
border-color: #f59e0b55;
|
|
background: #fffbeb;
|
|
color: #92400e;
|
|
}
|
|
|
|
.discount {
|
|
color: #166534;
|
|
font-weight: 700;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.products-label {
|
|
font-size: 18px;
|
|
font-weight: 700;
|
|
margin-bottom: 20px;
|
|
color: #0f172a;
|
|
}
|
|
|
|
.nav-bar {
|
|
background: white;
|
|
border-bottom: 1px solid #e9ecef;
|
|
padding: 16px 24px;
|
|
margin-bottom: 32px;
|
|
}
|
|
|
|
.nav-content {
|
|
max-width: 1400px;
|
|
margin: 0 auto;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
}
|
|
|
|
.brand {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.brand-text {
|
|
font-family: 'Space Grotesk', sans-serif;
|
|
font-size: 20px;
|
|
font-weight: 600;
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.nav-links {
|
|
display: flex;
|
|
gap: 24px;
|
|
align-items: center;
|
|
}
|
|
|
|
.user-chip {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
padding: 8px 10px;
|
|
border: 1px solid #e9ecef;
|
|
border-radius: 12px;
|
|
text-decoration: none;
|
|
color: inherit;
|
|
background: #fff;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.user-chip:hover {
|
|
border-color: var(--shopify-green);
|
|
background: #f8f9fa;
|
|
}
|
|
|
|
.user-chip-avatar {
|
|
width: 34px;
|
|
height: 34px;
|
|
border-radius: 999px;
|
|
background: var(--shopify-green-light);
|
|
color: var(--shopify-green);
|
|
display: grid;
|
|
place-items: center;
|
|
font-weight: 700;
|
|
letter-spacing: 0.02em;
|
|
text-transform: uppercase;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.user-chip-status {
|
|
font-size: 11px;
|
|
color: #6c757d;
|
|
letter-spacing: 0.04em;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.user-chip-email {
|
|
font-size: 13px;
|
|
font-weight: 700;
|
|
color: #1a1a1a;
|
|
max-width: 160px;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
.user-menu-container {
|
|
position: relative;
|
|
display: inline-block;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<nav class="nav-bar">
|
|
<div class="nav-content">
|
|
<a href="/" class="brand">
|
|
<img src="/assets/Plugin.png" alt="Plugin Compass" style="width: 32px; height: 32px; border-radius: 8px;">
|
|
<span class="brand-text">Plugin Compass</span>
|
|
</a>
|
|
<div class="nav-links">
|
|
<div class="user-menu-container">
|
|
<div class="user-chip" id="topup-user-chip" title="Admin">
|
|
<div class="user-chip-avatar" id="topup-user-avatar">ADM</div>
|
|
<div style="display:flex; flex-direction:column; gap:2px; min-width:0;">
|
|
<span class="user-chip-status">Admin</span>
|
|
<span class="user-chip-email" id="topup-user-email">Checking admin…</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="shell">
|
|
<div class="topbar">
|
|
<div>
|
|
<div class="crumb">Admin Tools</div>
|
|
<h1 class="title">Test Payments Checkout</h1>
|
|
<p class="subtitle">This is an admin-only test page for exercising the full Dodo Payments checkout flow.</p>
|
|
</div>
|
|
<div class="actions">
|
|
<a class="btn ghost" href="/admin">Admin Dashboard</a>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="status" class="status"></div>
|
|
|
|
<div class="currency-section">
|
|
<label class="currency-label">Select Currency</label>
|
|
<div class="relative currency-dropdown" id="currency-dropdown">
|
|
<button id="currency-btn" class="currency-select flex items-center gap-2">
|
|
<span id="currency-flag">🇺🇸</span>
|
|
<span id="currency-code">USD</span>
|
|
</button>
|
|
<div id="currency-options" class="hidden absolute top-full left-0 mt-1 w-full bg-white border border-gray-200 rounded-xl shadow-lg z-50 overflow-hidden">
|
|
<button class="currency-option w-full px-4 py-2 text-sm text-gray-700 hover:bg-green-50 hover:text-gray-900 font-medium text-left flex items-center gap-2" data-value="USD">
|
|
🇺🇸 $<span class="ml-1">USD</span>
|
|
</button>
|
|
<button class="currency-option w-full px-4 py-2 text-sm text-gray-700 hover:bg-green-50 hover:text-gray-900 font-medium text-left flex items-center gap-2" data-value="GBP">
|
|
🇬🇧 £<span class="ml-1">GBP</span>
|
|
</button>
|
|
<button class="currency-option w-full px-4 py-2 text-sm text-gray-700 hover:bg-green-50 hover:text-gray-900 font-medium text-left flex items-center gap-2" data-value="EUR">
|
|
🇪🇺 €<span class="ml-1">EUR</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="products-label">Available Top-up Packages</div>
|
|
<div id="cards" class="grid"></div>
|
|
</div>
|
|
|
|
<script>
|
|
// Minimal JS patched from topup.js for admin flows: uses /api/admin/topups/* endpoints and does not require a user session.
|
|
const statusEl = document.getElementById('status');
|
|
const cardsEl = document.getElementById('cards');
|
|
let loading = false;
|
|
let currentOptions = [];
|
|
let currentDiscount = 0;
|
|
let selectedCurrency = 'USD';
|
|
|
|
const currencyConfig = {
|
|
USD: { symbol: '$', flag: '🇺🇸', rate: 1 },
|
|
EUR: { symbol: '€', flag: '🇪🇺', rate: 0.85 },
|
|
GBP: { symbol: '£', flag: '🇬🇧', rate: 0.73 }
|
|
};
|
|
|
|
function showStatus(message, type = 'success') {
|
|
statusEl.textContent = message;
|
|
statusEl.className = 'status show ' + type;
|
|
}
|
|
|
|
function clearStatus() {
|
|
statusEl.textContent = '';
|
|
statusEl.className = 'status';
|
|
}
|
|
|
|
function formatMoney(amount, currency = 'usd') {
|
|
const config = currencyConfig[currency.toUpperCase()] || currencyConfig.USD;
|
|
return `${config.symbol}${(amount / 100).toFixed(2)}`;
|
|
}
|
|
|
|
async function fetchJson(url, opts = {}) {
|
|
const res = await fetch(url, { credentials: 'same-origin', ...opts });
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok) {
|
|
const error = data.error || 'Request failed';
|
|
throw new Error(error);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
async function loadAdminInfo() {
|
|
try {
|
|
const data = await fetchJson('/api/admin/me');
|
|
if (data && data.admin) {
|
|
document.getElementById('topup-user-email').textContent = data.admin.username || 'admin';
|
|
document.getElementById('topup-user-avatar').textContent = (data.admin.username || 'A').charAt(0).toUpperCase();
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to load admin info:', err);
|
|
// If we get a 401 or any authentication error, redirect to admin login
|
|
if (err.message.includes('Admin login required') || err.message.includes('login required')) {
|
|
window.location.href = '/admin/login?next=' + encodeURIComponent(window.location.pathname);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
function renderOptions(options, discountRate) {
|
|
currentOptions = options;
|
|
currentDiscount = discountRate;
|
|
|
|
// Filter options by selected currency
|
|
const filteredOptions = options.filter(opt => opt.currency === selectedCurrency.toLowerCase());
|
|
cardsEl.innerHTML = '';
|
|
|
|
filteredOptions.forEach((opt) => {
|
|
const card = document.createElement('div');
|
|
card.className = 'card';
|
|
const priceLine = document.createElement('div');
|
|
priceLine.className = 'price';
|
|
const finalPrice = formatMoney(opt.finalAmount, opt.currency);
|
|
const basePrice = formatMoney(opt.baseAmount, opt.currency);
|
|
if (opt.finalAmount !== opt.baseAmount) {
|
|
priceLine.innerHTML = `${finalPrice} <span class="old-price">${basePrice}</span>`;
|
|
} else {
|
|
priceLine.textContent = finalPrice;
|
|
}
|
|
const tokenLine = document.createElement('div');
|
|
tokenLine.className = 'muted';
|
|
tokenLine.textContent = `${Number(opt.tokens || 0).toLocaleString()} token credits`;
|
|
const discount = document.createElement('div');
|
|
discount.className = 'discount';
|
|
discount.textContent = discountRate > 0 ? `Plan discount applied: ${(discountRate * 100).toFixed(1)}%` : 'Admin test price';
|
|
const badge = document.createElement('span');
|
|
badge.className = 'pill';
|
|
const tierNum = opt.tier.startsWith('topup_') ? opt.tier.split('_')[1] : '';
|
|
badge.textContent = tierNum ? `Option ${tierNum} pack` : opt.tier.toUpperCase() + ' pack';
|
|
const button = document.createElement('button');
|
|
button.className = 'btn primary';
|
|
button.textContent = loading ? 'Preparing…' : 'Start test checkout';
|
|
button.disabled = loading;
|
|
button.onclick = () => startCheckout(opt.tier, opt.currency);
|
|
|
|
card.appendChild(badge);
|
|
card.appendChild(priceLine);
|
|
card.appendChild(tokenLine);
|
|
card.appendChild(discount);
|
|
card.appendChild(button);
|
|
cardsEl.appendChild(card);
|
|
});
|
|
}
|
|
|
|
async function loadOptions() {
|
|
try {
|
|
const data = await fetchJson('/api/admin/topups/options');
|
|
if (data && data.options) {
|
|
renderOptions(data.options, data.discount || 0);
|
|
} else {
|
|
throw new Error('Invalid options response');
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to load top-up options:', err);
|
|
const message = err && err.message ? `Failed to load top-up options: ${err.message}` : 'Failed to load top-up options. Please refresh the page.';
|
|
showStatus(message, 'error');
|
|
}
|
|
}
|
|
|
|
async function startCheckout(tier, currency) {
|
|
if (loading) return;
|
|
loading = true;
|
|
renderOptions(currentOptions, currentDiscount);
|
|
clearStatus();
|
|
|
|
try {
|
|
const res = await fetch('/api/admin/topups/checkout', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ tier, currency, inline: true })
|
|
});
|
|
const data = await res.json().catch(() => ({}));
|
|
|
|
if (!res.ok) throw new Error(data.error || 'Checkout failed');
|
|
|
|
if (data.sessionId && data.checkoutUrl) {
|
|
localStorage.setItem('dodo_admin_topup_session_id', data.sessionId);
|
|
localStorage.setItem('dodo_admin_topup_session_started', Date.now().toString());
|
|
|
|
// Use Dodo Payments Inline Checkout
|
|
await openInlineCheckout(data.inlineCheckoutUrl || data.checkoutUrl, data.sessionId);
|
|
} else {
|
|
throw new Error('Invalid checkout response from server');
|
|
}
|
|
} catch (err) {
|
|
console.error('Checkout error:', err);
|
|
showStatus(err.message || 'Failed to start checkout', 'error');
|
|
} finally {
|
|
loading = false;
|
|
renderOptions(currentOptions, currentDiscount);
|
|
}
|
|
}
|
|
|
|
async function ensureDodoPaymentsLoaded() {
|
|
if (typeof DodoPaymentsCheckout !== 'undefined' && DodoPaymentsCheckout.DodoPayments) {
|
|
return true;
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
const timeout = setTimeout(() => {
|
|
console.warn('DodoPayments script load timeout');
|
|
resolve(false);
|
|
}, 5000);
|
|
|
|
const checkInterval = setInterval(() => {
|
|
if (typeof DodoPaymentsCheckout !== 'undefined' && DodoPaymentsCheckout.DodoPayments) {
|
|
clearTimeout(timeout);
|
|
clearInterval(checkInterval);
|
|
resolve(true);
|
|
}
|
|
}, 100);
|
|
});
|
|
}
|
|
|
|
function closeCheckoutModal() {
|
|
const modalOverlay = document.getElementById('checkout-modal-overlay');
|
|
if (modalOverlay) {
|
|
modalOverlay.remove();
|
|
}
|
|
}
|
|
|
|
async function openInlineCheckout(checkoutUrl, sessionId) {
|
|
// Create a modal overlay for the inline checkout
|
|
const modalOverlay = document.createElement('div');
|
|
modalOverlay.id = 'checkout-modal-overlay';
|
|
modalOverlay.style.cssText = `
|
|
position: fixed;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 100%;
|
|
background: rgba(0, 0, 0, 0.7);
|
|
z-index: 9998;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
`;
|
|
|
|
// Create the checkout container
|
|
const checkoutContainer = document.createElement('div');
|
|
checkoutContainer.id = 'checkout-container';
|
|
checkoutContainer.style.cssText = `
|
|
width: 100%;
|
|
max-width: 500px;
|
|
height: 600px;
|
|
background: white;
|
|
border-radius: 16px;
|
|
position: relative;
|
|
overflow: hidden;
|
|
`;
|
|
|
|
// Create close button
|
|
const closeButton = document.createElement('button');
|
|
closeButton.innerHTML = '×';
|
|
closeButton.style.cssText = `
|
|
position: absolute;
|
|
top: 15px;
|
|
right: 15px;
|
|
width: 32px;
|
|
height: 32px;
|
|
border-radius: 50%;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
border: 1px solid var(--border);
|
|
font-size: 20px;
|
|
cursor: pointer;
|
|
z-index: 10000;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
color: var(--muted);
|
|
transition: all 0.2s ease;
|
|
`;
|
|
closeButton.onmouseover = () => {
|
|
closeButton.style.background = '#f8fafc';
|
|
closeButton.style.color = '#1f2937';
|
|
};
|
|
closeButton.onmouseout = () => {
|
|
closeButton.style.background = 'rgba(255, 255, 255, 0.9)';
|
|
closeButton.style.color = 'var(--muted)';
|
|
};
|
|
closeButton.onclick = () => {
|
|
closeCheckoutModal();
|
|
showStatus('Payment cancelled', 'error');
|
|
};
|
|
|
|
modalOverlay.appendChild(checkoutContainer);
|
|
modalOverlay.appendChild(closeButton);
|
|
document.body.appendChild(modalOverlay);
|
|
|
|
const loadingIndicator = document.createElement('div');
|
|
loadingIndicator.style.cssText = `
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
color: #6b7280;
|
|
font-size: 16px;
|
|
z-index: 1000;
|
|
`;
|
|
loadingIndicator.textContent = 'Loading secure payment...';
|
|
checkoutContainer.appendChild(loadingIndicator);
|
|
|
|
await ensureDodoPaymentsLoaded();
|
|
|
|
if (loadingIndicator.parentNode) {
|
|
checkoutContainer.removeChild(loadingIndicator);
|
|
}
|
|
|
|
if (typeof DodoPaymentsCheckout !== 'undefined' && DodoPaymentsCheckout.DodoPayments && DodoPaymentsCheckout.DodoPayments.Checkout) {
|
|
try {
|
|
if (!DodoPaymentsCheckout.DodoPayments.Initialize) {
|
|
console.warn('DodoPayments.Initialize not available, checking for Checkout.open directly');
|
|
} else {
|
|
DodoPaymentsCheckout.DodoPayments.Initialize({
|
|
mode: 'test'
|
|
});
|
|
}
|
|
|
|
DodoPaymentsCheckout.DodoPayments.Checkout.open({
|
|
checkoutUrl: checkoutUrl,
|
|
elementId: 'checkout-container',
|
|
options: {
|
|
themeConfig: {
|
|
light: {
|
|
bgPrimary: '#FFFFFF',
|
|
bgSecondary: '#F8FAFC',
|
|
textPrimary: '#0F172A',
|
|
textSecondary: '#6B7280',
|
|
textPlaceholder: '#9CA3AF',
|
|
textError: '#EF4444',
|
|
textSuccess: '#10B981',
|
|
borderPrimary: '#E5E7EB',
|
|
borderSecondary: '#D1D5DB',
|
|
inputFocusBorder: '#008060',
|
|
buttonPrimary: '#008060',
|
|
buttonPrimaryHover: '#004C3F',
|
|
buttonTextPrimary: '#FFFFFF',
|
|
buttonSecondary: '#FFFFFF',
|
|
buttonSecondaryHover: '#F8FAFC',
|
|
buttonTextSecondary: '#0F172A'
|
|
},
|
|
dark: {
|
|
bgPrimary: '#0D0D0D',
|
|
bgSecondary: '#1F2937',
|
|
textPrimary: '#F9FAFB',
|
|
textSecondary: '#9CA3AF',
|
|
textPlaceholder: '#6B7280',
|
|
textError: '#F87171',
|
|
textSuccess: '#34D399',
|
|
borderPrimary: '#374151',
|
|
borderSecondary: '#4B5563',
|
|
inputFocusBorder: '#10B981',
|
|
buttonPrimary: '#008060',
|
|
buttonPrimaryHover: '#10B981',
|
|
buttonTextPrimary: '#FFFFFF',
|
|
buttonSecondary: '#1F2937',
|
|
buttonSecondaryHover: '#374151',
|
|
buttonTextSecondary: '#F9FAFB'
|
|
},
|
|
radius: '8px'
|
|
},
|
|
payButtonText: 'Complete Payment',
|
|
fontSize: '14px',
|
|
fontWeight: '500',
|
|
showTimer: true,
|
|
showSecurityBadge: true
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('DodoPayments.Checkout.open failed:', error);
|
|
|
|
const errorMessage = document.createElement('div');
|
|
errorMessage.style.cssText = `
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
background: white;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
|
text-align: center;
|
|
max-width: 80%;
|
|
z-index: 1001;
|
|
`;
|
|
errorMessage.innerHTML = `
|
|
<h3 style="color: #ef4444; margin-bottom: 10px;">Payment Processing</h3>
|
|
<p style="color: #6b7280; margin-bottom: 15px;">
|
|
The secure payment method failed to load. Falling back to standard payment page.
|
|
</p>
|
|
<p style="color: #6b7280; font-size: 14px;">
|
|
This may take a moment to redirect...
|
|
</p>
|
|
`;
|
|
checkoutContainer.appendChild(errorMessage);
|
|
|
|
setTimeout(() => {
|
|
checkoutContainer.innerHTML = '';
|
|
const iframe = document.createElement('iframe');
|
|
iframe.src = checkoutUrl;
|
|
iframe.allowpaymentrequest = "true";
|
|
iframe.style.cssText = 'width: 100%; height: 100%; border: none;';
|
|
iframe.onload = () => {
|
|
if (loadingIndicator.parentNode) {
|
|
loadingIndicator.remove();
|
|
}
|
|
};
|
|
checkoutContainer.appendChild(iframe);
|
|
}, 2000);
|
|
}
|
|
} else {
|
|
console.warn('DodoPayments not available, using iframe fallback');
|
|
console.log('DodoPaymentsCheckout object:', typeof DodoPaymentsCheckout, DodoPaymentsCheckout);
|
|
console.log('DodoPayments object:', typeof DodoPaymentsCheckout?.DodoPayments, DodoPaymentsCheckout?.DodoPayments);
|
|
|
|
const fallbackMessage = document.createElement('div');
|
|
fallbackMessage.style.cssText = `
|
|
position: absolute;
|
|
top: 50%;
|
|
left: 50%;
|
|
transform: translate(-50%, -50%);
|
|
background: white;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
|
text-align: center;
|
|
max-width: 80%;
|
|
z-index: 1001;
|
|
`;
|
|
fallbackMessage.innerHTML = `
|
|
<h3 style="color: #008060; margin-bottom: 10px;">Processing Payment</h3>
|
|
<p style="color: #6b7280; margin-bottom: 15px;">
|
|
Loading secure payment page...
|
|
</p>
|
|
<p style="color: #6b7280; font-size: 14px;">
|
|
Please wait while we prepare your payment.
|
|
</p>
|
|
`;
|
|
checkoutContainer.appendChild(fallbackMessage);
|
|
|
|
setTimeout(() => {
|
|
checkoutContainer.innerHTML = '';
|
|
const iframe = document.createElement('iframe');
|
|
iframe.src = checkoutUrl;
|
|
iframe.allowpaymentrequest = "true";
|
|
iframe.style.cssText = 'width: 100%; height: 100%; border: none;';
|
|
iframe.onload = () => {
|
|
if (loadingIndicator.parentNode) {
|
|
loadingIndicator.remove();
|
|
}
|
|
};
|
|
checkoutContainer.appendChild(iframe);
|
|
}, 1500);
|
|
}
|
|
|
|
// Poll for admin confirmation
|
|
let pollCount = 0;
|
|
const maxPolls = 180;
|
|
const pollInterval = 1000;
|
|
|
|
const checkPaymentStatus = async () => {
|
|
try {
|
|
pollCount++;
|
|
|
|
const resp = await fetch(`/api/admin/topups/confirm?session_id=${encodeURIComponent(sessionId)}`, {
|
|
credentials: 'include'
|
|
});
|
|
|
|
if (resp.ok) {
|
|
const data = await resp.json();
|
|
if (data.ok && data.paid) {
|
|
clearInterval(pollTimer);
|
|
closeCheckoutModal();
|
|
showStatus('Success! Admin test checkout paid.', 'success');
|
|
localStorage.removeItem('dodo_admin_topup_session_id');
|
|
localStorage.removeItem('dodo_admin_topup_session_started');
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (pollCount >= maxPolls) {
|
|
clearInterval(pollTimer);
|
|
closeCheckoutModal();
|
|
showStatus('Payment confirmation timeout. Please check the Dodo dashboard.', 'error');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error checking payment status:', error);
|
|
}
|
|
};
|
|
|
|
const pollTimer = setInterval(checkPaymentStatus, pollInterval);
|
|
}
|
|
|
|
|
|
async function confirmIfNeeded() {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const sessionId = params.get('session_id') || params.get('checkout_id') || params.get('session') || localStorage.getItem('dodo_topup_session_id');
|
|
if (!sessionId) {
|
|
if (params.get('cancel')) showStatus('Checkout canceled.', 'pending');
|
|
return;
|
|
}
|
|
showStatus('Confirming admin checkout…', 'pending');
|
|
try {
|
|
const data = await fetchJson('/api/admin/topups/confirm?session_id=' + encodeURIComponent(sessionId));
|
|
if (data?.ok && data?.paid) {
|
|
showStatus('Payment confirmed (admin)', 'success');
|
|
localStorage.removeItem('dodo_admin_topup_session_id');
|
|
localStorage.removeItem('dodo_admin_topup_session_started');
|
|
params.delete('session_id');
|
|
params.delete('checkout_id');
|
|
params.delete('session');
|
|
window.history.replaceState({}, document.title, window.location.pathname + (params.toString() ? '?' + params.toString() : ''));
|
|
} else if (data?.pending) {
|
|
showStatus('Payment pending. Refresh in a moment.', 'pending');
|
|
} else {
|
|
showStatus(data?.error || 'Could not confirm payment.', 'error');
|
|
}
|
|
} catch (err) {
|
|
showStatus(err.message, 'error');
|
|
}
|
|
}
|
|
|
|
// Currency dropdown setup copied from topup.html (kept minimal)
|
|
function updateCurrencyDisplay() {
|
|
const config = currencyConfig[selectedCurrency] || currencyConfig.USD;
|
|
const flagEl = document.getElementById('currency-flag');
|
|
const codeEl = document.getElementById('currency-code');
|
|
if (flagEl) flagEl.textContent = config.flag;
|
|
if (codeEl) codeEl.textContent = selectedCurrency;
|
|
}
|
|
|
|
function setupCurrencyDropdown() {
|
|
const dropdown = document.getElementById('currency-dropdown');
|
|
const btn = document.getElementById('currency-btn');
|
|
const options = document.getElementById('currency-options');
|
|
|
|
if (!dropdown || !btn || !options) return;
|
|
|
|
btn.addEventListener('click', (e) => {
|
|
e.stopPropagation();
|
|
const isOpen = !options.classList.contains('hidden');
|
|
|
|
if (isOpen) {
|
|
options.classList.add('hidden');
|
|
dropdown.classList.remove('open');
|
|
} else {
|
|
options.classList.remove('hidden');
|
|
dropdown.classList.add('open');
|
|
}
|
|
});
|
|
|
|
document.querySelectorAll('.currency-option').forEach(option => {
|
|
option.addEventListener('click', (e) => {
|
|
e.preventDefault();
|
|
const currency = option.getAttribute('data-value');
|
|
selectedCurrency = currency;
|
|
updateCurrencyDisplay();
|
|
renderOptions(currentOptions, currentDiscount);
|
|
options.classList.add('hidden');
|
|
dropdown.classList.remove('open');
|
|
});
|
|
});
|
|
|
|
document.addEventListener('click', () => {
|
|
options.classList.add('hidden');
|
|
dropdown.classList.remove('open');
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
try {
|
|
setupCurrencyDropdown();
|
|
updateCurrencyDisplay();
|
|
await loadAdminInfo();
|
|
await loadOptions();
|
|
await confirmIfNeeded();
|
|
} catch (err) {
|
|
showStatus(err.message, 'error');
|
|
}
|
|
})();
|
|
</script>
|
|
|
|
<footer style="margin-top: 80px; padding: 40px 24px; border-top: 1px solid #e1e3e5; text-align: center; color: #6d7175; font-size: 0.875rem;">
|
|
<p>© 2026 Plugin Compass. Admin test page.</p>
|
|
</footer>
|
|
</body>
|
|
|
|
</html> |