@@ -20,9 +20,13 @@ import {
2020 Divider
2121} from '@salesforce/retail-react-app/app/components/shared/ui'
2222import { useForm } from 'react-hook-form'
23- import { useShopperBasketsMutation } from '@salesforce/commerce-sdk-react'
23+ import {
24+ useShopperBasketsMutation ,
25+ useCustomer ,
26+ useCustomerId ,
27+ useCustomerType
28+ } from '@salesforce/commerce-sdk-react'
2429import { useCurrentBasket } from '@salesforce/retail-react-app/app/hooks/use-current-basket'
25- import { useCurrentCustomer } from '@salesforce/retail-react-app/app/hooks/use-current-customer'
2630import { useCurrency } from '@salesforce/retail-react-app/app/hooks/use-currency'
2731import { useCheckout } from '@salesforce/retail-react-app/app/pages/checkout/util/checkout-context'
2832import { usePaymentConfiguration } from '@salesforce/commerce-sdk-react'
@@ -48,8 +52,7 @@ import {
4852 buildTheme ,
4953 getSFPaymentsInstrument ,
5054 createPaymentInstrumentBody ,
51- transformPaymentMethodReferences ,
52- getClientSecret
55+ transformPaymentMethodReferences
5356} from '@salesforce/retail-react-app/app/utils/sf-payments-utils'
5457import logger from '@salesforce/retail-react-app/app/utils/logger-instance'
5558
@@ -61,7 +64,25 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
6164 const navigate = useNavigation ( )
6265
6366 const { data : basket } = useCurrentBasket ( )
64- const { data : customer } = useCurrentCustomer ( )
67+ const customerId = useCustomerId ( )
68+ const { isRegistered} = useCustomerType ( )
69+ const { data : customerData } = useCustomer (
70+ {
71+ parameters : {
72+ customerId,
73+ expand : [ 'paymentmethodreferences' ]
74+ }
75+ } ,
76+ { enabled : ! ! customerId && isRegistered }
77+ )
78+ // Add customerId and isRegistered to customer data for consistency with useCurrentCustomer
79+ const customer = customerData
80+ ? {
81+ ...customerData ,
82+ customerId,
83+ isRegistered
84+ }
85+ : null
6586
6687 const isPickupOnly =
6788 basket ?. shipments ?. length > 0 &&
@@ -359,11 +380,20 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
359380
360381 // Track created payment intent
361382 const paymentIntent = {
362- client_secret : getClientSecret ( orderPaymentInstrument ) ,
383+ client_secret :
384+ orderPaymentInstrument ?. paymentReference ?. gatewayProperties ?. stripe
385+ ?. clientSecret ,
363386 id : orderPaymentInstrument . paymentReference . paymentReferenceId
364387 }
365388
366- if ( futureUsageOffSession ) {
389+ // Read setup_future_usage from backend response, fallback to manual calculation if not available
390+ // TODO: The fallback is temporary that's to be removed in next iteration.
391+ const setupFutureUsage =
392+ orderPaymentInstrument ?. paymentReference ?. gatewayProperties ?. stripe
393+ ?. setup_future_usage
394+ if ( setupFutureUsage ) {
395+ paymentIntent . setup_future_usage = setupFutureUsage
396+ } else if ( futureUsageOffSession ) {
367397 paymentIntent . setup_future_usage = 'off_session'
368398 } else if ( shouldSavePaymentMethod ) {
369399 paymentIntent . setup_future_usage = 'on_session'
@@ -406,22 +436,9 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
406436 // Update the redirect return URL to include the related order no
407437 config . current . options . returnUrl += '?orderNo=' + updatedOrder . orderNo
408438
409- // Update Elements to match Payment Intent's setup_future_usage before SDK confirms
410- const paymentIntentFunction = async ( ) => {
411- const selectedPaymentMethod = checkoutComponent . current ?. selectedPaymentMethod
412- if ( selectedPaymentMethod ?. asSavedPaymentMethodComponent ) {
413- const spmComponent = selectedPaymentMethod . asSavedPaymentMethodComponent ( )
414- if ( spmComponent ) {
415- spmComponent . setSavePaymentMethodFuture ( futureUsageOffSession )
416- }
417- }
418-
419- return paymentIntent
420- }
421-
422439 // Confirm the payment
423440 const result = await checkoutComponent . current . confirm (
424- paymentIntentFunction ,
441+ async ( ) => paymentIntent ,
425442 billingDetails ,
426443 shippingDetails
427444 )
@@ -487,15 +504,14 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
487504 confirmPayment
488505 } ) )
489506
490- const savedPaymentMethods = useMemo ( ( ) => {
491- if ( ! customer ?. paymentMethodReferences || ! paymentConfig ?. paymentMethodSetAccounts ) {
492- return [ ]
493- }
494- return transformPaymentMethodReferences (
495- customer . paymentMethodReferences ,
496- paymentConfig . paymentMethodSetAccounts
497- )
498- } , [ customer ?. paymentMethodReferences , paymentConfig ?. paymentMethodSetAccounts ] )
507+ const savedPaymentMethods = useMemo (
508+ ( ) =>
509+ transformPaymentMethodReferences (
510+ customer ?. paymentMethodReferences ,
511+ paymentConfig ?. paymentMethodSetAccounts
512+ ) ,
513+ [ customer ?. paymentMethodReferences , paymentConfig ?. paymentMethodSetAccounts ]
514+ )
499515
500516 useEffect ( ( ) => {
501517 if ( sfp && metadata && containerElementRef . current && paymentConfig ) {
@@ -515,14 +531,15 @@ const SFPaymentsSheet = forwardRef((props, ref) => {
515531 theme : buildTheme ( ) ,
516532 actions : {
517533 createIntent : createPaymentInstrument ,
518- onClick : ( ) => { }
534+ onClick : ( ) => { } // No-op: return empty function since its not applicable and SDK proceeds immediately
519535 } ,
520536 options : {
521537 useManualCapture : ! cardCaptureAutomatic ,
522538 returnUrl : `${ window . location . protocol } //${ window . location . host } /checkout/payment-processing` ,
523539 showSaveForFutureUsageCheckbox : ! ! (
524540 customer ?. isRegistered && customer ?. customerId
525541 ) ,
542+ // Suppress "Make payment method default" checkbox since we don't support default SPM yet
526543 showSaveAsDefaultCheckbox : false ,
527544 savedPaymentMethods : savedPaymentMethods
528545 }
0 commit comments