Skip to content

Commit f173605

Browse files
feat(payment): PAYPAL-5717 Added AppSwitch to PPCP button strategy
1 parent 6f76c78 commit f173605

File tree

2 files changed

+99
-4
lines changed

2 files changed

+99
-4
lines changed

packages/paypal-commerce-integration/src/paypal-commerce/paypal-commerce-button-strategy.spec.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ describe('PayPalCommerceButtonStrategy', () => {
103103
};
104104

105105
const storeConfig = getConfig().storeConfig;
106+
const resumeMock = jest.fn();
106107

107108
beforeEach(() => {
108109
buyNowCart = getBuyNowCart();
@@ -244,6 +245,8 @@ describe('PayPalCommerceButtonStrategy', () => {
244245
isEligible: jest.fn(() => true),
245246
render: jest.fn(),
246247
close: jest.fn(),
248+
hasReturned: jest.fn().mockReturnValue(true),
249+
resume: resumeMock,
247250
};
248251
},
249252
);
@@ -371,17 +374,46 @@ describe('PayPalCommerceButtonStrategy', () => {
371374

372375
describe('#renderButton', () => {
373376
it('initializes PayPal button to render (default flow)', async () => {
377+
jest.spyOn(
378+
paymentIntegrationService.getState(),
379+
'getStoreConfigOrThrow',
380+
).mockReturnValue({
381+
...storeConfig,
382+
checkoutSettings: {
383+
...storeConfig.checkoutSettings,
384+
features: {
385+
'PAYPAL-5716.app_switch_functionality': false,
386+
},
387+
},
388+
});
374389
await strategy.initialize(initializationOptions);
375390

376391
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
377-
appSwitchWhenAvailable: true,
378392
fundingSource: paypalSdk.FUNDING.PAYPAL,
379393
style: paypalCommerceOptions.style,
380394
createOrder: expect.any(Function),
381395
onApprove: expect.any(Function),
382396
});
383397
});
384398

399+
it('calls PayPal button resume', async () => {
400+
jest.spyOn(
401+
paymentIntegrationService.getState(),
402+
'getStoreConfigOrThrow',
403+
).mockReturnValue({
404+
...storeConfig,
405+
checkoutSettings: {
406+
...storeConfig.checkoutSettings,
407+
features: {
408+
'PAYPAL-5716.app_switch_functionality': true,
409+
},
410+
},
411+
});
412+
await strategy.initialize(initializationOptions);
413+
414+
expect(resumeMock).toHaveBeenCalled();
415+
});
416+
385417
it('initializes PayPal button to render (buy now flow)', async () => {
386418
await strategy.initialize(buyNowInitializationOptions);
387419

@@ -413,7 +445,6 @@ describe('PayPalCommerceButtonStrategy', () => {
413445
await strategy.initialize(initializationOptions);
414446

415447
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
416-
appSwitchWhenAvailable: true,
417448
fundingSource: paypalSdk.FUNDING.PAYPAL,
418449
style: paypalCommerceOptions.style,
419450
createOrder: expect.any(Function),
@@ -486,6 +517,30 @@ describe('PayPalCommerceButtonStrategy', () => {
486517
defaultButtonContainerId,
487518
);
488519
});
520+
521+
it('initializes PayPal button to render with appSwitch flag', async () => {
522+
jest.spyOn(
523+
paymentIntegrationService.getState(),
524+
'getStoreConfigOrThrow',
525+
).mockReturnValue({
526+
...storeConfig,
527+
checkoutSettings: {
528+
...storeConfig.checkoutSettings,
529+
features: {
530+
'PAYPAL-5716.app_switch_functionality': true,
531+
},
532+
},
533+
});
534+
await strategy.initialize(initializationOptions);
535+
536+
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
537+
appSwitchWhenAvailable: true,
538+
fundingSource: paypalSdk.FUNDING.PAYPAL,
539+
style: paypalCommerceOptions.style,
540+
createOrder: expect.any(Function),
541+
onApprove: expect.any(Function),
542+
});
543+
});
489544
});
490545

491546
describe('#createOrder', () => {
@@ -512,6 +567,35 @@ describe('PayPalCommerceButtonStrategy', () => {
512567
'paypalcommerce',
513568
);
514569
});
570+
571+
it('creates paypal order with user agent', async () => {
572+
Object.defineProperty(window.navigator, 'userAgent', {
573+
value: 'Mozilla',
574+
configurable: true,
575+
});
576+
jest.spyOn(
577+
paymentIntegrationService.getState(),
578+
'getStoreConfigOrThrow',
579+
).mockReturnValue({
580+
...storeConfig,
581+
checkoutSettings: {
582+
...storeConfig.checkoutSettings,
583+
features: {
584+
'PAYPAL-5716.app_switch_functionality': true,
585+
},
586+
},
587+
});
588+
await strategy.initialize(initializationOptions);
589+
590+
eventEmitter.emit('createOrder');
591+
592+
await new Promise((resolve) => process.nextTick(resolve));
593+
594+
expect(paypalCommerceIntegrationService.createOrder).toHaveBeenCalledWith(
595+
'paypalcommerce',
596+
{ userAgent: 'Mozilla' },
597+
);
598+
});
515599
});
516600

517601
describe('#handleClick', () => {

packages/paypal-commerce-integration/src/paypal-commerce/paypal-commerce-button-strategy.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import PayPalCommerceButtonInitializeOptions, {
2424
import { isExperimentEnabled } from '@bigcommerce/checkout-sdk/utility';
2525

2626
export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrategy {
27+
private methodId?: string;
28+
2729
constructor(
2830
private paymentIntegrationService: PaymentIntegrationService,
2931
private paypalCommerceIntegrationService: PayPalCommerceIntegrationService,
@@ -69,6 +71,8 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
6971
);
7072
}
7173

74+
this.methodId = methodId;
75+
7276
if (!isBuyNowFlow) {
7377
// Info: default checkout should not be loaded for BuyNow flow,
7478
// since there is no checkout session available for that.
@@ -264,13 +268,20 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
264268

265269
/**
266270
*
267-
* PayPal AppSwitch experiments handling
271+
* PayPal AppSwitch enabling handling
268272
*
269273
*/
270274
private isPaypalCommerceAppSwitchEnabled(): boolean {
271275
const state = this.paymentIntegrationService.getState();
272276
const features = state.getStoreConfigOrThrow().checkoutSettings.features;
277+
const paymentMethod = state.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(
278+
this.methodId || '',
279+
);
280+
const { isHostedCheckoutEnabled } = paymentMethod.initializationData || {};
273281

274-
return isExperimentEnabled(features, 'PAYPAL-5716.app_switch_functionality');
282+
return (
283+
!isHostedCheckoutEnabled &&
284+
isExperimentEnabled(features, 'PAYPAL-5716.app_switch_functionality')
285+
);
275286
}
276287
}

0 commit comments

Comments
 (0)