Skip to content

Commit 9d905de

Browse files
committed
Addressing comments
1 parent ca3dceb commit 9d905de

File tree

4 files changed

+406
-89
lines changed

4 files changed

+406
-89
lines changed

packages/template-retail-react-app/app/pages/checkout/partials/sf-payments-sheet.jsx

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ import {
2020
Divider
2121
} from '@salesforce/retail-react-app/app/components/shared/ui'
2222
import {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'
2429
import {useCurrentBasket} from '@salesforce/retail-react-app/app/hooks/use-current-basket'
25-
import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-current-customer'
2630
import {useCurrency} from '@salesforce/retail-react-app/app/hooks/use-currency'
2731
import {useCheckout} from '@salesforce/retail-react-app/app/pages/checkout/util/checkout-context'
2832
import {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'
5457
import 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
}

packages/template-retail-react-app/app/pages/checkout/partials/sf-payments-sheet.test.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,14 @@ jest.mock('@salesforce/commerce-sdk-react', () => {
101101
defaultShippingMethodId: 'DefaultShippingMethod'
102102
},
103103
refetch: mockRefetchShippingMethods
104-
})
104+
}),
105+
useCustomerId: () => 'customer123',
106+
useCustomerType: () => ({
107+
isRegistered: true,
108+
isGuest: false,
109+
customerType: 'registered'
110+
}),
111+
useCustomer: jest.fn()
105112
}
106113
})
107114

@@ -147,16 +154,14 @@ const mockCustomer = {
147154
paymentMethodReferences: []
148155
}
149156

150-
let mockUseCurrentCustomer
157+
// Get the mocked useCustomer from commerce-sdk-react
158+
// eslint-disable-next-line @typescript-eslint/no-var-requires
159+
const mockUseCustomer = require('@salesforce/commerce-sdk-react').useCustomer
151160

152-
jest.mock('@salesforce/retail-react-app/app/hooks/use-current-customer', () => {
153-
mockUseCurrentCustomer = jest.fn(() => ({
154-
data: mockCustomer
155-
}))
156-
return {
157-
useCurrentCustomer: mockUseCurrentCustomer
158-
}
159-
})
161+
// Set default implementation
162+
mockUseCustomer.mockImplementation(() => ({
163+
data: mockCustomer
164+
}))
160165

161166
jest.mock('@salesforce/retail-react-app/app/hooks/use-einstein', () => {
162167
return jest.fn(() => ({
@@ -1069,7 +1074,7 @@ describe('SFPaymentsSheet', () => {
10691074
beforeEach(() => {
10701075
jest.clearAllMocks()
10711076
mockCustomer.paymentMethodReferences = []
1072-
mockUseCurrentCustomer.mockImplementation(() => ({
1077+
mockUseCustomer.mockImplementation(() => ({
10731078
data: {...mockCustomer}
10741079
}))
10751080
})
@@ -1182,7 +1187,6 @@ describe('SFPaymentsSheet', () => {
11821187

11831188
expect(config.options.savedPaymentMethods).toEqual([])
11841189
})
1185-
11861190
})
11871191

11881192
describe('lifecycle', () => {

packages/template-retail-react-app/app/utils/sf-payments-utils.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ export const createPaymentInstrumentBody = ({
267267
}
268268
}
269269

270-
if (gateway === PAYMENT_GATEWAYS.ADYEN && storePaymentMethod) {
271-
paymentReferenceRequest.gateway = PAYMENT_GATEWAYS.ADYEN
272-
}
273-
274270
return {
275271
paymentMethodId: 'Salesforce Payments',
276272
amount: amount,
@@ -288,7 +284,7 @@ export const transformPaymentMethodReferences = (
288284
paymentMethodReferences,
289285
paymentMethodSetAccounts = []
290286
) => {
291-
if (!paymentMethodReferences || !Array.isArray(paymentMethodReferences)) {
287+
if (!Array.isArray(paymentMethodReferences) || !Array.isArray(paymentMethodSetAccounts)) {
292288
return []
293289
}
294290

@@ -309,11 +305,7 @@ export const transformPaymentMethodReferences = (
309305
}
310306

311307
// Determine gatewayId for SDK matching
312-
if (
313-
!pmr.accountId ||
314-
!paymentMethodSetAccounts ||
315-
!Array.isArray(paymentMethodSetAccounts)
316-
) {
308+
if (!pmr.accountId) {
317309
return null
318310
}
319311

0 commit comments

Comments
 (0)