Skip to content

Commit

Permalink
Merge pull request #16481 from mozilla/FXA-8932
Browse files Browse the repository at this point in the history
feat(payments-paypal): Create PayPalManager getCheckoutToken
  • Loading branch information
xlisachan authored Feb 27, 2024
2 parents 7f6172c + f8314a7 commit 4dfbfbc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
49 changes: 37 additions & 12 deletions libs/payments/paypal/src/lib/paypal.manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import { PayPalManager } from './paypal.manager';
import { faker } from '@faker-js/faker';
import { Kysely } from 'kysely';

import { DB, testAccountDatabaseSetup } from '@fxa/shared/db/mysql/account';

import { NVPSetExpressCheckoutResponseFactory } from './factories';
import { PayPalClient } from './paypal.client';
import { faker } from '@faker-js/faker';
import { PayPalManager } from './paypal.manager';

describe('paypalManager', () => {
let paypalManager: PayPalManager;
let kyselyDb: Kysely<DB>;
let paypalClient: PayPalClient;
let paypalManager: PayPalManager;

beforeAll(async () => {
kyselyDb = await testAccountDatabaseSetup([
'paypalCustomers',
'accountCustomers',
]);
paypalManager = new PayPalManager(
kyselyDb,
new PayPalClient({
sandbox: false,
user: faker.string.uuid(),
pwd: faker.string.uuid(),
signature: faker.string.uuid(),
})
);

paypalClient = new PayPalClient({
sandbox: false,
user: faker.string.uuid(),
pwd: faker.string.uuid(),
signature: faker.string.uuid(),
});

paypalManager = new PayPalManager(kyselyDb, paypalClient);
});

afterAll(async () => {
Expand All @@ -33,4 +37,25 @@ describe('paypalManager', () => {
it('instantiates class (TODO: remove me)', () => {
expect(paypalManager).toBeTruthy();
});

describe('getCheckoutToken', () => {
it('returns token and calls setExpressCheckout with passed options', async () => {
const currencyCode = faker.finance.currencyCode();
const token = faker.string.uuid();
const successfulSetExpressCheckoutResponse =
NVPSetExpressCheckoutResponseFactory({
TOKEN: token,
});

paypalClient.setExpressCheckout = jest
.fn()
.mockResolvedValueOnce(successfulSetExpressCheckoutResponse);

const result = await paypalManager.getCheckoutToken(currencyCode);

expect(result).toEqual(successfulSetExpressCheckoutResponse.TOKEN);
expect(paypalClient.setExpressCheckout).toBeCalledTimes(1);
expect(paypalClient.setExpressCheckout).toBeCalledWith({ currencyCode });
});
});
});
13 changes: 12 additions & 1 deletion libs/payments/paypal/src/lib/paypal.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { Injectable } from '@nestjs/common';
import { PayPalClient } from './paypal.client';
import { AccountDatabase } from '@fxa/shared/db/mysql/account';
import { PayPalClient } from './paypal.client';

@Injectable()
export class PayPalManager {
constructor(private db: AccountDatabase, private client: PayPalClient) {}

/**
* Get a token authorizing transaction to move to the next stage.
*
* If the call to PayPal fails, a PayPalClientError will be thrown.
*
*/
async getCheckoutToken(currencyCode: string) {
const response = await this.client.setExpressCheckout({ currencyCode });
return response.TOKEN;
}
}

0 comments on commit 4dfbfbc

Please sign in to comment.