@jetthai/cuora 1.1.8 → 1.1.9-bc88-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.
@@ -0,0 +1,141 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import payment from '../../api/payment.js'
3
+
4
+ export default function (request) {
5
+ return createMachine(
6
+ {
7
+ id: 'vpay',
8
+ initial: 'loading',
9
+ context: {
10
+ method: 'vpay',
11
+ userAccount: {},
12
+ amount: 0,
13
+
14
+ options: [],
15
+ minimum: 0,
16
+ maximum: 0,
17
+
18
+ qrcode: null,
19
+ vpayId: null,
20
+ calculatedAmount: 0,
21
+ },
22
+ states: {
23
+ loading: {
24
+ initial: 'checkingUserAccount',
25
+ states: {
26
+ checkingUserAccount: {
27
+ invoke: {
28
+ src: 'fetchPaymentInfo',
29
+ onDone: [
30
+ {
31
+ target: 'initializing',
32
+ actions: 'setUserAccount',
33
+ cond: 'hasUserAccount',
34
+ },
35
+ { target: '#vpay.accountUnbound' },
36
+ ],
37
+ onError: {
38
+ target: '#vpay.unavailable',
39
+ },
40
+ },
41
+ },
42
+ initializing: {
43
+ invoke: {
44
+ src: 'initialize',
45
+ onDone: { target: '#vpay.submittable', actions: 'setInitialization' },
46
+ onError: {
47
+ target: '#vpay.unavailable',
48
+ },
49
+ },
50
+ },
51
+ },
52
+ },
53
+ submittable: {
54
+ on: {
55
+ ADD: {
56
+ actions: 'add',
57
+ },
58
+ CLEAR: {
59
+ actions: 'clear',
60
+ },
61
+ SUBMIT: {
62
+ target: 'submitting',
63
+ },
64
+ },
65
+ },
66
+ submitting: {
67
+ invoke: {
68
+ src: 'submitDeposit',
69
+ onDone: { target: 'submitted', actions: 'setQrcode' },
70
+ onError: {
71
+ target: 'submittable',
72
+ },
73
+ },
74
+ },
75
+ submitted: {},
76
+ accountUnbound: {},
77
+ unavailable: {},
78
+ },
79
+ },
80
+ {
81
+ actions: {
82
+ setUserAccount: assign((context, event) => {
83
+ return {
84
+ userAccount: event.data.bankInfo?.[0],
85
+ }
86
+ }),
87
+ setQrcode: assign((context, event) => {
88
+ const { amount, qrcode, vpayId } = event.data
89
+ return {
90
+ qrcode,
91
+ vpayId,
92
+ calculatedAmount: amount,
93
+ }
94
+ }),
95
+ setInitialization: assign((context, event) => {
96
+ const { housePayment } = event.data
97
+ const { optional, minimum, maximum } = housePayment
98
+ return {
99
+ options: optional,
100
+ minimum,
101
+ maximum,
102
+ }
103
+ }),
104
+ add: assign((context, event) => {
105
+ const { amount, maximum } = context
106
+ return {
107
+ amount: Math.min(amount + event.data, maximum),
108
+ }
109
+ }),
110
+ clear: assign((context, event) => {
111
+ return {
112
+ amount: 0,
113
+ }
114
+ }),
115
+ },
116
+ guards: {
117
+ hasUserAccount: (context, event) => {
118
+ return event.data.bankInfo?.[0]
119
+ },
120
+ },
121
+ services: {
122
+ fetchPaymentInfo: async (context, event) => {
123
+ return await request.send(payment.getPaymentInfo())
124
+ },
125
+ initialize: async (context, event) => {
126
+ const payments = await request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' }))
127
+ return { housePayment: payments.find((payment) => payment.method === context.method) }
128
+ },
129
+ submitDeposit: async (context, event) => {
130
+ const { method, amount } = context
131
+ return await request.send(
132
+ payment.submitDepositTransaction({
133
+ method,
134
+ amount,
135
+ })
136
+ )
137
+ },
138
+ },
139
+ }
140
+ )
141
+ }
@@ -22,9 +22,11 @@ import user_info from './user_info.js'
22
22
  import user_sign_up from './user_sign_up.js'
23
23
  import user_reset_password from './user_reset_password.js'
24
24
  import bank_binding from './bank_binding.js'
25
+ import bank_binding_amb from './bank_binding_amb.js'
25
26
  import withdraw_fc from './withdraw_fc.js'
