Skip to content

Commit 4439a6c

Browse files
authored
Merge pull request #18901 from mozilla/fxa-11679-paypal-payment-error
fix(paypal): do not return inactive billing agreemnt ids
2 parents 1e1c39b + ec0e05e commit 4439a6c

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('PaypalBillingAgreementManager', () => {
8181
const mockBillingAgreementId = faker.string.uuid();
8282

8383
jest
84-
.spyOn(paypalBillingAgreementManager, 'retrieveId')
84+
.spyOn(paypalBillingAgreementManager, 'retrieveActiveId')
8585
.mockResolvedValue(undefined);
8686

8787
jest
@@ -258,16 +258,16 @@ describe('PaypalBillingAgreementManager', () => {
258258
});
259259
});
260260

261-
describe('getCustomerBillingAgreementId', () => {
262-
it("returns the customer's current PayPal billing agreement ID", async () => {
261+
describe('retrieveActiveId', () => {
262+
it("returns the customer's current active PayPal billing agreement ID", async () => {
263263
const uid = faker.string.uuid();
264264
const mockPayPalCustomer = ResultPaypalCustomerFactory();
265265

266266
jest
267267
.spyOn(paypalCustomerManager, 'fetchPaypalCustomersByUid')
268268
.mockResolvedValue([mockPayPalCustomer]);
269269

270-
const result = await paypalBillingAgreementManager.retrieveId(uid);
270+
const result = await paypalBillingAgreementManager.retrieveActiveId(uid);
271271
expect(result).toEqual(mockPayPalCustomer.billingAgreementId);
272272
});
273273

@@ -278,7 +278,19 @@ describe('PaypalBillingAgreementManager', () => {
278278
.spyOn(paypalCustomerManager, 'fetchPaypalCustomersByUid')
279279
.mockResolvedValue([]);
280280

281-
const result = await paypalBillingAgreementManager.retrieveId(uid);
281+
const result = await paypalBillingAgreementManager.retrieveActiveId(uid);
282+
expect(result).toEqual(undefined);
283+
});
284+
285+
it('returns undefined if billing agreement is not active', async () => {
286+
const uid = faker.string.uuid();
287+
const mockPayPalCustomer = ResultPaypalCustomerFactory({ status: 'Cancelled' });
288+
289+
jest
290+
.spyOn(paypalCustomerManager, 'fetchPaypalCustomersByUid')
291+
.mockResolvedValue([mockPayPalCustomer]);
292+
293+
const result = await paypalBillingAgreementManager.retrieveActiveId(uid);
282294
expect(result).toEqual(undefined);
283295
});
284296

@@ -292,7 +304,7 @@ describe('PaypalBillingAgreementManager', () => {
292304
.mockResolvedValue([mockPayPalCustomer1, mockPayPalCustomer2]);
293305

294306
await expect(
295-
paypalBillingAgreementManager.retrieveId(uid)
307+
paypalBillingAgreementManager.retrieveActiveId(uid)
296308
).rejects.toBeInstanceOf(PaypalCustomerMultipleRecordsError);
297309
});
298310
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export class PaypalBillingAgreementManager {
1515
constructor(
1616
private client: PayPalClient,
1717
private paypalCustomerManager: PaypalCustomerManager
18-
) {}
18+
) { }
1919

2020
public async retrieveOrCreateId(
2121
uid: string,
2222
hasSubscriptions: boolean,
2323
token?: string
2424
) {
25-
const existingBillingAgreementId = await this.retrieveId(uid);
25+
const existingBillingAgreementId = await this.retrieveActiveId(uid);
2626
if (existingBillingAgreementId) return existingBillingAgreementId;
2727

2828
if (hasSubscriptions) {
@@ -96,12 +96,12 @@ export class PaypalBillingAgreementManager {
9696
* Retrieves the customer’s current paypal billing agreement ID from the
9797
* auth database via the Paypal repository
9898
*/
99-
async retrieveId(uid: string): Promise<string | undefined> {
99+
async retrieveActiveId(uid: string): Promise<string | undefined> {
100100
const paypalCustomer =
101101
await this.paypalCustomerManager.fetchPaypalCustomersByUid(uid);
102102
const firstRecord = paypalCustomer.at(0);
103103

104-
if (!firstRecord) return;
104+
if (!firstRecord || firstRecord?.status !== 'active') return;
105105
if (paypalCustomer.length > 1)
106106
throw new PaypalCustomerMultipleRecordsError(uid);
107107

0 commit comments

Comments
 (0)