-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathpayments.jsx
More file actions
493 lines (477 loc) · 20.6 KB
/
payments.jsx
File metadata and controls
493 lines (477 loc) · 20.6 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
/*
* Copyright (c) 2025, Salesforce, 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, {useState} from 'react'
import {FormattedMessage, useIntl} from 'react-intl'
import {
Box,
Button,
Container,
Heading,
Stack,
Text,
SimpleGrid,
Flex,
Badge,
Checkbox
} from '@salesforce/retail-react-app/app/components/shared/ui'
import {useCurrentCustomer} from '@salesforce/retail-react-app/app/hooks/use-current-customer'
import {
getCreditCardIcon,
createCreditCardPaymentBodyFromForm
} from '@salesforce/retail-react-app/app/utils/cc-utils'
import AccountPaymentForm from '@salesforce/retail-react-app/app/pages/account/partials/account-payment-form'
import {useForm} from 'react-hook-form'
import {PlusIcon, CreditCardIcon} from '@salesforce/retail-react-app/app/components/icons'
import FormActionButtons from '@salesforce/retail-react-app/app/components/forms/form-action-buttons'
import {useShopperCustomersMutation} from '@salesforce/commerce-sdk-react'
import ActionCard from '@salesforce/retail-react-app/app/components/action-card'
import {useToast} from '@salesforce/retail-react-app/app/hooks/use-toast'
import {useConfigurations} from '@salesforce/commerce-sdk-react'
import ConfirmationModal from '@salesforce/retail-react-app/app/components/confirmation-modal'
export const SALESFORCE_PAYMENTS_ALLOWED = 'SalesforcePaymentsAllowed'
const BoxArrow = () => {
return (
<Box
width={3}
height={3}
borderLeft="1px solid"
borderTop="1px solid"
borderColor="blue.600"
position="absolute"
left="50%"
bottom="-23px"
zIndex={1}
background="white"
transform="rotate(45deg)"
/>
)
}
const AccountPayments = () => {
const {formatMessage} = useIntl()
const {data: customer, isLoading: isLoadingCustomer, error, refetch} = useCurrentCustomer()
const showToast = useToast()
const [isAdding, setIsAdding] = useState(false)
const [formKey, setFormKey] = useState(0)
const addPaymentForm = useForm()
const [isDefaultModalOpen, setIsDefaultModalOpen] = useState(false)
const [pendingDefaultPayment, setPendingDefaultPayment] = useState(null)
const {
data: configurations,
isLoading: isLoadingConfigurations,
error: configurationsError
} = useConfigurations()
const createCustomerPaymentInstrument = useShopperCustomersMutation(
'createCustomerPaymentInstrument'
)
const deleteCustomerPaymentInstrument = useShopperCustomersMutation(
'deleteCustomerPaymentInstrument'
)
const updateCustomerPaymentInstrument = useShopperCustomersMutation(
'updateCustomerPaymentInstrument'
)
const isSalesforcePaymentsEnabled = configurations?.configurations?.find(
(config) => config.id === SALESFORCE_PAYMENTS_ALLOWED
)?.value
const onAddPaymentSubmit = async (values) => {
const body = createCreditCardPaymentBodyFromForm(values)
body.paymentMethodId = 'CREDIT_CARD'
// Remove fields not supported by CustomerPaymentCardRequest
if (body.paymentCard && 'securityCode' in body.paymentCard) {
delete body.paymentCard.securityCode
}
try {
await createCustomerPaymentInstrument.mutateAsync(
{
body,
parameters: {customerId: customer?.customerId}
},
{
onSuccess: () => {
showToast({
title: formatMessage({
defaultMessage: 'New payment method was saved.',
id: 'account.payments.info.payment_method_saved'
}),
status: 'success',
isClosable: true
})
}
}
)
setIsAdding(false)
await refetch()
} catch (e) {
showToast({
title: formatMessage({
defaultMessage: "We couldn't save the payment method. Try again.",
id: 'account.payments.error.payment_method_save_failed'
}),
status: 'error',
isClosable: true
})
}
}
const openAdd = () => {
// Reset all card fields to ensure a blank form
addPaymentForm.reset({
number: '',
cardType: '',
holder: '',
expiry: '',
securityCode: '',
default: false
})
// Force form subtree remount to clear any internal state
setFormKey((k) => k + 1)
setIsAdding(true)
}
const closeAdd = () => setIsAdding(false)
const openDefaultModal = (payment) => {
setPendingDefaultPayment(payment)
setIsDefaultModalOpen(true)
}
const closeDefaultModal = () => {
setIsDefaultModalOpen(false)
setPendingDefaultPayment(null)
}
const confirmSetDefault = async () => {
if (!pendingDefaultPayment) return
try {
await updateCustomerPaymentInstrument.mutateAsync({
parameters: {
customerId: customer?.customerId,
paymentInstrumentId: pendingDefaultPayment.paymentInstrumentId
},
body: {default: true}
})
showToast({
title: formatMessage({
defaultMessage: 'Payment method was made as default.',
id: 'account.payments.info.default_payment_updated'
}),
status: 'success',
isClosable: true
})
await refetch()
} catch (e) {
showToast({
title: formatMessage({
defaultMessage: 'We couldnt make the payment method as default. Try again.',
id: 'account.payments.error.set_default_failed'
}),
status: 'error',
isClosable: true
})
} finally {
closeDefaultModal()
}
}
const removePayment = async (paymentInstrumentId) => {
try {
await deleteCustomerPaymentInstrument.mutateAsync(
{
parameters: {customerId: customer?.customerId, paymentInstrumentId}
},
{
onSuccess: () => {
showToast({
title: formatMessage({
defaultMessage: 'Payment method was removed.',
id: 'account.payments.info.payment_method_removed'
}),
status: 'success',
isClosable: true
})
}
}
)
await refetch()
} catch (e) {
showToast({
title: formatMessage({
defaultMessage: "We couldn't remove the payment method. Try again.",
id: 'account.payments.error.payment_method_remove_failed'
}),
status: 'error',
isClosable: true
})
}
}
// Show loading state
if (isLoadingCustomer || isLoadingConfigurations) {
return (
<Container layerStyle="page">
<Stack spacing={6}>
<Heading as="h1" fontSize="2xl">
<FormattedMessage
defaultMessage="Payment Methods"
id="account.payments.heading.payment_methods"
/>
</Heading>
<Box textAlign="center" py={12}>
<Text color="gray.600">
<FormattedMessage
defaultMessage="Loading payment methods..."
id="account.payments.message.loading"
/>
</Text>
</Box>
</Stack>
</Container>
)
}
// Show error state
if (error || configurationsError) {
return (
<Container layerStyle="page">
<Stack spacing={6}>
<Heading as="h1" fontSize="2xl">
<FormattedMessage
defaultMessage="Payment Methods"
id="account.payments.heading.payment_methods"
/>
</Heading>
<Box textAlign="center" py={12}>
<Stack spacing={4}>
<Text color="red.600">
<FormattedMessage
defaultMessage="We couldn't load the payment methods. Try again."
id="account.payments.message.error"
/>
</Text>
<Button onClick={() => refetch()} variant="outline">
<FormattedMessage
defaultMessage="Retry"
id="account.payments.action.retry"
/>
</Button>
</Stack>
</Box>
</Stack>
</Container>
)
}
if (!customer?.paymentInstruments?.length) {
return (
<Container layerStyle="page">
<Stack spacing={6}>
<Heading as="h1" fontSize="2xl">
<FormattedMessage
defaultMessage="Payment Methods"
id="account.payments.heading.payment_methods"
/>
</Heading>
<Box bg="gray.50" borderRadius="base" py={12} textAlign="center">
<CreditCardIcon boxSize={6} color="gray.700" />
<Text mt={4} fontWeight="semibold">
<FormattedMessage
defaultMessage="No Saved Payment Methods"
id="account.payments.placeholder.heading"
/>
</Text>
{!isSalesforcePaymentsEnabled && (
<div>
<Text color="gray.600" mt={1}>
<FormattedMessage
defaultMessage="Add a new payment method to check out faster."
id="account.payments.placeholder.text"
/>
</Text>
<Button
mt={4}
onClick={openAdd}
leftIcon={<PlusIcon boxSize={3} />}
>
<FormattedMessage
defaultMessage="Add Payment Method"
id="account_payments.button.add_payment"
/>
</Button>
</div>
)}
</Box>
{isAdding && (
<Box
border="1px solid"
borderColor="gray.200"
borderRadius="base"
position="relative"
paddingX={[4, 4, 6]}
paddingY={6}
rounded="base"
borderWidth="1px"
style={{borderColor: 'rgb(33, 109, 236)'}}
>
<Container variant="form">
<AccountPaymentForm
key={formKey}
form={addPaymentForm}
onSubmit={onAddPaymentSubmit}
>
<FormActionButtons onCancel={closeAdd} />
</AccountPaymentForm>
</Container>
</Box>
)}
</Stack>
</Container>
)
}
return (
<Container layerStyle="page">
<Stack spacing={6}>
<Heading as="h1" fontSize="2xl">
<FormattedMessage
defaultMessage="Payment Methods"
id="account.payments.heading.payment_methods"
/>
</Heading>
<SimpleGrid columns={[1, 2, 2, 2, 3]} spacing={4} gridAutoFlow="row dense">
{!isSalesforcePaymentsEnabled && (
<Button
variant="outline"
border="1px dashed"
borderColor="gray.200"
color="blue.600"
height={{lg: 'full'}}
minHeight={11}
rounded="base"
fontWeight="medium"
leftIcon={<PlusIcon display="block" boxSize={'15px'} />}
onClick={openAdd}
>
<FormattedMessage
defaultMessage="Add Payment Method"
id="account_payments.button.add_payment"
/>
{isAdding && <BoxArrow />}
</Button>
)}
{isAdding && (
<Box
border="1px solid"
borderColor="gray.200"
borderRadius="base"
position="relative"
gridColumn={[1, 'span 2', 'span 2', 'span 2', 'span 3']}
paddingX={[4, 4, 6]}
paddingY={6}
rounded="base"
borderWidth="1px"
style={{borderColor: 'rgb(33, 109, 236)'}}
>
<Box>
<Container variant="form">
<AccountPaymentForm
form={addPaymentForm}
onSubmit={onAddPaymentSubmit}
>
<FormActionButtons onCancel={closeAdd} />
</AccountPaymentForm>
</Container>
</Box>
</Box>
)}
{customer.paymentInstruments?.map((payment) => {
const CardIcon = getCreditCardIcon(payment.paymentCard?.cardType)
return (
<ActionCard
key={payment.paymentInstrumentId}
onRemove={() => removePayment(payment.paymentInstrumentId)}
borderColor="gray.200"
footerLeft={
!payment.default ? (
<Checkbox
onChange={(e) => {
if (e.target.checked) openDefaultModal(payment)
e.target.checked = false
}}
>
<FormattedMessage
defaultMessage="Make default"
id="account.payments.checkbox.make_default"
/>
</Checkbox>
) : null
}
>
<Stack spacing={3} flex="1">
{payment.default && (
<Badge
position="absolute"
fontSize="xs"
right={4}
variant="solid"
bg="gray.100"
color="gray.900"
>
<FormattedMessage
defaultMessage="Default"
id="account.payments.badge.default"
/>
</Badge>
)}
<Flex align="center" gap={2}>
{CardIcon && <CardIcon layerStyle="ccIcon" />}
<Text fontWeight="semibold">
{payment.paymentCard?.cardType}
</Text>
</Flex>
<Stack spacing={1}>
<Text>
••••{' '}
{payment.paymentCard?.numberLastDigits}
</Text>
<Text color="gray.600">{payment.paymentCard?.holder}</Text>
<Text color="gray.600">
<FormattedMessage
defaultMessage="Expires {month}/{year}"
id="account.payments.label.expires"
values={{
month: payment.paymentCard?.expirationMonth,
year: payment.paymentCard?.expirationYear
}}
/>
</Text>
</Stack>
</Stack>
</ActionCard>
)
})}
</SimpleGrid>
<ConfirmationModal
isOpen={isDefaultModalOpen}
onOpen={() => setIsDefaultModalOpen(true)}
onClose={closeDefaultModal}
dialogTitle={{
defaultMessage: 'Set default payment method?',
id: 'account.payments.modal.title.set_default'
}}
confirmationMessage={{
defaultMessage: '{brand} ... {last4} will be the default at checkout.',
id: 'account.payments.modal.message.set_default'
}}
confirmationMessageValues={{
brand: pendingDefaultPayment?.paymentCard?.cardType || '',
last4: pendingDefaultPayment?.paymentCard?.numberLastDigits || ''
}}
primaryActionLabel={{
defaultMessage: 'Set Default',
id: 'account.payments.modal.action.set_default'
}}
alternateActionLabel={{
defaultMessage: 'Cancel',
id: 'account.payments.modal.action.cancel'
}}
onPrimaryAction={confirmSetDefault}
onAlternateAction={closeDefaultModal}
/>
</Stack>
</Container>
)
}
export default AccountPayments