Skip to content

Commit 03bbfc0

Browse files
committed
Add connect webhook
1 parent 3f994ee commit 03bbfc0

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

apps/web/app/api/stripe/webhook/account-updated.ts renamed to apps/web/app/api/stripe/connect/webhook/account-updated.ts

File renamed without changes.
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,56 @@
1-
export * from "../../integration/webhook/route";
1+
import { stripe } from "@/lib/stripe";
2+
import { log } from "@dub/utils";
3+
import { NextResponse } from "next/server";
4+
import Stripe from "stripe";
5+
import { accountUpdated } from "./account-updated";
6+
7+
const relevantEvents = new Set([
8+
"account.updated",
9+
"charge.succeeded",
10+
"payment_method.attached",
11+
"checkout.session.completed",
12+
"customer.subscription.updated",
13+
"customer.subscription.deleted",
14+
"invoice.payment_failed",
15+
]);
16+
17+
// POST /api/stripe/webhook – listen to Stripe webhooks
18+
export const POST = async (req: Request) => {
19+
const buf = await req.text();
20+
const sig = req.headers.get("Stripe-Signature") as string;
21+
const webhookSecret = process.env.STRIPE_CONNECT_WEBHOOK_SECRET;
22+
let event: Stripe.Event;
23+
try {
24+
if (!sig || !webhookSecret) return;
25+
event = stripe.webhooks.constructEvent(buf, sig, webhookSecret);
26+
} catch (err: any) {
27+
console.log(`❌ Error message: ${err.message}`);
28+
return new Response(`Webhook Error: ${err.message}`, {
29+
status: 400,
30+
});
31+
}
32+
33+
// Ignore unsupported events
34+
if (!relevantEvents.has(event.type)) {
35+
return new Response("Unsupported event, skipping...", {
36+
status: 200,
37+
});
38+
}
39+
try {
40+
switch (event.type) {
41+
case "account.updated":
42+
await accountUpdated(event);
43+
break;
44+
}
45+
} catch (error) {
46+
await log({
47+
message: `Stripe webhook failed. Error: ${error.message}`,
48+
type: "errors",
49+
});
50+
return new Response('Webhook error: "Webhook handler failed. View logs."', {
51+
status: 400,
52+
});
53+
}
54+
55+
return NextResponse.json({ received: true });
56+
};

apps/web/app/api/stripe/webhook/route.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { stripe } from "@/lib/stripe";
22
import { log } from "@dub/utils";
33
import { NextResponse } from "next/server";
44
import Stripe from "stripe";
5-
import { accountUpdated } from "./account-updated";
65
import { chargeSucceeded } from "./charge-succeeded";
76
import { checkoutSessionCompleted } from "./checkout-session-completed";
87
import { customerSubscriptionDeleted } from "./customer-subscription-deleted";
@@ -11,7 +10,6 @@ import { invoicePaymentFailed } from "./invoice-payment-failed";
1110
import { paymentMethodAttached } from "./payment-method-attached";
1211

1312
const relevantEvents = new Set([
14-
"account.updated",
1513
"charge.succeeded",
1614
"payment_method.attached",
1715
"checkout.session.completed",
@@ -44,9 +42,6 @@ export const POST = async (req: Request) => {
4442
}
4543
try {
4644
switch (event.type) {
47-
case "account.updated":
48-
await accountUpdated(event);
49-
break;
5045
case "charge.succeeded":
5146
await chargeSucceeded(event);
5247
break;

0 commit comments

Comments
 (0)