Skip to content

Commit

Permalink
fix(paypal): Fix map accessing for paypal country code
Browse files Browse the repository at this point in the history
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 #
  • Loading branch information
david1alvarez authored and dschom committed Feb 19, 2025
1 parent a88bb11 commit 6904854
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
4 changes: 3 additions & 1 deletion libs/payments/paypal/src/lib/paypal.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,16 @@ export class PayPalClient {
const data = {
AMT: options.amount,
CURRENCYCODE: options.currencyCode.toUpperCase(),
COUNTRYCODE: options.countryCode.toUpperCase(),
CUSTOM: options.idempotencyKey,
INVNUM: options.invoiceNumber,
...(options.ipaddress && { IPADDRESS: options.ipaddress }),
MSGSUBID: options.idempotencyKey,
PAYMENTACTION: 'Sale',
PAYMENTTYPE: 'instant',
REFERENCEID: options.billingAgreementId,
...(options.countryCode && {
COUNTRYCODE: options.countryCode.toUpperCase(),
}),
...(options.taxAmount && {
TAXAMT: options.taxAmount,
// PayPal wants all of this when you include taxes 🤷
Expand Down
2 changes: 1 addition & 1 deletion libs/payments/paypal/src/lib/paypal.client.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export interface DoReferenceTransactionOptions {
invoiceNumber: string;
idempotencyKey: string;
currencyCode: string;
countryCode: string;
countryCode?: string;
taxAmount?: string;
ipaddress?: string;
}
Expand Down
29 changes: 18 additions & 11 deletions packages/fxa-auth-server/lib/payments/paypal/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ export type ChargeCustomerOptions = {
amountInCents: number;
billingAgreementId: string;
currencyCode: string;
countryCode: string;
idempotencyKey: string;
invoiceNumber: string;
countryCode?: string;
ipaddress?: string;
taxAmountInCents?: number;
};
Expand Down Expand Up @@ -278,9 +278,9 @@ export class PayPalHelper {
),
billingAgreementId: options.billingAgreementId,
currencyCode: options.currencyCode,
countryCode: options.countryCode,
idempotencyKey: options.idempotencyKey,
invoiceNumber: options.invoiceNumber,
...(options.countryCode && { countryCode: options.countryCode }),
...(options.ipaddress && { ipaddress: options.ipaddress }),
...(options.taxAmountInCents && {
taxAmount: this.currencyHelper.getPayPalAmountStringFromAmountInCents(
Expand Down Expand Up @@ -485,16 +485,23 @@ export class PayPalHelper {
paymentAttempt
);

const countryCode: string =
invoice.customer_shipping?.address?.country ??
this.currencyHelper.currencyToCountryMap[
invoice.currency.toUpperCase()
][0];
let countryCode: string | undefined =
invoice.customer_shipping?.address?.country ?? undefined;

if (!countryCode) {
throw error.internalValidationError('processInvoice', {
message: 'Invalid country code',
});
const validCountries = this.currencyHelper.currencyToCountryMap.get(
invoice.currency.toUpperCase()
);

if (validCountries && validCountries.length > 0) {
countryCode = validCountries[0];
} else {
this.log.error('processInvoice.countryCode', {
message: 'No valid country code found for invoice',
invoiceId: invoice.id,
currency: invoice.currency,
});
}
}

const promises: Promise<any>[] = [
Expand All @@ -504,8 +511,8 @@ export class PayPalHelper {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
invoiceNumber: invoice.id!,
currencyCode: invoice.currency,
countryCode,
idempotencyKey,
...(countryCode && { countryCode }),
...(ipaddress && { ipaddress }),
...(invoice.tax && { taxAmountInCents: invoice.tax }),
}),
Expand Down

0 comments on commit 6904854

Please sign in to comment.