Skip to content

Commit 4a6d7c0

Browse files
committed
Simplify getStripeId. Move null checks into callers
1 parent 1fe1966 commit 4a6d7c0

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

packages/billing/src/subscription-webhooks.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ export async function handleSubscriptionInvoicePaid(params: {
5353

5454
if (!invoice.subscription) return
5555
const subscriptionId = getStripeId(invoice.subscription)
56-
const customerId = getStripeId(invoice.customer)
5756

58-
if (!customerId) {
57+
if (!invoice.customer) {
5958
logger.warn(
6059
{ invoiceId: invoice.id },
6160
'Subscription invoice has no customer ID',
6261
)
6362
return
6463
}
64+
const customerId = getStripeId(invoice.customer)
6565

6666
const stripeSub = await stripeServer.subscriptions.retrieve(subscriptionId)
6767
const priceId = stripeSub.items.data[0]?.price.id
@@ -186,9 +186,9 @@ export async function handleSubscriptionInvoicePaymentFailed(params: {
186186

187187
if (!invoice.subscription) return
188188
const subscriptionId = getStripeId(invoice.subscription)
189-
const customerId = getStripeId(invoice.customer)
190189
let userId = null
191-
if (customerId) {
190+
if (invoice.customer) {
191+
const customerId = getStripeId(invoice.customer)
192192
const user = await getUserByStripeCustomerId(customerId)
193193
userId = user?.id
194194
}

packages/internal/src/util/stripe.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import Stripe from 'stripe'
77
/**
88
* Extracts the ID string from a Stripe expandable field.
99
*/
10-
export function getStripeId(expandable: string | { id: string }): string
11-
export function getStripeId(expandable: string | { id: string } | null | undefined): string | undefined
12-
export function getStripeId(expandable: string | { id: string } | null | undefined): string | undefined {
13-
if (expandable == null) return undefined
10+
export function getStripeId(expandable: string | { id: string }): string {
1411
return typeof expandable === 'string' ? expandable : expandable.id
1512
}
1613

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ const webhookHandler = async (req: NextRequest): Promise<NextResponse> => {
392392
}
393393
case 'charge.dispute.created': {
394394
const dispute = event.data.object as Stripe.Dispute
395-
const chargeId = getStripeId(dispute.charge)
396395

397-
if (!chargeId) {
396+
if (!dispute.charge) {
398397
logger.warn(
399398
{ disputeId: dispute.id },
400399
'Dispute received without charge ID',
401400
)
402401
break
403402
}
403+
const chargeId = getStripeId(dispute.charge)
404404

405405
// Get the charge to find the customer
406406
const charge = await stripeServer.charges.retrieve(chargeId)
@@ -539,14 +539,16 @@ const webhookHandler = async (req: NextRequest): Promise<NextResponse> => {
539539
case 'invoice.paid': {
540540
const invoice = event.data.object as Stripe.Invoice
541541
if (invoice.subscription) {
542-
const customerId = getStripeId(invoice.customer)
543-
if (!customerId) {
542+
if (!invoice.customer) {
544543
logger.warn(
545544
{ invoiceId: invoice.id },
546545
'Subscription invoice has no customer — skipping',
547546
)
548-
} else if (!(await isOrgCustomer(customerId))) {
549-
await handleSubscriptionInvoicePaid({ invoice, logger })
547+
} else {
548+
const customerId = getStripeId(invoice.customer)
549+
if (!(await isOrgCustomer(customerId))) {
550+
await handleSubscriptionInvoicePaid({ invoice, logger })
551+
}
550552
}
551553
} else {
552554
await handleInvoicePaid(invoice)
@@ -556,14 +558,16 @@ const webhookHandler = async (req: NextRequest): Promise<NextResponse> => {
556558
case 'invoice.payment_failed': {
557559
const invoice = event.data.object as Stripe.Invoice
558560
if (invoice.subscription) {
559-
const customerId = getStripeId(invoice.customer)
560-
if (!customerId) {
561+
if (!invoice.customer) {
561562
logger.warn(
562563
{ invoiceId: invoice.id },
563564
'Subscription invoice has no customer — skipping',
564565
)
565-
} else if (!(await isOrgCustomer(customerId))) {
566-
await handleSubscriptionInvoicePaymentFailed({ invoice, logger })
566+
} else {
567+
const customerId = getStripeId(invoice.customer)
568+
if (!(await isOrgCustomer(customerId))) {
569+
await handleSubscriptionInvoicePaymentFailed({ invoice, logger })
570+
}
567571
}
568572
}
569573
if (

0 commit comments

Comments
 (0)