Files
NightSpire/web-next/app/[locale]/service-success/page.tsx
T
Inna 389dac68c4 Stripe: webhook que entrega el servicio aunque el usuario no vuelva
- app/api/stripe/webhook: verifica firma (constructEventAsync) y en
  checkout.session.completed registra la IP de Stripe (como Django) y ENTREGA
  el servicio. Idempotente frente a la página de éxito.
- lib/stripe.ts: claimPaidCheckout ahora es ATÓMICO (UPDATE ... WHERE fulfilled=0
  + affectedRows) para evitar doble entrega entre webhook y página de éxito;
  nuevo recordStripeIp.
- lib/fulfill.ts: fulfillCheckoutSession compartido (reclama + resuelve servicio
  desde metadata.service + ejecuta). checkout guarda `service` en metadata.
- service-success: usa la entrega compartida y distingue entregado/ya-procesado/error.
- i18n: Paid.alreadyProcessed.

Requiere configurar STRIPE_WEBHOOK_SECRET en .env.local (endpoint apuntando a
/api/stripe/webhook, evento checkout.session.completed).

Verificado: build OK, webhook 400 sin firma / firma inválida.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 00:55:18 +00:00

49 lines
1.7 KiB
TypeScript

import { getTranslations, setRequestLocale } from 'next-intl/server'
import { fulfillCheckoutSession } from '@/lib/fulfill'
import { isSessionPaid } from '@/lib/stripe'
export const dynamic = 'force-dynamic'
export default async function ServiceSuccessPage({
params,
searchParams,
}: {
params: Promise<{ locale: string }>
searchParams: Promise<{ service?: string; session_id?: string }>
}) {
const { locale } = await params
setRequestLocale(locale)
const { service: urlService, session_id } = await searchParams
const t = await getTranslations('Paid')
// Entrega compartida con el webhook (idempotente). Si el webhook ya entregó,
// el reclamo atómico devuelve ok=false pero el pago sigue confirmado.
let status: 'delivered' | 'already' | 'error' = 'error'
let okName: string | null = null
let service = urlService ?? null
if (session_id) {
const r = await fulfillCheckoutSession(session_id, urlService)
service = r.service ?? urlService ?? null
if (r.ok) {
status = 'delivered'
okName = r.character
} else if (await isSessionPaid(session_id)) {
// Pago confirmado pero ya reclamado antes (webhook o recarga de la página).
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">{service ? t(`${service}.title`) : ''}</h1>
{status === 'delivered' ? (
<p className="text-green-400">{t(`${service}.success`, { name: okName ?? '' })}</p>
) : status === 'already' ? (
<p className="text-green-400">{t('alreadyProcessed')}</p>
) : (
<p className="text-red-400">{t('error')}</p>
)}
</main>
)
}