@jetthai/cuora 1.1.359-dev-7 → 1.1.360

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,546 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import findLastIndex from 'lodash/findLastIndex.js'
3
+ import payment from '../../api/payment.js'
4
+ import common from '../../api/common.js'
5
+ import user from '../../api/user.js'
6
+
7
+ export default function (request) {
8
+ const { appSecret } = request.config
9
+ return createMachine(
10
+ {
11
+ id: 'withdraw_vn',
12
+ initial: 'loading',
13
+ context: {
14
+ siteConfig: null,
15
+ userInfo: null,
16
+ balance: null,
17
+ charge: null,
18
+ realAmount: null,
19
+ applyId: null,
20
+ methods: [],
21
+ paymentDetail: [],
22
+ bankInfo: [],
23
+ bankList: [],
24
+ withdrawLimit: null,
25
+ targetWithdrawLimit: null,
26
+ attachmentType: null, // null | 'withdraw' | 'pool'
27
+ maximumWithdrawAmount: 0,
28
+ enableWithdrawAmount: 0,
29
+ enableWithdraw: false,
30
+ withdrawRules: [],
31
+ withdrawCheck: null,
32
+
33
+ amount: null,
34
+ bank: null,
35
+ branch: null,
36
+ accountNumber: null,
37
+ holderName: null,
38
+
39
+ // otp
40
+ requestId: null,
41
+ status: null,
42
+ target: null,
43
+ ttl: null,
44
+ },
45
+ states: {
46
+ loading: {
47
+ invoke: {
48
+ src: 'fetchInitData',
49
+ onDone: {
50
+ target: 'mode',
51
+ actions: ['setInitData', 'preset', 'updateWithdrawCondition'],
52
+ },
53
+ onError: {
54
+ target: 'fail',
55
+ },
56
+ },
57
+ },
58
+ mode: {
59
+ always: [{ target: 'free', cond: 'isFree' }, { target: 'cash' }],
60
+ },
61
+ free: {
62
+ initial: 'checking',
63
+ states: {
64
+ // loading: {
65
+ // invoke: {
66
+ // src: 'fetchWithdrawRules',
67
+ // onDone: {
68
+ // target: 'checking',
69
+ // actions: 'updateWithdrawCondition',
70
+ // },
71
+ // onError: {
72
+ // target: 'notConvertible',
73
+ // },
74
+ // },
75
+ // },
76
+ checking: {
77
+ always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
78
+ },
79
+ notConvertible: {
80
+ on: {
81
+ GIVE_UP: {
82
+ target: 'givingUp',
83
+ },
84
+ },
85
+ },
86
+ givingUp: {
87
+ invoke: {
88
+ src: 'giveUp',
89
+ onDone: {
90
+ target: 'gaveUp',
91
+ },
92
+ onError: {
93
+ target: 'convertible',
94
+ },
95
+ },
96
+ },
97
+ gaveUp: {},
98
+ convertible: {
99
+ on: {
100
+ SUBMIT: {
101
+ target: 'converting',
102
+ actions: 'setAttachmentType',
103
+ },
104
+ },
105
+ },
106
+ converting: {
107
+ invoke: {
108
+ src: 'convertFreeCredit',
109
+ onDone: [
110
+ // {
111
+ // target: '#withdraw_vn.otp',
112
+ // actions: 'setOtp',
113
+ // cond: 'isOtpRequired',
114
+ // },
115
+ { target: 'converted' },
116
+ ],
117
+ onError: {
118
+ target: 'convertible',
119
+ },
120
+ },
121
+ },
122
+ converted: {
123
+ // always: [
124
+ // { target: '#withdraw_vn.cash.unbound', cond: 'isBankNotBound' },
125
+ // { target: '#withdraw_vn.cash.withdrawable', cond: 'hasAttachmentType' },
126
+ // ],
127
+ },
128
+ },
129
+ },
130
+ cash: {
131
+ initial: 'checking',
132
+ states: {
133
+ checking: {
134
+ always: [
135
+ { target: 'inProcessing', cond: 'haveProcessing' },
136
+ { target: 'unbound', cond: 'isBankNotBound' },
137
+ { target: 'withdrawCheck' },
138
+ ],
139
+ },
140
+ withdrawCheck: {
141
+ invoke: {
142
+ src: 'withdrawCheck',
143
+ onDone: [
144
+ {
145
+ target: 'checkingWithdrawStatus',
146
+ actions: assign((context, event) => {
147
+ return {
148
+ withdrawCheck: event.data,
149
+ }
150
+ }),
151
+ },
152
+ ],
153
+ onError: {
154
+ target: '#withdraw_vn.fail',
155
+ },
156
+ },
157
+ },
158
+ checkingWithdrawStatus: {
159
+ always: [
160
+ // 提款最低存款金額
161
+ { target: 'notWithdrawMinDepositAmount', cond: 'isNotWithdrawMinDepositAmount' },
162
+ // 提款冷卻時間
163
+ { target: 'passWithdrawCooldownTime', cond: 'isPassWithdrawCooldownTime' },
164
+ // 提款服務時間
165
+ { target: 'withdrawServiceTime', cond: 'isNotWIthdrawServiceTime' },
166
+ { target: 'notWithdrawable', cond: 'isNotWithdrawable' },
167
+ { target: 'withdrawable' },
168
+ ],
169
+ },
170
+ notWithdrawMinDepositAmount: {},
171
+ passWithdrawCooldownTime: {},
172
+ withdrawServiceTime: {},
173
+ notWithdrawable: {},
174
+ unbound: {
175
+ // to bank_binding.js
176
+ },
177
+ inProcessing: {},
178
+ withdrawable: {
179
+ on: {
180
+ SET_CURRENT_BANK: {
181
+ actions: assign((context, event) => {
182
+ return {
183
+ bank: event.data,
184
+ }
185
+ }),
186
+ },
187
+ SET_AMOUNT: {
188
+ actions: assign((context, event) => {
189
+ return {
190
+ amount: event.data,
191
+ }
192
+ }),
193
+ },
194
+ SET_ATTACHMENT_TYPE: {
195
+ actions: 'setAttachmentType',
196
+ },
197
+ SUBMIT: [
198
+ {
199
+ target: 'pooling',
200
+ cond: (context, event) => {
201
+ const { amount, attachmentType } = context
202
+ return attachmentType === 'pool' && amount > 0
203
+ },
204
+ },
205
+ {
206
+ target: 'submitting',
207
+ cond: 'isSubmittable',
208
+ },
209
+ ],
210
+ },
211
+ },
212
+ submitting: {
213
+ invoke: {
214
+ src: 'submitWithdraw',
215
+ onDone: [
216
+ {
217
+ target: '#withdraw_vn.otp',
218
+ actions: 'setOtp',
219
+ cond: 'isOtpRequired',
220
+ },
221
+ {
222
+ target: 'submitted',
223
+ },
224
+ ],
225
+ onError: {
226
+ target: 'withdrawable',
227
+ },
228
+ },
229
+ },
230
+ submitted: {
231
+ entry: assign((context, event) => {
232
+ const { applyId, balance, realAmount, charge } = event.data
233
+ return { applyId, balance, realAmount, charge }
234
+ }),
235
+ invoke: {
236
+ src: 'fetchPaymentData',
237
+ onDone: {
238
+ target: 'inProcessing',
239
+ actions: 'setPaymentData',
240
+ },
241
+ onError: {
242
+ target: '#withdraw_vn.fail',
243
+ },
244
+ },
245
+ },
246
+ pooling: {
247
+ invoke: {
248
+ src: 'pool',
249
+ onDone: { target: 'pooled' },
250
+ onError: {
251
+ target: 'withdrawable',
252
+ },
253
+ },
254
+ },
255
+ pooled: {},
256
+ },
257
+ },
258
+ otp: {
259
+ type: 'parallel',
260
+ states: {
261
+ countdown: {
262
+ after: {
263
+ 1000: {
264
+ target: 'countdown',
265
+ actions: 'decreaseTtl',
266
+ cond: (context) => context.ttl > 0,
267
+ },
268
+ },
269
+ },
270
+ verification: {
271
+ initial: 'idle',
272
+ states: {
273
+ idle: {
274
+ on: {
275
+ ACKNOWLEDGE: {
276
+ target: 'acknowledging',
277
+ },
278
+ RESET_OTP: {
279
+ target: 'reset',
280
+ },
281
+ BACK: [
282
+ {
283
+ target: '#withdraw_vn.free.convertible',
284
+ cond: 'isFree',
285
+ },
286
+ {
287
+ target: '#withdraw_vn.cash.withdrawable',
288
+ },
289
+ ],
290
+ },
291
+ },
292
+ acknowledging: {
293
+ invoke: {
294
+ src: 'acknowledge',
295
+ onDone: [
296
+ {
297
+ target: '#withdraw_vn.free.converted',
298
+ cond: 'isFree',
299
+ },
300
+ {
301
+ target: '#withdraw_vn.cash.submitted',
302
+ },
303
+ ],
304
+ onError: {
305
+ target: 'idle',
306
+ },
307
+ },
308
+ },
309
+ reset: {
310
+ invoke: {
311
+ src: 'resetOtp',
312
+ onDone: {
313
+ target: 'idle',
314
+ actions: 'setVerificationData',
315
+ },
316
+ onError: { target: 'idle' },
317
+ },
318
+ },
319
+ },
320
+ },
321
+ },
322
+ },
323
+ fail: {},
324
+ },
325
+ on: {
326
+ REFRESH: {
327
+ target: 'loading',
328
+ },
329
+ },
330
+ },
331
+ {
332
+ actions: {
333
+ setInitData: assign((context, event) => {
334
+ return {
335
+ paymentDetail: event.data.paymentDetail,
336
+ bankInfo: event.data.bankInfo,
337
+ bank: event.data.bankInfo[0],
338
+ bonus: event.data.bonus,
339
+ bankList: event.data.bankList,
340
+ }
341
+ }),
342
+ setPaymentData: assign((context, event) => {
343
+ return {
344
+ bankInfo: event.data.bankInfo,
345
+ bonus: event.data.bonus,
346
+ paymentDetail: event.data.paymentDetail,
347
+ }
348
+ }),
349
+ preset: assign((context, event) => {
350
+ const { userInfo, siteConfig } = event.data
351
+ const { withdraw_limit, target_withdraw_limit } = userInfo || {}
352
+ const balance = parseFloat(userInfo?.balance)
353
+ const methods = siteConfig?.paymentMethods?.withdraw || []
354
+ return {
355
+ withdrawLimit: Math.abs(withdraw_limit),
356
+ targetWithdrawLimit: Math.abs(target_withdraw_limit),
357
+ methods,
358
+ balance,
359
+ siteConfig,
360
+ userInfo,
361
+ attachmentType: 'withdraw',
362
+ }
363
+ }),
364
+ updateWithdrawCondition: assign((context, event) => {
365
+ const { differAmountLimit, enableWithdraw, enableWithdrawAmount, maxWithdrawStatus, maximumWithdrawAmount, minimumWithdrawAmount } =
366
+ event.data?.withdrawRule
367
+
368
+ const withdrawRules = event.data?.withdrawRule?.rules || []
369
+ const rules = withdrawRules.filter((rule) => rule.maxWithdrawAmount > 0)
370
+
371
+ const { balance } = context
372
+ const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
373
+
374
+ let maxWithdrawAmount = 0
375
+
376
+ if (lastMatchedIndex > -1) {
377
+ rules[lastMatchedIndex].match = true
378
+ maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
379
+ } else if (rules.length > 0) {
380
+ maxWithdrawAmount = rules[0].maxWithdrawAmount
381
+ } else {
382
+ maxWithdrawAmount = balance
383
+ }
384
+
385
+ return {
386
+ maxWithdrawAmount,
387
+ enableWithdrawAmount: +enableWithdrawAmount,
388
+ enableWithdraw,
389
+ withdrawRules,
390
+ }
391
+ }),
392
+ setOtp: assign((context, event) => {
393
+ const { requestId, status, target, ttl } = event.data
394
+ return {
395
+ requestId,
396
+ status,
397
+ target,
398
+ ttl,
399
+ }
400
+ }),
401
+ decreaseTtl: assign((context, evnet) => {
402
+ const second = context.ttl
403
+ return {
404
+ ttl: Math.max(second - 1, 0),
405
+ }
406
+ }),
407
+ setVerificationData: assign((context, event) => {
408
+ const { requestId, ttl, target } = event.data
409
+ return {
410
+ requestId,
411
+ ttl,
412
+ target,
413
+ }
414
+ }),
415
+ setAttachmentType: assign((context, event) => {
416
+ return {
417
+ attachmentType: event.data,
418
+ }
419
+ }),
420
+ },
421
+ guards: {
422
+ isFree: (context, event) => {
423
+ return context.userInfo?.withdraw_rule !== 'DEFAULT'
424
+ },
425
+ hasAttachmentType: (context, event) => {
426
+ return ['withdraw', 'pool'].includes(context.attachmentType)
427
+ },
428
+ haveProcessing: (context, event) => {
429
+ return (context.paymentDetail?.[0]?.processing || []).length > 0
430
+ },
431
+ isWithdrawDisable: (context, event) => {
432
+ const { siteConfig } = context
433
+ const methods = siteConfig?.paymentMethods?.withdraw || []
434
+ return methods.length === 0
435
+ },
436
+ isBankNotBound: (context, event) => {
437
+ return !context.bankInfo?.length
438
+ },
439
+ isSubmittable: (context, event) => {
440
+ const { bank, amount } = context
441
+ return bank && amount > 0
442
+ },
443
+ isConvertible: (context, event) => {
444
+ const { balance, enableWithdraw } = context
445
+ return balance > 0 && enableWithdraw
446
+ },
447
+ isNotWithdrawable: (context, event) => {
448
+ const { balance, enableWithdraw, paymentDetail } = context
449
+ const minWithdrawAmount = paymentDetail[0]?.minimum || 0
450
+ return !enableWithdraw || balance < minWithdrawAmount
451
+ },
452
+ isOtpRequired: (context, event) => {
453
+ return event.data?.status === 'otp-verifying'
454
+ },
455
+ isNotWithdrawMinDepositAmount: (context, event) => {
456
+ const { withdrawCheck } = context
457
+ return !withdrawCheck?.passDepositeLimit
458
+ },
459
+ isPassWithdrawCooldownTime: (context, event) => {
460
+ const { withdrawCheck } = context
461
+ return !withdrawCheck?.passWithdrawCooldownTime
462
+ },
463
+ isNotWIthdrawServiceTime: (context, event) => {
464
+ const { withdrawCheck } = context
465
+ return !withdrawCheck?.withdrawInServiceTime
466
+ },
467
+ },
468
+ services: {
469
+ fetchInitData: async (context, event) => {
470
+ const [{ config }, userInfo, paymentDetail = [], { bonus, bankInfo }, withdrawRule] = await Promise.all([
471
+ request.send(common.getConfig({ appSecret })),
472
+ request.send(user.getUserInfo()),
473
+ request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' })),
474
+ request.send(payment.getPaymentInfo()),
475
+ request.send(payment.getCurrentWithdrawRule()),
476
+ ])
477
+ const siteConfig = JSON.parse(window.atob(config))
478
+ const bankList = await request.send(payment.getWithdrawBankList())
479
+ const newBankInfo = bankInfo
480
+ .map((item) => {
481
+ const matchedBank = bankList.find((bank) => bank.bankCode === item.bankCode)
482
+ return matchedBank ? { ...item, ...matchedBank } : null
483
+ })
484
+ .filter(Boolean)
485
+ return { siteConfig, userInfo, paymentDetail, bonus, bankInfo: newBankInfo, bankList, withdrawRule }
486
+ },
487
+ fetchPaymentData: async (context, event) => {
488
+ const paymentDetail = await request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' }))
489
+ const { bonus, bankInfo } = await request.send(payment.getPaymentInfo())
490
+ return { bonus, bankInfo, paymentDetail }
491
+ },
492
+ updatePaymentInfo: async (context, event) => {
493
+ const { bank, branch, accountNumber, holderName } = context
494
+ return await request.send(
495
+ payment.updatePaymentInfo({
496
+ bankCode: bank.bankCode,
497
+ branch,
498
+ bankAccount: accountNumber,
499
+ holderName,
500
+ })
501
+ )
502
+ },
503
+ fetchWithdrawRules: async (context, event) => {
504
+ return await request.send(payment.getCurrentWithdrawRule())
505
+ },
506
+ submitWithdraw: async (context, event) => {
507
+ const { bank, amount } = context
508
+ const { method } = context.paymentDetail[0]
509
+ const branchTemp = ['GCASH', 'PAYM', 'PIX'].includes(bank.bankCode) ? '' : bank.branch
510
+ return await request.send(
511
+ payment.submitWithdrawTransactionVN({
512
+ method,
513
+ amount,
514
+ applyerBankCode: bank.bankCode,
515
+ applyerAccountNumber: bank.accountNumber,
516
+ branch: branchTemp,
517
+ holderName: bank.holderName,
518
+ })
519
+ )
520
+ },
521
+ convertFreeCredit: async (context, event) => {
522
+ return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
523
+ },
524
+ resetOtp: async (context, event) => {
525
+ const { requestId, target } = context
526
+ return await request.send(user.otpReset({ requestId, target }))
527
+ },
528
+ acknowledge: async (context, event) => {
529
+ const { requestId, target } = context
530
+ const code = event.data
531
+ return await request.send(user.otpAcknowledge({ requestId, target, code }))
532
+ },
533
+ pool: async (context, event) => {
534
+ const { amount } = context
535
+ return await request.send(payment.restore({ amount }))
536
+ },
537
+ giveUp: async (context, event) => {
538
+ return await request.send(payment.giveUp())
539
+ },
540
+ withdrawCheck: async (context, event) => {
541
+ return await request.send(payment.withdrawCheck())
542
+ },
543
+ },
544
+ }
545
+ )
546
+ }