@@ -23,6 +23,8 @@ import BigCommercePaymentsButtonInitializeOptions, {
2323} from './bigcommerce-payments-button-initialize-options' ;
2424
2525export default class BigCommercePaymentsButtonStrategy implements CheckoutButtonStrategy {
26+ private buyNowCartId ?: string ;
27+
2628 constructor (
2729 private paymentIntegrationService : PaymentIntegrationService ,
2830 private bigCommercePaymentsIntegrationService : BigCommercePaymentsIntegrationService ,
@@ -88,18 +90,18 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
8890 false ,
8991 ) ;
9092
91- this . renderButton ( containerId , methodId , bigcommerce_payments , isBuyNowFlow ) ;
93+ this . renderButton ( containerId , methodId , bigcommerce_payments ) ;
9294 }
9395
9496 deinitialize ( ) : Promise < void > {
97+ this . buyNowCartId = undefined ;
9598 return Promise . resolve ( ) ;
9699 }
97100
98101 private renderButton (
99102 containerId : string ,
100103 methodId : string ,
101104 bigcommerce_payments : BigCommercePaymentsButtonInitializeOptions ,
102- isBuyNowFlow ?: boolean ,
103105 ) : void {
104106 const { buyNowInitializeOptions, style, onComplete, onEligibilityFailure } =
105107 bigcommerce_payments ;
@@ -108,14 +110,10 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
108110 const state = this . paymentIntegrationService . getState ( ) ;
109111 const paymentMethod =
110112 state . getPaymentMethodOrThrow < BigCommercePaymentsInitializationData > ( methodId ) ;
111- const { isHostedCheckoutEnabled, isAppSwitchEnabled } =
113+ const { isHostedCheckoutEnabled, isServerSideShippingCallbacksEnabled } =
112114 paymentMethod . initializationData || { } ;
113115
114116 const defaultCallbacks = {
115- ...( ! isBuyNowFlow &&
116- this . isPaypalCommerceAppSwitchEnabled ( methodId ) && {
117- appSwitchWhenAvailable : true ,
118- } ) ,
119117 createOrder : ( ) =>
120118 this . bigCommercePaymentsIntegrationService . createOrder ( 'bigcommerce_payments' ) ,
121119 onApprove : ( { orderID } : ApproveCallbackPayload ) =>
@@ -128,14 +126,20 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
128126 } ;
129127
130128 const hostedCheckoutCallbacks = {
131- ...( ! isAppSwitchEnabled && {
129+ ...( ! isServerSideShippingCallbacksEnabled && {
132130 onShippingAddressChange : ( data : ShippingAddressChangeCallbackPayload ) =>
133131 this . onShippingAddressChange ( data ) ,
134132 onShippingOptionsChange : ( data : ShippingOptionChangeCallbackPayload ) =>
135133 this . onShippingOptionsChange ( data ) ,
136134 } ) ,
137135 onApprove : ( data : ApproveCallbackPayload , actions : ApproveCallbackActions ) =>
138- this . onHostedCheckoutApprove ( data , actions , methodId , onComplete ) ,
136+ this . onHostedCheckoutApprove (
137+ data ,
138+ actions ,
139+ methodId ,
140+ onComplete ,
141+ isServerSideShippingCallbacksEnabled ,
142+ ) ,
139143 } ;
140144
141145 const buttonRenderOptions : BigCommercePaymentsButtonsOptions = {
@@ -149,11 +153,7 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
149153 const paypalButton = paypalSdk . Buttons ( buttonRenderOptions ) ;
150154
151155 if ( paypalButton . isEligible ( ) ) {
152- if ( paypalButton . hasReturned ?.( ) && this . isPaypalCommerceAppSwitchEnabled ( methodId ) ) {
153- paypalButton . resume ?.( ) ;
154- } else {
155- paypalButton . render ( `#${ containerId } ` ) ;
156- }
156+ paypalButton . render ( `#${ containerId } ` ) ;
157157 } else if ( onEligibilityFailure && typeof onEligibilityFailure === 'function' ) {
158158 onEligibilityFailure ( ) ;
159159 } else {
@@ -169,6 +169,7 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
169169 await this . bigCommercePaymentsIntegrationService . createBuyNowCartOrThrow (
170170 buyNowInitializeOptions ,
171171 ) ;
172+ this . buyNowCartId = buyNowCart . id ;
172173
173174 await this . paymentIntegrationService . loadCheckout ( buyNowCart . id ) ;
174175 }
@@ -179,6 +180,7 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
179180 actions : ApproveCallbackActions ,
180181 methodId : string ,
181182 onComplete ?: ( ) => void ,
183+ isServerSideShippingCallbacksEnabled ?: boolean ,
182184 ) : Promise < boolean > {
183185 if ( ! data . orderID ) {
184186 throw new MissingDataError ( MissingDataErrorType . MissingOrderId ) ;
@@ -187,6 +189,8 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
187189 const state = this . paymentIntegrationService . getState ( ) ;
188190 const cart = state . getCartOrThrow ( ) ;
189191 const orderDetails = await actions . order . get ( ) ;
192+ let shippingAddress ;
193+ let selectedShippingOptionId ;
190194
191195 try {
192196 const billingAddress =
@@ -197,12 +201,33 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
197201 await this . paymentIntegrationService . updateBillingAddress ( billingAddress ) ;
198202
199203 if ( cart . lineItems . physicalItems . length > 0 ) {
200- const shippingAddress =
201- this . bigCommercePaymentsIntegrationService . getShippingAddressFromOrderDetails (
202- orderDetails ,
203- ) ;
204+ if ( isServerSideShippingCallbacksEnabled ) {
205+ await this . paymentIntegrationService . loadCheckout ( this . buyNowCartId || cart . id ) ;
206+ const refreshedState = this . paymentIntegrationService . getState ( ) ;
207+ const consignment = refreshedState . getConsignmentsOrThrow ( ) [ 0 ] ;
208+ selectedShippingOptionId = consignment . selectedShippingOption ?. id ;
209+ const quoteShippingAddress = consignment . shippingAddress ;
210+ shippingAddress = {
211+ ...quoteShippingAddress ,
212+ ...this . bigCommercePaymentsIntegrationService . getShippingAddressFromOrderDetails (
213+ orderDetails ,
214+ ) ,
215+ } ;
216+ } else {
217+ shippingAddress =
218+ this . bigCommercePaymentsIntegrationService . getShippingAddressFromOrderDetails (
219+ orderDetails ,
220+ ) ;
221+ }
204222
205223 await this . paymentIntegrationService . updateShippingAddress ( shippingAddress ) ;
224+
225+ if ( selectedShippingOptionId ) {
226+ await this . paymentIntegrationService . selectShippingOption (
227+ selectedShippingOptionId ,
228+ ) ;
229+ }
230+
206231 await this . bigCommercePaymentsIntegrationService . updateOrder ( ) ;
207232 }
208233
@@ -271,17 +296,4 @@ export default class BigCommercePaymentsButtonStrategy implements CheckoutButton
271296 throw error ;
272297 }
273298 }
274-
275- /**
276- *
277- * PayPal AppSwitch enabling handling
278- *
279- */
280- private isPaypalCommerceAppSwitchEnabled ( methodId : string ) : boolean {
281- const state = this . paymentIntegrationService . getState ( ) ;
282- const paymentMethod =
283- state . getPaymentMethodOrThrow < BigCommercePaymentsInitializationData > ( methodId ) ;
284-
285- return paymentMethod . initializationData ?. isAppSwitchEnabled ?? false ;
286- }
287299}
0 commit comments