Skip to content

Commit 4dfbfbc

Browse files
authored
Merge pull request #16481 from mozilla/FXA-8932
feat(payments-paypal): Create PayPalManager getCheckoutToken
2 parents 7f6172c + f8314a7 commit 4dfbfbc

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed
Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import { PayPalManager } from './paypal.manager';
1+
import { faker } from '@faker-js/faker';
22
import { Kysely } from 'kysely';
3+
34
import { DB, testAccountDatabaseSetup } from '@fxa/shared/db/mysql/account';
5+
6+
import { NVPSetExpressCheckoutResponseFactory } from './factories';
47
import { PayPalClient } from './paypal.client';
5-
import { faker } from '@faker-js/faker';
8+
import { PayPalManager } from './paypal.manager';
69

710
describe('paypalManager', () => {
8-
let paypalManager: PayPalManager;
911
let kyselyDb: Kysely<DB>;
12+
let paypalClient: PayPalClient;
13+
let paypalManager: PayPalManager;
1014

1115
beforeAll(async () => {
1216
kyselyDb = await testAccountDatabaseSetup([
1317
'paypalCustomers',
1418
'accountCustomers',
1519
]);
16-
paypalManager = new PayPalManager(
17-
kyselyDb,
18-
new PayPalClient({
19-
sandbox: false,
20-
user: faker.string.uuid(),
21-
pwd: faker.string.uuid(),
22-
signature: faker.string.uuid(),
23-
})
24-
);
20+
21+
paypalClient = new PayPalClient({
22+
sandbox: false,
23+
user: faker.string.uuid(),
24+
pwd: faker.string.uuid(),
25+
signature: faker.string.uuid(),
26+
});
27+
28+
paypalManager = new PayPalManager(kyselyDb, paypalClient);
2529
});
2630

2731
afterAll(async () => {
@@ -33,4 +37,25 @@ describe('paypalManager', () => {
3337
it('instantiates class (TODO: remove me)', () => {
3438
expect(paypalManager).toBeTruthy();
3539
});
40+
41+
describe('getCheckoutToken', () => {
42+
it('returns token and calls setExpressCheckout with passed options', async () => {
43+
const currencyCode = faker.finance.currencyCode();
44+
const token = faker.string.uuid();
45+
const successfulSetExpressCheckoutResponse =
46+
NVPSetExpressCheckoutResponseFactory({
47+
TOKEN: token,
48+
});
49+
50+
paypalClient.setExpressCheckout = jest
51+
.fn()
52+
.mockResolvedValueOnce(successfulSetExpressCheckoutResponse);
53+
54+
const result = await paypalManager.getCheckoutToken(currencyCode);
55+
56+
expect(result).toEqual(successfulSetExpressCheckoutResponse.TOKEN);
57+
expect(paypalClient.setExpressCheckout).toBeCalledTimes(1);
58+
expect(paypalClient.setExpressCheckout).toBeCalledWith({ currencyCode });
59+
});
60+
});
3661
});

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { Injectable } from '@nestjs/common';
6-
import { PayPalClient } from './paypal.client';
76
import { AccountDatabase } from '@fxa/shared/db/mysql/account';
7+
import { PayPalClient } from './paypal.client';
88

99
@Injectable()
1010
export class PayPalManager {
1111
constructor(private db: AccountDatabase, private client: PayPalClient) {}
12+
13+
/**
14+
* Get a token authorizing transaction to move to the next stage.
15+
*
16+
* If the call to PayPal fails, a PayPalClientError will be thrown.
17+
*
18+
*/
19+
async getCheckoutToken(currencyCode: string) {
20+
const response = await this.client.setExpressCheckout({ currencyCode });
21+
return response.TOKEN;
22+
}
1223
}

0 commit comments

Comments
 (0)