@@ -160,24 +160,35 @@ export class StripeService implements IStripeService {
160160 }
161161
162162 async createInvoice ( invoice : IStripeInvoice ) : Promise < IStripeInvoice > {
163- const createdInvoice = await this . stripe . invoices . create ( {
163+ const addr = invoice . shippingAddress ;
164+ const shippingName = addr
165+ ? [ addr . firstName ?? '' , addr . lastName ?? '' ] . join ( ' ' ) . trim ( )
166+ : '' ;
167+
168+ const createParams : Stripe . InvoiceCreateParams = {
164169 customer : invoice . customerId ,
165- auto_advance : invoice . options ?. autoAdvnace ?? false , // Optional
166- shipping_details : {
167- address : {
168- city : invoice . shippingAddress ?. city ,
169- country : invoice . shippingAddress ?. country ,
170- line1 : invoice . shippingAddress ?. line1 ,
171- line2 :
172- invoice . shippingAddress ?. line2 +
173- ' ' +
174- invoice . shippingAddress ?. line3 ,
175- postal_code : invoice . shippingAddress ?. zip ,
176- state : invoice . shippingAddress ?. state ,
177- } ,
178- name : invoice . customerId ,
179- } ,
180- } ) ;
170+ auto_advance : invoice . options ?. autoAdvance ?? false ,
171+ ...( addr && shippingName
172+ ? {
173+ shipping_details : {
174+ name : shippingName ,
175+ address : {
176+ city : addr . city ,
177+ country : addr . country ,
178+ line1 : addr . line1 ,
179+ line2 :
180+ [ addr . line2 , addr . line3 ] . filter ( Boolean ) . join ( ' ' ) ||
181+ undefined ,
182+ postal_code : addr . zip ,
183+ state : addr . state ,
184+ } ,
185+ phone : addr . phone ,
186+ } ,
187+ }
188+ : { } ) ,
189+ } ;
190+
191+ const createdInvoice = await this . stripe . invoices . create ( createParams ) ;
181192 // First, create invoice items for the customer
182193 for ( const lineItem of invoice . charges ?? [ ] ) {
183194 // Assuming items is an array in TInvoice
@@ -206,9 +217,12 @@ export class StripeService implements IStripeService {
206217 ) : Promise < IStripeInvoice > {
207218 const updateData : Stripe . InvoiceUpdateParams = { } ;
208219 if ( invoice . shippingAddress ) {
209- updateData . shipping_details = this . buildShippingDetails (
220+ const shippingDetails = this . buildShippingDetails (
210221 invoice . shippingAddress ,
211222 ) ;
223+ if ( shippingDetails ) {
224+ updateData . shipping_details = shippingDetails ;
225+ }
212226 }
213227 const updatedInvoice = await this . stripe . invoices . update (
214228 invoiceId ,
@@ -219,8 +233,9 @@ export class StripeService implements IStripeService {
219233
220234 private buildShippingDetails (
221235 addr : IStripeInvoice [ 'shippingAddress' ] ,
222- ) : Stripe . InvoiceUpdateParams . ShippingDetails {
236+ ) : Stripe . InvoiceUpdateParams . ShippingDetails | undefined {
223237 const name = [ addr ?. firstName ?? '' , addr ?. lastName ?? '' ] . join ( ' ' ) . trim ( ) ;
238+ if ( ! name ) return undefined ;
224239 return {
225240 name,
226241 address : {
0 commit comments