// --- 4. payment.js --- function goToStep2(amount, type, label, usd) { selectedPkg = { amount, type, label, usd }; document.getElementById('step-pkg').classList.add('hidden'); document.getElementById('step-method').classList.remove('hidden'); } async function showPayment(method) { if (!currentUser) return; const msg = `${selectedPkg.type}_${currentUser.id.substring(0,8)}`; const qrDisplay = document.getElementById('qr-display'), titleBox = document.getElementById('qr-title-box'), qrImg = document.getElementById('qr-img'), ppContainer = document.getElementById('paypal-button-container'), note = document.getElementById('qr-note'); document.getElementById('step-method').classList.add('hidden'); qrDisplay.classList.remove('hidden'); if (method === 'BANK') { ppContainer.classList.add('hidden'); document.getElementById('copy-btn').classList.remove('hidden'); qrImg.style.display = 'block'; titleBox.innerHTML = `Nội dung chuyển khoản:
${msg}`; note.innerHTML = "Hệ thống sẽ tự động kích hoạt sau khi nhận được tiền.
Vui lòng chuyển đúng nội dung!"; try { const res = await fetch(`${CONFIG.SB_URL}/functions/v1/create-order`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ amount: selectedPkg.amount, description: msg }) }); const d = await res.json(); qrImg.src = `https://api.qrserver.com/v1/create-qr-code/?size=260x260&data=${encodeURIComponent(d.qrCode)}`; } catch(e) {} } else { qrImg.style.display = 'none'; document.getElementById('copy-btn').classList.add('hidden'); ppContainer.classList.remove('hidden'); titleBox.innerHTML = `Thanh toán PayPal $${selectedPkg.usd}`; note.innerHTML = "Sau khi thanh toán, VIP sẽ được kích hoạt tự động ngay lập tức."; loadPayPalButton(); } } function getPayPalObject() { return window.paypal || (unsafeWindow && unsafeWindow.paypal) || null; } function loadPayPalButton() { const container = document.getElementById('paypal-button-container'); container.innerHTML = "

Đang tải PayPal...

"; if (unsafeWindow && unsafeWindow.paypal) { renderPayPalNative(); } else { const script = document.createElement('script'); script.src = `https://www.paypal.com/sdk/js?client-id=${CONFIG.PAYPAL_CLIENT_ID}¤cy=USD`; script.onload = () => renderPayPalNative(); script.onerror = () => container.innerHTML = "

Lỗi tải PayPal. Vui lòng tắt chặn quảng cáo.

"; document.head.insertAdjacentElement('beforeend', script); } } function renderPayPalNative() { const inlineScript = document.createElement('script'); inlineScript.textContent = ` if (window.paypal && window.paypal.Buttons) { document.getElementById('paypal-button-container').innerHTML = ''; window.paypal.Buttons({ style: { color: 'blue', shape: 'pill', label: 'pay', height: 48 }, createOrder: function(data, actions) { return actions.order.create({ purchase_units: [{ amount: { value: '${selectedPkg.usd}' } }] }); }, onApprove: function(data, actions) { return actions.order.capture().then(function(details) { document.dispatchEvent(new CustomEvent('XV_PAYPAL_SUCCESS')); }); } }).render('#paypal-button-container'); } else { document.getElementById('paypal-button-container').innerHTML = "

Không thể khởi tạo PayPal API.

"; } `; document.body.insertAdjacentElement('beforeend', inlineScript); } document.addEventListener('XV_PAYPAL_SUCCESS', async () => { document.getElementById('paypal-button-container').innerHTML = "

Đang kích hoạt VIP...

"; await autoActivateVIP(); }); async function autoActivateVIP() { try { const shortId = currentUser.id.substring(0,8); const { error } = await supabaseClient.rpc('activate_vip_by_short_id', { short_id: shortId, pkg_type: selectedPkg.type }); if (error) throw error; showToast("🎉 VIP đã được kích hoạt thành công!", 'success'); setTimeout(() => location.reload(), 1800); } catch (err) { showToast("Lỗi kích hoạt: " + err.message, 'error'); } }