@@ -32,6 +32,7 @@ import { reviewSchema } from './schemas/review'
3232import { refundSchema } from './schemas/refund'
3333import { activeEntitlementSchema } from './schemas/active_entitlement'
3434import { featureSchema } from './schemas/feature'
35+ import { invoicePaymentSchema } from './schemas/invoice_payment'
3536import type { PoolConfig } from 'pg'
3637
3738function getUniqueIds < T > ( entries : T [ ] , key : string ) : string [ ] {
@@ -553,6 +554,24 @@ export class StripeSync {
553554 )
554555 break
555556 }
557+ case 'invoice_payment.paid' : {
558+ const { entity : invoicePayment , refetched } = await this . fetchOrUseWebhookData (
559+ event . data . object as Stripe . InvoicePayment ,
560+ ( id ) => this . stripe . invoicePayments . retrieve ( id )
561+ )
562+
563+ this . config . logger ?. info (
564+ `Received webhook ${ event . id } : ${ event . type } for invoicePayment ${ invoicePayment . id } `
565+ )
566+
567+ await this . upsertInvoicePayments (
568+ [ invoicePayment ] ,
569+ false ,
570+ this . getSyncTimestamp ( event , refetched )
571+ )
572+
573+ break
574+ }
556575 default :
557576 throw new Error ( 'Unhandled webhook event' )
558577 }
@@ -627,6 +646,10 @@ export class StripeSync {
627646 return this . stripe . reviews . retrieve ( stripeId ) . then ( ( it ) => this . upsertReviews ( [ it ] ) )
628647 } else if ( stripeId . startsWith ( 're_' ) ) {
629648 return this . stripe . refunds . retrieve ( stripeId ) . then ( ( it ) => this . upsertRefunds ( [ it ] ) )
649+ } else if ( stripeId . startsWith ( 'inpay_' ) ) {
650+ return this . stripe . invoicePayments
651+ . retrieve ( stripeId )
652+ . then ( ( it ) => this . upsertInvoicePayments ( [ it ] ) )
630653 } else if ( stripeId . startsWith ( 'feat_' ) ) {
631654 return this . stripe . entitlements . features
632655 . retrieve ( stripeId )
@@ -1280,6 +1303,43 @@ export class StripeSync {
12801303 )
12811304 }
12821305
1306+ async upsertInvoicePayments (
1307+ invoicePayments : Stripe . InvoicePayment [ ] ,
1308+ backfillRelatedEntities ?: boolean ,
1309+ syncTimestamp ?: string
1310+ ) : Promise < Stripe . InvoicePayment [ ] > {
1311+ if ( backfillRelatedEntities ?? this . config . backfillRelatedEntities ) {
1312+ const invoiceIds = getUniqueIds ( invoicePayments , 'invoice' )
1313+ const paymentIntentIds : string [ ] = [ ]
1314+ const chargeIds : string [ ] = [ ]
1315+
1316+ for ( const invoicePayment of invoicePayments ) {
1317+ const payment = invoicePayment . payment as
1318+ | { type ?: string ; payment_intent ?: string ; charge ?: string }
1319+ | undefined
1320+
1321+ if ( payment ?. type === 'payment_intent' && payment . payment_intent ) {
1322+ paymentIntentIds . push ( payment . payment_intent . toString ( ) )
1323+ } else if ( payment ?. type === 'charge' && payment . charge ) {
1324+ chargeIds . push ( payment . charge . toString ( ) )
1325+ }
1326+ }
1327+
1328+ await Promise . all ( [
1329+ this . backfillInvoices ( invoiceIds ) ,
1330+ this . backfillPaymentIntents ( [ ...new Set ( paymentIntentIds ) ] ) ,
1331+ this . backfillCharges ( [ ...new Set ( chargeIds ) ] ) ,
1332+ ] )
1333+ }
1334+
1335+ return this . postgresClient . upsertManyWithTimestampProtection (
1336+ invoicePayments ,
1337+ 'invoice_payments' ,
1338+ invoicePaymentSchema ,
1339+ syncTimestamp
1340+ )
1341+ }
1342+
12831343 async upsertPlans (
12841344 plans : Stripe . Plan [ ] ,
12851345 backfillRelatedEntities ?: boolean ,
0 commit comments