Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve Stripe webhook type handling (#3739)

Steven Tey2c11477b1b37
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/account-application-deauthorized.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/account-application-deauthorized.ts
index 32f6826214..a0a8199619 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/account-application-deauthorized.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/account-application-deauthorized.ts
@@ -2,7 +2,9 @@ import { recomputePartnerPayoutState } from "@/lib/payouts/recompute-partner-pay
import { prisma } from "@dub/prisma";
import type Stripe from "stripe";
-export async function accountApplicationDeauthorized(event: Stripe.Event) {
+export async function accountApplicationDeauthorized(
+ event: Stripe.AccountApplicationDeauthorizedEvent,
+) {
const stripeAccount = event.account;
if (!stripeAccount) {
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/account-updated.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/account-updated.ts
index 6e406d514d..8048d9659a 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/account-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/account-updated.ts
@@ -19,8 +19,8 @@ const balanceAvailableQueue = qstash.queue({
queueName: "handle-balance-available",
});
-export async function accountUpdated(event: Stripe.Event) {
- const account = event.data.object as Stripe.Account;
+export async function accountUpdated(event: Stripe.AccountUpdatedEvent) {
+ const account = event.data.object;
const { country, business_type } = account;
const partner = await prisma.partner.findUnique({
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
index 3eade9c9dc..35a69a4f1e 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/balance-available.ts
@@ -6,7 +6,11 @@ const queue = qstash.queue({
queueName: "handle-balance-available",
});
-export async function balanceAvailable(event: Stripe.Event) {
+export async function balanceAvailable(
+ event:
+ | Stripe.AccountExternalAccountUpdatedEvent
+ | Stripe.BalanceAvailableEvent,
+) {
const stripeAccount = event.account;
if (!stripeAccount) {
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-failed.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-failed.ts
index 2a058326f7..cf3ce0d6e5 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-failed.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-failed.ts
@@ -6,14 +6,14 @@ const queue = qstash.queue({
queueName: "handle-payout-failed",
});
-export async function payoutFailed(event: Stripe.Event) {
+export async function payoutFailed(event: Stripe.PayoutFailedEvent) {
const stripeAccount = event.account;
if (!stripeAccount) {
return "No stripeConnectId found in event. Skipping...";
}
- const stripePayout = event.data.object as Stripe.Payout;
+ const stripePayout = event.data.object;
const response = await queue.enqueueJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/payouts/payout-failed`,
diff --git a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts
index 9a376f615c..462f522195 100644
--- a/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts
+++ b/apps/web/app/(ee)/api/stripe/connect/webhook/payout-paid.ts
@@ -6,14 +6,14 @@ const queue = qstash.queue({
queueName: "handle-payout-paid",
});
-export async function payoutPaid(event: Stripe.Event) {
+export async function payoutPaid(event: Stripe.PayoutPaidEvent) {
const stripeAccount = event.account;
if (!stripeAccount) {
return "No stripeConnectId found in event. Skipping...";
}
- const stripePayout = event.data.object as Stripe.Payout;
+ const stripePayout = event.data.object;
const stripePayoutTraceId = stripePayout.trace_id?.value ?? null;
const response = await queue.enqueueJSON({
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
index 32f14f4e12..334d301308 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/account-application-deauthorized.ts
@@ -5,7 +5,7 @@ import type Stripe from "stripe";
// Handle event "account.application.deauthorized"
export async function accountApplicationDeauthorized(
- event: Stripe.Event,
+ event: Stripe.AccountApplicationDeauthorizedEvent,
mode: StripeMode,
) {
const stripeAccountId = event.account;
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
index b59533c9dc..c1719e1710 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/charge-refunded.ts
@@ -6,8 +6,11 @@ import { prisma } from "@dub/prisma";
import type Stripe from "stripe";
// Handle event "charge.refunded"
-export async function chargeRefunded(event: Stripe.Event, mode: StripeMode) {
- const charge = event.data.object as Stripe.Charge;
+export async function chargeRefunded(
+ event: Stripe.ChargeRefundedEvent,
+ mode: StripeMode,
+) {
+ const charge = event.data.object;
const stripeAccountId = event.account as string;
const stripe = stripeAppClient({
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
index 82804ea09d..2915c5c4a6 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/checkout-session-completed.ts
@@ -34,10 +34,10 @@ import { updateCustomerWithStripeCustomerId } from "./utils/update-customer-with
// Handle event "checkout.session.completed"
export async function checkoutSessionCompleted(
- event: Stripe.Event,
+ event: Stripe.CheckoutSessionCompletedEvent,
mode: StripeMode,
) {
- let charge = event.data.object as Stripe.Checkout.Session;
+ let charge = event.data.object;
let dubCustomerExternalId =
charge.metadata?.dubCustomerExternalId || charge.metadata?.dubCustomerId;
const clientReferenceId = charge.client_reference_id;
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
index b90bdc4910..36b20631f4 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/coupon-deleted.ts
@@ -9,8 +9,8 @@ import { waitUntil } from "@vercel/functions";
import type Stripe from "stripe";
// Handle event "coupon.deleted"
-export async function couponDeleted(event: Stripe.Event) {
- const coupon = event.data.object as Stripe.Coupon;
+export async function couponDeleted(event: Stripe.CouponDeletedEvent) {
+ const coupon = event.data.object;
const stripeAccountId = event.account as string;
const workspace = await prisma.project.findUnique({
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-created.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-created.ts
index c65d17598f..dd2b82b685 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-created.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-created.ts
@@ -3,8 +3,8 @@ import type Stripe from "stripe";
import { createNewCustomer } from "./utils/create-new-customer";
// Handle event "customer.created"
-export async function customerCreated(event: Stripe.Event) {
- const stripeCustomer = event.data.object as Stripe.Customer;
+export async function customerCreated(event: Stripe.CustomerCreatedEvent) {
+ const stripeCustomer = event.data.object;
const stripeAccountId = event.account as string;
const dubCustomerExternalId =
stripeCustomer.metadata?.dubCustomerExternalId ||
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
index 19ae485a3c..b37d49fdd7 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-created.ts
@@ -11,10 +11,10 @@ import { getConnectedCustomer } from "./utils/get-connected-customer";
// Handle event "customer.subscription.created"
// only used for recording free trial creations
export async function customerSubscriptionCreated(
- event: Stripe.Event,
+ event: Stripe.CustomerSubscriptionCreatedEvent,
mode: StripeMode,
) {
- const createdSubscription = event.data.object as Stripe.Subscription;
+ const createdSubscription = event.data.object;
if (createdSubscription.status !== "trialing") {
return {
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-deleted.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-deleted.ts
index ba3b0928b2..7d67cd0977 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-subscription-deleted.ts
@@ -2,8 +2,10 @@ import { prisma } from "@dub/prisma";
import type Stripe from "stripe";
// Handle event "customer.subscription.deleted"
-export async function customerSubscriptionDeleted(event: Stripe.Event) {
- const deletedSubscription = event.data.object as Stripe.Subscription;
+export async function customerSubscriptionDeleted(
+ event: Stripe.CustomerSubscriptionDeletedEvent,
+) {
+ const deletedSubscription = event.data.object;
const customer = await prisma.customer.findUnique({
where: {
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-updated.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-updated.ts
index 8d5104a821..773f90874e 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/customer-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/customer-updated.ts
@@ -3,8 +3,8 @@ import type Stripe from "stripe";
import { createNewCustomer } from "./utils/create-new-customer";
// Handle event "customer.updated"
-export async function customerUpdated(event: Stripe.Event) {
- const stripeCustomer = event.data.object as Stripe.Customer;
+export async function customerUpdated(event: Stripe.CustomerUpdatedEvent) {
+ const stripeCustomer = event.data.object;
const stripeAccountId = event.account as string;
const dubCustomerExternalId =
stripeCustomer.metadata?.dubCustomerExternalId ||
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
index 1354643a5b..7d55570096 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/invoice-paid.ts
@@ -18,8 +18,11 @@ import type Stripe from "stripe";
import { getConnectedCustomer } from "./utils/get-connected-customer";
// Handle event "invoice.paid"
-export async function invoicePaid(event: Stripe.Event, mode: StripeMode) {
- const invoice = event.data.object as Stripe.Invoice;
+export async function invoicePaid(
+ event: Stripe.InvoicePaidEvent,
+ mode: StripeMode,
+) {
+ const invoice = event.data.object;
const stripeAccountId = event.account as string;
const stripeCustomerId = invoice.customer as string;
const invoiceId = invoice.id;
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts
index 585ba6dddf..121ec1183f 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/promotion-code-updated.ts
@@ -2,8 +2,10 @@ import { prisma } from "@dub/prisma";
import type Stripe from "stripe";
// Handle event "promotion_code.updated"
-export async function promotionCodeUpdated(event: Stripe.Event) {
- const promotionCode = event.data.object as Stripe.PromotionCode;
+export async function promotionCodeUpdated(
+ event: Stripe.PromotionCodeUpdatedEvent,
+) {
+ const promotionCode = event.data.object;
const stripeAccountId = event.account as string;
const workspace = await prisma.project.findUnique({
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
index 173799e70e..ec3525e597 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-failed.ts
@@ -3,8 +3,8 @@ import Stripe from "stripe";
import { processDomainRenewalFailure } from "./utils/process-domain-renewal-failure";
import { processPayoutInvoiceFailure } from "./utils/process-payout-invoice-failure";
-export async function chargeFailed(event: Stripe.Event) {
- const charge = event.data.object as Stripe.Charge;
+export async function chargeFailed(event: Stripe.ChargeFailedEvent) {
+ const charge = event.data.object;
const { transfer_group: invoiceId, failure_message: failedReason } = charge;
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-refunded.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-refunded.ts
index f0304a5dd8..e7011f4eb3 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-refunded.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-refunded.ts
@@ -3,8 +3,8 @@ import { prisma } from "@dub/prisma";
import { Invoice } from "@dub/prisma/client";
import Stripe from "stripe";
-export async function chargeRefunded(event: Stripe.Event) {
- const charge = event.data.object as Stripe.Charge;
+export async function chargeRefunded(event: Stripe.ChargeRefundedEvent) {
+ const charge = event.data.object;
const { transfer_group: invoiceId } = charge;
diff --git a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
index 554865aa3d..4287624667 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/charge-succeeded.ts
@@ -8,8 +8,8 @@ import { APP_DOMAIN_WITH_NGROK, pluralize } from "@dub/utils";
import { addDays } from "date-fns";
import Stripe from "stripe";
-export async function chargeSucceeded(event: Stripe.Event) {
- const charge = event.data.object as Stripe.Charge;
+export async function chargeSucceeded(event: Stripe.ChargeSucceededEvent) {
+ const charge = event.data.object;
const { transfer_group: invoiceId } = charge;
diff --git a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
index b8a5b790ce..54dcdd0f18 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/checkout-session-completed.ts
@@ -12,8 +12,10 @@ import { Program, User } from "@dub/prisma/client";
import { getPlanAndTierFromPriceId, log, prettyPrint } from "@dub/utils";
import Stripe from "stripe";
-export async function checkoutSessionCompleted(event: Stripe.Event) {
- const checkoutSession = event.data.object as Stripe.Checkout.Session;
+export async function checkoutSessionCompleted(
+ event: Stripe.CheckoutSessionCompletedEvent,
+) {
+ const checkoutSession = event.data.object;
if (
checkoutSession.mode === "setup" ||
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
index 36d6c45770..692c3d6519 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-deleted.ts
@@ -14,8 +14,10 @@ import Stripe from "stripe";
import { sendCancellationFeedback } from "./utils/send-cancellation-feedback";
import { updateWorkspacePlan } from "./utils/update-workspace-plan";
-export async function customerSubscriptionDeleted(event: Stripe.Event) {
- const subscriptionDeleted = event.data.object as Stripe.Subscription;
+export async function customerSubscriptionDeleted(
+ event: Stripe.CustomerSubscriptionDeletedEvent,
+) {
+ const subscriptionDeleted = event.data.object;
const stripeId = subscriptionDeleted.customer.toString();
diff --git a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
index f04d38fa9b..f6837c0e88 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/customer-subscription-updated.ts
@@ -4,8 +4,10 @@ import Stripe from "stripe";
import { sendCancellationFeedback } from "./utils/send-cancellation-feedback";
import { updateWorkspacePlan } from "./utils/update-workspace-plan";
-export async function customerSubscriptionUpdated(event: Stripe.Event) {
- const subscriptionUpdated = event.data.object as Stripe.Subscription;
+export async function customerSubscriptionUpdated(
+ event: Stripe.CustomerSubscriptionUpdatedEvent,
+) {
+ const subscriptionUpdated = event.data.object;
const priceId = subscriptionUpdated.items.data[0].price.id;
const { plan } = getPlanAndTierFromPriceId({ priceId });
diff --git a/apps/web/app/(ee)/api/stripe/webhook/invoice-payment-failed.tsx b/apps/web/app/(ee)/api/stripe/webhook/invoice-payment-failed.tsx
index 1b4e39fd9c..1156fbd54b 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/invoice-payment-failed.tsx
+++ b/apps/web/app/(ee)/api/stripe/webhook/invoice-payment-failed.tsx
@@ -3,12 +3,14 @@ import FailedPayment from "@dub/email/templates/failed-payment";
import { prisma } from "@dub/prisma";
import Stripe from "stripe";
-export async function invoicePaymentFailed(event: Stripe.Event) {
+export async function invoicePaymentFailed(
+ event: Stripe.InvoicePaymentFailedEvent,
+) {
const {
customer: stripeId,
attempt_count: attemptCount,
amount_due: amountDue,
- } = event.data.object as Stripe.Invoice;
+ } = event.data.object;
if (!stripeId) {
return "No customer found in invoice.payment_failed event.";
diff --git a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts b/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts
index d2e31f3398..75183a30a5 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/payment-intent-requires-action.ts
@@ -3,9 +3,10 @@ import Stripe from "stripe";
import { processDomainRenewalFailure } from "./utils/process-domain-renewal-failure";
import { processPayoutInvoiceFailure } from "./utils/process-payout-invoice-failure";
-export async function paymentIntentRequiresAction(event: Stripe.Event) {
- const { transfer_group: invoiceId, latest_charge: charge } = event.data
- .object as Stripe.PaymentIntent;
+export async function paymentIntentRequiresAction(
+ event: Stripe.PaymentIntentRequiresActionEvent,
+) {
+ const { transfer_group: invoiceId } = event.data.object;
if (!invoiceId) {
return "No transfer group found, skipping...";
diff --git a/apps/web/app/(ee)/api/stripe/webhook/transfer-reversed.ts b/apps/web/app/(ee)/api/stripe/webhook/transfer-reversed.ts
index 6bc7146f5d..b0673d6040 100644
--- a/apps/web/app/(ee)/api/stripe/webhook/transfer-reversed.ts
+++ b/apps/web/app/(ee)/api/stripe/webhook/transfer-reversed.ts
@@ -2,8 +2,8 @@ import { prisma } from "@dub/prisma";
import { pluralize } from "@dub/utils";
import Stripe from "stripe";
-export async function transferReversed(event: Stripe.Event) {
- const stripeTransfer = event.data.object as Stripe.Transfer;
+export async function transferReversed(event: Stripe.TransferReversedEvent) {
+ const stripeTransfer = event.data.object;
// when transfer is reversed on Stripe, we update any sent payouts with matching stripeTransferId to:
// - set the status to processed (so it can be resent to the partner later)