Skip to content

Commit 407c095

Browse files
feat(payment): PAYPAL-5725 add bcp payments app switch
1 parent bc3e4d3 commit 407c095

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

packages/bigcommerce-payments-integration/src/bigcommerce-payments/bigcommerce-payments-payment-strategy.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,64 @@ describe('BigCommercePaymentsPaymentStrategy', () => {
225225
});
226226
});
227227

228+
it('render button with appSwitch flag', async () => {
229+
jest.spyOn(
230+
paymentIntegrationService.getState(),
231+
'getPaymentMethodOrThrow',
232+
).mockReturnValue({
233+
...paymentMethod,
234+
initializationData: {
235+
...paymentMethod.initializationData,
236+
isAppSwitchEnabled: true,
237+
},
238+
});
239+
240+
await strategy.initialize(initializationOptions);
241+
242+
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
243+
appSwitchWhenAvailable: true,
244+
fundingSource: paypalSdk.FUNDING.PAYPAL,
245+
style: {
246+
color: 'black',
247+
height: 55,
248+
label: 'pay',
249+
},
250+
createOrder: expect.any(Function),
251+
onClick: expect.any(Function),
252+
onApprove: expect.any(Function),
253+
onCancel: expect.any(Function),
254+
onError: expect.any(Function),
255+
});
256+
});
257+
258+
it('calls resume when appSwitch enabled and returned from app', async () => {
259+
jest.spyOn(
260+
paymentIntegrationService.getState(),
261+
'getPaymentMethodOrThrow',
262+
).mockReturnValue({
263+
...paymentMethod,
264+
initializationData: {
265+
...paymentMethod.initializationData,
266+
isAppSwitchEnabled: true,
267+
},
268+
});
269+
270+
const resumeMock = jest.fn();
271+
const bigCommercePaymentsSdkRenderMock = jest.fn();
272+
273+
jest.spyOn(paypalSdk, 'Buttons').mockImplementation(() => ({
274+
close: jest.fn(),
275+
isEligible: jest.fn(() => true),
276+
render: bigCommercePaymentsSdkRenderMock,
277+
hasReturned: jest.fn(() => true),
278+
resume: resumeMock,
279+
}));
280+
281+
await strategy.initialize(initializationOptions);
282+
283+
expect(resumeMock).toHaveBeenCalled();
284+
});
285+
228286
it('does not render paypal button if it is not eligible', async () => {
229287
const bigCommercePaymentsSdkRenderMock = jest.fn();
230288

packages/bigcommerce-payments-integration/src/bigcommerce-payments/bigcommerce-payments-payment-strategy.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ export default class BigCommercePaymentsPaymentStrategy implements PaymentStrate
236236
const { container, onError, onRenderButton, onValidate, submitForm } = bigcommerce_payments;
237237

238238
const buttonOptions: BigCommercePaymentsButtonsOptions = {
239+
...(this.isPaypalCommerceAppSwitchEnabled(methodId) && {
240+
appSwitchWhenAvailable: true,
241+
}),
239242
fundingSource: paypalSdk.FUNDING.PAYPAL,
240243
style: this.bigCommercePaymentsIntegrationService.getValidButtonStyle(
241244
checkoutPaymentButtonStyles,
@@ -257,7 +260,11 @@ export default class BigCommercePaymentsPaymentStrategy implements PaymentStrate
257260
onRenderButton();
258261
}
259262

260-
this.paypalButton.render(container);
263+
if (this.paypalButton.hasReturned?.() && this.isPaypalCommerceAppSwitchEnabled(methodId)) {
264+
this.paypalButton.resume?.();
265+
} else {
266+
this.paypalButton.render(container);
267+
}
261268
}
262269

263270
private async handleClick(
@@ -373,4 +380,17 @@ export default class BigCommercePaymentsPaymentStrategy implements PaymentStrate
373380

374381
return false;
375382
}
383+
384+
/**
385+
*
386+
* PayPal AppSwitch enabling handling
387+
*
388+
*/
389+
private isPaypalCommerceAppSwitchEnabled(methodId: string): boolean {
390+
const state = this.paymentIntegrationService.getState();
391+
const paymentMethod =
392+
state.getPaymentMethodOrThrow<BigCommercePaymentsInitializationData>(methodId);
393+
394+
return paymentMethod.initializationData?.isAppSwitchEnabled ?? false;
395+
}
376396
}

0 commit comments

Comments
 (0)