26
27
  import withdraw_vn from './withdraw_vn.js'
27
28
  import withdraw_crypto from './withdraw_crypto.js'
29
+ import withdraw_amb from './withdraw_amb.js'
28
30
  import deposit from './deposit/index.js'
29
31
  import bonus from './bonus.js'
30
32
  import leaderboard from './leaderboard.js'
@@ -58,9 +60,11 @@ export default {
58
60
  user_sign_up,
59
61
  user_reset_password,
60
62
  bank_binding,
63
+ bank_binding_amb,
61
64
  withdraw_fc,
62
65
  withdraw_vn,
63
66
  withdraw_crypto,
67
+ withdraw_amb,
64
68
  ...deposit,
65
69
  bonus,
66
70
  leaderboard,
@@ -0,0 +1,424 @@
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_amb',
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
+
27
+ amount: null,
28
+ bank: null,
29
+ branch: null,
30
+ accountNumber: null,
31
+ holderName: null,
32
+
33
+ withdrawDistance: null,
34
+ withdrawAmount: null,
35
+
36
+ // otp
37
+ requestId: null,
38
+ status: null,
39
+ target: null,
40
+ ttl: null,
41
+ },
42
+ states: {
43
+ loading: {
44
+ invoke: {
45
+ src: 'fetchInitData',
46
+ onDone: {
47
+ target: 'mode',
48
+ actions: ['setInitData', 'preset'],
49
+ },
50
+ onError: {
51
+ target: 'fail',
52
+ },
53
+ },
54
+ },
55
+ mode: {
56
+ always: [{ target: 'free', cond: 'isFree' }, { target: 'cash' }],
57
+ },
58
+ free: {
59
+ initial: 'loading',
60
+ states: {
61
+ loading: {
62
+ invoke: {
63
+ src: 'fetchWithdrawRules',
64
+ onDone: {
65
+ target: 'checking',
66
+ actions: 'updateWithdrawCondition',
67
+ },
68
+ onError: {
69
+ target: 'notConvertible',
70
+ },
71
+ },
72
+ },
73
+ checking: {
74
+ always: [{ target: 'convertible', cond: 'isConvertible' }, { target: 'notConvertible' }],
75
+ },
76
+ notConvertible: {},
77
+ convertible: {
78
+ on: {
79
+ SUBMIT: {
80
+ target: 'converting',
81
+ },
82
+ },
83
+ },
84
+ converting: {
85
+ invoke: {
86
+ src: 'submitWithdrawFree',
87
+ onDone: [
88
+ {
89
+ target: '#withdraw_amb.otp',
90
+ actions: 'setOtp',
91
+ cond: 'isOtpRequired',
92
+ },
93
+ { target: 'converted' },
94
+ ],
95
+ onError: {
96
+ target: 'convertible',
97
+ },
98
+ },
99
+ },
100
+ converted: {},
101
+ },
102
+ },
103
+ cash: {
104
+ initial: 'checking',
105
+ states: {
106
+ checking: {
107
+ always: [
108
+ { target: 'inProcessing', cond: 'haveProcessing' },
109
+ { target: 'notWithdrawable', cond: 'isNotWithdrawable' },
110
+ { target: 'unbound', cond: 'isBankNotBound' },
111
+ { target: 'withdrawable' },
112
+ ],
113
+ },
114
+ notWithdrawable: {},
115
+ unbound: {
116
+ // to bank_binding.js
117
+ },
118
+ inProcessing: {},
119
+ withdrawable: {
120
+ on: {
121
+ SET_CURRENT_BANK: {
122
+ actions: assign((context, event) => {
123
+ return {
124
+ bank: event.data,
125
+ }
126
+ }),
127
+ },
128
+ SET_AMOUNT: {
129
+ actions: assign((context, event) => {
130
+ return {
131
+ amount: event.data,
132
+ }
133
+ }),
134
+ },
135
+ SUBMIT: {
136
+ target: 'submitting',
137
+ cond: 'isSubmittable',
138
+ },
139
+ },
140
+ },
141
+ submitting: {
142
+ invoke: {
143
+ src: 'submitWithdraw',
144
+ onDone: [
145
+ {
146
+ target: '#withdraw_amb.otp',
147
+ actions: 'setOtp',
148
+ cond: 'isOtpRequired',
149
+ },
150
+ {
151
+ target: 'submitted',
152
+ },
153
+ ],
154
+ onError: {
155
+ target: 'withdrawable',
156
+ },
157
+ },
158
+ },
159
+ submitted: {
160
+ entry: assign((context, event) => {
161
+ const { applyId, balance, realAmount, charge } = event.data
162
+ return { applyId, balance, realAmount, charge }
163
+ }),
164
+ invoke: {
165
+ src: 'fetchPaymentData',
166
+ onDone: {
167
+ target: 'inProcessing',
168
+ actions: 'setPaymentData',
169
+ },
170
+ onError: {
171
+ target: '#withdraw_amb.fail',
172
+ },
173
+ },
174
+ },
175
+ },
176
+ },
177
+ otp: {
178
+ type: 'parallel',
179
+ states: {
180
+ countdown: {
181
+ after: {
182
+ 1000: {
183
+ target: 'countdown',
184
+ actions: 'decreaseTtl',
185
+ cond: (context) => context.ttl > 0,
186
+ },
187
+ },
188
+ },
189
+ verification: {
190
+ initial: 'idle',
191
+ states: {
192
+ idle: {
193
+ on: {
194
+ ACKNOWLEDGE: {
195
+ target: 'acknowledging',
196
+ },
197
+ RESET_OTP: {
198
+ target: 'reset',
199
+ },
200
+ BACK: [
201
+ {
202
+ target: '#withdraw_amb.free.convertible',
203
+ cond: 'isFree',
204
+ },
205
+ {
206
+ target: '#withdraw_amb.cash.withdrawable',
207
+ },
208
+ ],
209
+ },
210
+ },
211
+ acknowledging: {
212
+ invoke: {
213
+ src: 'acknowledge',
214
+ onDone: [
215
+ {
216
+ target: '#withdraw_amb.free.converted',
217
+ cond: 'isFree',
218
+ },
219
+ {
220
+ target: '#withdraw_amb.cash.submitted',
221
+ },
222
+ ],
223
+ onError: {
224
+ target: 'idle',
225
+ },
226
+ },
227
+ },
228
+ reset: {
229
+ invoke: {
230
+ src: 'resetOtp',
231
+ onDone: {
232
+ target: 'idle',
233
+ actions: 'setVerificationData',
234
+ },
235
+ onError: { target: 'idle' },
236
+ },
237
+ },
238
+ },
239
+ },
240
+ },
241
+ },
242
+ fail: {},
243
+ },
244
+ on: {
245
+ REFRESH: {
246
+ target: 'loading',
247
+ },
248
+ },
249
+ },
250
+ {
251
+ actions: {
252
+ setInitData: assign((context, event) => {
253
+ return {
254
+ paymentDetail: event.data.paymentDetail,
255
+ bankInfo: event.data.bankInfo,
256
+ bonus: event.data.bonus,
257
+ bankList: event.data.bankList,
258
+ }
259
+ }),
260
+ setPaymentData: assign((context, event) => {
261
+ return {
262
+ bankInfo: event.data.bankInfo,
263
+ bonus: event.data.bonus,
264
+ paymentDetail: event.data.paymentDetail,
265
+ }
266
+ }),
267
+ preset: assign((context, event) => {
268
+ const { userInfo, siteConfig } = event.data
269
+ const { withdraw_limit, target_withdraw_limit } = userInfo || {}
270
+ const balance = parseFloat(userInfo?.balance)
271
+ const methods = siteConfig?.paymentMethods?.withdraw || []
272
+ return {
273
+ withdrawLimit: Math.abs(withdraw_limit),
274
+ targetWithdrawLimit: Math.abs(target_withdraw_limit),
275
+ methods,
276
+ balance,
277
+ siteConfig,
278
+ userInfo,
279
+ }
280
+ }),
281
+ updateWithdrawCondition: assign((context, event) => {
282
+ const rules = event.data.rules.filter((rule) => rule.maxWithdrawAmount > 0)
283
+
284
+ const { balance, withdrawLimit } = context
285
+ const lastMatchedIndex = findLastIndex(rules, (rule) => balance >= rule.greaterEqualThen)
286
+
287
+ let maxWithdrawAmount = 0,
288
+ availableWithdrawAmount = 0
289
+
290
+ if (lastMatchedIndex > -1) {
291
+ rules[lastMatchedIndex].match = true
292
+ maxWithdrawAmount = rules[lastMatchedIndex].maxWithdrawAmount
293
+
294
+ if (withdrawLimit === 0) {
295
+ availableWithdrawAmount = Math.min(balance, maxWithdrawAmount)
296
+ }
297
+ } else if (rules.length > 0) {
298
+ maxWithdrawAmount = rules[0].maxWithdrawAmount
299
+ } else {
300
+ maxWithdrawAmount = balance
301
+ }
302
+
303
+ return {
304
+ maxWithdrawAmount,
305
+ availableWithdrawAmount,
306
+ }
307
+ }),
308
+ setOtp: assign((context, event) => {
309
+ const { requestId, status, target, ttl } = event.data
310
+ return {
311
+ requestId,
312
+ status,
313
+ target,
314
+ ttl,
315
+ }
316
+ }),
317
+ decreaseTtl: assign((context, evnet) => {
318
+ const second = context.ttl
319
+ return {
320
+ ttl: Math.max(second - 1, 0),
321
+ }
322
+ }),
323
+ setVerificationData: assign((context, event) => {
324
+ const { requestId, ttl, target } = event.data
325
+ return {
326
+ requestId,
327
+ ttl,
328
+ target,
329
+ }
330
+ }),
331
+ },
332
+ guards: {
333
+ isFree: (context, event) => {
334
+ return context.userInfo?.withdraw_rule !== 'DEFAULT'
335
+ },
336
+ haveProcessing: (context, event) => {
337
+ return (context.paymentDetail?.[0]?.processing || []).length > 0
338
+ },
339
+ isWithdrawDisable: (context, event) => {
340
+ const { siteConfig } = context
341
+ const methods = siteConfig?.paymentMethods?.withdraw || []
342
+ return methods.length === 0
343
+ },
344
+ isBankNotBound: (context, event) => {
345
+ return !context.bankInfo?.length
346
+ },
347
+ isSubmittable: (context, event) => {
348
+ const { applyerBankCode, applyerAccountNumber, branch, holderName, amount } = event.data
349
+ return applyerBankCode && applyerAccountNumber && branch && holderName && amount
350
+ },
351
+ isConvertible: (context, event) => {
352
+ const { balance, withdrawLimit } = context
353
+ return balance > 0 && withdrawLimit === 0
354
+ },
355
+ isNotWithdrawable: (context, event) => {
356
+ const { balance, withdrawLimit, paymentDetail } = context
357
+ const minWithdrawAmount = paymentDetail[0]?.minimum || 0
358
+ return withdrawLimit !== 0 || balance < minWithdrawAmount
359
+ },
360
+ isOtpRequired: (context, event) => {
361
+ return event.data?.status === 'otp-verifying'
362
+ },
363
+ },
364
+ services: {
365
+ fetchInitData: async (context, event) => {
366
+ const [{ config }, userInfo, paymentDetail = [], { bonus, bankInfo }] = await Promise.all([
367
+ request.send(common.getConfig({ appSecret })),
368
+ request.send(user.getUserInfo()),
369
+ request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' })),
370
+ request.send(payment.getPaymentInfo()),
371
+ ])
372
+ const siteConfig = JSON.parse(window.atob(config))
373
+ const method = paymentDetail.find((payment) => payment.category === 'amb').method
374
+ const bankList = await request.send(payment.getBankList(method))
375
+ const newBankInfo = bankInfo.map((item) => {
376
+ return { ...item, ...bankList.find((bank) => bank.bankCode === item.bankCode) }
377
+ })
378
+ return { siteConfig, userInfo, paymentDetail, bonus, bankInfo: newBankInfo, bankList }
379
+ },
380
+ fetchPaymentData: async (context, event) => {
381
+ const paymentDetail = await request.send(payment.getPaymentDetail({ transferType: 'WITHDRAW' }))
382
+ const { bonus, bankInfo } = await request.send(payment.getPaymentInfo())
383
+ return { bonus, bankInfo, paymentDetail }
384
+ },
385
+ updatePaymentInfo: async (context, event) => {
386
+ const { bank, branch, accountNumber, holderName } = context
387
+ return await request.send(
388
+ payment.updatePaymentInfo({
389
+ bankCode: bank.bankCode,
390
+ branch,
391
+ bankAccount: accountNumber,
392
+ holderName,
393
+ })
394
+ )
395
+ },
396
+ fetchWithdrawRules: async (context, event) => {
397
+ return await request.send(payment.getCurrentWithdrawRule())
398
+ },
399
+ submitWithdraw: async (context, event) => {
400
+ const { amount } = event.data
401
+ const { method } = context.paymentDetail[0]
402
+ return await request.send(
403
+ payment.submitWithdrawTransactionAmb({
404
+ method,
405
+ amount,
406
+ })
407
+ )
408
+ },
409
+ submitWithdrawFree: async (context, event) => {
410
+ return await request.send(payment.convertFreeCredit({ exchangeType: 1 }))
411
+ },
412
+ resetOtp: async (context, event) => {
413
+ const { requestId, target } = context
414
+ return await request.send(user.otpReset({ requestId, target }))
415
+ },
416
+ acknowledge: async (context, event) => {
417
+ const { requestId, target } = context
418
+ const code = event.data
419
+ return await request.send(user.otpAcknowledge({ requestId, target, code }))
420
+ },
421
+ },
422
+ }
423
+ )
424
+ }
@@ -84,13 +84,16 @@ export default function (request) {
84
84
  converting: {
85
85
  invoke: {
86
86
  src: 'submitWithdrawFree',
87
- onDone: [{
87
+ onDone: [
88
+ {
88
89
  target: '#withdraw_crypto.otp',
89
90
  actions: 'setOtp',
90
- cond: 'isOtpRequired'
91
- }, {
91
+ cond: 'isOtpRequired',
92
+ },
93
+ {
92
94
  target: 'converted',
93
- }],
95
+ },
96
+ ],
94
97
  onError: {
95
98
  target: 'convertible',
96
99
  },
@@ -158,13 +161,16 @@ export default function (request) {
158
161
  submitting: {
159
162
  invoke: {
160
163
  src: 'submitWithdraw',
161
- onDone: [{
162
- target: '#withdraw_crypto.otp',
163
- actions: 'setOtp',
164
- cond: 'isOtpRequired'
165
- }, {
166
- target: 'submitted',
167
- }],
164
+ onDone: [
165
+ {
166
+ target: '#withdraw_crypto.otp',
167
+ actions: 'setOtp',
168
+ cond: 'isOtpRequired',
169
+ },
170
+ {
171
+ target: 'submitted',
172
+ },
173
+ ],
168
174
  onError: {
169
175
  target: 'withdrawable',
170
176
  },
@@ -196,23 +202,29 @@ export default function (request) {
196
202
  RESET_OTP: {
197
203
  target: 'reset',
198
204
  },
199
- BACK: [{
200
- target: '#withdraw_crypto.free.convertible',
201
- cond: 'isFree'
202
- }, {
203
- target: '#withdraw_crypto.cash.submittable',
204
- }],
205
+ BACK: [
206
+ {
207
+ target: '#withdraw_crypto.free.convertible',
208
+ cond: 'isFree',
209
+ },
210
+ {
211
+ target: '#withdraw_crypto.cash.submittable',
212
+ },
213
+ ],
205
214
  },
206
215
  },
207
216
  acknowledging: {
208
217
  invoke: {
209
218
  src: 'acknowledge',
210
- onDone: [{
211
- target: '#withdraw_crypto.free.converted',
212
- cond: 'isFree'
213
- }, {
214
- target: '#withdraw_crypto.cash.submitted',
215
- }],
219
+ onDone: [
220
+ {
221
+ target: '#withdraw_crypto.free.converted',
222
+ cond: 'isFree',
223
+ },
224
+ {
225
+ target: '#withdraw_crypto.cash.submitted',
226
+ },
227
+ ],
216
228
  onError: {
217
229
  target: 'idle',
218
230
  },
@@ -389,7 +401,7 @@ export default function (request) {
389
401
  },
390
402
  isOtpRequired: (context, event) => {
391
403
  return event.data?.status === 'otp-verifying'
392
- }
404
+ },
393
405
  },
394
406
  services: {
395
407
  fetchInitData: async (context, event) => {
@@ -411,7 +423,7 @@ export default function (request) {
411
423
  submitWithdraw: async (context, event) => {
412
424
  const { crypto, selectedNetwork, address, amount } = context
413
425
  return await request.send(
414
- api.submitWithdrawTransaction({
426
+ api.submitWithdrawTransactionCrypto({
415
427
  method: crypto.method,
416
428
  amount,
417
429
  cryptoCurrency: crypto.channel,
@@ -215,7 +215,7 @@ export default function (request) {
215
215
  const { crypto, currentNetwork, currentAddress, withdrawAmount } = context
216
216
  const method = crypto.method
217
217
  return await request.send(
218
- api.submitWithdrawTransaction({
218
+ api.submitWithdrawTransactionCrypto({
219
219
  method,
220
220
  amount: withdrawAmount,
221
221
  cryptoCurrency: crypto.channel,