Skip to content

Commit 2c11477

Browse files
authored
Improve Stripe webhook type handling (dubinc#3739)
1 parent f5c4f25 commit 2c11477

24 files changed

Lines changed: 71 additions & 46 deletions

apps/web/app/(ee)/api/stripe/connect/webhook/account-application-deauthorized.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { recomputePartnerPayoutState } from "@/lib/payouts/recompute-partner-pay
22
import { prisma } from "@dub/prisma";
33
import type Stripe from "stripe";
44

5-
export async function accountApplicationDeauthorized(event: Stripe.Event) {
5+
export async function accountApplicationDeauthorized(
6+
event: Stripe.AccountApplicationDeauthorizedEvent,
7+
) {
68
const stripeAccount = event.account;
79

810
if (!stripeAccount) {

apps/web/app/(ee)/api/stripe/connect/webhook/account-updated.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const balanceAvailableQueue = qstash.queue({
1919
queueName: "handle-balance-available",
2020
});
2121

22-
export async function accountUpdated(event: Stripe.Event) {
23-
const account = event.data.object as Stripe.Account;
22+
export async function accountUpdated(event: Stripe.AccountUpdatedEvent) {
23+
const account = event.data.object;
2424
const { country, business_type } = account;
2525

2626
const partner = await prisma.partner.findUnique({

apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ const queue = qstash.queue({
66
queueName: "handle-balance-available",
77
});
88

9-
export async function balanceAvailable(event: Stripe.Event) {
9+
export async function balanceAvailable(
10+
event:
11+
| Stripe.AccountExternalAccountUpdatedEvent
12+
| Stripe.BalanceAvailableEvent,
13+
) {
1014
const stripeAccount = event.account;
1115

1216
if (!stripeAccount) {

apps/web/app/(ee)/api/stripe/connect/webhook/payout-failed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const queue = qstash.queue({
66
queueName: "handle-payout-failed",
77
});
88

9-
export async function payoutFailed(event: Stripe.Event) {
9+
export async function payoutFailed(event: Stripe.PayoutFailedEvent) {
1010
const stripeAccount = event.account;
1111

1212
if (!stripeAccount) {
1313
return "No stripeConnectId found in event. Skipping...";
1414
}
1515

16-
const stripePayout = event.data.object as Stripe.Payout;
16+
const stripePayout = event.data.object;
1717

1818
const response = await queue.enqueueJSON({
1919
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/payouts/payout-failed`,

apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ const queue = qstash.queue({
66
queueName: "handle-payout-paid",
77
});
88

9-
export async function payoutPaid(event: Stripe.Event) {
9+
export async function payoutPaid(event: Stripe.PayoutPaidEvent) {
1010
const stripeAccount = event.account;
1111

1212
if (!stripeAccount) {
1313
return "No stripeConnectId found in event. Skipping...";
1414
}
1515

16-
const stripePayout = event.data.object as Stripe.Payout;
16+
const stripePayout = event.data.object;
1717
const stripePayoutTraceId = stripePayout.trace_id?.value ?? null;
1818

1919
const response = await queue.enqueueJSON({

apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type Stripe from "stripe";
55

66
// Handle event "account.application.deauthorized"
77
export async function accountApplicationDeauthorized(
8-
event: Stripe.Event,
8+
event: Stripe.AccountApplicationDeauthorizedEvent,
99
mode: StripeMode,
1010
) {
1111
const stripeAccountId = event.account;

apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import { prisma } from "@dub/prisma";
66
import type Stripe from "stripe";
77

88
// Handle event "charge.refunded"
9-
export async function chargeRefunded(event: Stripe.Event, mode: StripeMode) {
10-
const charge = event.data.object as Stripe.Charge;
9+
export async function chargeRefunded(
10+
event: Stripe.ChargeRefundedEvent,
11+
mode: StripeMode,
12+
) {
13+
const charge = event.data.object;
1114
const stripeAccountId = event.account as string;
1215

1316
const stripe = stripeAppClient({

apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import { updateCustomerWithStripeCustomerId } from "./utils/update-customer-with
3434

3535
// Handle event "checkout.session.completed"
3636
export async function checkoutSessionCompleted(
37-
event: Stripe.Event,
37+
event: Stripe.CheckoutSessionCompletedEvent,
3838
mode: StripeMode,
3939
) {
40-
let charge = event.data.object as Stripe.Checkout.Session;
40+
let charge = event.data.object;
4141
let dubCustomerExternalId =
4242
charge.metadata?.dubCustomerExternalId || charge.metadata?.dubCustomerId;
4343
const clientReferenceId = charge.client_reference_id;

apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { waitUntil } from "@vercel/functions";
99
import type Stripe from "stripe";
1010

1111
// Handle event "coupon.deleted"
12-
export async function couponDeleted(event: Stripe.Event) {
13-
const coupon = event.data.object as Stripe.Coupon;
12+
export async function couponDeleted(event: Stripe.CouponDeletedEvent) {
13+
const coupon = event.data.object;
1414
const stripeAccountId = event.account as string;
1515

1616
const workspace = await prisma.project.findUnique({

apps/web/app/(ee)/api/stripe/integration/webhook/customer-created.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import type Stripe from "stripe";
33
import { createNewCustomer } from "./utils/create-new-customer";
44

55
// Handle event "customer.created"
6-
export async function customerCreated(event: Stripe.Event) {
7-
const stripeCustomer = event.data.object as Stripe.Customer;
6+
export async function customerCreated(event: Stripe.CustomerCreatedEvent) {
7+
const stripeCustomer = event.data.object;
88
const stripeAccountId = event.account as string;
99
const dubCustomerExternalId =
1010
stripeCustomer.metadata?.dubCustomerExternalId ||

0 commit comments

Comments
 (0)