@jetthai/cuora 1.1.360 → 1.1.361-dev-1

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