Skip to content

Commit 5b28845

Browse files
feat(payment): PAYPAL-5717 Added AppSwitch to PPCP button strategy
1 parent e653ddf commit 5b28845

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

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

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -397,24 +397,37 @@ describe('PayPalCommerceButtonStrategy', () => {
397397
});
398398

399399
it('calls PayPal button resume', async () => {
400+
const paymentMethodWithShippingOptionsFeature = {
401+
...paymentMethod,
402+
initializationData: {
403+
...paymentMethod.initializationData,
404+
isAppSwitchEnabled: true,
405+
},
406+
};
407+
400408
jest.spyOn(
401409
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-
});
410+
'getPaymentMethodOrThrow',
411+
).mockReturnValue(paymentMethodWithShippingOptionsFeature);
412412
await strategy.initialize(initializationOptions);
413413

414414
expect(resumeMock).toHaveBeenCalled();
415415
});
416416

417417
it('initializes PayPal button to render (buy now flow)', async () => {
418+
const paymentMethodWithShippingOptionsFeature = {
419+
...paymentMethod,
420+
initializationData: {
421+
...paymentMethod.initializationData,
422+
isAppSwitchEnabled: true,
423+
},
424+
};
425+
426+
jest.spyOn(
427+
paymentIntegrationService.getState(),
428+
'getPaymentMethodOrThrow',
429+
).mockReturnValue(paymentMethodWithShippingOptionsFeature);
430+
await strategy.initialize(initializationOptions);
418431
await strategy.initialize(buyNowInitializationOptions);
419432

420433
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
@@ -485,10 +498,11 @@ describe('PayPalCommerceButtonStrategy', () => {
485498
await strategy.initialize(initializationOptions);
486499

487500
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
488-
fundingSource: paypalSdk.FUNDING.PAYPAL,
489-
style: paypalCommerceOptions.style,
501+
appSwitchWhenAvailable: true,
490502
createOrder: expect.any(Function),
503+
fundingSource: paypalSdk.FUNDING.PAYPAL,
491504
onApprove: expect.any(Function),
505+
style: paypalCommerceOptions.style,
492506
});
493507
});
494508

@@ -557,26 +571,27 @@ describe('PayPalCommerceButtonStrategy', () => {
557571
});
558572

559573
it('initializes PayPal button to render with appSwitch flag', async () => {
574+
const paymentMethodWithShippingOptionsFeature = {
575+
...paymentMethod,
576+
initializationData: {
577+
...paymentMethod.initializationData,
578+
isAppSwitchEnabled: true,
579+
},
580+
};
581+
560582
jest.spyOn(
561583
paymentIntegrationService.getState(),
562-
'getStoreConfigOrThrow',
563-
).mockReturnValue({
564-
...storeConfig,
565-
checkoutSettings: {
566-
...storeConfig.checkoutSettings,
567-
features: {
568-
'PAYPAL-5716.app_switch_functionality': true,
569-
},
570-
},
571-
});
584+
'getPaymentMethodOrThrow',
585+
).mockReturnValue(paymentMethodWithShippingOptionsFeature);
586+
await strategy.initialize(initializationOptions);
572587
await strategy.initialize(initializationOptions);
573588

574589
expect(paypalSdk.Buttons).toHaveBeenCalledWith({
575590
appSwitchWhenAvailable: true,
576-
fundingSource: paypalSdk.FUNDING.PAYPAL,
577-
style: paypalCommerceOptions.style,
578591
createOrder: expect.any(Function),
592+
fundingSource: paypalSdk.FUNDING.PAYPAL,
579593
onApprove: expect.any(Function),
594+
style: paypalCommerceOptions.style,
580595
});
581596
});
582597
});

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ import {
2121
import PayPalCommerceButtonInitializeOptions, {
2222
WithPayPalCommerceButtonInitializeOptions,
2323
} from './paypal-commerce-button-initialize-options';
24-
import { isExperimentEnabled } from '@bigcommerce/checkout-sdk/utility';
2524

2625
export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrategy {
27-
private methodId?: string;
28-
2926
constructor(
3027
private paymentIntegrationService: PaymentIntegrationService,
3128
private paypalCommerceIntegrationService: PayPalCommerceIntegrationService,
@@ -71,8 +68,6 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
7168
);
7269
}
7370

74-
this.methodId = methodId;
75-
7671
if (!isBuyNowFlow) {
7772
// Info: default checkout should not be loaded for BuyNow flow,
7873
// since there is no checkout session available for that.
@@ -110,9 +105,10 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
110105
paymentMethod.initializationData || {};
111106

112107
const defaultCallbacks = {
113-
...(this.isPaypalCommerceAppSwitchEnabled() && { appSwitchWhenAvailable: true }),
114-
createOrder: () =>
115-
this.paypalCommerceIntegrationService.createOrder('paypalcommerce'),
108+
// ...(this.isPaypalCommerceAppSwitchEnabled(methodId) && {
109+
// appSwitchWhenAvailable: true,
110+
// }),
111+
createOrder: () => this.paypalCommerceIntegrationService.createOrder('paypalcommerce'),
116112
onApprove: ({ orderID }: ApproveCallbackPayload) =>
117113
this.paypalCommerceIntegrationService.tokenizePayment(methodId, orderID),
118114
};
@@ -144,7 +140,7 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
144140
const paypalButton = paypalSdk.Buttons(buttonRenderOptions);
145141

146142
if (paypalButton.isEligible()) {
147-
if (paypalButton.hasReturned?.() && this.isPaypalCommerceAppSwitchEnabled()) {
143+
if (paypalButton.hasReturned?.() && this.isPaypalCommerceAppSwitchEnabled(methodId)) {
148144
paypalButton.resume?.();
149145
} else {
150146
paypalButton.render(`#${containerId}`);
@@ -270,17 +266,11 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
270266
* PayPal AppSwitch enabling handling
271267
*
272268
*/
273-
private isPaypalCommerceAppSwitchEnabled(): boolean {
269+
private isPaypalCommerceAppSwitchEnabled(methodId: string): boolean {
274270
const state = this.paymentIntegrationService.getState();
275-
const features = state.getStoreConfigOrThrow().checkoutSettings.features;
276-
const paymentMethod = state.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(
277-
this.methodId || '',
278-
);
279-
const { isHostedCheckoutEnabled } = paymentMethod.initializationData || {};
271+
const paymentMethod =
272+
state.getPaymentMethodOrThrow<PayPalCommerceInitializationData>(methodId);
280273

281-
return (
282-
!isHostedCheckoutEnabled &&
283-
isExperimentEnabled(features, 'PAYPAL-5716.app_switch_functionality')
284-
);
274+
return paymentMethod.initializationData?.isAppSwitchEnabled || false;
285275
}
286276
}

0 commit comments

Comments
 (0)