@@ -147,48 +147,41 @@ export async function POST(input: APIEvent) {
147147 . where ( eq ( BillingTable . workspaceID , workspaceID ) )
148148 } )
149149 }
150- if ( body . type === "invoice.payment_succeeded" ) {
151- const invoice = body . data . object
152- if ( invoice . billing_reason === "subscription_cycle" ) {
153- const invoiceID = invoice . id as string
154- const amountInCents = invoice . amount_paid
155- const customerID = invoice . customer as string
156- const subscriptionID = invoice . parent ?. subscription_details ?. subscription as string
150+ if ( body . type === "invoice.payment_succeeded" && body . data . object . billing_reason === "subscription_cycle" ) {
151+ const invoiceID = body . data . object . id as string
152+ const amountInCents = body . data . object . amount_paid
153+ const customerID = body . data . object . customer as string
154+ const subscriptionID = body . data . object . parent ?. subscription_details ?. subscription as string
157155
158- if ( ! customerID ) throw new Error ( "Customer ID not found" )
159- if ( ! invoiceID ) throw new Error ( "Invoice ID not found" )
160- if ( ! subscriptionID ) throw new Error ( "Subscription ID not found" )
156+ if ( ! customerID ) throw new Error ( "Customer ID not found" )
157+ if ( ! invoiceID ) throw new Error ( "Invoice ID not found" )
158+ if ( ! subscriptionID ) throw new Error ( "Subscription ID not found" )
161159
162- const payment = await Billing . stripe ( ) . invoicePayments . retrieve ( invoiceID )
163- const paymentID = payment . id as string
164- if ( ! paymentID ) throw new Error ( "Payment ID not found" )
160+ const invoice = await Billing . stripe ( ) . invoices . retrieve ( invoiceID , {
161+ expand : [ "payments" ] ,
162+ } )
163+ const paymentID = invoice . payments ?. data [ 0 ] . payment . payment_intent as string
164+ if ( ! paymentID ) throw new Error ( "Payment ID not found" )
165165
166- const workspaceID = await Database . use ( ( tx ) =>
167- tx
168- . select ( { workspaceID : BillingTable . workspaceID } )
169- . from ( BillingTable )
170- . where ( eq ( BillingTable . customerID , customerID ) )
171- . then ( ( rows ) => rows [ 0 ] ?. workspaceID ) ,
172- )
173- if ( ! workspaceID ) throw new Error ( "Workspace ID not found for customer" )
166+ const workspaceID = await Database . use ( ( tx ) =>
167+ tx
168+ . select ( { workspaceID : BillingTable . workspaceID } )
169+ . from ( BillingTable )
170+ . where ( eq ( BillingTable . customerID , customerID ) )
171+ . then ( ( rows ) => rows [ 0 ] ?. workspaceID ) ,
172+ )
173+ if ( ! workspaceID ) throw new Error ( "Workspace ID not found for customer" )
174174
175- await Database . transaction ( async ( tx ) => {
176- await tx
177- . update ( BillingTable )
178- . set ( {
179- balance : sql `${ BillingTable . balance } + ${ centsToMicroCents ( amountInCents ) } ` ,
180- } )
181- . where ( eq ( BillingTable . workspaceID , workspaceID ) )
182- await tx . insert ( PaymentTable ) . values ( {
183- workspaceID,
184- id : Identifier . create ( "payment" ) ,
185- amount : centsToMicroCents ( amountInCents ) ,
186- paymentID,
187- invoiceID,
188- customerID,
189- } )
190- } )
191- }
175+ await Database . use ( ( tx ) =>
176+ tx . insert ( PaymentTable ) . values ( {
177+ workspaceID,
178+ id : Identifier . create ( "payment" ) ,
179+ amount : centsToMicroCents ( amountInCents ) ,
180+ paymentID,
181+ invoiceID,
182+ customerID,
183+ } ) ,
184+ )
192185 }
193186 if ( body . type === "customer.subscription.created" ) {
194187 const data = {
0 commit comments