Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions libs/payments/currency/src/lib/currency.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { Test } from '@nestjs/testing';
import { faker } from '@faker-js/faker';

import { CurrencyManager } from './currency.manager';
import {
CurrencyCodeInvalidError,
CountryCodeInvalidError,
CurrencyCountryMismatchError,
CurrencyCodeMissingError,
CountryCodeMissingError,
} from './currency.error';
import {
CurrencyConfig,
MockCurrencyConfig,
MockCurrencyConfigProvider,
} from './currency.config';

Expand All @@ -31,65 +22,6 @@ describe('CurrencyManager', () => {
mockCurrencyConfig = module.get(CurrencyConfig);
});

describe('assertCurrencyCompatibleWithCountry', () => {
const validCountry = faker.helpers.arrayElement(
MockCurrencyConfig.currenciesToCountries.USD
);
const validCurrency = 'USD';

it('asserts when currency to country is valid', () => {
currencyManager.assertCurrencyCompatibleWithCountry(
validCurrency,
validCountry
);
});

it('throws an error when currency is empty', () => {
expect(() =>
currencyManager.assertCurrencyCompatibleWithCountry('', validCountry)
).toThrow(CurrencyCodeMissingError);
});

it('throws an error when currency is invalid', () => {
expect(() =>
currencyManager.assertCurrencyCompatibleWithCountry('KPW', validCountry)
).toThrow(CurrencyCodeInvalidError);
});

it('throws an error when country is missing', () => {
const countryCode = '';

expect(() =>
currencyManager.assertCurrencyCompatibleWithCountry(
validCurrency,
countryCode
)
).toThrow(CountryCodeMissingError);
});

it('throws an error when country is invalid', () => {
const countryCode = faker.location.countryCode('alpha-3');

expect(() =>
currencyManager.assertCurrencyCompatibleWithCountry(
validCurrency,
countryCode
)
).toThrow(CountryCodeInvalidError);
});

it('throws an error when currency to country do not match', () => {
const currencyCode = 'EUR';

expect(() =>
currencyManager.assertCurrencyCompatibleWithCountry(
currencyCode,
validCountry
)
).toThrow(CurrencyCountryMismatchError);
});
});

describe('getTaxId', () => {
it('returns the correct tax id for currency', async () => {
const mockCurrency = Object.entries(mockCurrencyConfig.taxIds)[0];
Expand Down
48 changes: 0 additions & 48 deletions libs/payments/currency/src/lib/currency.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { Injectable } from '@nestjs/common';
import {
SUPPORTED_PAYPAL_CURRENCIES,
VALID_COUNTRY_CODES,
VALID_CURRENCY_CODES,
} from './currency.constants';
import {
CurrencyCodeInvalidError,
CountryCodeInvalidError,
CurrencyCountryMismatchError,
CurrencyCodeMissingError,
CountryCodeMissingError,
} from './currency.error';
import { CurrencyConfig } from './currency.config';

@Injectable()
Expand All @@ -24,42 +12,6 @@ export class CurrencyManager {
this.taxIds = this.config.taxIds;
}

/**
* Verify that provided source country and plan currency are compatible with
* valid currencies and countries as listed in constants
*
* @param currency Currency of customer
* @param country Country of customer
* @returns True if currency is compatible with country, else throws error
*/
assertCurrencyCompatibleWithCountry(currency: string, country: string): void {
if (!currency) throw new CurrencyCodeMissingError();
if (!country) throw new CountryCodeMissingError();

const currencyUpper = currency.toUpperCase();

if (
!VALID_CURRENCY_CODES.includes(currencyUpper) ||
!SUPPORTED_PAYPAL_CURRENCIES.includes(currencyUpper) ||
!this.config.currenciesToCountries.hasOwnProperty(currencyUpper)
) {
throw new CurrencyCodeInvalidError(currencyUpper);
}

if (!VALID_COUNTRY_CODES.includes(country)) {
throw new CountryCodeInvalidError(country);
}

if (
currencyUpper in this.config.currenciesToCountries &&
!this.config.currenciesToCountries[
currencyUpper as keyof typeof this.config.currenciesToCountries
].includes(country)
) {
throw new CurrencyCountryMismatchError(currencyUpper, country);
}
}

getTaxId(currency: string) {
return this.taxIds[currency.toUpperCase()];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import {
TrialSubscriptionContentFactory,
} from '@fxa/payments/management';
import {
BillingAgreementFactory,
MockPaypalClientConfigProvider,
PaypalBillingAgreementManager,
PayPalClient,
Expand Down Expand Up @@ -2242,7 +2241,6 @@ describe('SubscriptionManagementService', () => {
collection_method: 'send_invoice',
})
);
const mockBillingAgreement = BillingAgreementFactory();
const mockStripeCustomer = StripeResponseFactory(StripeCustomerFactory());

jest
Expand All @@ -2266,12 +2264,6 @@ describe('SubscriptionManagementService', () => {
jest
.spyOn(paypalBillingAgreementManager, 'create')
.mockResolvedValue(faker.string.uuid());
jest
.spyOn(paypalBillingAgreementManager, 'retrieve')
.mockResolvedValue(mockBillingAgreement);
jest
.spyOn(currencyManager, 'assertCurrencyCompatibleWithCountry')
.mockReturnValue();
jest
.spyOn(customerManager, 'update')
.mockResolvedValue(mockStripeCustomer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1228,13 +1228,6 @@ export class SubscriptionManagementService {
token
);

const billingAgreement =
await this.paypalBillingAgreementManager.retrieve(billingAgreementId);
this.currencyManager.assertCurrencyCompatibleWithCountry(
currency,
billingAgreement.countryCode
);

await this.customerManager.update(accountCustomer.stripeCustomerId, {
metadata: {
[STRIPE_CUSTOMER_METADATA.PaypalAgreement]: billingAgreementId,
Expand Down
Loading