@@ -593,21 +593,32 @@ async function handleCreditPurchaseSuccess(invoice: Stripe.Invoice): Promise<voi
593593 * Handle invoice payment succeeded webhook.
594594 * Handles both credit purchases and subscription payments.
595595 */
596- /** Whether the customer still has another open, automatically charged invoice that failed. */
597- async function hasOtherFailedInvoice ( invoice : Stripe . Invoice ) : Promise < boolean > {
596+ /**
597+ * Whether the same subscription still has another open, automatically charged invoice that failed:
598+ * its renewals and its metadata-linked overage and seat true-up invoices. A Stripe customer can
599+ * pay for several subscriptions, so invoices for any other one never hold this one's access.
600+ */
601+ async function hasOtherFailedInvoice (
602+ invoice : Stripe . Invoice ,
603+ stripeSubscriptionId : string
604+ ) : Promise < boolean > {
598605 const customerId = getCustomerId ( invoice . customer )
599606 if ( ! customerId ) return false
600- const openInvoices = await requireStripeClient ( ) . invoices . list ( {
607+ for await ( const open of requireStripeClient ( ) . invoices . list ( {
601608 customer : customerId ,
602609 status : 'open' ,
603610 limit : 100 ,
604- } )
605- return openInvoices . data . some (
606- ( open ) =>
611+ } ) ) {
612+ if (
607613 open . id !== invoice . id &&
608614 open . collection_method === 'charge_automatically' &&
609- ( open . attempt_count ?? 0 ) > 0
610- )
615+ ( open . attempt_count ?? 0 ) > 0 &&
616+ resolveInvoiceSubscriptionContext ( open ) . stripeSubscriptionId === stripeSubscriptionId
617+ ) {
618+ return true
619+ }
620+ }
621+ return false
611622}
612623
613624export async function handleInvoicePaymentSucceeded ( event : Stripe . Event ) {
@@ -640,7 +651,7 @@ export async function handleInvoicePaymentSucceeded(event: Stripe.Event) {
640651 // succeeds.
641652 const shouldUnblock =
642653 ( ! isProrationInvoice || ( invoice . amount_paid ?? 0 ) > 0 ) &&
643- ! ( await hasOtherFailedInvoice ( invoice ) )
654+ ! ( await hasOtherFailedInvoice ( invoice , resolvedInvoice . stripeSubscriptionId ) )
644655
645656 if ( shouldUnblock ) {
646657 if ( subIsOrgScoped ) {
0 commit comments