@@ -37,11 +37,12 @@ type LedgerTransaction = {
3737 expirationTime : Date ,
3838} ;
3939
40- type StripePayoutList = {
40+ type StripeBalanceTransactionList = {
4141 data : Array < {
4242 id : string ,
4343 amount : number ,
4444 currency : string ,
45+ reporting_category ?: string | null ,
4546 } > ,
4647 has_more : boolean ,
4748} ;
@@ -912,7 +913,7 @@ function sumMoneyTransfersUsdMinorUnits(transactions: Transaction[]): bigint {
912913 return total ;
913914}
914915
915- async function fetchStripePayoutTotalUsdMinorUnits ( options : {
916+ async function fetchStripeBalanceTransactionTotalUsdMinorUnits ( options : {
916917 tenancy : Tenancy ,
917918 stripeAccountId : string ,
918919} ) : Promise < bigint > {
@@ -922,18 +923,27 @@ async function fetchStripePayoutTotalUsdMinorUnits(options: {
922923 } ) ;
923924
924925 let total = 0n ;
926+ const includeCategories = new Set ( [
927+ "charge" ,
928+ "refund" ,
929+ "dispute" ,
930+ "dispute_reversal" ,
931+ "partial_capture_reversal" ,
932+ ] ) ;
925933 let startingAfter : string | undefined = undefined ;
926934
927935 do {
928- const payouts : StripePayoutList = await stripe . payouts . list ( {
936+ const page : StripeBalanceTransactionList = await stripe . balanceTransactions . list ( {
929937 limit : 100 ,
930938 ...( startingAfter ? { starting_after : startingAfter } : { } ) ,
931939 } ) ;
932- for ( const payout of payouts . data ) {
933- if ( payout . currency !== "usd" ) continue ;
934- total += BigInt ( payout . amount ) ;
940+ for ( const balanceTransaction of page . data ) {
941+ if ( balanceTransaction . currency !== "usd" ) continue ;
942+ if ( ! balanceTransaction . reporting_category ) continue ;
943+ if ( ! includeCategories . has ( balanceTransaction . reporting_category ) ) continue ;
944+ total += BigInt ( balanceTransaction . amount ) ;
935945 }
936- startingAfter = payouts . has_more ? payouts . data . at ( - 1 ) ?. id : undefined ;
946+ startingAfter = page . has_more ? page . data . at ( - 1 ) ?. id : undefined ;
937947 } while ( startingAfter ) ;
938948
939949 return total ;
@@ -944,21 +954,24 @@ async function verifyStripePayoutIntegrity(options: {
944954 tenancy : Tenancy ,
945955 stripeAccountId : string ,
946956} ) {
957+ if ( options . projectId === '6fbbf22e-f4b2-4c6e-95a1-beab6fa41063' ) {
958+ // Dummy project doesn't have a real stripe account, so we skip the verification.
959+ return ;
960+ }
947961 const transactions = await fetchAllTransactionsForProject ( options . projectId ) ;
948962 const moneyTransferTotalUsdMinor = sumMoneyTransfersUsdMinorUnits ( transactions ) ;
949- const stripePayoutTotalUsdMinor = await fetchStripePayoutTotalUsdMinorUnits ( {
963+ const stripeBalanceTransactionTotalUsdMinor = await fetchStripeBalanceTransactionTotalUsdMinorUnits ( {
950964 tenancy : options . tenancy ,
951965 stripeAccountId : options . stripeAccountId ,
952966 } ) ;
953-
954- if ( moneyTransferTotalUsdMinor !== stripePayoutTotalUsdMinor ) {
967+ if ( moneyTransferTotalUsdMinor !== stripeBalanceTransactionTotalUsdMinor ) {
955968 throw new StackAssertionError ( deindent `
956- Stripe payout mismatch for project ${ options . projectId } .
957- Money transfers total USD ${ formatMinorUnitsToMoneyString ( moneyTransferTotalUsdMinor , 2 ) } vs Stripe payouts USD ${ formatMinorUnitsToMoneyString ( stripePayoutTotalUsdMinor , 2 ) } .
969+ Stripe balance transaction mismatch for project ${ options . projectId } .
970+ Money transfers total USD ${ formatMinorUnitsToMoneyString ( moneyTransferTotalUsdMinor , 2 ) } vs Stripe balance transactions USD ${ formatMinorUnitsToMoneyString ( stripeBalanceTransactionTotalUsdMinor , 2 ) } .
958971 ` , {
959972 projectId : options . projectId ,
960973 moneyTransferTotalUsdMinor : moneyTransferTotalUsdMinor . toString ( ) ,
961- stripePayoutTotalUsdMinor : stripePayoutTotalUsdMinor . toString ( ) ,
974+ stripeBalanceTransactionTotalUsdMinor : stripeBalanceTransactionTotalUsdMinor . toString ( ) ,
962975 } ) ;
963976 }
964977}
@@ -1008,7 +1021,7 @@ async function createPaymentsVerifier(options: {
10081021 . join ( "; " ) ;
10091022 console . warn ( `Skipping payments verification for project ${ options . projectId } due to include-by-default conflicts (${ conflictSummary } ).` ) ;
10101023 return {
1011- verifyCustomerPayments : async ( ) => { } ,
1024+ verifyCustomerPayments : async ( ) => { } ,
10121025 customCustomerIds : new Set < string > ( ) ,
10131026 } ;
10141027 }
0 commit comments