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>
This commit is contained in:
2026-07-13 00:55:18 +00:00
parent 7534b9cbbb
commit 389dac68c4
7 changed files with 116 additions and 20 deletions
+20 -12
View File
@@ -1,6 +1,6 @@
import { getTranslations, setRequestLocale } from 'next-intl/server'
import { claimPaidCheckout } from '@/lib/stripe'
import { getPaidService } from '@/lib/paid-services'
import { fulfillCheckoutSession } from '@/lib/fulfill'
import { isSessionPaid } from '@/lib/stripe'
export const dynamic = 'force-dynamic'
@@ -13,25 +13,33 @@ export default async function ServiceSuccessPage({
}) {
const { locale } = await params
setRequestLocale(locale)
const { service, session_id } = await searchParams
const { service: urlService, session_id } = await searchParams
const t = await getTranslations('Paid')
const cfg = service ? getPaidService(service) : null
// Verifica el pago (una vez) y ejecuta el comando SOAP del servicio.
// 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
if (cfg && session_id) {
const claim = await claimPaidCheckout(session_id)
if (claim) {
const ok = await cfg.fulfill(claim.characterName, claim.metadata)
if (ok) okName = claim.characterName
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>
{okName ? (
<p className="text-green-400">{t(`${service}.success`, { name: okName })}</p>
{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>
)}