|
| 1 | +import { stripe } from "@/lib/stripe"; |
| 2 | +import { log } from "@dub/utils"; |
| 3 | +import { logAndRespond } from "app/(ee)/api/cron/utils"; |
| 4 | +import Stripe from "stripe"; |
| 5 | +import { outboundPaymentFailed } from "./outbound-payment-failed"; |
| 6 | +import { outboundPaymentPosted } from "./outbound-payment-posted"; |
| 7 | +import { outboundPaymentReturned } from "./outbound-payment-returned"; |
| 8 | +import { recipientAccountClosed } from "./recipient-account-closed"; |
| 9 | +import { recipientConfigurationUpdated } from "./recipient-configuration-updated"; |
| 10 | + |
| 11 | +const relevantEvents = new Set([ |
| 12 | + "v2.core.account.closed", |
| 13 | + "v2.core.account[configuration.recipient].updated", |
| 14 | + "v2.money_management.outbound_payment.posted", |
| 15 | + "v2.money_management.outbound_payment.returned", |
| 16 | + "v2.money_management.outbound_payment.failed", |
| 17 | +]); |
| 18 | + |
| 19 | +const webhookSecret = process.env.STRIPE_STABLECOIN_WEBHOOK_SECRET; |
| 20 | + |
| 21 | +// POST /api/stripe/stablecoin/webhook – Stripe Stablecoin webhooks |
| 22 | +export const POST = async (req: Request) => { |
| 23 | + const body = await req.text(); |
| 24 | + const signature = req.headers.get("Stripe-Signature"); |
| 25 | + |
| 26 | + if (!signature) { |
| 27 | + return logAndRespond("Missing Stripe-Signature header."); |
| 28 | + } |
| 29 | + |
| 30 | + if (!webhookSecret) { |
| 31 | + return logAndRespond( |
| 32 | + "STRIPE_STABLECOIN_WEBHOOK_SECRET environment variable is not set.", |
| 33 | + { |
| 34 | + status: 500, |
| 35 | + }, |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + let event: Stripe.Event; |
| 40 | + |
| 41 | + try { |
| 42 | + event = stripe.webhooks.constructEvent(body, signature, webhookSecret); |
| 43 | + } catch (error) { |
| 44 | + return logAndRespond(`[Webhook error]:${error.message}`, { |
| 45 | + status: 400, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + if (!relevantEvents.has(event.type)) { |
| 50 | + return logAndRespond(`Unsupported event ${event.type}, skipping...`); |
| 51 | + } |
| 52 | + |
| 53 | + let response = "OK"; |
| 54 | + try { |
| 55 | + switch (event.type) { |
| 56 | + // @ts-ignore |
| 57 | + case "v2.core.account.closed": |
| 58 | + response = await recipientAccountClosed(event); |
| 59 | + break; |
| 60 | + // @ts-ignore |
| 61 | + case "v2.core.account[configuration.recipient].updated": |
| 62 | + response = await recipientConfigurationUpdated(event); |
| 63 | + break; |
| 64 | + // @ts-ignore |
| 65 | + case "v2.money_management.outbound_payment.posted": |
| 66 | + response = await outboundPaymentPosted(event); |
| 67 | + break; |
| 68 | + // @ts-ignore |
| 69 | + case "v2.money_management.outbound_payment.returned": |
| 70 | + response = await outboundPaymentReturned(event); |
| 71 | + break; |
| 72 | + // @ts-ignore |
| 73 | + case "v2.money_management.outbound_payment.failed": |
| 74 | + response = await outboundPaymentFailed(event); |
| 75 | + break; |
| 76 | + } |
| 77 | + } catch (error) { |
| 78 | + await log({ |
| 79 | + message: `/api/stripe/stablecoin/webhook webhook failed (${event.type}). Error: ${error.message}`, |
| 80 | + type: "errors", |
| 81 | + }); |
| 82 | + |
| 83 | + return logAndRespond(`[Webhook error]: ${error.message}`, { |
| 84 | + status: 400, |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + return logAndRespond(`[${event.type}]: ${response}`); |
| 89 | +}; |
0 commit comments