@@ -21,6 +21,7 @@ import {
2121import PayPalCommerceButtonInitializeOptions , {
2222 WithPayPalCommerceButtonInitializeOptions ,
2323} from './paypal-commerce-button-initialize-options' ;
24+ import { isExperimentEnabled } from '@bigcommerce/checkout-sdk/utility' ;
2425
2526export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrategy {
2627 constructor (
@@ -96,6 +97,7 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
9697 paypalcommerce : PayPalCommerceButtonInitializeOptions ,
9798 ) : void {
9899 const { buyNowInitializeOptions, style, onComplete, onEligibilityFailure } = paypalcommerce ;
100+ const userAgent = navigator . userAgent ;
99101
100102 const paypalSdk = this . paypalCommerceIntegrationService . getPayPalSdkOrThrow ( ) ;
101103 const state = this . paymentIntegrationService . getState ( ) ;
@@ -104,7 +106,11 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
104106 const { isHostedCheckoutEnabled } = paymentMethod . initializationData || { } ;
105107
106108 const defaultCallbacks = {
107- createOrder : ( ) => this . paypalCommerceIntegrationService . createOrder ( 'paypalcommerce' ) ,
109+ ...( this . isPaypalCommerceAppSwitchEnabled ( ) && { appSwitchWhenAvailable : true } ) ,
110+ createOrder : ( ) => this . paypalCommerceIntegrationService . createOrder (
111+ 'paypalcommerce' ,
112+ ...( this . isPaypalCommerceAppSwitchEnabled ( ) ? [ { userAgent : userAgent } ] : [ ] )
113+ ) ,
108114 onApprove : ( { orderID } : ApproveCallbackPayload ) =>
109115 this . paypalCommerceIntegrationService . tokenizePayment ( methodId , orderID ) ,
110116 } ;
@@ -134,7 +140,14 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
134140 const paypalButton = paypalSdk . Buttons ( buttonRenderOptions ) ;
135141
136142 if ( paypalButton . isEligible ( ) ) {
137- paypalButton . render ( `#${ containerId } ` ) ;
143+ if (
144+ paypalButton . hasReturned ?.( ) &&
145+ this . isPaypalCommerceAppSwitchEnabled ( )
146+ ) {
147+ paypalButton . resume ?.( ) ;
148+ } else {
149+ paypalButton . render ( `#${ containerId } ` ) ;
150+ }
138151 } else if ( onEligibilityFailure && typeof onEligibilityFailure === 'function' ) {
139152 onEligibilityFailure ( ) ;
140153 } else {
@@ -250,4 +263,16 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
250263 throw error ;
251264 }
252265 }
266+
267+ /**
268+ *
269+ * PayPal AppSwitch experiments handling
270+ *
271+ */
272+ private isPaypalCommerceAppSwitchEnabled ( ) : boolean {
273+ const state = this . paymentIntegrationService . getState ( ) ;
274+ const features = state . getStoreConfigOrThrow ( ) . checkoutSettings . features ;
275+
276+ return isExperimentEnabled ( features , 'PAYPAL-5716.app_switch_functionality' ) ;
277+ }
253278}
0 commit comments