Generaliza los servicios de personaje de pago (5) con infra común
- lib/paid-services.ts: config PAID_SERVICES (rename/customize/change-race/ change-faction/level-up) con comando SOAP, precio y productName por servicio. - app/api/character/[service]/checkout: ruta de checkout GENÉRICA (valida servicio + personaje, crea la sesión Stripe con successUrl /service-success?service=...). - app/[locale]/service-success: página de éxito ÚNICA que verifica el pago (claimPaidCheckout) y ejecuta el comando SOAP del servicio. - components/ServicePageContent: contenido común (guardas + PaidServiceForm). - 5 páginas mínimas /rename /customize /change-race /change-faction /level-up. - Catálogo Paid (por servicio: title/pay/success) sustituye a Rename. Verificado: 5 páginas redirigen a login, checkout 401 sin sesión, servicio inválido 404, service-success con pago inválido no entrega (muestra error). Pendiente: gold (cantidad) y transfer (destino+token), variantes del patrón. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
import { ServicePageContent } from '@/components/ServicePageContent'
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
||||||
|
const { locale } = await params
|
||||||
|
return <ServicePageContent service="change-faction" locale={locale} />
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { ServicePageContent } from '@/components/ServicePageContent'
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
||||||
|
const { locale } = await params
|
||||||
|
return <ServicePageContent service="change-race" locale={locale} />
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { ServicePageContent } from '@/components/ServicePageContent'
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
||||||
|
const { locale } = await params
|
||||||
|
return <ServicePageContent service="customize" locale={locale} />
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { ServicePageContent } from '@/components/ServicePageContent'
|
||||||
|
|
||||||
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
|
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
||||||
|
const { locale } = await params
|
||||||
|
return <ServicePageContent service="level-up" locale={locale} />
|
||||||
|
}
|
||||||
@@ -1,31 +1,8 @@
|
|||||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
import { ServicePageContent } from '@/components/ServicePageContent'
|
||||||
import { redirect } from '@/i18n/navigation'
|
|
||||||
import { getSession } from '@/lib/session'
|
|
||||||
import { getGameCharacters } from '@/lib/characters'
|
|
||||||
import { getRenamePrice } from '@/lib/prices'
|
|
||||||
import { PaidServiceForm } from '@/components/PaidServiceForm'
|
|
||||||
|
|
||||||
export const dynamic = 'force-dynamic'
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
export default async function RenamePage({ params }: { params: Promise<{ locale: string }> }) {
|
export default async function Page({ params }: { params: Promise<{ locale: string }> }) {
|
||||||
const { locale } = await params
|
const { locale } = await params
|
||||||
setRequestLocale(locale)
|
return <ServicePageContent service="rename" locale={locale} />
|
||||||
const t = await getTranslations('Rename')
|
|
||||||
|
|
||||||
const session = await getSession()
|
|
||||||
if (!session.bnetId) redirect({ href: '/login', locale })
|
|
||||||
if (!session.username) redirect({ href: '/select-account', locale })
|
|
||||||
|
|
||||||
const [chars, price] = await Promise.all([getGameCharacters(session.accountId!), getRenamePrice()])
|
|
||||||
|
|
||||||
return (
|
|
||||||
<main className="mx-auto max-w-3xl px-4 py-8">
|
|
||||||
<h1 className="mb-6 text-center text-2xl font-bold text-amber-500">{t('title')}</h1>
|
|
||||||
<PaidServiceForm
|
|
||||||
characters={chars.map((c) => c.name)}
|
|
||||||
checkoutEndpoint="/api/character/rename/checkout"
|
|
||||||
payLabel={t('pay', { price })}
|
|
||||||
/>
|
|
||||||
</main>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-9
@@ -1,36 +1,38 @@
|
|||||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||||
import { claimPaidCheckout } from '@/lib/stripe'
|
import { claimPaidCheckout } from '@/lib/stripe'
|
||||||
import { executeSoapCommand } from '@/lib/soap'
|
import { executeSoapCommand } from '@/lib/soap'
|
||||||
|
import { getPaidService } from '@/lib/paid-services'
|
||||||
|
|
||||||
export const dynamic = 'force-dynamic'
|
export const dynamic = 'force-dynamic'
|
||||||
|
|
||||||
export default async function RenameSuccessPage({
|
export default async function ServiceSuccessPage({
|
||||||
params,
|
params,
|
||||||
searchParams,
|
searchParams,
|
||||||
}: {
|
}: {
|
||||||
params: Promise<{ locale: string }>
|
params: Promise<{ locale: string }>
|
||||||
searchParams: Promise<{ session_id?: string }>
|
searchParams: Promise<{ service?: string; session_id?: string }>
|
||||||
}) {
|
}) {
|
||||||
const { locale } = await params
|
const { locale } = await params
|
||||||
setRequestLocale(locale)
|
setRequestLocale(locale)
|
||||||
const { session_id } = await searchParams
|
const { service, session_id } = await searchParams
|
||||||
const t = await getTranslations('Rename')
|
const t = await getTranslations('Paid')
|
||||||
|
const cfg = service ? getPaidService(service) : null
|
||||||
|
|
||||||
// Verifica el pago (una sola vez) y ejecuta el comando SOAP.
|
// Verifica el pago (una vez) y ejecuta el comando SOAP del servicio.
|
||||||
let okName: string | null = null
|
let okName: string | null = null
|
||||||
if (session_id) {
|
if (cfg && session_id) {
|
||||||
const claim = await claimPaidCheckout(session_id)
|
const claim = await claimPaidCheckout(session_id)
|
||||||
if (claim) {
|
if (claim) {
|
||||||
const resp = await executeSoapCommand(`.char rename ${claim.characterName}`)
|
const resp = await executeSoapCommand(cfg.command(claim.characterName))
|
||||||
if (resp !== null) okName = claim.characterName
|
if (resp !== null) okName = claim.characterName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="mx-auto max-w-lg px-4 py-12 text-center">
|
<main className="mx-auto max-w-lg px-4 py-12 text-center">
|
||||||
<h1 className="mb-6 text-2xl font-bold text-amber-500">{t('successTitle')}</h1>
|
<h1 className="mb-6 text-2xl font-bold text-amber-500">{service ? t(`${service}.title`) : ''}</h1>
|
||||||
{okName ? (
|
{okName ? (
|
||||||
<p className="text-green-400">{t('success', { name: okName })}</p>
|
<p className="text-green-400">{t(`${service}.success`, { name: okName })}</p>
|
||||||
) : (
|
) : (
|
||||||
<p className="text-red-400">{t('error')}</p>
|
<p className="text-red-400">{t('error')}</p>
|
||||||
)}
|
)}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { getSession } from '@/lib/session'
|
||||||
|
import { getGameCharacters } from '@/lib/characters'
|
||||||
|
import { createCheckoutSession } from '@/lib/stripe'
|
||||||
|
import { getPaidService } from '@/lib/paid-services'
|
||||||
|
|
||||||
|
export async function POST(request: Request, { params }: { params: Promise<{ service: string }> }) {
|
||||||
|
const { service } = await params
|
||||||
|
const cfg = getPaidService(service)
|
||||||
|
if (!cfg) return Response.json({ success: false, error: 'invalidService' }, { status: 404 })
|
||||||
|
|
||||||
|
const session = await getSession()
|
||||||
|
if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
|
||||||
|
|
||||||
|
let character = ''
|
||||||
|
try {
|
||||||
|
const body = await request.json()
|
||||||
|
character = String(body.character ?? '')
|
||||||
|
} catch {
|
||||||
|
return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
|
||||||
|
}
|
||||||
|
if (!character) return Response.json({ success: false, error: 'invalidCharacter' })
|
||||||
|
|
||||||
|
const chars = await getGameCharacters(session.accountId)
|
||||||
|
if (!chars.find((c) => c.name === character)) {
|
||||||
|
return Response.json({ success: false, error: 'invalidCharacter' })
|
||||||
|
}
|
||||||
|
|
||||||
|
const price = await cfg.getPrice()
|
||||||
|
const site = process.env.SITE_URL || ''
|
||||||
|
const ip = (request.headers.get('x-forwarded-for') || '').split(',')[0].trim() || '0.0.0.0'
|
||||||
|
|
||||||
|
const r = await createCheckoutSession({
|
||||||
|
accountId: session.accountId,
|
||||||
|
username: session.username || '',
|
||||||
|
email: session.bnetEmail || '',
|
||||||
|
acoreIp: ip,
|
||||||
|
amount: price,
|
||||||
|
characterName: character,
|
||||||
|
productName: cfg.productName(character),
|
||||||
|
successUrl: `${site}/service-success?service=${service}`,
|
||||||
|
cancelUrl: `${site}/${service}`,
|
||||||
|
})
|
||||||
|
if (!r.success || !r.url) return Response.json({ success: false, error: 'stripeError' })
|
||||||
|
return Response.json({ success: true, url: r.url })
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { notFound } from 'next/navigation'
|
||||||
|
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||||
|
import { redirect } from '@/i18n/navigation'
|
||||||
|
import { getSession } from '@/lib/session'
|
||||||
|
import { getGameCharacters } from '@/lib/characters'
|
||||||
|
import { getPaidService } from '@/lib/paid-services'
|
||||||
|
import { PaidServiceForm } from '@/components/PaidServiceForm'
|
||||||
|
|
||||||
|
/** Contenido común de una página de servicio de pago (guardas + form). */
|
||||||
|
export async function ServicePageContent({ service, locale }: { service: string; locale: string }) {
|
||||||
|
setRequestLocale(locale)
|
||||||
|
const cfg = getPaidService(service)
|
||||||
|
if (!cfg) notFound()
|
||||||
|
|
||||||
|
const t = await getTranslations('Paid')
|
||||||
|
const session = await getSession()
|
||||||
|
if (!session.bnetId) redirect({ href: '/login', locale })
|
||||||
|
if (!session.username) redirect({ href: '/select-account', locale })
|
||||||
|
|
||||||
|
const [chars, price] = await Promise.all([getGameCharacters(session.accountId!), cfg!.getPrice()])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="mx-auto max-w-3xl px-4 py-8">
|
||||||
|
<h1 className="mb-6 text-center text-2xl font-bold text-amber-500">{t(`${service}.title`)}</h1>
|
||||||
|
<PaidServiceForm
|
||||||
|
characters={chars.map((c) => c.name)}
|
||||||
|
checkoutEndpoint={`/api/character/${service}/checkout`}
|
||||||
|
payLabel={t(`${service}.pay`, { price })}
|
||||||
|
/>
|
||||||
|
</main>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import {
|
||||||
|
getRenamePrice,
|
||||||
|
getCustomizePrice,
|
||||||
|
getChangeRacePrice,
|
||||||
|
getChangeFactionPrice,
|
||||||
|
getLevelUpPrice,
|
||||||
|
} from './prices'
|
||||||
|
|
||||||
|
export interface PaidServiceConfig {
|
||||||
|
command: (character: string) => string
|
||||||
|
getPrice: () => Promise<number>
|
||||||
|
productName: (character: string) => string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Servicios de personaje de pago. slug = ruta (/rename, /customize, ...) y clave de
|
||||||
|
// catálogo (Paid.<slug>). Comandos SOAP tal cual las success views de Django.
|
||||||
|
export const PAID_SERVICES: Record<string, PaidServiceConfig> = {
|
||||||
|
rename: {
|
||||||
|
command: (c) => `.char rename ${c}`,
|
||||||
|
getPrice: getRenamePrice,
|
||||||
|
productName: (c) => `Renombrar personaje: ${c}`,
|
||||||
|
},
|
||||||
|
customize: {
|
||||||
|
command: (c) => `.char customi ${c}`,
|
||||||
|
getPrice: getCustomizePrice,
|
||||||
|
productName: (c) => `Personalizar personaje: ${c}`,
|
||||||
|
},
|
||||||
|
'change-race': {
|
||||||
|
command: (c) => `.char changerace ${c}`,
|
||||||
|
getPrice: getChangeRacePrice,
|
||||||
|
productName: (c) => `Cambiar raza: ${c}`,
|
||||||
|
},
|
||||||
|
'change-faction': {
|
||||||
|
command: (c) => `.char changef ${c}`,
|
||||||
|
getPrice: getChangeFactionPrice,
|
||||||
|
productName: (c) => `Cambiar facción: ${c}`,
|
||||||
|
},
|
||||||
|
'level-up': {
|
||||||
|
command: (c) => `.char level ${c} 80`,
|
||||||
|
getPrice: getLevelUpPrice,
|
||||||
|
productName: (c) => `Subir a nivel 80: ${c}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPaidService(slug: string): PaidServiceConfig | null {
|
||||||
|
return PAID_SERVICES[slug] ?? null
|
||||||
|
}
|
||||||
@@ -136,13 +136,32 @@
|
|||||||
"soapError": "Server communication error. Please try again later.",
|
"soapError": "Server communication error. Please try again later.",
|
||||||
"genericError": "Something went wrong. Please try again later."
|
"genericError": "Something went wrong. Please try again later."
|
||||||
},
|
},
|
||||||
"Rename": {
|
"Paid": {
|
||||||
|
"rename": {
|
||||||
"title": "Rename character",
|
"title": "Rename character",
|
||||||
"pay": "Rename for {price} €",
|
"pay": "Rename for {price} €",
|
||||||
"successTitle": "Rename character",
|
"success": "Character {name} has been flagged for rename (applies on next login)."
|
||||||
"success": "Character {name} has been flagged for rename. The change will apply on next login.",
|
},
|
||||||
"error": "The operation could not be completed. If you were charged, contact support.",
|
"customize": {
|
||||||
"invalidCharacter": "Select a valid character.",
|
"title": "Customize character",
|
||||||
"stripeError": "Could not start the payment. Please try again later."
|
"pay": "Customize for {price} €",
|
||||||
|
"success": "Character {name} has been flagged for customization."
|
||||||
|
},
|
||||||
|
"change-race": {
|
||||||
|
"title": "Change race",
|
||||||
|
"pay": "Change race for {price} €",
|
||||||
|
"success": "Character {name} has been flagged for race change."
|
||||||
|
},
|
||||||
|
"change-faction": {
|
||||||
|
"title": "Change faction",
|
||||||
|
"pay": "Change faction for {price} €",
|
||||||
|
"success": "Character {name} has changed faction successfully."
|
||||||
|
},
|
||||||
|
"level-up": {
|
||||||
|
"title": "Level up to 80",
|
||||||
|
"pay": "Level up to 80 for {price} €",
|
||||||
|
"success": "Character {name} has been leveled to 80."
|
||||||
|
},
|
||||||
|
"error": "The operation could not be completed. If you were charged, contact support."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -136,13 +136,32 @@
|
|||||||
"soapError": "Error de comunicación con el servidor. Inténtalo más tarde.",
|
"soapError": "Error de comunicación con el servidor. Inténtalo más tarde.",
|
||||||
"genericError": "Algo ha salido mal. Inténtalo más tarde."
|
"genericError": "Algo ha salido mal. Inténtalo más tarde."
|
||||||
},
|
},
|
||||||
"Rename": {
|
"Paid": {
|
||||||
|
"rename": {
|
||||||
"title": "Renombrar personaje",
|
"title": "Renombrar personaje",
|
||||||
"pay": "Renombrar por {price} €",
|
"pay": "Renombrar por {price} €",
|
||||||
"successTitle": "Renombrar personaje",
|
"success": "El personaje {name} ha sido marcado para renombrar (se aplica al próximo inicio de sesión)."
|
||||||
"success": "El personaje {name} ha sido marcado para renombrar. El cambio se aplicará en el próximo inicio de sesión.",
|
},
|
||||||
"error": "No se pudo completar la operación. Si se te ha cobrado, contacta con soporte.",
|
"customize": {
|
||||||
"invalidCharacter": "Selecciona un personaje válido.",
|
"title": "Personalizar personaje",
|
||||||
"stripeError": "No se pudo iniciar el pago. Inténtalo más tarde."
|
"pay": "Personalizar por {price} €",
|
||||||
|
"success": "El personaje {name} ha sido marcado para personalización."
|
||||||
|
},
|
||||||
|
"change-race": {
|
||||||
|
"title": "Cambiar de raza",
|
||||||
|
"pay": "Cambiar raza por {price} €",
|
||||||
|
"success": "El personaje {name} ha sido marcado para cambio de raza."
|
||||||
|
},
|
||||||
|
"change-faction": {
|
||||||
|
"title": "Cambiar de facción",
|
||||||
|
"pay": "Cambiar facción por {price} €",
|
||||||
|
"success": "El personaje {name} ha cambiado de facción con éxito."
|
||||||
|
},
|
||||||
|
"level-up": {
|
||||||
|
"title": "Subir a nivel 80",
|
||||||
|
"pay": "Subir a nivel 80 por {price} €",
|
||||||
|
"success": "El personaje {name} ha sido subido al nivel 80."
|
||||||
|
},
|
||||||
|
"error": "No se pudo completar la operación. Si se te ha cobrado, contacta con soporte."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user