/
Recharge.tsx
376 lines (362 loc) · 11.7 KB
/
Recharge.tsx
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
import { checkWechatPay, createPayment, handlePaymentResult } from '@/api/payment';
import { getSystemEnv, uploadConvertData } from '@/api/system';
import { LeftIcon, StripeIcon, WechatIcon } from '@/components/Icon';
import useBonusBox from '@/hooks/useBonusBox';
import { PaymentStatus, TPayMethod, WechatPaymentData } from '@/types';
import { deFormatMoney } from '@/utils/tools';
import {
Button,
Checkbox,
Flex,
Link,
Modal,
ModalCloseButton,
ModalContent,
ModalHeader,
ModalOverlay,
Spinner,
Text,
useDisclosure,
useToast
} from '@chakra-ui/react';
import { loadStripe } from '@stripe/stripe-js';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useTranslation } from 'next-i18next';
import { useRouter } from 'next/router';
import { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
import WechatPayment from '@/components/WechatPayment';
import { createLicense } from '@/api/license';
import usePaymentDataStore from '@/stores/payment';
import useClusterDetail from '@/stores/cluster';
import { findClusterById } from '@/api/cluster';
export default forwardRef(function RechargeComponent(props, ref) {
const { clusterDetail, setClusterDetail } = useClusterDetail();
const router = useRouter();
const queryClient = useQueryClient();
const toast = useToast({ position: 'top', duration: 2000 });
const { t } = useTranslation();
const [isAgree, setIsAgree] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);
const { BonusBox, selectAmount } = useBonusBox();
const { isOpen, onOpen, onClose } = useDisclosure();
// 整个流程跑通需要状态管理, 0 初始态, 1 创建支付单, 2 支付中, 3 支付成功
const [complete, setComplete] = useState<0 | 1 | 2 | 3>(0);
const [payType, setPayType] = useState<TPayMethod>('wechat');
const [detail, setDetail] = useState(false);
const [orderID, setOrderID] = useState('');
const [wechatPaymentData, setWechatPaymentData] = useState<WechatPaymentData>();
const { data: platformEnv } = useQuery(['getPlatformEnv'], getSystemEnv);
// 用于避免微信支付,窗口关闭后感知不到的问题
const { paymentData, setPaymentData, deletePaymentData, isExpired } = usePaymentDataStore();
const onClosePayment = useCallback(() => {
setOrderID('');
setComplete(0);
setDetail(false);
onClose();
}, [onClose]);
useImperativeHandle(
ref,
() => {
return {
onOpen() {
onOpen();
}
};
},
[]
);
const handlePayConfirm = (type: TPayMethod) => {
setPayType(type);
if (!isAgree) {
return toast({
status: 'error',
title: t('Please read and agree to the agreement'),
isClosable: true,
position: 'top'
});
}
if (type === 'stripe' && selectAmount < 10) {
return toast({
status: 'error',
title: t('Pay Minimum Tips')
});
}
setDetail(true);
setComplete(1);
paymentMutation.mutate();
};
const paymentMutation = useMutation(
() =>
createPayment({
amount: deFormatMoney(selectAmount).toString(),
payMethod: payType,
currency: 'CNY',
stripeSuccessCallBackUrl: `/cluster?clusterId=${clusterDetail?.clusterId}&tab=license&stripeState=success`,
stripeErrorCallBackUrl: `/cluster?clusterId=${clusterDetail?.clusterId}&tab=license&stripeState=error`
}),
{
async onSuccess(data) {
if (payType === 'stripe' && platformEnv && data?.sessionID) {
const stripe = await loadStripe(platformEnv?.stripePub);
stripe?.redirectToCheckout({
sessionId: data.sessionID
});
}
if (payType === 'wechat' && data?.tradeNO && data?.codeURL) {
setOrderID(data.orderID);
setWechatPaymentData({ tradeNO: data?.tradeNO, codeURL: data?.codeURL });
setComplete(2);
setPaymentData(data.orderID);
}
},
onError(err: any) {
toast({
status: 'error',
title: err?.message || '',
isClosable: true,
position: 'top'
});
setComplete(0);
}
}
);
const licenseMutation = useMutation(
({ orderID, clusterId }: { orderID: string; clusterId: string }) =>
createLicense({ orderID, clusterId }),
{
onSuccess(data) {
onClosePayment();
toast({
status: 'success',
title: t('License issued successfully'),
isClosable: true,
position: 'top'
});
queryClient.invalidateQueries(['getLicenseByClusterId']);
deletePaymentData();
},
onError(err: any) {
toast({
status: 'error',
title: err?.message || '',
isClosable: true,
position: 'top'
});
setComplete(0);
}
}
);
useQuery(['getPaymentResult', orderID], () => handlePaymentResult({ orderID }), {
refetchInterval: complete === 2 ? 3 * 1000 : false,
enabled: complete === 2 && !!orderID,
cacheTime: 0,
staleTime: 0,
onSuccess(data) {
console.log(data, clusterDetail, 1111111111111);
if (!clusterDetail?.clusterId) {
return toast({
position: 'top',
status: 'error',
title: '请联系管理员'
});
}
if (data.status === PaymentStatus.PaymentSuccess) {
licenseMutation.mutate({ orderID: data.orderID, clusterId: clusterDetail.clusterId });
uploadConvertData([90])
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
}
},
onError(err: any) {
toast({
status: 'error',
title: err?.message || '',
isClosable: true,
position: 'top'
});
setComplete(0);
}
});
// checkWechatPay;
useQuery(['checkWechatPay'], () => checkWechatPay('license'), {
enabled: !isExpired() && !!paymentData?.orderId,
onSuccess(data) {
console.log(data, 'Handle wechat shutdown situation');
if (data.status === PaymentStatus.PaymentSuccess) {
if (!clusterDetail?.clusterId) {
return toast({
position: 'top',
status: 'error',
title: '请联系管理员'
});
}
licenseMutation.mutate({ orderID: data.orderID, clusterId: clusterDetail.clusterId });
}
}
});
const getCluster = async (id: string) => {
const cluster = await findClusterById({
clusterId: id
});
setClusterDetail(cluster);
};
useEffect(() => {
const { stripeState, orderID, clusterId, tab } = router.query;
console.log(stripeState, orderID, clusterId);
const clearQuery = () => {
router.replace({
pathname: '/cluster',
query: { tab }
});
};
if (stripeState === 'success') {
clusterId ? getCluster(clusterId as string) : '';
setComplete(2);
setOrderID(orderID as string);
setTimeout(clearQuery, 0);
} else if (stripeState === 'error') {
clusterId ? getCluster(clusterId as string) : '';
toast({
status: 'error',
duration: 3000,
title: t('Stripe Cancel'),
isClosable: true,
position: 'top'
});
onClosePayment();
setTimeout(clearQuery, 0);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<Modal isOpen={isOpen} onClose={onClosePayment}>
<ModalOverlay />
<ModalContent maxW="502px" minH={'495px'} display={'flex'} flexDirection={'column'}>
<ModalCloseButton />
{detail ? (
<>
<ModalHeader
cursor={'pointer'}
pt={'24px'}
py={'20px'}
onClick={() => {
setOrderID('');
setComplete(0);
setDetail(false);
}}
display={'flex'}
alignItems={'center'}
>
<LeftIcon />
<Text ml="8px" color={'#24282C'}>
{t('Purchase License')}
</Text>
</ModalHeader>
{complete === 1 ? (
<Flex flex={1} alignItems={'center'} justifyContent={'center'}>
<Spinner size={'xl'} />
</Flex>
) : (
<WechatPayment
complete={complete}
codeURL={wechatPaymentData?.codeURL}
tradeNO={wechatPaymentData?.tradeNO}
/>
)}
</>
) : (
<>
<ModalHeader py={'20px'}>{t('Purchase License')}</ModalHeader>
<Flex flex={1} flexDirection="column" alignItems="center" px={'24px'} pb="45px">
<Text alignSelf={'start'} color="#7B838B" fontWeight={'normal'} mb={'16px'}>
{t('Select Amount')}
</Text>
<BonusBox />
<Flex alignSelf={'flex-start'} align={'center'} mt={'32px'}>
<Checkbox
isInvalid={isInvalid}
mr={'8px'}
isChecked={isAgree}
variant={'unstyled'}
onChange={(e) => {
setIsInvalid(false);
setIsAgree(e.target.checked);
}}
/>
<Text
fontStyle="normal"
fontWeight="400"
fontSize="12px"
lineHeight="140%"
color={'#1D8CDC'}
>
{t('agree policy')}
<Link
isExternal
href={platformEnv?.service_protocol}
_hover={{
color: 'rgba(94, 189, 242, 1)',
borderBottom: '1px solid rgba(94, 189, 242, 1)'
}}
px="4px"
>
{t('Service Agreement')}
</Link>
{t('and')}
<Link
isExternal
href={platformEnv?.private_protocol}
_hover={{
color: 'rgba(94, 189, 242, 1)',
borderBottom: '1px solid rgba(94, 189, 242, 1)'
}}
px="4px"
>
{t('Privacy Policy')}
</Link>
</Text>
{/* <OuterLink text={t('View Discount Rules')}></OuterLink> */}
</Flex>
<Flex gap={'16px'} width={'full'} mt="20px">
{platformEnv?.stripeEnabled && (
<Button
size="primary"
variant="primary"
mt="20px"
w="full"
h="auto"
py="14px"
px="34px"
onClick={() => handlePayConfirm('stripe')}
>
<StripeIcon />
<Text ml="12px">{t('pay with stripe')}</Text>
</Button>
)}
{platformEnv?.wechatEnabledRecharge && (
<Button
size="primary"
variant="primary"
mt="20px"
w="full"
h="auto"
py="14px"
px="34px"
onClick={() => handlePayConfirm('wechat')}
>
<WechatIcon fill={'#33BABB'} />
<Text ml="12px">{t('pay with wechat')}</Text>
</Button>
)}
</Flex>
</Flex>
</>
)}
</ModalContent>
</Modal>
);
});