Skip to content

Commit 6904854

Browse files
david1alvarezdschom
authored andcommitted
fix(paypal): Fix map accessing for paypal country code
Because: * The paypal country code logic is required by finance, but is a Map not a POJO, and can't be accessed via bracket notation This commit: * Fixes this Closes #
1 parent a88bb11 commit 6904854

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

libs/payments/paypal/src/lib/paypal.client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,16 @@ export class PayPalClient {
231231
const data = {
232232
AMT: options.amount,
233233
CURRENCYCODE: options.currencyCode.toUpperCase(),
234-
COUNTRYCODE: options.countryCode.toUpperCase(),
235234
CUSTOM: options.idempotencyKey,
236235
INVNUM: options.invoiceNumber,
237236
...(options.ipaddress && { IPADDRESS: options.ipaddress }),
238237
MSGSUBID: options.idempotencyKey,
239238
PAYMENTACTION: 'Sale',
240239
PAYMENTTYPE: 'instant',
241240
REFERENCEID: options.billingAgreementId,
241+
...(options.countryCode && {
242+
COUNTRYCODE: options.countryCode.toUpperCase(),
243+
}),
242244
...(options.taxAmount && {
243245
TAXAMT: options.taxAmount,
244246
// PayPal wants all of this when you include taxes 🤷

libs/payments/paypal/src/lib/paypal.client.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export interface DoReferenceTransactionOptions {
145145
invoiceNumber: string;
146146
idempotencyKey: string;
147147
currencyCode: string;
148-
countryCode: string;
148+
countryCode?: string;
149149
taxAmount?: string;
150150
ipaddress?: string;
151151
}

packages/fxa-auth-server/lib/payments/paypal/helper.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export type ChargeCustomerOptions = {
5252
amountInCents: number;
5353
billingAgreementId: string;
5454
currencyCode: string;
55-
countryCode: string;
5655
idempotencyKey: string;
5756
invoiceNumber: string;
57+
countryCode?: string;
5858
ipaddress?: string;
5959
taxAmountInCents?: number;
6060
};
@@ -278,9 +278,9 @@ export class PayPalHelper {
278278
),
279279
billingAgreementId: options.billingAgreementId,
280280
currencyCode: options.currencyCode,
281-
countryCode: options.countryCode,
282281
idempotencyKey: options.idempotencyKey,
283282
invoiceNumber: options.invoiceNumber,
283+
...(options.countryCode && { countryCode: options.countryCode }),
284284
...(options.ipaddress && { ipaddress: options.ipaddress }),
285285
...(options.taxAmountInCents && {
286286
taxAmount: this.currencyHelper.getPayPalAmountStringFromAmountInCents(
@@ -485,16 +485,23 @@ export class PayPalHelper {
485485
paymentAttempt
486486
);
487487

488-
const countryCode: string =
489-
invoice.customer_shipping?.address?.country ??
490-
this.currencyHelper.currencyToCountryMap[
491-
invoice.currency.toUpperCase()
492-
][0];
488+
let countryCode: string | undefined =
489+
invoice.customer_shipping?.address?.country ?? undefined;
493490

494491
if (!countryCode) {
495-
throw error.internalValidationError('processInvoice', {
496-
message: 'Invalid country code',
497-
});
492+
const validCountries = this.currencyHelper.currencyToCountryMap.get(
493+
invoice.currency.toUpperCase()
494+
);
495+
496+
if (validCountries && validCountries.length > 0) {
497+
countryCode = validCountries[0];
498+
} else {
499+
this.log.error('processInvoice.countryCode', {
500+
message: 'No valid country code found for invoice',
501+
invoiceId: invoice.id,
502+
currency: invoice.currency,
503+
});
504+
}
498505
}
499506

500507
const promises: Promise<any>[] = [
@@ -504,8 +511,8 @@ export class PayPalHelper {
504511
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
505512
invoiceNumber: invoice.id!,
506513
currencyCode: invoice.currency,
507-
countryCode,
508514
idempotencyKey,
515+
...(countryCode && { countryCode }),
509516
...(ipaddress && { ipaddress }),
510517
...(invoice.tax && { taxAmountInCents: invoice.tax }),
511518
}),

0 commit comments

Comments
 (0)