@jetthai/cuora 1.1.343-dev-11 → 1.1.343-qa-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,432 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import padStart from 'lodash/padStart.js'
3
+ import payment from '../../api/payment.js'
4
+ import common from '../../api/common.js'
5
+
6
+ export default function (request) {
7
+ const { appSecret } = request.config
8
+ return createMachine(
9
+ {
10
+ id: 'crypro_button',
11
+ initial: 'loading',
12
+ context: {
13
+ siteConfig: null,
14
+ paymentDetails: [],
15
+
16
+ channels: [],
17
+ cryptos: [],
18
+ networks: [],
19
+ options: [],
20
+ sourceAddress: '',
21
+
22
+ selectedChannel: null,
23
+ selectedAmount: null,
24
+ selectedCrypto: null,
25
+ selectedNetwork: null,
26
+
27
+ currentCategory: 'crypto',
28
+ currentPayment: null,
29
+
30
+ calculation: null,
31
+ result: null,
32
+
33
+ grandTotalEnable: 1,
34
+
35
+ // use for wallet mode 4
36
+ promotion: null,
37
+ bonusPreviewData: [],
38
+ },
39
+ states: {
40
+ loading: {
41
+ invoke: {
42
+ src: 'fetchInitData',
43
+ onDone: {
44
+ target: 'loaded',
45
+ actions: 'setInitData',
46
+ },
47
+ onError: {
48
+ target: 'fail',
49
+ },
50
+ },
51
+ },
52
+ checking: {
53
+ always: [
54
+ {
55
+ target: 'fetching',
56
+ cond: (context, event) => {
57
+ const { siteConfig = {}, promotion = {}, selectedCrypto } = context
58
+ const { walletMode = '', bonusPreviewEnable } = siteConfig
59
+ return !!selectedCrypto && walletMode === '4' && bonusPreviewEnable && promotion.activityNo != 'none'
60
+ },
61
+ },
62
+ { target: 'loaded' },
63
+ ],
64
+ },
65
+ fetching: {
66
+ invoke: {
67
+ src: 'fetchBonusPreview',
68
+ onDone: {
69
+ target: 'loaded',
70
+ actions: 'setBonusPreview',
71
+ },
72
+ onError: {
73
+ target: 'loaded',
74
+ },
75
+ },
76
+ },
77
+ loaded: {
78
+ on: {
79
+ SET_CHANNEL: {
80
+ actions: 'setChannel',
81
+ },
82
+ SET_CRYPTO: {
83
+ actions: 'setCrypto',
84
+ },
85
+ SET_NETWORK: {
86
+ actions: 'setNetwork',
87
+ },
88
+ SET_ADDRESS: {
89
+ actions: 'setAddress',
90
+ },
91
+ CONFIRM: {
92
+ target: 'selecting',
93
+ cond: 'isConfirmable',
94
+ },
95
+ },
96
+ },
97
+ fail: {},
98
+ selecting: {
99
+ entry: 'reset',
100
+ on: {
101
+ SELECT_AMOUNT: [
102
+ {
103
+ target: 'debouncing',
104
+ actions: 'setAmount',
105
+ cond: 'isDoubleWalletMode',
106
+ },
107
+ {
108
+ actions: 'addAmount',
109
+ cond: (context, event) => context.siteConfig?.walletMode === '2' && context.grandTotalEnable === 1,
110
+ },
111
+ {
112
+ actions: 'setAmount',
113
+ cond: 'isFreeWalletMode',
114
+ },
115
+ {
116
+ target: 'debouncing',
117
+ actions: 'addAmount',
118
+ cond: 'isGrandTotalEnable',
119
+ },
120
+ {
121
+ target: 'debouncing',
122
+ actions: 'setAmount',
123
+ },
124
+ ],
125
+ KEY_IN: [
126
+ {
127
+ actions: 'setAmount',
128
+ cond: 'isFreeWalletMode',
129
+ },
130
+ {
131
+ target: 'debouncing',
132
+ actions: 'setAmount',
133
+ },
134
+ ],
135
+ SUBMIT: {
136
+ target: 'debouncing',
137
+ },
138
+ BACK: {
139
+ target: 'loaded',
140
+ },
141
+ CLEAR_ALL: {
142
+ actions: 'reset',
143
+ },
144
+ },
145
+ },
146
+ debouncing: {
147
+ on: {
148
+ SELECT_AMOUNT: [
149
+ {
150
+ target: 'debouncing',
151
+ actions: 'addAmount',
152
+ cond: 'isGrandTotalEnable',
153
+ },
154
+ {
155
+ target: 'debouncing',
156
+ actions: 'setAmount',
157
+ },
158
+ ],
159
+ KEY_IN: {
160
+ target: 'debouncing',
161
+ actions: 'setAmount',
162
+ },
163
+ },
164
+ after: {
165
+ 1000: {
166
+ target: 'calculating',
167
+ },
168
+ },
169
+ },
170
+ calculating: {
171
+ invoke: {
172
+ src: 'fetchCalculation',
173
+ onDone: [
174
+ {
175
+ target: 'submitting',
176
+ actions: 'setCalculation',
177
+ cond: 'isFreeWalletMode',
178
+ },
179
+ {
180
+ target: 'calculated',
181
+ actions: 'setCalculation',
182
+ },
183
+ ],
184
+ onError: {
185
+ target: 'selecting',
186
+ actions: 'reset',
187
+ },
188
+ },
189
+ },
190
+ calculated: {
191
+ on: {
192
+ SELECT_AMOUNT: [
193
+ {
194
+ target: 'debouncing',
195
+ actions: 'addAmount',
196
+ cond: 'isGrandTotalEnable',
197
+ },
198
+ {
199
+ target: 'debouncing',
200
+ actions: 'setAmount',
201
+ },
202
+ ],
203
+ KEY_IN: {
204
+ target: 'debouncing',
205
+ actions: 'setAmount',
206
+ },
207
+ SUBMIT: {
208
+ target: 'submitting',
209
+ cond: 'isSubmittable',
210
+ },
211
+ CLEAR_CALCULATION: {
212
+ target: 'debouncing',
213
+ actions: 'clearCalculation',
214
+ },
215
+ CLEAR_ALL: {
216
+ target: 'selecting',
217
+ actions: 'reset',
218
+ },
219
+ },
220
+ },
221
+ submitting: {
222
+ invoke: {
223
+ src: 'submitDeposit',
224
+ onDone: {
225
+ target: 'submitted',
226
+ actions: 'setResult',
227
+ },
228
+ onError: {
229
+ target: 'loaded',
230
+ },
231
+ },
232
+ },
233
+ submitted: {},
234
+ },
235
+ on: {
236
+ SET_PROMOTION: {
237
+ actions: 'setPromotion',
238
+ },
239
+ },
240
+ },
241
+ {
242
+ actions: {
243
+ reset: assign((context, event) => {
244
+ return {
245
+ selectedAmount: null,
246
+ calculation: null,
247
+ result: null,
248
+ }
249
+ }),
250
+ setInitData: assign((context, event) => {
251
+ const { siteConfig, paymentDetail } = event.data
252
+ const cryptos =
253
+ paymentDetail
254
+ ?.filter((element) => element.category === context.currentCategory)
255
+ ?.map((crypto) => ({ ...crypto, label: crypto.channel, value: crypto.method, icon: crypto.channel })) || []
256
+ const currentPayment = cryptos[0]
257
+ const channels = cryptos.map((item, i) => ({ ...item, label: `Channel ${padStart(String(i + 1), 2, '0')}`, icon: 'channel' }))
258
+ const defaultNetworks =
259
+ cryptos[0].networkName.map((value, i) => {
260
+ return {
261
+ label: cryptos[0].networkOptional?.[i],
262
+ value,
263
+ icon: value,
264
+ }
265
+ }) || []
266
+ const options = cryptos[0].optional
267
+ return {
268
+ siteConfig,
269
+ channels,
270
+ cryptos,
271
+ networks: defaultNetworks,
272
+ selectedChannel: channels[0],
273
+ selectedCrypto: cryptos[0],
274
+ selectedNetwork: defaultNetworks[0],
275
+ grandTotalEnable: cryptos[0].grandTotalEnable,
276
+ currentPayment,
277
+ options,
278
+ }
279
+ }),
280
+ setChannel: assign((context, event) => {
281
+ const { cryptos } = context
282
+ const selectedCrypto = cryptos.find((crypto) => crypto.method === event.data.value)
283
+ const networks =
284
+ selectedCrypto?.networkName.map((value, i) => {
285
+ return {
286
+ label: selectedCrypto?.networkOptional?.[i],
287
+ value,
288
+ icon: value,
289
+ }
290
+ }) || []
291
+ const options = selectedCrypto.optional
292
+ return {
293
+ networks,
294
+ selectedChannel: event.data,
295
+ selectedCrypto: selectedCrypto,
296
+ selectedNetwork: networks[0],
297
+ selectedAmount: null,
298
+ calculation: null,
299
+ grandTotalEnable: selectedCrypto.grandTotalEnable,
300
+ options,
301
+ }
302
+ }),
303
+ setCrypto: assign((context, event) => {
304
+ const { cryptos, selectedNetwork } = context
305
+ const method = cryptos.find((crypto) => crypto.method === event.data.value)
306
+ const networks =
307
+ method?.networkName.map((value, i) => {
308
+ return {
309
+ label: method?.networkOptional?.[i],
310
+ value,
311
+ icon: value,
312
+ }
313
+ }) || []
314
+ const properties = {
315
+ selectedCrypto: event.data,
316
+ networks,
317
+ }
318
+
319
+ if (!networks.includes(selectedNetwork)) {
320
+ properties.selectedNetwork = null
321
+ properties.selectedAmount = null
322
+ properties.calculation = null
323
+ }
324
+
325
+ return properties
326
+ }),
327
+ setNetwork: assign((context, event) => {
328
+ return { selectedNetwork: event.data }
329
+ }),
330
+ setBonusPreview: assign((context, event) => {
331
+ return {
332
+ bonusPreviewData: event.data,
333
+ }
334
+ }),
335
+ addAmount: assign((context, event) => {
336
+ return { selectedAmount: (context.selectedAmount || 0) + event.data }
337
+ }),
338
+ setAmount: assign((context, event) => {
339
+ return { selectedAmount: event.data }
340
+ }),
341
+ setAddress: assign((context, event) => {
342
+ return {
343
+ sourceAddress: event.data,
344
+ }
345
+ }),
346
+ setCalculation: assign((context, event) => {
347
+ return {
348
+ calculation: event.data,
349
+ }
350
+ }),
351
+ clearCalculation: assign((context, event) => {
352
+ return {
353
+ calculation: null,
354
+ }
355
+ }),
356
+ setResult: assign((context, event) => {
357
+ return {
358
+ result: event.data,
359
+ }
360
+ }),
361
+ setPromotion: assign((context, event) => {
362
+ const promotion = event.data
363
+ return {
364
+ promotion,
365
+ }
366
+ }),
367
+ },
368
+ guards: {
369
+ isFreeWalletMode: (context, event) => {
370
+ return context.siteConfig?.walletMode === '2'
371
+ },
372
+ isDoubleWalletMode: (context, event) => {
373
+ return context.siteConfig?.walletMode === '4'
374
+ },
375
+ isGrandTotalEnable: (context, event) => {
376
+ return context.grandTotalEnable === 1
377
+ },
378
+ isConfirmable: (context, event) => {
379
+ const { selectedCrypto, selectedNetwork } = context
380
+ return selectedCrypto && selectedNetwork
381
+ },
382
+ isSubmittable: (context, event) => {
383
+ const { selectedCrypto, selectedNetwork, selectedAmount } = context
384
+ return selectedCrypto && selectedNetwork && selectedAmount
385
+ },
386
+ },
387
+ services: {
388
+ fetchInitData: async (context, event) => {
389
+ const [{ config }, paymentDetail = []] = await Promise.all([
390
+ request.send(common.getConfig({ appSecret })),
391
+ request.send(payment.getPaymentDetail({ transferType: 'DEPOSIT' })),
392
+ ])
393
+ const siteConfig = JSON.parse(window.atob(config))
394
+ return { siteConfig, paymentDetail }
395
+ },
396
+ fetchBonusPreview: async (context, event) => {
397
+ const { selectedCrypto, promotion } = context
398
+ return await request.send(
399
+ payment.getBonusPreview({
400
+ method: selectedCrypto.method,
401
+ promotionsNo: promotion?.activityNo,
402
+ })
403
+ )
404
+ },
405
+ fetchCalculation: async (context, event) => {
406
+ const { selectedCrypto, selectedAmount, promotion } = context
407
+ return await request.send(
408
+ payment.getDepositExpectedResult({
409
+ method: selectedCrypto.method,
410
+ amount: selectedAmount,
411
+ cryptoCurrency: selectedCrypto.channel,
412
+ promotionsNo: promotion?.activityNo,
413
+ })
414
+ )
415
+ },
416
+ submitDeposit: async (context, event) => {
417
+ const { selectedCrypto, selectedNetwork, calculation, sourceAddress, promotion } = context
418
+ return await request.send(
419
+ payment.submitDepositTransactionCrypto({
420
+ method: selectedCrypto.method,
421
+ amount: calculation.actualAmount,
422
+ cryptoCurrency: selectedCrypto.channel,
423
+ cryptoNetwork: selectedNetwork.value,
424
+ sourceAddress: sourceAddress,
425
+ promotionsNo: promotion?.activityNo,
426
+ })
427
+ )
428
+ },
429
+ },
430
+ }
431
+ )
432
+ }
@@ -0,0 +1,255 @@
1
+ import { assign, createMachine } from 'xstate'
2
+ import payment from '@jetthai/cuora/src/api/payment.js'
3
+ import { max } from '../../util/index.js'
4
+
5
+ export default function (request, context) {
6
+ const { paymentDetail = [], promotion, selectedAmount } = context || {}
7
+ return createMachine(
8
+ {
9
+ id: 'crypto_cash',
10
+ initial: 'loading',
11
+ context: {
12
+ paymentDetail,
13
+ promotion,
14
+ selectedAmount,
15
+
16
+ cryptos: [],
17
+ networks: [],
18
+ sourceAddress: '',
19
+ options: [],
20
+
21
+ selectedCrypto: null,
22
+ selectedNetwork: null,
23
+
24
+ currentCategory: 'crypto',
25
+
26
+ calculation: null,
27
+ result: null,
28
+ },
29
+ states: {
30
+ loading: {
31
+ always: [
32
+ {
33
+ target: 'debouncing',
34
+ actions: 'setInitData',
35
+ },
36
+ ],
37
+ },
38
+ loaded: {},
39
+ debouncing: {
40
+ entry: 'clearCalculation',
41
+ after: {
42
+ 1000: [
43
+ {
44
+ target: 'calculating',
45
+ cond: 'isCalculable',
46
+ },
47
+ {
48
+ target: 'loaded',
49
+ },
50
+ ],
51
+ },
52
+ },
53
+ calculating: {
54
+ invoke: {
55
+ src: 'fetchCalculation',
56
+ onDone: {
57
+ target: 'calculated',
58
+ actions: 'setCalculation',
59
+ },
60
+ onError: {
61
+ target: 'calculated',
62
+ },
63
+ },
64
+ },
65
+ calculated: {
66
+ on: {
67
+ SUBMIT: [
68
+ {
69
+ target: 'submitting',
70
+ cond: 'isSubmittable',
71
+ },
72
+ ],
73
+ },
74
+ },
75
+ submitting: {
76
+ invoke: {
77
+ src: 'submitDeposit',
78
+ onDone: {
79
+ target: 'submitted',
80
+ actions: 'setResult',
81
+ },
82
+ onError: {
83
+ target: 'loaded',
84
+ },
85
+ },
86
+ },
87
+ submitted: {},
88
+ },
89
+ on: {
90
+ SET_PROMOTION: {
91
+ target: '.debouncing',
92
+ actions: 'setPromotion',
93
+ internal: false,
94
+ },
95
+ SET_CRYPTO: {
96
+ target: '.debouncing',
97
+ actions: 'setCrypto',
98
+ internal: false,
99
+ },
100
+ SET_NETWORK: {
101
+ target: '.debouncing',
102
+ actions: 'setNetwork',
103
+ internal: false,
104
+ },
105
+ SET_ADDRESS: {
106
+ actions: 'setAddress',
107
+ internal: false,
108
+ },
109
+ SET_AMOUNT: {
110
+ target: '.debouncing',
111
+ actions: 'setAmount',
112
+ internal: false,
113
+ },
114
+ INPUT_AMOUNT: {
115
+ target: '.debouncing',
116
+ actions: 'inputAmount',
117
+ internal: false,
118
+ },
119
+ },
120
+ },
121
+ {
122
+ actions: {
123
+ setInitData: assign((context, event) => {
124
+ const { paymentDetail } = context
125
+ const cryptos = paymentDetail
126
+ .filter((element) => element.transferType === 'deposit' && element.category === context.currentCategory)
127
+ .map((crypto) => ({
128
+ ...crypto,
129
+ label: crypto.channel,
130
+ value: crypto.method,
131
+ icon: crypto.channel,
132
+ }))
133
+ const defaultNetworks = cryptos[0].networkName.map((value, i) => {
134
+ return {
135
+ label: cryptos[0].networkOptional?.[i],
136
+ value,
137
+ icon: value,
138
+ }
139
+ })
140
+ return {
141
+ cryptos,
142
+ networks: defaultNetworks,
143
+ selectedCrypto: cryptos[0],
144
+ selectedNetwork: defaultNetworks[0],
145
+ options: cryptos[0].optional,
146
+ }
147
+ }),
148
+ setCrypto: assign((context, event) => {
149
+ const { cryptos } = context
150
+ const selectedCrypto = cryptos.find((crypto) => crypto.method === event.data.value)
151
+ const networks = selectedCrypto?.networkName.map((value, i) => {
152
+ return {
153
+ label: selectedCrypto?.networkOptional?.[i],
154
+ value,
155
+ icon: value,
156
+ }
157
+ })
158
+ return {
159
+ networks,
160
+ selectedCrypto: selectedCrypto,
161
+ selectedNetwork: networks[0],
162
+ selectedAmount: null,
163
+ calculation: null,
164
+ options: selectedCrypto.optional,
165
+ }
166
+ }),
167
+ setNetwork: assign((context, event) => {
168
+ return { selectedNetwork: event.data }
169
+ }),
170
+ setAmount: assign((context, event) => {
171
+ // 現金網可自由填額度,grandTotalEnable 欄位感覺不再需要?
172
+ if (context.selectedCrypto?.grandTotalEnable === 1) {
173
+ return { selectedAmount: parseFloat(context.selectedAmount || 0) + parseFloat(event.data) }
174
+ } else {
175
+ return { selectedAmount: event.data }
176
+ }
177
+ }),
178
+ inputAmount: assign((context, event) => {
179
+ return { selectedAmount: event.data }
180
+ }),
181
+ setAddress: assign((context, event) => {
182
+ return {
183
+ sourceAddress: event.data,
184
+ }
185
+ }),
186
+ setCalculation: assign((context, event) => {
187
+ return {
188
+ calculation: event.data,
189
+ }
190
+ }),
191
+ clearCalculation: assign((context, event) => {
192
+ return {
193
+ calculation: null,
194
+ }
195
+ }),
196
+ setResult: assign((context, event) => {
197
+ return {
198
+ result: event.data,
199
+ }
200
+ }),
201
+ setPromotion: assign((context, event) => {
202
+ const { selectedCrypto, selectedAmount } = context
203
+ const promotion = event.data
204
+ return {
205
+ promotion,
206
+ selectedAmount: max(promotion.minimumAmount, selectedCrypto.minimum, selectedAmount, 0),
207
+ calculation: null,
208
+ }
209
+ }),
210
+ },
211
+ guards: {
212
+ isCalculable: (context, event) => {
213
+ const { selectedCrypto, selectedNetwork, selectedAmount } = context
214
+ return (
215
+ selectedCrypto &&
216
+ selectedNetwork &&
217
+ selectedAmount &&
218
+ selectedCrypto.minimum <= +selectedAmount &&
219
+ +selectedAmount <= selectedCrypto.maximum
220
+ )
221
+ },
222
+ isSubmittable: (context, event) => {
223
+ const { selectedCrypto, selectedNetwork, selectedAmount } = context
224
+ return selectedCrypto && selectedNetwork && selectedAmount
225
+ },
226
+ },
227
+ services: {
228
+ fetchCalculation: async (context, event) => {
229
+ const { selectedCrypto, selectedAmount, promotion } = context
230
+ return await request.send(
231
+ payment.getDepositExpectedResult({
232
+ method: selectedCrypto.method,
233
+ amount: selectedAmount,
234
+ cryptoCurrency: selectedCrypto.channel,
235
+ promotionsNo: promotion?.promotionsNo,
236
+ })
237
+ )
238
+ },
239
+ submitDeposit: async (context, event) => {
240
+ const { selectedCrypto, selectedNetwork, calculation, sourceAddress, promotion } = context
241
+ return await request.send(
242
+ payment.submitDepositTransactionCrypto({
243
+ method: selectedCrypto.method,
244
+ amount: calculation.actualAmount,
245
+ cryptoCurrency: selectedCrypto.channel,
246
+ cryptoNetwork: selectedNetwork.value,
247
+ sourceAddress: sourceAddress,
248
+ promotionsNo: promotion?.promotionsNo,
249
+ })
250
+ )
251
+ },
252
+ },
253
+ }
254
+ )
255
+ }
@@ -2,6 +2,8 @@ import manual_vn from './manual_vn.js'
2
2
  import loadcard_vn from './loadcard_vn.js'
