Skip to content

Commit a23c7d8

Browse files
Piyush Singh GaurPiyush Singh Gaur
authored andcommitted
fix: resolve runtime bugs found during sandbox e2e testing
- chargebee: guard invoice.options?.autoCollection against undefined - stripe: fix createInvoice to skip shipping_details when not provided - stripe: fix line2 concatenation (undefined undefined) bug - stripe: buildShippingDetails returns undefined for empty name - stripe: fix autoAdvnace typo to autoAdvance across service/adapter/type GH-1
1 parent 799fd29 commit a23c7d8

4 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/providers/sdk/chargebee/charge-bee.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ export class ChargeBeeService implements IChargeBeeService {
233233
country: invoice.shippingAddress?.country,
234234
},
235235
charges: invoice.charges,
236-
auto_collection: invoice.options.autoCollection,
236+
auto_collection: invoice.options?.autoCollection,
237237
discounts:
238-
invoice.options.discounts?.map(discount => ({
238+
invoice.options?.discounts?.map(discount => ({
239239
...discount,
240240
apply_on: discount.applyOn, // Convert to snake_case
241241
})) ?? [],

src/providers/sdk/stripe/adapter/invoice.adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class StripeInvoiceAdapter implements IAdapter<IStripeInvoice> {
3535
}),
3636
),
3737
options: {
38-
autoAdvnace: resp.auto_advance || false,
38+
autoAdvance: resp.auto_advance || false,
3939
},
4040
};
4141
}
@@ -66,7 +66,7 @@ export class StripeInvoiceAdapter implements IAdapter<IStripeInvoice> {
6666
customer: data.customerId,
6767
currency: data.currencyCode,
6868
...shippingDetails,
69-
auto_advance: data.options?.autoAdvnace ?? false,
69+
auto_advance: data.options?.autoAdvance ?? false,
7070
};
7171
}
7272
}

src/providers/sdk/stripe/stripe.service.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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: {

src/providers/sdk/stripe/type/invoice.type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import {IAddressDto} from '../../chargebee';
44
export interface IStripeInvoice extends TInvoice {
55
shippingAddress: IAddressDto | undefined;
66
options?: {
7-
autoAdvnace?: boolean;
7+
autoAdvance?: boolean;
88
};
99
}

0 commit comments

Comments
 (0)