-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
1038 lines (949 loc) · 45.2 KB
/
index.jsx
File metadata and controls
1038 lines (949 loc) · 45.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2025, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useEffect, useRef} from 'react'
import PropTypes from 'prop-types'
import {useIntl} from 'react-intl'
import {Box} from '@salesforce/retail-react-app/app/components/shared/ui'
import logger from '@salesforce/retail-react-app/app/utils/logger-instance'
import {DEFAULT_SHIPMENT_ID} from '@salesforce/retail-react-app/app/constants'
import {useShopperBasketsMutation} from '@salesforce/commerce-sdk-react'
import {useShopperOrdersMutation} from '@salesforce/commerce-sdk-react'
import {useShippingMethodsForShipment} from '@salesforce/commerce-sdk-react'
import {usePaymentConfiguration} from '@salesforce/commerce-sdk-react'
import useNavigation from '@salesforce/retail-react-app/app/hooks/use-navigation'
import {useSFPaymentsCountry} from '@salesforce/retail-react-app/app/hooks/use-sf-payments-country'
import {
EXPRESS_BUY_NOW,
EXPRESS_PAY_NOW,
useSFPayments,
useAutomaticCapture
} from '@salesforce/retail-react-app/app/hooks/use-sf-payments'
import {useToast} from '@salesforce/retail-react-app/app/hooks/use-toast'
import {
buildTheme,
getSFPaymentsInstrument,
transformAddressDetails,
transformShippingMethods,
getSelectedShippingMethodId,
createPaymentInstrumentBody,
isPayPalPaymentMethodType,
getClientSecret,
getGatewayFromPaymentMethod,
getExpressPaymentMethodType
} from '@salesforce/retail-react-app/app/utils/sf-payments-utils'
import {PAYMENT_GATEWAYS} from '@salesforce/retail-react-app/app/constants'
/*
These imports are needed during the failOrder process. The useAccessToken hook is not a commonly used hook
BUT it is needed to get the current order details and gaurd the failOrder process from failing
It's not a new pattern we are introducing: use-einstein.js uses it to get the access token
We are basically bypassing the React Query Wrapper and going directly to the Commerce API to get the order details
The React Query hooks are designed for declarative data fetching, so for imperative calls, you use the raw API client, which requires manual auth.
If we use the React Query hooks, we would need to wait for the query to complete before we can call the failOrder mutation
The useQueryClient is needed to clear the stale cache when the basket can't be recovered
*/
import {useCommerceApi, useAccessToken} from '@salesforce/commerce-sdk-react'
import {useQueryClient} from '@tanstack/react-query'
const SFPaymentsExpressButtons = ({
usage,
paymentCurrency,
paymentCountryCode,
initialAmount,
prepareBasket,
expressButtonLayout = 'vertical',
maximumButtonCount = undefined,
onPaymentMethodsRendered,
onExpressPaymentCompleted
}) => {
const intl = useIntl()
const navigate = useNavigation()
const toast = useToast()
const queryClient = useQueryClient()
const {countryCode: fallbackCountryCode} = useSFPaymentsCountry()
const {sfp, metadata, startConfirming, endConfirming} = useSFPayments()
const {data: paymentConfig} = usePaymentConfiguration({
parameters: {
currency: paymentCurrency,
countryCode: paymentCountryCode || fallbackCountryCode || 'US' // TODO: remove US when parameter made optional
//,zoneId: "stripeUSTest" //if you need to test with a different zone
}
})
const cardCaptureAutomatic = useAutomaticCapture()
const zoneId = paymentConfig?.zoneId
const {mutateAsync: updateBillingAddressForBasket} = useShopperBasketsMutation(
'updateBillingAddressForBasket'
)
const updateShippingAddressForShipment = useShopperBasketsMutation(
'updateShippingAddressForShipment'
)
const updateShippingMethod = useShopperBasketsMutation('updateShippingMethodForShipment')
const {mutateAsync: addPaymentInstrumentToBasket} = useShopperBasketsMutation(
'addPaymentInstrumentToBasket'
)
const {mutateAsync: updatePaymentInstrumentForOrder} = useShopperOrdersMutation(
'updatePaymentInstrumentForOrder'
)
const {mutateAsync: createOrder} = useShopperOrdersMutation('createOrder')
const {mutateAsync: removePaymentInstrumentFromBasket} = useShopperBasketsMutation(
'removePaymentInstrumentFromBasket'
)
const {mutateAsync: deleteBasket} = useShopperBasketsMutation('deleteBasket')
const {mutateAsync: failOrder} = useShopperOrdersMutation('failOrder')
const expressBasket = useRef(null)
const prepareBasketPromise = useRef(null)
const containerElementRef = useRef(null)
const expressComponent = useRef(null)
const prepareBasketRef = useRef(prepareBasket)
const failOrderCalledRef = useRef(false)
const orderRef = useRef(null)
// used to call failOrder
const api = useCommerceApi()
const {getTokenWhenReady} = useAccessToken()
// tracks if payment is in progress
const isPaymentInProgress = useRef(false)
// Update the ref whenever prepareBasket changes, including when the variant changes on PDP
// Using prepareBasketRef.current also ensures the function handlers always call the latest prepareBasket function
useEffect(() => {
prepareBasketRef.current = prepareBasket
}, [prepareBasket])
const {refetch: refetchShippingMethods} = useShippingMethodsForShipment(
{
parameters: {
basketId: expressBasket.current?.basketId,
shipmentId: DEFAULT_SHIPMENT_ID
}
},
{
enabled: Boolean(expressBasket.current?.basketId)
}
)
const ERROR_MESSAGE_KEYS = {
DEFAULT: 'DEFAULT',
FAIL_ORDER: 'FAIL_ORDER',
PREPARE_BASKET: 'PREPARE_BASKET',
PROCESS_PAYMENT: 'PROCESS_PAYMENT',
ORDER_RECOVERY_FAILED: 'ORDER_RECOVERY_FAILED'
}
const ERROR_MESSAGES = {
DEFAULT: {
defaultMessage:
'Your attempted payment was unsuccessful. You have not been charged and your order has not been placed. Please select a different payment method and submit payment again to complete your checkout and place your order.',
id: 'sfp_payments_express.error.default'
},
FAIL_ORDER: {
defaultMessage:
'Payment processing failed. Your order has been cancelled and your basket has been restored. Please try again or select a different payment method.',
id: 'sfp_payments_express.error.fail_order'
},
PREPARE_BASKET: {
defaultMessage:
'Unable to prepare basket for express payments. Please select all required product attributes.',
id: 'sfp_payments_express.error.prepare_basket'
},
PROCESS_PAYMENT: {
defaultMessage:
'Unable to process payment. Please try again or select a different payment method.',
id: 'sfp_payments_express.error.process_payment'
},
ORDER_RECOVERY_FAILED: {
defaultMessage:
'Order recovery failed. Please try again or select a different payment method.',
id: 'sfp_payments_express.error.order_recovery_failed'
}
}
const showErrorMessage = (messageKey = 'DEFAULT') => {
// If messageKey is a valid key in ERROR_MESSAGES, use it
if (ERROR_MESSAGES[messageKey]) {
toast({
title: intl.formatMessage(ERROR_MESSAGES[messageKey]),
status: 'error'
})
} else {
// Otherwise, treat it as a custom error message string
// (e.g., from e.message) or fallback to DEFAULT if empty
toast({
title: messageKey || ERROR_MESSAGES.DEFAULT.defaultMessage,
status: 'error'
})
}
}
/**
* Validate current shipping method is still applicable, update to first applicable if not
*/
const validateAndUpdateShippingMethod = async (
basketId,
currentBasket,
updatedShippingMethods
) => {
const currentShippingMethodId = currentBasket.shipments[0].shippingMethod?.id
if (
!updatedShippingMethods.applicableShippingMethods.find(
(method) => method.id === currentShippingMethodId
)
) {
// If the current shipping method isn't set or is inapplicable, set it to the first applicable one
return await updateShippingMethod.mutateAsync({
parameters: {
basketId: basketId,
shipmentId: DEFAULT_SHIPMENT_ID
},
body: {
id: updatedShippingMethods.applicableShippingMethods[0].id
}
})
}
return currentBasket
}
/**
* Attempts to fail an order and reopen the basket.
* Only calls failOrder if the order status supports the transition.
* @param {string} orderNo - The order number to fail
* @returns {Promise<boolean>} - true if failOrder succeeded and basket was reopened
*/
const attemptFailOrder = async (orderNo) => {
if (!orderNo || failOrderCalledRef.current) {
return false
}
try {
// Fetch current order status
const token = await getTokenWhenReady()
const currentOrder = await api.shopperOrders.getOrder({
parameters: {orderNo},
headers: {Authorization: `Bearer ${token}`}
})
// Only call failOrder if status allows the transition
if (currentOrder.status === 'created') {
await failOrder({
parameters: {orderNo, reopenBasket: true},
body: {reasonCode: 'payment_confirm_failure'}
})
return true // Basket was reopened
} else {
return false // Can't recover basket
}
} catch (error) {
return false
} finally {
// Mark as attempted to prevent retries
failOrderCalledRef.current = true
// Refresh cache to get updated basket state
queryClient.invalidateQueries()
}
}
/**
* Create order from basket and update payment instrument
*/
const createOrderAndUpdatePayment = async (basketId, paymentType, paymentData = {}) => {
// Create order from the basket
let order = await createOrder({
body: {basketId}
})
// Store orderNo immediately - basket is consumed at this point
const createdOrderNo = order.orderNo
// Find SF Payments payment instrument in created order
const orderPaymentInstrument = getSFPaymentsInstrument(order)
// Build the return URL (needed for updatePaymentInstrumentForOrder )
const baseReturnUrl = `${window.location.protocol}//${window.location.host}/checkout/payment-processing`
paymentData.returnUrl = baseReturnUrl +
'?orderNo=' + encodeURIComponent(createdOrderNo) +
'&zoneId=' + encodeURIComponent(zoneId) +
'&type=' + encodeURIComponent(paymentType)
try {
const paymentInstrumentBody = createPaymentInstrumentBody({
amount: order.orderTotal,
paymentMethodType: paymentType,
zoneId: zoneId,
paymentData: paymentData,
paymentMethods: paymentConfig?.paymentMethods,
paymentMethodSetAccounts: paymentConfig?.paymentMethodSetAccounts
})
// Update order payment instrument to create payment
order = await updatePaymentInstrumentForOrder({
parameters: {
orderNo: order.orderNo,
paymentInstrumentId: orderPaymentInstrument.paymentInstrumentId
},
body: paymentInstrumentBody
})
} catch (error) {
const statusCode = error?.response?.status || error?.status
const errorMessage = error?.message || error?.response?.data?.message || 'Unknown error'
const errorDetails = error?.response?.data || error?.body || {}
logger.error('Failed to patch payment instrument to order', {
namespace: 'SFPaymentsExpressButtons.createOrderAndUpdatePayment',
additionalProperties: {
statusCode,
errorMessage,
errorDetails,
basketId: expressBasket.current?.basketId,
paymentMethodType: paymentType,
orderTotal: expressBasket.current?.orderTotal,
productSubTotal: expressBasket.current?.productSubTotal,
error: error
}
})
const basketRecovered = await attemptFailOrder(createdOrderNo)
if (basketRecovered) {
showErrorMessage(ERROR_MESSAGE_KEYS.FAIL_ORDER)
} else {
showErrorMessage(ERROR_MESSAGE_KEYS.ORDER_RECOVERY_FAILED)
if (usage !== EXPRESS_BUY_NOW) {
navigate('/cart')
}
}
// Attach orderNo to the error so caller knows order was created
error.orderNo = createdOrderNo
throw error
}
return order
}
const createExpressCallback = (currentBasket, currentShippingMethods) => {
// Get currently selected shipping method
const selectedShippingMethodId = getSelectedShippingMethodId(
currentBasket,
currentShippingMethods
)
// Get representation of applicable shipping methods with the current one sorted at the top
const expressShippingMethods = transformShippingMethods(
currentShippingMethods.applicableShippingMethods,
currentBasket,
selectedShippingMethodId,
true
)
// Get representation of currently selected shipping method
const selectedShippingMethod =
expressShippingMethods.find((method) => method.id === selectedShippingMethodId) ||
expressShippingMethods[0]
// Create line items
const orderTotal = currentBasket?.orderTotal
const productSubTotal = currentBasket?.productSubTotal
const total = orderTotal || productSubTotal
// Validate that total is a valid number
if (isNaN(total) || total <= 0) {
logger.error('Invalid total amount', {
namespace: 'SFPaymentsExpressButtons.createExpressCallback',
additionalProperties: {orderTotal, productSubTotal, initialAmount, total}
})
throw new Error('Invalid basket total amount')
}
const lineItems = [
{
name: intl.formatMessage({
defaultMessage: 'Subtotal',
id: 'order_summary.label.subtotal'
}),
amount: total.toString()
}
]
// TODO: add discounts from currentBasket.orderPriceAdjustments
if (currentBasket.shippingTotal) {
lineItems.push({
name: intl.formatMessage({
defaultMessage: 'Shipping',
id: 'order_summary.label.shipping'
}),
amount: currentBasket.shippingTotal.toString()
})
}
if (currentBasket.taxTotal) {
lineItems.push({
name: intl.formatMessage({
defaultMessage: 'Tax',
id: 'order_summary.label.tax'
}),
amount: currentBasket.taxTotal.toString()
})
}
return {
total: total.toString(),
shippingMethods: expressShippingMethods,
selectedShippingMethod: selectedShippingMethod,
lineItems: lineItems
}
}
useEffect(() => {
// Remove containerElementRef.current from effect dependencies to prevent unnecessary re-renders
// instead use it in the if statement to check if the container element is attached to the DOM
if (metadata && sfp && paymentConfig && containerElementRef.current) {
// Skip re-initialization if payment is in progress
if (isPaymentInProgress.current && expressComponent.current) {
return
}
if (expressComponent.current) {
expressComponent.current.destroy()
expressComponent.current = null
}
let paymentMethodType = null
orderRef.current = null
const onClick = async (type) => {
// reset payment, order and failorder flags
isPaymentInProgress.current = true
failOrderCalledRef.current = false
orderRef.current = null
paymentMethodType = getExpressPaymentMethodType(
type,
paymentConfig?.paymentMethods,
paymentConfig?.paymentMethodSetAccounts
)
// For non-PayPal payment methods, prepare basket immediately
if (!isPayPalPaymentMethodType(paymentMethodType)) {
prepareBasketPromise.current = prepareBasketRef.current()
// Don't await - call asynchronously to avoid a potential gateway timeout
prepareBasketPromise.current
.then((basket) => {
expressBasket.current = basket
})
.catch((e) => {
prepareBasketPromise.current = null // Clear the promise so handlers don't try to await it
// Don't show toast for validation errors
if (!e.isValidationError) {
showErrorMessage(e.message || ERROR_MESSAGE_KEYS.PREPARE_BASKET)
}
})
}
return {
amount: initialAmount.toString(),
shippingRates: []
}
}
// Helper function to clean up express basket state
const cleanupExpressBasket = async () => {
// If an order was already created, the basket was consumed - don't try to clean it up
if (orderRef.current) {
expressBasket.current = null
return
}
// Only clean up if no order was created (basket still exists)
if (expressBasket.current) {
const sfPaymentsInstrument = getSFPaymentsInstrument(expressBasket.current)
if (sfPaymentsInstrument) {
try {
expressBasket.current = await removePaymentInstrumentFromBasket({
parameters: {
basketId: expressBasket.current.basketId,
paymentInstrumentId: sfPaymentsInstrument.paymentInstrumentId
}
})
} catch (cleanupError) {
logger.warn('Failed to remove payment instrument during cleanup', {
namespace: 'SFPaymentsExpressButtons.cleanupExpressBasket',
additionalProperties: {cleanupError}
})
}
}
// Delete the temporary basket if it exists
if (expressBasket.current?.basketId && expressBasket.current?.temporaryBasket) {
try {
await deleteBasket({
parameters: {basketId: expressBasket.current.basketId}
})
} catch (cleanupError) {
logger.warn('Failed to delete temporary basket during cleanup', {
namespace: 'SFPaymentsExpressButtons.cleanupExpressBasket',
additionalProperties: {cleanupError}
})
}
}
// Clear the ref after cleanup
expressBasket.current = null
}
}
const onCancel = async () => {
isPaymentInProgress.current = false
endConfirming()
await cleanupExpressBasket()
showErrorMessage(ERROR_MESSAGE_KEYS.DEFAULT)
}
const onShippingAddressChange = async (shippingAddress, callback) => {
try {
// Wait for basket to be prepared if it's not ready yet
if (prepareBasketPromise.current) {
try {
expressBasket.current = await prepareBasketPromise.current
} catch (e) {
// Promise failed - show error and return early
callback.updateShippingAddress({
errors: ['fail']
})
return
}
}
// Update the shipping address in the default shipment
let updatedBasket = await updateShippingAddressForShipment.mutateAsync({
parameters: {
basketId: expressBasket.current.basketId,
shipmentId: DEFAULT_SHIPMENT_ID,
useAsBilling: false
},
body: {
firstName: null,
lastName: null,
address1: null,
address2: null,
city: shippingAddress.city,
stateCode: shippingAddress.state,
postalCode: shippingAddress.postal_code,
countryCode: shippingAddress.country,
phone: null
}
})
// Fetch applicable shipping methods after address update
const {data: updatedShippingMethods} = await refetchShippingMethods()
// Validate and update shipping method if needed
updatedBasket = await validateAndUpdateShippingMethod(
expressBasket.current.basketId,
updatedBasket,
updatedShippingMethods
)
const expressCallback = createExpressCallback(
updatedBasket,
updatedShippingMethods
)
callback.updateShippingAddress(expressCallback)
} catch (e) {
callback.updateShippingAddress({
errors: ['fail']
})
showErrorMessage()
}
}
const onShippingMethodChange = async (shippingMethod, callback) => {
try {
// Wait for basket to be prepared if it's not ready yet
if (prepareBasketPromise.current) {
try {
expressBasket.current = await prepareBasketPromise.current
} catch (e) {
// Promise failed - show error and return early
callback.updateShippingMethod({
errors: ['fail']
})
return
}
}
// Update the shipping method in the default shipment
const updatedBasket = await updateShippingMethod.mutateAsync({
parameters: {
basketId: expressBasket.current.basketId,
shipmentId: DEFAULT_SHIPMENT_ID
},
body: {
id: shippingMethod.id
}
})
// Update expressBasket.current with the fresh basket data
expressBasket.current = updatedBasket
// Fetch applicable shipping methods after shipping method update
const {data: updatedShippingMethods} = await refetchShippingMethods()
const expressCallback = createExpressCallback(
updatedBasket,
updatedShippingMethods
)
callback.updateShippingMethod(expressCallback)
} catch (e) {
callback.updateShippingMethod({
errors: ['fail']
})
showErrorMessage()
}
}
//Async function to handle before payer approve event. This is called before payment is confirmed
const onPayerApprove = async (billingDetails, shippingDetails) => {
// Set confirmingBasket to show loading spinner during address updates
startConfirming(expressBasket.current)
// For non-PayPal methods, if order was already created in createIntentFunction,
// the basket is consumed and we shouldn't try to update addresses
if (!isPayPalPaymentMethodType(paymentMethodType) && orderRef.current) {
logger.info('Order already created, skipping address updates', {
namespace: 'SFPaymentsExpressButtons.onPayerApprove'
})
return
}
try {
// Transform both billing and shipping addresses
const {billingAddress, shippingAddress} = transformAddressDetails(
billingDetails,
shippingDetails
)
// Next update shipping address in basket
const updatedBasket = await updateShippingAddressForShipment.mutateAsync({
parameters: {
basketId: expressBasket.current.basketId,
shipmentId: DEFAULT_SHIPMENT_ID,
useAsBilling: false
},
body: shippingAddress
})
// Update expressBasket.current with the updated basket
expressBasket.current = updatedBasket
// Update billing address in basket
await updateBillingAddressForBasket({
parameters: {basketId: expressBasket.current.basketId},
body: billingAddress
})
// For Stripe, create SF Payments basket payment instrument before creating order
if (!isPayPalPaymentMethodType(paymentMethodType)) {
try {
expressBasket.current = await addPaymentInstrumentToBasket({
parameters: {basketId: updatedBasket.basketId},
body: createPaymentInstrumentBody({
amount:
updatedBasket.orderTotal || updatedBasket.productSubTotal,
paymentMethodType: paymentMethodType,
zoneId: zoneId
})
})
} catch (error) {
const statusCode = error?.response?.status || error?.status
const errorMessage =
error?.message || error?.response?.data?.message || 'Unknown error'
const errorDetails = error?.response?.data || error?.body || {}
logger.error('Failed to add payment instrument to basket', {
namespace: 'SFPaymentsExpressButtons.onPayerApprove',
additionalProperties: {
statusCode,
errorMessage,
errorDetails,
basketId: expressBasket.current?.basketId,
paymentMethodType,
orderTotal: expressBasket.current?.orderTotal,
productSubTotal: expressBasket.current?.productSubTotal,
error: error
}
})
showErrorMessage(ERROR_MESSAGE_KEYS.PROCESS_PAYMENT)
throw error
}
}
} catch (error) {
endConfirming()
throw error
}
}
/**
* Ensures a Salesforce Payments payment instrument exists in the basket.
* If one doesn't exist, removes any existing one and adds a new one.
* @param {Object} basket - The basket object
* @param {string} paymentMethodType - Type of payment method
* @returns {Promise<Object>} Updated basket with payment instrument
*/
const ensurePaymentInstrumentInBasket = async (basket, paymentMethodType) => {
// Check if payment instrument already exists
let sfPaymentsInstrument = getSFPaymentsInstrument(basket)
if (sfPaymentsInstrument) {
// Payment instrument already exists, return basket as-is
return basket
}
// Remove any existing Salesforce Payments payment instrument first
sfPaymentsInstrument = getSFPaymentsInstrument(basket)
if (sfPaymentsInstrument) {
basket = await removePaymentInstrumentFromBasket({
parameters: {
basketId: basket.basketId,
paymentInstrumentId: sfPaymentsInstrument.paymentInstrumentId
}
})
}
// Add Salesforce Payments payment instrument to basket
try {
basket = await addPaymentInstrumentToBasket({
parameters: {basketId: basket.basketId},
body: createPaymentInstrumentBody({
amount: basket.orderTotal || basket.productSubTotal,
paymentMethodType: paymentMethodType,
zoneId: zoneId
})
})
} catch (error) {
const statusCode = error?.response?.status || error?.status
const errorMessage =
error?.message || error?.response?.data?.message || 'Unknown error'
const errorDetails = error?.response?.data || error?.body || {}
logger.error('Failed to add payment instrument to basket', {
namespace: 'SFPaymentsExpressButtons.ensurePaymentInstrumentInBasket',
additionalProperties: {
statusCode,
errorMessage,
errorDetails,
basketId: basket?.basketId,
paymentMethodType,
orderTotal: basket?.orderTotal,
productSubTotal: basket?.productSubTotal,
error: error
}
})
showErrorMessage(ERROR_MESSAGE_KEYS.PROCESS_PAYMENT)
throw error
}
return basket
}
const createIntentFunction = async (paymentData = {}) => {
const gateway = getGatewayFromPaymentMethod(
paymentMethodType,
paymentConfig?.paymentMethods,
paymentConfig?.paymentMethodSetAccounts
)
const isAdyen = gateway === PAYMENT_GATEWAYS.ADYEN
// For PayPal/Venmo, prepare basket here since createIntentFunction is called after button click
if (isPayPalPaymentMethodType(paymentMethodType)) {
const currentBasket = await prepareBasketRef.current()
// update expressBasket.current with the fresh basket
expressBasket.current = currentBasket
}
if (!expressBasket.current) {
logger.error('Basket not ready', {
namespace: 'SFPaymentsExpressButtons.createIntentFunction'
})
throw new Error()
}
let updatedPaymentInstrument
if (isPayPalPaymentMethodType(paymentMethodType)) {
// Remove any leftover Salesforce Payments payment instrument from basket
const sfPaymentsInstrument = getSFPaymentsInstrument(expressBasket.current)
if (sfPaymentsInstrument) {
expressBasket.current = await removePaymentInstrumentFromBasket({
parameters: {
basketId: expressBasket.current.basketId,
paymentInstrumentId: sfPaymentsInstrument.paymentInstrumentId
}
})
}
// Add Salesforce Payments payment instrument to basket
try {
expressBasket.current = await addPaymentInstrumentToBasket({
parameters: {basketId: expressBasket.current.basketId},
body: createPaymentInstrumentBody({
amount:
expressBasket.current.orderTotal ||
expressBasket.current.productSubTotal,
paymentMethodType: paymentMethodType,
zoneId: zoneId
})
})
} catch (error) {
const statusCode = error?.response?.status || error?.status
const errorMessage =
error?.message || error?.response?.data?.message || 'Unknown error'
const errorDetails = error?.response?.data || error?.body || {}
logger.error('Failed to add payment instrument to basket', {
namespace: 'SFPaymentsExpressButtons.createIntentFunction',
additionalProperties: {
statusCode,
errorMessage,
errorDetails,
basketId: expressBasket.current?.basketId,
paymentMethodType,
orderTotal: expressBasket.current?.orderTotal,
productSubTotal: expressBasket.current?.productSubTotal,
error: error
}
})
showErrorMessage(ERROR_MESSAGE_KEYS.PROCESS_PAYMENT)
// Re-throw so SF Payments SDK can handle the error if needed
throw error
}
updatedPaymentInstrument = getSFPaymentsInstrument(expressBasket.current)
} else {
// For Adyen: Update addresses from paymentData before creating order
// (Stripe uses onPayerApprove instead, PayPal is handled above)
if (isAdyen && paymentData?.shippingDetails) {
// Set confirmingBasket to show loading spinner during address updates
startConfirming(expressBasket.current)
try {
const {billingAddress, shippingAddress} = transformAddressDetails(
paymentData.billingDetails,
paymentData.shippingDetails
)
// Update shipping address
expressBasket.current =
await updateShippingAddressForShipment.mutateAsync({
parameters: {
basketId: expressBasket.current.basketId,
shipmentId: DEFAULT_SHIPMENT_ID,
useAsBilling: false
},
body: shippingAddress
})
// Update billing address
await updateBillingAddressForBasket({
parameters: {basketId: expressBasket.current.basketId},
body: billingAddress
})
} catch (error) {
endConfirming()
throw error
}
}
// Create order and update payment instrument
try {
// For non-PayPal methods, ensure payment instrument exists in basket
// (e.g., Stripe adds it in onPayerApprove, but Adyen does not call onPayerApprove before createIntentFunction)
// keeping it for all as safety measure
expressBasket.current = await ensurePaymentInstrumentInBasket(
expressBasket.current,
paymentMethodType,
zoneId
)
const order = await createOrderAndUpdatePayment(
expressBasket.current.basketId,
paymentMethodType,
paymentData
)
orderRef.current = order
updatedPaymentInstrument = getSFPaymentsInstrument(order)
} catch (error) {
// If order was created but updatePaymentInstrumentForOrder failed,
// orderNo will be attached to the error
if (error.orderNo) {
orderRef.current = {orderNo: error.orderNo}
}
endConfirming()
throw error
}
}
const paymentReference = updatedPaymentInstrument?.paymentReference
if (isAdyen) {
const adyenIntent =
paymentReference?.gatewayProperties?.adyen?.adyenPaymentIntent
return {
pspReference: adyenIntent?.id,
guid: paymentReference?.paymentReferenceId,
resultCode: adyenIntent?.resultCode,
action: adyenIntent?.adyenPaymentIntentAction
}
} else {
return {
client_secret: getClientSecret(updatedPaymentInstrument),
id: paymentReference?.paymentReferenceId
}
}
}
const onApproveEvent = async () => {
try {
let order
if (isPayPalPaymentMethodType(paymentMethodType)) {
// Create order and update payment instrument
order = await createOrderAndUpdatePayment(
expressBasket.current.basketId,
paymentMethodType
)
orderRef.current = order
}
// Close modal if callback provided (for mini cart)
if (onExpressPaymentCompleted) {
onExpressPaymentCompleted()
}
endConfirming()
// Navigate to confirmation page with the order number
navigate(`/checkout/confirmation/${orderRef.current?.orderNo}`)
isPaymentInProgress.current = false
} catch (error) {
endConfirming()
}
}
/**
* Handles payment error event.
* Attempts to fail an order and reopen the basket.
* Only calls failOrder if the order status supports the transition.
* @returns {Promise<void>}
*/
const paymentError = async () => {
isPaymentInProgress.current = false
const basketRecovered = await attemptFailOrder(orderRef.current?.orderNo)
endConfirming()
if (basketRecovered) {
showErrorMessage(ERROR_MESSAGE_KEYS.FAIL_ORDER)
} else {
showErrorMessage(ERROR_MESSAGE_KEYS.ORDER_RECOVERY_FAILED)
// Only navigate to cart if NOT on PDP
if (usage !== EXPRESS_BUY_NOW) {
navigate('/cart')
}
}
}
const handlePaymentMethodsRendered = (details) => {
if (onPaymentMethodsRendered && details.detail.rendered.length > 0) {
onPaymentMethodsRendered()
}
}
const paymentMethodSet = {
paymentMethods: paymentConfig.paymentMethods,
paymentMethodSetAccounts: paymentConfig.paymentMethodSetAccounts || []
}
const config = {
theme: buildTheme({
expressButtonLayout,
...(usage === EXPRESS_BUY_NOW && {
expressButtonLabels: {
applepay: 'buy',
googlepay: 'buy',
paypal: 'buynow',
venmo: 'buynow'
}
})
}),
actions: {
onClick: onClick,
onShippingAddressChange: onShippingAddressChange,
onShippingMethodChange: onShippingMethodChange,
createIntent: createIntentFunction,
onPayerApprove: onPayerApprove
},
options: {
shippingAddressRequired: true,
emailAddressRequired: true,
billingAddressRequired: true,
phoneNumberRequired: true,
useManualCapture: !cardCaptureAutomatic,
maximumButtonCount
}
}
const paymentRequest = {
amount: initialAmount,
currency: paymentCurrency,
country: 'US', // TODO: see W-18812582
locale: intl.locale
}
containerElementRef.current.innerHTML = '<div></div>'
const paymentElement = containerElementRef.current.firstChild
paymentElement.addEventListener('sfp:paymentcancel', onCancel)
paymentElement.addEventListener('sfp:paymentapprove', onApproveEvent)
paymentElement.addEventListener('sfp:paymenterror', paymentError)
paymentElement.addEventListener(
'sfp:paymentmethodsrendered',