@@ -204,39 +204,37 @@ export class StripeService implements IStripeService {
204204 invoiceId : string ,
205205 invoice : Partial < IStripeInvoice > ,
206206 ) : Promise < IStripeInvoice > {
207- // Create the update object conditionally based on which fields are defined
208207 const updateData : Stripe . InvoiceUpdateParams = { } ;
209-
210208 if ( invoice . shippingAddress ) {
211- updateData . shipping_details = {
212- name : [
213- invoice . shippingAddress . firstName ?? '' , // Avoid 'undefined' in the name
214- invoice . shippingAddress . lastName ?? '' ,
215- ]
216- . join ( ' ' )
217- . trim ( ) , // Trim to avoid extra spaces
218- address : {
219- line1 : invoice . shippingAddress . line1 ?? undefined , // Only set if defined
220- line2 : invoice . shippingAddress . line2 ?? undefined ,
221- city : invoice . shippingAddress . city ?? undefined ,
222- state : invoice . shippingAddress . state ?? undefined ,
223- postal_code : invoice . shippingAddress . zip ?? undefined ,
224- country : invoice . shippingAddress . country ?? undefined ,
225- } ,
226- phone : invoice . shippingAddress . phone ?? undefined , // Only set phone if provided
227- } ;
209+ updateData . shipping_details = this . buildShippingDetails (
210+ invoice . shippingAddress ,
211+ ) ;
228212 }
229-
230- // Call the Stripe API with the built update data
231213 const updatedInvoice = await this . stripe . invoices . update (
232214 invoiceId ,
233215 updateData ,
234216 ) ;
235-
236- // Adapt the updated invoice to your model
237217 return this . stripeInvoiceAdapter . adaptToModel ( updatedInvoice ) ;
238218 }
239219
220+ private buildShippingDetails (
221+ addr : IStripeInvoice [ 'shippingAddress' ] ,
222+ ) : Stripe . InvoiceUpdateParams . ShippingDetails {
223+ const name = [ addr ?. firstName ?? '' , addr ?. lastName ?? '' ] . join ( ' ' ) . trim ( ) ;
224+ return {
225+ name,
226+ address : {
227+ line1 : addr ?. line1 ,
228+ line2 : addr ?. line2 ,
229+ city : addr ?. city ,
230+ state : addr ?. state ,
231+ postal_code : addr ?. zip ,
232+ country : addr ?. country ,
233+ } ,
234+ phone : addr ?. phone ,
235+ } ;
236+ }
237+
240238 async deleteInvoice ( invoiceId : string ) : Promise < void > {
241239 await this . stripe . invoices . del ( invoiceId ) ;
242240 }
@@ -407,16 +405,19 @@ export class StripeService implements IStripeService {
407405 invoices . data . map ( async invoice => {
408406 if ( invoice . status === 'open' && invoice . id ) {
409407 return this . stripe . invoices . voidInvoice ( invoice . id ) ;
410- } else if ( invoice . status === 'draft' && invoice . id ) {
408+ }
409+ if ( invoice . status === 'draft' && invoice . id ) {
411410 await this . stripe . invoices . finalizeInvoice ( invoice . id ) ;
412411 return this . stripe . invoices . voidInvoice ( invoice . id ) ;
413- } else {
414- return Promise . resolve ( ) ;
415412 }
416413 } ) ,
417414 ) ;
418- } catch ( _err ) {
419- // Non-fatal — subscription is already cancelled in Stripe
415+ } catch ( err ) {
416+ // Non-fatal — subscription is already cancelled in Stripe; log for observability
417+ console . info (
418+ '[StripeService] cancelSubscription: invoice cleanup failed' ,
419+ err ,
420+ ) ;
420421 }
421422 }
422423
0 commit comments