@@ -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