3
3
  import qrcode_v2 from './qrcode_v2.js'
4
4
  import deposit_fc from './deposit_fc.js'
5
+ import crypto from './crypto.js'
6
+ import crypto_button from './crypto_button.js'
5
7
  import direct from './direct.js'
6
8
  import amb from './amb.js'
7
9
  import float from './float.js'
@@ -11,6 +13,7 @@ import usdt from './usdt.js'
11
13
  import manual_cash from './manual_cash.js'
12
14
  import qrcode_cash from './qrcode_cash.js'
13
15
  import usdt_cash from './usdt_cash.js'
16
+ import crypto_cash from './crypto_cash.js'
14
17
  import direct_cash from './direct_cash.js'
15
18
  import loadcard_cash from './loadcard_cash.js'
16
19
  import crypto_bank from './crypto_bank.js'
@@ -27,6 +30,8 @@ export default {
27
30
  loadcard_vn,
28
31
  qrcode_v2,
29
32
  deposit_fc,
33
+ crypto,
34
+ crypto_button,
30
35
  direct,
31
36
  amb,
32
37
  float,
@@ -36,6 +41,7 @@ export default {
36
41
  manual_cash,
37
42
  qrcode_cash,
38
43
  usdt_cash,
44
+ crypto_cash,
39
45
  direct_cash,
40
46
  loadcard_cash,
41
47
  crypto_bank,