2258df4b09
Componente genérico CharacterPurchaseForm (selector de personaje + botón con precio)
+ entry character_purchase, configurable por data-* en la plantilla. Al enviar, la
vista devuelve {session_id, stripe_public_key} y se redirige al Checkout de Stripe
(helper stripe.ts que carga Stripe.js). Reutilizable por rename/customize/race/
faction/level/gold/transfer... cambiando solo los data-* y la lista de personajes
(json_script). Primer uso: rename_character.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
interface StripeInstance {
|
|
redirectToCheckout: (opts: { sessionId: string }) => Promise<{ error?: { message: string } }>
|
|
}
|
|
|
|
declare global {
|
|
interface Window {
|
|
Stripe?: (key: string) => StripeInstance
|
|
}
|
|
}
|
|
|
|
const SCRIPT_ID = 'stripe-js'
|
|
|
|
/**
|
|
* Carga Stripe.js (una vez) y redirige al Checkout con el session_id dado.
|
|
* Rechaza si Stripe no carga o devuelve error.
|
|
*/
|
|
export function redirectToStripeCheckout(publicKey: string, sessionId: string): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
function go() {
|
|
if (!window.Stripe) return reject(new Error('Stripe no está disponible'))
|
|
const stripe = window.Stripe(publicKey)
|
|
stripe
|
|
.redirectToCheckout({ sessionId })
|
|
.then((r) => (r.error ? reject(new Error(r.error.message)) : resolve()))
|
|
.catch(reject)
|
|
}
|
|
|
|
if (window.Stripe) return go()
|
|
|
|
if (!document.getElementById(SCRIPT_ID)) {
|
|
const s = document.createElement('script')
|
|
s.id = SCRIPT_ID
|
|
s.src = 'https://js.stripe.com/v3/'
|
|
document.head.appendChild(s)
|
|
}
|
|
let waited = 0
|
|
const iv = window.setInterval(() => {
|
|
if (window.Stripe) {
|
|
window.clearInterval(iv)
|
|
go()
|
|
} else if ((waited += 150) > 8000) {
|
|
window.clearInterval(iv)
|
|
reject(new Error('No se pudo cargar Stripe.'))
|
|
}
|
|
}, 150)
|
|
})
|
|
}
|