@blocklet/payment-react 1.19.17 → 1.19.19
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.
- package/README.md +313 -0
- package/es/checkout/form.js +2 -2
- package/es/components/auto-topup/index.d.ts +14 -0
- package/es/components/auto-topup/index.js +417 -0
- package/es/components/auto-topup/modal.d.ts +35 -0
- package/es/components/auto-topup/modal.js +734 -0
- package/es/components/auto-topup/product-card.d.ts +13 -0
- package/es/components/auto-topup/product-card.js +173 -0
- package/es/components/collapse.d.ts +13 -0
- package/es/components/collapse.js +76 -0
- package/es/components/input.d.ts +2 -1
- package/es/components/input.js +64 -13
- package/es/components/label.d.ts +2 -1
- package/es/components/label.js +2 -1
- package/es/index.d.ts +4 -1
- package/es/index.js +7 -1
- package/es/libs/util.js +2 -1
- package/es/locales/en.js +56 -0
- package/es/locales/zh.js +56 -0
- package/es/payment/form/index.js +6 -0
- package/es/payment/product-item.js +19 -12
- package/lib/checkout/form.js +2 -2
- package/lib/components/auto-topup/index.d.ts +14 -0
- package/lib/components/auto-topup/index.js +451 -0
- package/lib/components/auto-topup/modal.d.ts +35 -0
- package/lib/components/auto-topup/modal.js +803 -0
- package/lib/components/auto-topup/product-card.d.ts +13 -0
- package/lib/components/auto-topup/product-card.js +149 -0
- package/lib/components/collapse.d.ts +13 -0
- package/lib/components/collapse.js +74 -0
- package/lib/components/input.d.ts +2 -1
- package/lib/components/input.js +66 -24
- package/lib/components/label.d.ts +2 -1
- package/lib/components/label.js +3 -1
- package/lib/index.d.ts +4 -1
- package/lib/index.js +24 -0
- package/lib/libs/util.js +2 -1
- package/lib/locales/en.js +56 -0
- package/lib/locales/zh.js +56 -0
- package/lib/payment/form/index.js +6 -0
- package/lib/payment/product-item.js +21 -12
- package/package.json +9 -9
- package/src/checkout/form.tsx +2 -2
- package/src/components/auto-topup/index.tsx +449 -0
- package/src/components/auto-topup/modal.tsx +773 -0
- package/src/components/auto-topup/product-card.tsx +156 -0
- package/src/components/collapse.tsx +82 -0
- package/src/components/input.tsx +71 -22
- package/src/components/label.tsx +8 -2
- package/src/index.ts +7 -0
- package/src/libs/util.ts +1 -0
- package/src/locales/en.tsx +59 -0
- package/src/locales/zh.tsx +57 -0
- package/src/payment/form/index.tsx +6 -0
- package/src/payment/product-item.tsx +20 -13
|
@@ -0,0 +1,803 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
module.exports = AutoTopup;
|
|
7
|
+
exports.waitForAutoRechargeComplete = void 0;
|
|
8
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
9
|
+
var _react = require("react");
|
|
10
|
+
var _material = require("@mui/material");
|
|
11
|
+
var _iconsMaterial = require("@mui/icons-material");
|
|
12
|
+
var _reactHookForm = require("react-hook-form");
|
|
13
|
+
var _context = require("@arcblock/ux/lib/Locale/context");
|
|
14
|
+
var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
|
|
15
|
+
var _Dialog = _interopRequireDefault(require("@arcblock/ux/lib/Dialog"));
|
|
16
|
+
var _ahooks = require("ahooks");
|
|
17
|
+
var _reactRouterDom = require("react-router-dom");
|
|
18
|
+
var _ufo = require("ufo");
|
|
19
|
+
var _pWaitFor = _interopRequireDefault(require("p-wait-for"));
|
|
20
|
+
var _DID = _interopRequireDefault(require("@arcblock/ux/lib/DID"));
|
|
21
|
+
var _switchButton = _interopRequireDefault(require("../switch-button"));
|
|
22
|
+
var _api = _interopRequireDefault(require("../../libs/api"));
|
|
23
|
+
var _util = require("../../libs/util");
|
|
24
|
+
var _payment = require("../../contexts/payment");
|
|
25
|
+
var _navigation = require("../../libs/navigation");
|
|
26
|
+
var _collapse = _interopRequireDefault(require("../collapse"));
|
|
27
|
+
var _input = _interopRequireDefault(require("../input"));
|
|
28
|
+
var _stripe = _interopRequireDefault(require("../../payment/form/stripe"));
|
|
29
|
+
var _productCard = _interopRequireDefault(require("./product-card"));
|
|
30
|
+
var _label = _interopRequireDefault(require("../label"));
|
|
31
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
32
|
+
const fetchConfig = async (customerId, currencyId) => {
|
|
33
|
+
const {
|
|
34
|
+
data
|
|
35
|
+
} = await _api.default.get(`/api/auto-recharge-configs/customer/${customerId}`, {
|
|
36
|
+
params: {
|
|
37
|
+
currency_id: currencyId
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
return data;
|
|
41
|
+
};
|
|
42
|
+
const fetchCurrencyBalance = async (currencyId, payerAddress) => {
|
|
43
|
+
const {
|
|
44
|
+
data
|
|
45
|
+
} = await _api.default.get("/api/customers/payer-token", {
|
|
46
|
+
params: {
|
|
47
|
+
currencyId,
|
|
48
|
+
payerAddress
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return data;
|
|
52
|
+
};
|
|
53
|
+
const DEFAULT_VALUES = {
|
|
54
|
+
enabled: false,
|
|
55
|
+
threshold: "100",
|
|
56
|
+
quantity: 1,
|
|
57
|
+
payment_method_id: "",
|
|
58
|
+
recharge_currency_id: "",
|
|
59
|
+
price_id: "",
|
|
60
|
+
daily_max_amount: 0,
|
|
61
|
+
daily_max_attempts: 0
|
|
62
|
+
};
|
|
63
|
+
const waitForAutoRechargeComplete = async configId => {
|
|
64
|
+
let result;
|
|
65
|
+
await (0, _pWaitFor.default)(async () => {
|
|
66
|
+
const {
|
|
67
|
+
data
|
|
68
|
+
} = await _api.default.get(`/api/auto-recharge-configs/retrieve/${configId}`);
|
|
69
|
+
result = data;
|
|
70
|
+
return !!result.payment_settings?.payment_method_options?.[result.paymentMethod.type]?.payer;
|
|
71
|
+
}, {
|
|
72
|
+
interval: 2e3,
|
|
73
|
+
timeout: 3 * 60 * 1e3
|
|
74
|
+
});
|
|
75
|
+
return result;
|
|
76
|
+
};
|
|
77
|
+
exports.waitForAutoRechargeComplete = waitForAutoRechargeComplete;
|
|
78
|
+
function PaymentMethodDisplay({
|
|
79
|
+
config,
|
|
80
|
+
onChangePaymentMethod,
|
|
81
|
+
paymentMethod,
|
|
82
|
+
currency
|
|
83
|
+
}) {
|
|
84
|
+
const {
|
|
85
|
+
t
|
|
86
|
+
} = (0, _context.useLocaleContext)();
|
|
87
|
+
const [changePaymentMethod, setChangePaymentMethod] = (0, _react.useState)(false);
|
|
88
|
+
const navigate = (0, _reactRouterDom.useNavigate)();
|
|
89
|
+
const paymentInfo = config?.payment_settings?.payment_method_options?.[paymentMethod.type || ""];
|
|
90
|
+
const {
|
|
91
|
+
data: balanceInfo,
|
|
92
|
+
loading: balanceLoading
|
|
93
|
+
} = (0, _ahooks.useRequest)(async () => {
|
|
94
|
+
if (paymentMethod.type === "stripe") {
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const result = await fetchCurrencyBalance(currency.id, paymentInfo?.payer);
|
|
98
|
+
return result;
|
|
99
|
+
}, {
|
|
100
|
+
refreshDeps: [currency.id, paymentInfo?.payer],
|
|
101
|
+
ready: !!currency.id && !!paymentInfo?.payer
|
|
102
|
+
});
|
|
103
|
+
const handleChangeToggle = () => {
|
|
104
|
+
const newChange = !changePaymentMethod;
|
|
105
|
+
setChangePaymentMethod(newChange);
|
|
106
|
+
onChangePaymentMethod(newChange);
|
|
107
|
+
};
|
|
108
|
+
if (!paymentInfo) {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const handleRecharge = e => {
|
|
112
|
+
const url = (0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/recharge/${currency.id}?rechargeAddress=${paymentInfo?.payer}`);
|
|
113
|
+
const link = (0, _navigation.createLink)(url, true);
|
|
114
|
+
(0, _navigation.handleNavigation)(e, link, navigate);
|
|
115
|
+
};
|
|
116
|
+
const renderPaymentMethodInfo = () => {
|
|
117
|
+
if (paymentMethod.type === "stripe") {
|
|
118
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
119
|
+
direction: "row",
|
|
120
|
+
spacing: 1,
|
|
121
|
+
sx: {
|
|
122
|
+
alignItems: "center"
|
|
123
|
+
},
|
|
124
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.CreditCard, {
|
|
125
|
+
fontSize: "small",
|
|
126
|
+
color: "primary"
|
|
127
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
|
|
128
|
+
variant: "body2",
|
|
129
|
+
children: ["**** **** **** ", paymentInfo?.card_last4 || "****"]
|
|
130
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
131
|
+
variant: "body2",
|
|
132
|
+
sx: {
|
|
133
|
+
color: "text.secondary",
|
|
134
|
+
textTransform: "uppercase"
|
|
135
|
+
},
|
|
136
|
+
children: paymentInfo?.card_brand || "CARD"
|
|
137
|
+
}), paymentInfo?.exp_time && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
138
|
+
variant: "body2",
|
|
139
|
+
sx: {
|
|
140
|
+
color: "text.secondary",
|
|
141
|
+
borderLeft: "1px solid",
|
|
142
|
+
borderColor: "divider",
|
|
143
|
+
pl: 1
|
|
144
|
+
},
|
|
145
|
+
children: paymentInfo?.exp_time
|
|
146
|
+
})]
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
150
|
+
spacing: 1,
|
|
151
|
+
sx: {
|
|
152
|
+
borderRadius: 1,
|
|
153
|
+
backgroundColor: theme => theme.palette.mode === "dark" ? "grey.100" : "grey.50",
|
|
154
|
+
p: 2
|
|
155
|
+
},
|
|
156
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_DID.default, {
|
|
157
|
+
did: paymentInfo?.payer,
|
|
158
|
+
responsive: false,
|
|
159
|
+
compact: true,
|
|
160
|
+
copyable: false
|
|
161
|
+
}), (balanceInfo || balanceLoading) && /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
162
|
+
direction: "row",
|
|
163
|
+
spacing: 1,
|
|
164
|
+
sx: {
|
|
165
|
+
alignItems: "center"
|
|
166
|
+
},
|
|
167
|
+
children: [balanceLoading ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.CircularProgress, {
|
|
168
|
+
size: 14,
|
|
169
|
+
sx: {
|
|
170
|
+
mr: 0.5
|
|
171
|
+
}
|
|
172
|
+
}) : /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Box, {
|
|
173
|
+
sx: {
|
|
174
|
+
display: "flex",
|
|
175
|
+
alignItems: "center",
|
|
176
|
+
gap: 1
|
|
177
|
+
},
|
|
178
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.AccountBalanceWalletOutlined, {
|
|
179
|
+
fontSize: "small",
|
|
180
|
+
sx: {
|
|
181
|
+
color: "text.lighter"
|
|
182
|
+
}
|
|
183
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
|
|
184
|
+
variant: "body2",
|
|
185
|
+
sx: {
|
|
186
|
+
color: "text.primary"
|
|
187
|
+
},
|
|
188
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)("strong", {
|
|
189
|
+
children: [(0, _util.formatBNStr)(balanceInfo?.token || "0", currency?.decimal), " "]
|
|
190
|
+
}), currency?.symbol || ""]
|
|
191
|
+
})]
|
|
192
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Button, {
|
|
193
|
+
size: "small",
|
|
194
|
+
variant: "text",
|
|
195
|
+
onClick: handleRecharge,
|
|
196
|
+
sx: {
|
|
197
|
+
fontSize: "smaller",
|
|
198
|
+
color: "primary.main"
|
|
199
|
+
},
|
|
200
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.AddOutlined, {
|
|
201
|
+
fontSize: "small"
|
|
202
|
+
}), t("payment.autoTopup.addFunds")]
|
|
203
|
+
})]
|
|
204
|
+
})]
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
|
|
208
|
+
sx: {
|
|
209
|
+
p: 2,
|
|
210
|
+
border: "1px solid",
|
|
211
|
+
borderColor: changePaymentMethod ? "primary.main" : "divider",
|
|
212
|
+
borderRadius: 1,
|
|
213
|
+
bgcolor: changePaymentMethod ? "primary.50" : "background.paper"
|
|
214
|
+
},
|
|
215
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
216
|
+
spacing: 2,
|
|
217
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
218
|
+
sx: {
|
|
219
|
+
flexDirection: {
|
|
220
|
+
xs: "column",
|
|
221
|
+
sm: "row"
|
|
222
|
+
},
|
|
223
|
+
alignItems: {
|
|
224
|
+
xs: "flex-start",
|
|
225
|
+
sm: "center"
|
|
226
|
+
},
|
|
227
|
+
justifyContent: {
|
|
228
|
+
xs: "flex-start",
|
|
229
|
+
sm: "space-between"
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
233
|
+
variant: "subtitle2",
|
|
234
|
+
sx: {
|
|
235
|
+
display: "flex",
|
|
236
|
+
alignItems: "center",
|
|
237
|
+
gap: 1,
|
|
238
|
+
whiteSpace: "nowrap"
|
|
239
|
+
},
|
|
240
|
+
children: t("payment.autoTopup.currentPaymentMethod")
|
|
241
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
|
|
242
|
+
size: "small",
|
|
243
|
+
startIcon: /* @__PURE__ */(0, _jsxRuntime.jsx)(_iconsMaterial.SwapHoriz, {}),
|
|
244
|
+
onClick: handleChangeToggle,
|
|
245
|
+
variant: "text",
|
|
246
|
+
sx: {
|
|
247
|
+
color: "primary.main",
|
|
248
|
+
whiteSpace: "nowrap",
|
|
249
|
+
alignSelf: {
|
|
250
|
+
xs: "flex-end",
|
|
251
|
+
sm: "center"
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
children: changePaymentMethod ? t("payment.autoTopup.keepCurrent") : t("payment.autoTopup.changePaymentMethod")
|
|
255
|
+
})]
|
|
256
|
+
}), changePaymentMethod ? /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
257
|
+
variant: "body2",
|
|
258
|
+
sx: {
|
|
259
|
+
color: "text.secondary"
|
|
260
|
+
},
|
|
261
|
+
children: t("payment.autoTopup.changePaymentMethodTip")
|
|
262
|
+
}) : renderPaymentMethodInfo()]
|
|
263
|
+
})
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
function AutoTopup({
|
|
267
|
+
open,
|
|
268
|
+
onClose,
|
|
269
|
+
currencyId,
|
|
270
|
+
onSuccess = () => {},
|
|
271
|
+
onError = () => {},
|
|
272
|
+
defaultEnabled = void 0
|
|
273
|
+
}) {
|
|
274
|
+
const {
|
|
275
|
+
t,
|
|
276
|
+
locale
|
|
277
|
+
} = (0, _context.useLocaleContext)();
|
|
278
|
+
const {
|
|
279
|
+
session,
|
|
280
|
+
connect,
|
|
281
|
+
settings
|
|
282
|
+
} = (0, _payment.usePaymentContext)();
|
|
283
|
+
const [changePaymentMethod, setChangePaymentMethod] = (0, _react.useState)(false);
|
|
284
|
+
const [state, setState] = (0, _ahooks.useSetState)({
|
|
285
|
+
loading: false,
|
|
286
|
+
submitting: false,
|
|
287
|
+
authorizationRequired: false,
|
|
288
|
+
stripeContext: {
|
|
289
|
+
client_secret: "",
|
|
290
|
+
intent_type: "",
|
|
291
|
+
status: "",
|
|
292
|
+
public_key: "",
|
|
293
|
+
customer: {}
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
const currencies = (0, _util.flattenPaymentMethods)(settings.paymentMethods);
|
|
297
|
+
const methods = (0, _reactHookForm.useForm)({
|
|
298
|
+
defaultValues: {
|
|
299
|
+
enabled: defaultEnabled || DEFAULT_VALUES.enabled,
|
|
300
|
+
threshold: DEFAULT_VALUES.threshold,
|
|
301
|
+
quantity: DEFAULT_VALUES.quantity,
|
|
302
|
+
payment_method_id: DEFAULT_VALUES.payment_method_id,
|
|
303
|
+
recharge_currency_id: DEFAULT_VALUES.recharge_currency_id,
|
|
304
|
+
price_id: DEFAULT_VALUES.price_id,
|
|
305
|
+
daily_limits: {
|
|
306
|
+
max_attempts: DEFAULT_VALUES.daily_max_attempts,
|
|
307
|
+
max_amount: DEFAULT_VALUES.daily_max_amount
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
const {
|
|
312
|
+
handleSubmit,
|
|
313
|
+
setValue,
|
|
314
|
+
watch
|
|
315
|
+
} = methods;
|
|
316
|
+
const enabled = watch("enabled");
|
|
317
|
+
const quantity = watch("quantity");
|
|
318
|
+
const rechargeCurrencyId = watch("recharge_currency_id");
|
|
319
|
+
const handleClose = () => {
|
|
320
|
+
setState({
|
|
321
|
+
loading: false,
|
|
322
|
+
submitting: false,
|
|
323
|
+
authorizationRequired: false
|
|
324
|
+
});
|
|
325
|
+
onClose();
|
|
326
|
+
};
|
|
327
|
+
const {
|
|
328
|
+
data: config
|
|
329
|
+
} = (0, _ahooks.useRequest)(() => fetchConfig(session?.user?.did, currencyId), {
|
|
330
|
+
refreshDeps: [session?.user?.did, currencyId],
|
|
331
|
+
ready: !!session?.user?.did && !!currencyId,
|
|
332
|
+
onError: error => {
|
|
333
|
+
_Toast.default.error((0, _util.formatError)(error));
|
|
334
|
+
},
|
|
335
|
+
onSuccess: data => {
|
|
336
|
+
setValue("enabled", defaultEnabled || data.enabled);
|
|
337
|
+
setValue("threshold", data.threshold);
|
|
338
|
+
setValue("quantity", data.quantity);
|
|
339
|
+
setValue("payment_method_id", data.payment_method_id);
|
|
340
|
+
setValue("recharge_currency_id", data.recharge_currency_id || data.price?.currency_id);
|
|
341
|
+
setValue("price_id", data.price_id);
|
|
342
|
+
setValue("daily_limits", {
|
|
343
|
+
max_amount: data.daily_limits?.max_amount || 0,
|
|
344
|
+
max_attempts: data.daily_limits?.max_attempts || 0
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
const filterCurrencies = (0, _react.useMemo)(() => {
|
|
349
|
+
return currencies.filter(c => config?.price?.currency_options?.find(o => o.currency_id === c.id));
|
|
350
|
+
}, [currencies, config]);
|
|
351
|
+
const handleConnected = async () => {
|
|
352
|
+
try {
|
|
353
|
+
const result = await waitForAutoRechargeComplete(config?.id);
|
|
354
|
+
if (result) {
|
|
355
|
+
setState({
|
|
356
|
+
submitting: false,
|
|
357
|
+
authorizationRequired: false
|
|
358
|
+
});
|
|
359
|
+
onSuccess?.(config);
|
|
360
|
+
handleClose();
|
|
361
|
+
_Toast.default.success(t("payment.autoTopup.saveSuccess"));
|
|
362
|
+
}
|
|
363
|
+
} catch (err) {
|
|
364
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
365
|
+
} finally {
|
|
366
|
+
setState({
|
|
367
|
+
submitting: false,
|
|
368
|
+
authorizationRequired: false
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
const handleDisable = async () => {
|
|
373
|
+
try {
|
|
374
|
+
const submitData = {
|
|
375
|
+
...config,
|
|
376
|
+
enabled: false
|
|
377
|
+
};
|
|
378
|
+
if (!config?.enabled) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const {
|
|
382
|
+
data
|
|
383
|
+
} = await _api.default.post("/api/auto-recharge-configs/submit", submitData);
|
|
384
|
+
onSuccess?.(data);
|
|
385
|
+
_Toast.default.success(t("payment.autoTopup.disableSuccess"));
|
|
386
|
+
} catch (error) {
|
|
387
|
+
_Toast.default.error((0, _util.formatError)(error));
|
|
388
|
+
onError?.(error);
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
const handleEnableChange = async checked => {
|
|
392
|
+
setValue("enabled", checked);
|
|
393
|
+
if (!checked) {
|
|
394
|
+
await handleDisable();
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const handleAuthorizationRequired = authData => {
|
|
398
|
+
setState({
|
|
399
|
+
authorizationRequired: true
|
|
400
|
+
});
|
|
401
|
+
if (authData.stripeContext) {
|
|
402
|
+
setState({
|
|
403
|
+
stripeContext: {
|
|
404
|
+
client_secret: authData.stripeContext.client_secret,
|
|
405
|
+
intent_type: authData.stripeContext.intent_type,
|
|
406
|
+
status: authData.stripeContext.status,
|
|
407
|
+
public_key: authData.paymentMethod.settings.stripe.publishable_key,
|
|
408
|
+
customer: authData.customer
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
} else if (authData.delegation) {
|
|
412
|
+
handleDidConnect();
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
const handleDidConnect = () => {
|
|
416
|
+
try {
|
|
417
|
+
setState({
|
|
418
|
+
submitting: true
|
|
419
|
+
});
|
|
420
|
+
connect.open({
|
|
421
|
+
containerEl: void 0,
|
|
422
|
+
saveConnect: false,
|
|
423
|
+
locale,
|
|
424
|
+
action: "auto-recharge-auth",
|
|
425
|
+
prefix: (0, _ufo.joinURL)((0, _util.getPrefix)(), "/api/did"),
|
|
426
|
+
extraParams: {
|
|
427
|
+
autoRechargeConfigId: config?.id
|
|
428
|
+
},
|
|
429
|
+
messages: {
|
|
430
|
+
scan: t("payment.autoTopup.authTip"),
|
|
431
|
+
title: t("payment.autoTopup.authTitle"),
|
|
432
|
+
confirm: t("common.connect.confirm")
|
|
433
|
+
},
|
|
434
|
+
onSuccess: async () => {
|
|
435
|
+
connect.close();
|
|
436
|
+
await handleConnected();
|
|
437
|
+
},
|
|
438
|
+
onClose: () => {
|
|
439
|
+
connect.close();
|
|
440
|
+
setState({
|
|
441
|
+
submitting: false,
|
|
442
|
+
authorizationRequired: false
|
|
443
|
+
});
|
|
444
|
+
},
|
|
445
|
+
onError: err => {
|
|
446
|
+
setState({
|
|
447
|
+
submitting: false,
|
|
448
|
+
authorizationRequired: false
|
|
449
|
+
});
|
|
450
|
+
_Toast.default.error((0, _util.formatError)(err));
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
} catch (error) {
|
|
454
|
+
setState({
|
|
455
|
+
submitting: false,
|
|
456
|
+
authorizationRequired: false
|
|
457
|
+
});
|
|
458
|
+
_Toast.default.error((0, _util.formatError)(error));
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
const handleFormSubmit = async formData => {
|
|
462
|
+
setState({
|
|
463
|
+
submitting: true
|
|
464
|
+
});
|
|
465
|
+
try {
|
|
466
|
+
const submitData = {
|
|
467
|
+
customer_id: session?.user?.did,
|
|
468
|
+
enabled: formData.enabled,
|
|
469
|
+
threshold: formData.threshold,
|
|
470
|
+
currency_id: currencyId,
|
|
471
|
+
recharge_currency_id: formData.recharge_currency_id,
|
|
472
|
+
price_id: formData.price_id,
|
|
473
|
+
quantity: formData.quantity,
|
|
474
|
+
daily_limits: {
|
|
475
|
+
max_attempts: formData.daily_limits.max_attempts || 0,
|
|
476
|
+
max_amount: formData.daily_limits.max_amount || "0"
|
|
477
|
+
},
|
|
478
|
+
change_payment_method: changePaymentMethod
|
|
479
|
+
};
|
|
480
|
+
const {
|
|
481
|
+
data
|
|
482
|
+
} = await _api.default.post("/api/auto-recharge-configs/submit", submitData);
|
|
483
|
+
if (data.balanceResult && !data.balanceResult.sufficient) {
|
|
484
|
+
await handleAuthorizationRequired({
|
|
485
|
+
...data.balanceResult,
|
|
486
|
+
paymentMethod: data.paymentMethod,
|
|
487
|
+
customer: data.customer
|
|
488
|
+
});
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
setState({
|
|
492
|
+
submitting: false,
|
|
493
|
+
authorizationRequired: false
|
|
494
|
+
});
|
|
495
|
+
onSuccess?.(data);
|
|
496
|
+
handleClose();
|
|
497
|
+
_Toast.default.success(t("payment.autoTopup.saveSuccess"));
|
|
498
|
+
} catch (error) {
|
|
499
|
+
setState({
|
|
500
|
+
submitting: false,
|
|
501
|
+
authorizationRequired: false
|
|
502
|
+
});
|
|
503
|
+
_Toast.default.error((0, _util.formatError)(error));
|
|
504
|
+
onError?.(error);
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
const onSubmit = formData => {
|
|
508
|
+
handleFormSubmit(formData);
|
|
509
|
+
};
|
|
510
|
+
const rechargeCurrency = filterCurrencies.find(c => c.id === rechargeCurrencyId);
|
|
511
|
+
const selectedMethod = settings.paymentMethods.find(method => {
|
|
512
|
+
return method.payment_currencies.find(c => c.id === rechargeCurrencyId);
|
|
513
|
+
});
|
|
514
|
+
const showStripeForm = state.authorizationRequired && selectedMethod?.type === "stripe";
|
|
515
|
+
const onStripeConfirm = async () => {
|
|
516
|
+
await handleConnected();
|
|
517
|
+
};
|
|
518
|
+
const onStripeCancel = () => {
|
|
519
|
+
setState({
|
|
520
|
+
submitting: false,
|
|
521
|
+
authorizationRequired: false
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(_Dialog.default, {
|
|
525
|
+
open,
|
|
526
|
+
onClose: handleClose,
|
|
527
|
+
maxWidth: "sm",
|
|
528
|
+
fullWidth: true,
|
|
529
|
+
className: "base-dialog",
|
|
530
|
+
title: t("payment.autoTopup.title"),
|
|
531
|
+
actions: enabled ? /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
532
|
+
direction: "row",
|
|
533
|
+
spacing: 2,
|
|
534
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Button, {
|
|
535
|
+
variant: "outlined",
|
|
536
|
+
onClick: handleClose,
|
|
537
|
+
disabled: state.submitting,
|
|
538
|
+
children: t("common.cancel")
|
|
539
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Button, {
|
|
540
|
+
variant: "contained",
|
|
541
|
+
onClick: () => handleSubmit(onSubmit)(),
|
|
542
|
+
disabled: state.loading || state.authorizationRequired || state.submitting,
|
|
543
|
+
children: [state.submitting && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.CircularProgress, {
|
|
544
|
+
size: 20,
|
|
545
|
+
sx: {
|
|
546
|
+
mr: 1
|
|
547
|
+
}
|
|
548
|
+
}), t("payment.autoTopup.saveConfiguration")]
|
|
549
|
+
})]
|
|
550
|
+
}) : null,
|
|
551
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.FormProvider, {
|
|
552
|
+
...methods,
|
|
553
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
|
|
554
|
+
component: "form",
|
|
555
|
+
onSubmit: handleSubmit(onSubmit),
|
|
556
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
557
|
+
sx: {
|
|
558
|
+
gap: 2
|
|
559
|
+
},
|
|
560
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Alert, {
|
|
561
|
+
severity: "info",
|
|
562
|
+
children: t("payment.autoTopup.tip")
|
|
563
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
564
|
+
spacing: 2,
|
|
565
|
+
direction: "row",
|
|
566
|
+
sx: {
|
|
567
|
+
alignItems: "center"
|
|
568
|
+
},
|
|
569
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
570
|
+
variant: "subtitle1",
|
|
571
|
+
children: t("payment.autoTopup.enableLabel")
|
|
572
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.FormControlLabel, {
|
|
573
|
+
control: /* @__PURE__ */(0, _jsxRuntime.jsx)(_switchButton.default, {
|
|
574
|
+
checked: enabled,
|
|
575
|
+
onChange: e => handleEnableChange(e.target.checked)
|
|
576
|
+
}),
|
|
577
|
+
label: ""
|
|
578
|
+
})]
|
|
579
|
+
}), enabled && /* @__PURE__ */(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
|
580
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
|
|
581
|
+
sx: {
|
|
582
|
+
pt: 1
|
|
583
|
+
},
|
|
584
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
585
|
+
spacing: 2.5,
|
|
586
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
587
|
+
sx: {
|
|
588
|
+
gap: 2,
|
|
589
|
+
flexDirection: {
|
|
590
|
+
xs: "column",
|
|
591
|
+
sm: "row"
|
|
592
|
+
},
|
|
593
|
+
alignItems: {
|
|
594
|
+
xs: "flex-start",
|
|
595
|
+
sm: "center"
|
|
596
|
+
},
|
|
597
|
+
justifyContent: {
|
|
598
|
+
xs: "flex-start",
|
|
599
|
+
sm: "space-between"
|
|
600
|
+
},
|
|
601
|
+
".MuiTextField-root": {
|
|
602
|
+
width: {
|
|
603
|
+
xs: "100%",
|
|
604
|
+
sm: "auto"
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_label.default, {
|
|
609
|
+
boxSx: {
|
|
610
|
+
width: "fit-content",
|
|
611
|
+
whiteSpace: "nowrap",
|
|
612
|
+
marginBottom: 0
|
|
613
|
+
},
|
|
614
|
+
children: t("payment.autoTopup.triggerThreshold")
|
|
615
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
|
|
616
|
+
name: "threshold",
|
|
617
|
+
control: methods.control,
|
|
618
|
+
rules: {
|
|
619
|
+
required: t("payment.checkout.required"),
|
|
620
|
+
min: {
|
|
621
|
+
value: 0,
|
|
622
|
+
message: t("payment.autoTopup.thresholdMinError")
|
|
623
|
+
}
|
|
624
|
+
},
|
|
625
|
+
render: ({
|
|
626
|
+
field
|
|
627
|
+
}) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.TextField, {
|
|
628
|
+
...field,
|
|
629
|
+
type: "number",
|
|
630
|
+
placeholder: t("payment.autoTopup.thresholdPlaceholder"),
|
|
631
|
+
sx: {
|
|
632
|
+
input: {
|
|
633
|
+
minWidth: 80
|
|
634
|
+
}
|
|
635
|
+
},
|
|
636
|
+
slotProps: {
|
|
637
|
+
input: {
|
|
638
|
+
endAdornment: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.InputAdornment, {
|
|
639
|
+
position: "end",
|
|
640
|
+
children: config?.currency?.name
|
|
641
|
+
})
|
|
642
|
+
},
|
|
643
|
+
htmlInput: {
|
|
644
|
+
min: 0,
|
|
645
|
+
step: 0.01
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
})
|
|
649
|
+
})]
|
|
650
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
651
|
+
sx: {
|
|
652
|
+
gap: 2,
|
|
653
|
+
flexDirection: {
|
|
654
|
+
xs: "column",
|
|
655
|
+
sm: "row"
|
|
656
|
+
},
|
|
657
|
+
alignItems: {
|
|
658
|
+
xs: "flex-start",
|
|
659
|
+
sm: "center"
|
|
660
|
+
},
|
|
661
|
+
justifyContent: {
|
|
662
|
+
xs: "flex-start",
|
|
663
|
+
sm: "space-between"
|
|
664
|
+
}
|
|
665
|
+
},
|
|
666
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_label.default, {
|
|
667
|
+
boxSx: {
|
|
668
|
+
width: "fit-content",
|
|
669
|
+
whiteSpace: "nowrap",
|
|
670
|
+
marginBottom: 0
|
|
671
|
+
},
|
|
672
|
+
children: t("payment.autoTopup.purchaseBelow")
|
|
673
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
|
|
674
|
+
name: "recharge_currency_id",
|
|
675
|
+
control: methods.control,
|
|
676
|
+
render: ({
|
|
677
|
+
field
|
|
678
|
+
}) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Select, {
|
|
679
|
+
...field,
|
|
680
|
+
value: field.value,
|
|
681
|
+
onChange: e => field.onChange(e.target.value),
|
|
682
|
+
size: "small",
|
|
683
|
+
children: filterCurrencies.map(x => /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.MenuItem, {
|
|
684
|
+
value: x?.id,
|
|
685
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
686
|
+
direction: "row",
|
|
687
|
+
sx: {
|
|
688
|
+
alignItems: "center",
|
|
689
|
+
gap: 1
|
|
690
|
+
},
|
|
691
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Avatar, {
|
|
692
|
+
src: x?.logo,
|
|
693
|
+
sx: {
|
|
694
|
+
width: 20,
|
|
695
|
+
height: 20
|
|
696
|
+
},
|
|
697
|
+
alt: x?.symbol
|
|
698
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Typography, {
|
|
699
|
+
sx: {
|
|
700
|
+
color: "text.secondary"
|
|
701
|
+
},
|
|
702
|
+
children: [x?.symbol, " (", x?.method?.name, ")"]
|
|
703
|
+
})]
|
|
704
|
+
})
|
|
705
|
+
}, x?.id))
|
|
706
|
+
})
|
|
707
|
+
})]
|
|
708
|
+
}), config?.price?.product && /* @__PURE__ */(0, _jsxRuntime.jsx)(_productCard.default, {
|
|
709
|
+
product: config.price.product,
|
|
710
|
+
price: config.price,
|
|
711
|
+
creditCurrency: config.currency,
|
|
712
|
+
currency: filterCurrencies.find(c => c.id === rechargeCurrencyId) || filterCurrencies[0],
|
|
713
|
+
quantity,
|
|
714
|
+
onQuantityChange: newQuantity => setValue("quantity", newQuantity),
|
|
715
|
+
maxQuantity: 9999,
|
|
716
|
+
minQuantity: 1
|
|
717
|
+
}), config && rechargeCurrency && /* @__PURE__ */(0, _jsxRuntime.jsx)(PaymentMethodDisplay, {
|
|
718
|
+
config,
|
|
719
|
+
onChangePaymentMethod: setChangePaymentMethod,
|
|
720
|
+
currency: rechargeCurrency,
|
|
721
|
+
paymentMethod: selectedMethod
|
|
722
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_collapse.default, {
|
|
723
|
+
trigger: t("payment.autoTopup.advanced"),
|
|
724
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
725
|
+
sx: {
|
|
726
|
+
gap: 2,
|
|
727
|
+
pl: 2,
|
|
728
|
+
pr: 1
|
|
729
|
+
},
|
|
730
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
|
|
731
|
+
name: "daily_limits.max_amount",
|
|
732
|
+
label: t("payment.autoTopup.dailyLimits.maxAmount"),
|
|
733
|
+
type: "number",
|
|
734
|
+
placeholder: t("payment.autoTopup.dailyLimits.maxAmountPlaceholder"),
|
|
735
|
+
tooltip: t("payment.autoTopup.dailyLimits.maxAmountDescription"),
|
|
736
|
+
inputProps: {
|
|
737
|
+
min: 0,
|
|
738
|
+
step: 0.01
|
|
739
|
+
},
|
|
740
|
+
slotProps: {
|
|
741
|
+
input: {
|
|
742
|
+
endAdornment: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.InputAdornment, {
|
|
743
|
+
position: "end",
|
|
744
|
+
children: filterCurrencies.find(c => c.id === rechargeCurrencyId)?.symbol || ""
|
|
745
|
+
})
|
|
746
|
+
}
|
|
747
|
+
},
|
|
748
|
+
sx: {
|
|
749
|
+
maxWidth: {
|
|
750
|
+
xs: "100%",
|
|
751
|
+
sm: "220px"
|
|
752
|
+
}
|
|
753
|
+
},
|
|
754
|
+
layout: "horizontal"
|
|
755
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
|
|
756
|
+
name: "daily_limits.max_attempts",
|
|
757
|
+
label: t("payment.autoTopup.dailyLimits.maxAttempts"),
|
|
758
|
+
type: "number",
|
|
759
|
+
placeholder: t("payment.autoTopup.dailyLimits.maxAttemptsPlaceholder"),
|
|
760
|
+
tooltip: t("payment.autoTopup.dailyLimits.maxAttemptsDescription"),
|
|
761
|
+
inputProps: {
|
|
762
|
+
min: 0,
|
|
763
|
+
step: 1
|
|
764
|
+
},
|
|
765
|
+
sx: {
|
|
766
|
+
maxWidth: {
|
|
767
|
+
xs: "100%",
|
|
768
|
+
sm: "220px"
|
|
769
|
+
}
|
|
770
|
+
},
|
|
771
|
+
layout: "horizontal"
|
|
772
|
+
})]
|
|
773
|
+
})
|
|
774
|
+
})]
|
|
775
|
+
})
|
|
776
|
+
}), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Stack, {
|
|
777
|
+
spacing: 2,
|
|
778
|
+
children: showStripeForm && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
|
|
779
|
+
sx: {
|
|
780
|
+
mt: 2
|
|
781
|
+
},
|
|
782
|
+
children: state.stripeContext && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Box, {
|
|
783
|
+
sx: {
|
|
784
|
+
mt: 2
|
|
785
|
+
},
|
|
786
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_stripe.default, {
|
|
787
|
+
clientSecret: state.stripeContext.client_secret,
|
|
788
|
+
intentType: state.stripeContext.intent_type,
|
|
789
|
+
publicKey: state.stripeContext.public_key,
|
|
790
|
+
customer: state.stripeContext.customer,
|
|
791
|
+
mode: "setup",
|
|
792
|
+
onConfirm: onStripeConfirm,
|
|
793
|
+
onCancel: onStripeCancel
|
|
794
|
+
})
|
|
795
|
+
})
|
|
796
|
+
})
|
|
797
|
+
})]
|
|
798
|
+
})]
|
|
799
|
+
})
|
|
800
|
+
})
|
|
801
|
+
})
|
|
802
|
+
});
|
|
803
|
+
}
|