Tienda Battlepay: pago de órdenes pendientes vía Stripe
Reutiliza la infraestructura Stripe (sin depender de SumUp/credenciales externas): - lib/battlepay.ts: getPendingOrders/getPendingOrder/markOrderPaid sobre acore_auth.battlepay_orders (PENDING->PAID, el mod-battlepay entrega en juego). - fulfill.ts: rama battlepay (metadata.battlepay_reference -> markOrderPaid), idempotente vía el claim atómico compartido con el webhook. - API POST /api/battlepay/pay (valida orden PENDING de la cuenta, crea checkout). - UI: /battlepay (lista + pagar) y /battlepay-success (entrega compartida), enlace "Tienda" en la cabecera para usuarios logueados. - i18n es/en: namespace Battlepay + Nav.store. Verificado: build OK, /battlepay 307->login, API 401 sin sesión, success 200. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||
import { Link } from '@/i18n/navigation'
|
||||
import { fulfillCheckoutSession } from '@/lib/fulfill'
|
||||
import { isSessionPaid } from '@/lib/stripe'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function BattlepaySuccessPage({
|
||||
params,
|
||||
searchParams,
|
||||
}: {
|
||||
params: Promise<{ locale: string }>
|
||||
searchParams: Promise<{ session_id?: string }>
|
||||
}) {
|
||||
const { locale } = await params
|
||||
setRequestLocale(locale)
|
||||
const { session_id } = await searchParams
|
||||
const t = await getTranslations('Battlepay')
|
||||
|
||||
// Entrega compartida con el webhook (marca la orden PAID; idempotente).
|
||||
let status: 'paid' | 'already' | 'error' = 'error'
|
||||
if (session_id) {
|
||||
const r = await fulfillCheckoutSession(session_id)
|
||||
if (r.ok) status = 'paid'
|
||||
else if (await isSessionPaid(session_id)) status = 'already'
|
||||
}
|
||||
|
||||
return (
|
||||
<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('title')}</h1>
|
||||
{status === 'paid' ? (
|
||||
<p className="text-green-400">{t('paidOk')}</p>
|
||||
) : status === 'already' ? (
|
||||
<p className="text-green-400">{t('alreadyPaid')}</p>
|
||||
) : (
|
||||
<p className="text-red-400">{t('genericError')}</p>
|
||||
)}
|
||||
<p className="mt-6">
|
||||
<Link href="/battlepay" className="text-sky-400 hover:underline">
|
||||
← {t('backToStore')}
|
||||
</Link>
|
||||
</p>
|
||||
</main>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { getTranslations, setRequestLocale } from 'next-intl/server'
|
||||
import { redirect } from '@/i18n/navigation'
|
||||
import { getSession } from '@/lib/session'
|
||||
import { getPendingOrders } from '@/lib/battlepay'
|
||||
import { BattlepayList } from '@/components/BattlepayList'
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export default async function BattlepayPage({ params }: { params: Promise<{ locale: string }> }) {
|
||||
const { locale } = await params
|
||||
setRequestLocale(locale)
|
||||
const t = await getTranslations('Battlepay')
|
||||
const session = await getSession()
|
||||
if (!session.bnetId) redirect({ href: '/login', locale })
|
||||
if (!session.username) redirect({ href: '/select-account', locale })
|
||||
|
||||
const orders = await getPendingOrders(session.accountId ?? 0)
|
||||
|
||||
return (
|
||||
<main className="mx-auto max-w-2xl px-4 py-8">
|
||||
<h1 className="mb-2 text-2xl font-bold text-amber-500">{t('title')}</h1>
|
||||
<p className="mb-6 text-nw-muted">{t('intro')}</p>
|
||||
<BattlepayList orders={orders} />
|
||||
</main>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { getSession } from '@/lib/session'
|
||||
import { getPendingOrder } from '@/lib/battlepay'
|
||||
import { createCheckoutSession } from '@/lib/stripe'
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const session = await getSession()
|
||||
if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
|
||||
|
||||
let b: Record<string, string> = {}
|
||||
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
|
||||
const reference = String(b.reference ?? '').trim()
|
||||
if (!reference) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
|
||||
|
||||
const order = await getPendingOrder(reference, session.accountId)
|
||||
if (!order) return Response.json({ success: false, error: 'orderNotFound' })
|
||||
if (!(order.price_eur > 0)) return Response.json({ success: false, error: 'invalidRequest' })
|
||||
|
||||
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: order.price_eur,
|
||||
characterName: reference, // en battlepay guardamos la referencia como identificador
|
||||
productName: order.product_name,
|
||||
successUrl: `${site}/battlepay-success`,
|
||||
cancelUrl: `${site}/battlepay`,
|
||||
metadata: { battlepay_reference: reference },
|
||||
})
|
||||
if (!r.success || !r.url) return Response.json({ success: false, error: 'stripeError' })
|
||||
return Response.json({ success: true, url: r.url })
|
||||
}
|
||||
Reference in New Issue
Block a user