@blocklet/payment-react 1.18.20 → 1.18.22

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.
@@ -43,7 +43,8 @@ function OverdueInvoicePayment({
43
43
  detailLinkOptions = {
44
44
  enabled: true
45
45
  },
46
- successToast = true
46
+ successToast = true,
47
+ alertMessage = ""
47
48
  }) {
48
49
  const {
49
50
  t
@@ -274,19 +275,25 @@ function OverdueInvoicePayment({
274
275
  });
275
276
  }
276
277
  if (customerId) {
278
+ let title = "";
277
279
  if (summaryList.length === 1) {
278
- return t("payment.customer.overdue.title", {
280
+ title = t("payment.customer.overdue.title", {
279
281
  subscriptionCount: data.subscriptionCount || 0,
280
282
  count: data.invoices?.length,
281
283
  total: (0, _util.formatAmount)(summaryList[0]?.amount, summaryList[0]?.currency?.decimal),
282
284
  symbol: summaryList[0]?.currency?.symbol,
283
285
  method: getMethodText(summaryList[0]?.method)
284
286
  });
287
+ } else {
288
+ title = t("payment.customer.overdue.simpleTitle", {
289
+ subscriptionCount: data.subscriptionCount || 0,
290
+ count: data.invoices?.length
291
+ });
285
292
  }
286
- return t("payment.customer.overdue.simpleTitle", {
287
- subscriptionCount: data.subscriptionCount || 0,
288
- count: data.invoices?.length
289
- });
293
+ if (alertMessage) {
294
+ return `${title}${alertMessage}`;
295
+ }
296
+ return `${title}${t("payment.customer.overdue.defaultAlert")}`;
290
297
  }
291
298
  return "";
292
299
  };
@@ -442,6 +449,7 @@ OverdueInvoicePayment.defaultProps = {
442
449
  },
443
450
  subscriptionId: void 0,
444
451
  customerId: void 0,
445
- successToast: true
452
+ successToast: true,
453
+ alertMessage: ""
446
454
  };
447
455
  module.exports = OverdueInvoicePayment;
@@ -1,2 +1,9 @@
1
1
  export declare const getPhoneUtil: () => Promise<any>;
2
2
  export declare const validatePhoneNumber: (phoneNumber: string) => Promise<any>;
3
+ /**
4
+ * Format a phone number to international format
5
+ * @param phoneNumber The original phone number
6
+ * @param defaultCountry Default country code (ISO 3166-1 alpha-2)
7
+ * @returns Formatted phone number
8
+ */
9
+ export declare const formatPhone: (phoneNumber: string | undefined, defaultCountry?: string) => string;
@@ -3,13 +3,15 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.validatePhoneNumber = exports.getPhoneUtil = void 0;
6
+ exports.validatePhoneNumber = exports.getPhoneUtil = exports.formatPhone = void 0;
7
7
  let phoneUtil = null;
8
8
  const getPhoneUtil = async () => {
9
9
  if (!phoneUtil) {
10
- const {
11
- PhoneNumberUtil
12
- } = await Promise.resolve().then(() => require("google-libphonenumber"));
10
+ const result = await Promise.resolve().then(() => require("google-libphonenumber"));
11
+ const PhoneNumberUtil = (result.default || result)?.PhoneNumberUtil;
12
+ if (!PhoneNumberUtil) {
13
+ throw new Error("PhoneNumberUtil not found");
14
+ }
13
15
  phoneUtil = PhoneNumberUtil.getInstance();
14
16
  }
15
17
  return phoneUtil;
@@ -17,7 +19,13 @@ const getPhoneUtil = async () => {
17
19
  exports.getPhoneUtil = getPhoneUtil;
18
20
  const validatePhoneNumber = async phoneNumber => {
19
21
  try {
20
- const util = await getPhoneUtil();
22
+ let util = null;
23
+ try {
24
+ util = await getPhoneUtil();
25
+ } catch (err) {
26
+ const pattern = /^[+]?[(]?[0-9]{3}[)]?[-\s.]?[0-9]{3}[-\s.]?[0-9]{4,6}$/im;
27
+ return pattern.test(phoneNumber);
28
+ }
21
29
  const parsed = util.parseAndKeepRawInput(phoneNumber);
22
30
  return util.isValidNumber(parsed);
23
31
  } catch (err) {
@@ -25,4 +33,87 @@ const validatePhoneNumber = async phoneNumber => {
25
33
  return false;
26
34
  }
27
35
  };
28
- exports.validatePhoneNumber = validatePhoneNumber;
36
+ exports.validatePhoneNumber = validatePhoneNumber;
37
+ const formatPhone = (phoneNumber, defaultCountry = "US") => {
38
+ if (!phoneNumber || phoneNumber.trim() === "") {
39
+ return "";
40
+ }
41
+ const cleanedNumber = phoneNumber.replace(/[^\d+]/g, "");
42
+ if (cleanedNumber.startsWith("+")) {
43
+ return cleanedNumber;
44
+ }
45
+ const COUNTRY_CODES = {
46
+ US: "1",
47
+ // United States
48
+ CA: "1",
49
+ // Canada
50
+ CN: "86",
51
+ // China
52
+ HK: "852",
53
+ // Hong Kong
54
+ IN: "91",
55
+ // India
56
+ UK: "44",
57
+ // United Kingdom
58
+ GB: "44",
59
+ // United Kingdom
60
+ JP: "81",
61
+ // Japan
62
+ KR: "82",
63
+ // South Korea
64
+ AU: "61",
65
+ // Australia
66
+ DE: "49",
67
+ // Germany
68
+ FR: "33",
69
+ // France
70
+ IT: "39",
71
+ // Italy
72
+ ES: "34",
73
+ // Spain
74
+ BR: "55",
75
+ // Brazil
76
+ RU: "7",
77
+ // Russia
78
+ MX: "52",
79
+ // Mexico
80
+ SG: "65",
81
+ // Singapore
82
+ AE: "971"
83
+ // UAE
84
+ };
85
+
86
+ const COUNTRY_PATTERNS = [[/^1[3-9]\d{9}$/, "86"],
87
+ // China mobile: 11 digits, starts with 1
88
+ [/^\d{10}$/, "1"],
89
+ // US/Canada: 10 digits
90
+ [/^[6-9]\d{9}$/, "91"],
91
+ // India: 10 digits, starts with 6-9
92
+ [/^0[1-9]\d{8,9}$/, "81"],
93
+ // Japan: 10-11 digits, starts with 0
94
+ [/^07\d{9}$/, "44"],
95
+ // UK mobile: 11 digits, starts with 07
96
+ [/^04\d{8}$/, "61"],
97
+ // Australia mobile: 10 digits, starts with 04
98
+ [/^01[0-9]\d{7,8}$/, "82"],
99
+ // Korea mobile: 10-11 digits, starts with 01
100
+ [/^[0-9]\d{8}$/, "86"]
101
+ // China landline: 9 digits
102
+ ];
103
+
104
+ let numberToFormat = cleanedNumber;
105
+ if (numberToFormat.startsWith("0")) {
106
+ numberToFormat = numberToFormat.substring(1);
107
+ }
108
+ for (const [pattern, countryCode2] of COUNTRY_PATTERNS) {
109
+ if (pattern.test(cleanedNumber)) {
110
+ if (cleanedNumber.startsWith("0") && !["1", "86"].includes(countryCode2)) {
111
+ return `+${countryCode2}${cleanedNumber.substring(1)}`;
112
+ }
113
+ return `+${countryCode2}${cleanedNumber}`;
114
+ }
115
+ }
116
+ const countryCode = COUNTRY_CODES[defaultCountry.toUpperCase()] || defaultCountry;
117
+ return `+${countryCode}${numberToFormat}`;
118
+ };
119
+ exports.formatPhone = formatPhone;
package/lib/locales/en.js CHANGED
@@ -249,7 +249,7 @@ module.exports = (0, _flat.default)({
249
249
  warning: "Past due invoices need to be paid immediately, otherwise you can not make new purchases anymore.",
250
250
  alert: {
251
251
  title: "You have unpaid invoices",
252
- description: "Seems you have unpaid invoices from previous subscriptions, new purchases are not allowed unless you have paid all past due invoices.",
252
+ customMessage: "Please pay immediately, otherwise new purchases or subscriptions will be prohibited.",
253
253
  confirm: "Pay Now"
254
254
  },
255
255
  view: "View Due Invoices"
@@ -325,9 +325,10 @@ module.exports = (0, _flat.default)({
325
325
  owner: "Subscription Owner"
326
326
  },
327
327
  overdue: {
328
- title: "You have {count} due invoices for {subscriptionCount} subscriptions, totaling {total} {symbol}{method}. Please pay immediately to avoid service disruption.",
329
- simpleTitle: "You have {count} due invoices. Please pay now to ensure uninterrupted service.",
330
- empty: "Great! You have no due invoices."
328
+ title: "You have {count} due invoices for {subscriptionCount} subscriptions, totaling {total} {symbol}{method}. ",
329
+ simpleTitle: "You have {count} due invoices. ",
330
+ empty: "Great! You have no due invoices.",
331
+ defaultAlert: "Please pay immediately to avoid service disruption."
331
332
  }
332
333
  },
333
334
  invoice: {
package/lib/locales/zh.js CHANGED
@@ -250,6 +250,7 @@ module.exports = (0, _flat.default)({
250
250
  alert: {
251
251
  title: "\u4F60\u6709\u6B20\u8D39\u8D26\u5355",
252
252
  description: "\u770B\u8D77\u6765\u4F60\u6709\u6B20\u8D39\u7684\u8D26\u5355\uFF0C\u5728\u4F60\u652F\u4ED8\u6240\u6709\u6B20\u8D39\u8D26\u5355\u4E4B\u524D\uFF0C\u65B0\u7684\u8D2D\u4E70\u6216\u8005\u8BA2\u9605\u5C06\u88AB\u7981\u6B62\uFF0C\u8BF7\u4E0D\u8981\u8C03\u76AE\u3002",
253
+ customMessage: "\u8BF7\u7ACB\u5373\u652F\u4ED8\uFF0C\u5426\u5219\u65B0\u7684\u8D2D\u4E70\u6216\u8005\u8BA2\u9605\u5C06\u88AB\u7981\u6B62\u3002",
253
254
  confirm: "\u53BB\u652F\u4ED8"
254
255
  },
255
256
  view: "\u67E5\u770B\u6B20\u8D39\u660E\u7EC6"
@@ -325,9 +326,10 @@ module.exports = (0, _flat.default)({
325
326
  owner: "\u8BA2\u9605\u62E5\u6709\u8005"
326
327
  },
327
328
  overdue: {
328
- title: "\u60A8\u6709 {count} \u5F20\u6B20\u8D39\u8D26\u5355\uFF0C\u6D89\u53CA {subscriptionCount} \u4E2A\u8BA2\u9605\uFF0C\u603B\u91D1\u989D {total} {symbol}{method}\u3002\u8BF7\u7ACB\u5373\u652F\u4ED8\uFF0C\u4EE5\u514D\u5F71\u54CD\u60A8\u7684\u4F7F\u7528\u3002",
329
- simpleTitle: "\u60A8\u6709 {count} \u5F20\u6B20\u8D39\u8D26\u5355\uFF0C\u8BF7\u7ACB\u5373\u652F\u4ED8\uFF0C\u4EE5\u514D\u5F71\u54CD\u60A8\u7684\u4F7F\u7528\u3002",
330
- empty: "\u606D\u559C\uFF01\u60A8\u5F53\u524D\u6CA1\u6709\u6B20\u8D39\u8D26\u5355\u3002"
329
+ title: "\u60A8\u6709 {count} \u5F20\u6B20\u8D39\u8D26\u5355\uFF0C\u6D89\u53CA {subscriptionCount} \u4E2A\u8BA2\u9605\uFF0C\u603B\u91D1\u989D {total} {symbol}{method}\u3002",
330
+ simpleTitle: "\u60A8\u6709 {count} \u5F20\u6B20\u8D39\u8D26\u5355,",
331
+ empty: "\u606D\u559C\uFF01\u60A8\u5F53\u524D\u6CA1\u6709\u6B20\u8D39\u8D26\u5355\u3002",
332
+ defaultAlert: "\u8BF7\u7ACB\u5373\u652F\u4ED8\uFF0C\u4EE5\u514D\u5F71\u54CD\u60A8\u7684\u4F7F\u7528\u3002"
331
333
  }
332
334
  },
333
335
  invoice: {
@@ -28,6 +28,7 @@ var _productDonation = _interopRequireDefault(require("./product-donation"));
28
28
  var _confirm = _interopRequireDefault(require("../components/confirm"));
29
29
  var _paymentBeneficiaries = _interopRequireDefault(require("../components/payment-beneficiaries"));
30
30
  var _donation = _interopRequireDefault(require("./skeleton/donation"));
31
+ var _phoneValidator = require("../libs/phone-validator");
31
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33
  const getBenefits = async (id, url) => {
33
34
  const {
@@ -89,18 +90,18 @@ function PaymentInner({
89
90
  defaultValues: {
90
91
  customer_name: customer?.name || session?.user?.fullName || "",
91
92
  customer_email: customer?.email || session?.user?.email || "",
92
- customer_phone: customer?.phone || session?.user?.phone || "",
93
+ customer_phone: (0, _phoneValidator.formatPhone)(customer?.phone || session?.user?.phone || ""),
93
94
  payment_method: defaultMethodId,
94
95
  payment_currency: defaultCurrencyId,
95
96
  billing_address: Object.assign({
96
- country: "",
97
- state: "",
98
- city: "",
99
- line1: "",
100
- line2: "",
101
- postal_code: ""
97
+ country: session?.user?.address?.country || "",
98
+ state: session?.user?.address?.province || "",
99
+ city: session?.user?.address?.city || "",
100
+ line1: session?.user?.address?.line1 || "",
101
+ line2: session?.user?.address?.line2 || "",
102
+ postal_code: session?.user?.address?.postalCode || ""
102
103
  }, customer?.address || {}, {
103
- country: (0, _util2.isValidCountry)(customer?.address?.country || "") ? customer?.address?.country : "us"
104
+ country: (0, _util2.isValidCountry)(customer?.address?.country || session?.user?.address?.country || "") ? customer?.address?.country : "us"
104
105
  })
105
106
  }
106
107
  });
@@ -18,7 +18,6 @@ var _ufo = require("ufo");
18
18
  var _useBus = require("use-bus");
19
19
  var _isEmail = _interopRequireDefault(require("validator/es/lib/isEmail"));
20
20
  var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
21
- var _confirm = _interopRequireDefault(require("../../components/confirm"));
22
21
  var _input = _interopRequireDefault(require("../../components/input"));
23
22
  var _payment = require("../../contexts/payment");
24
23
  var _subscription = require("../../hooks/subscription");
@@ -31,6 +30,7 @@ var _stripe = _interopRequireDefault(require("./stripe"));
31
30
  var _mobile = require("../../hooks/mobile");
32
31
  var _phoneValidator = require("../../libs/phone-validator");
33
32
  var _loadingButton = _interopRequireDefault(require("../../components/loading-button"));
33
+ var _overDueInvoicePayment = _interopRequireDefault(require("../../components/over-due-invoice-payment"));
34
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
35
35
  const waitForCheckoutComplete = async sessionId => {
36
36
  let result;
@@ -58,6 +58,36 @@ const hasDidWallet = user => {
58
58
  return connected.some(x => x.provider === "wallet");
59
59
  };
60
60
  exports.hasDidWallet = hasDidWallet;
61
+ const setUserFormValues = (userInfo, currentValues, setValue, options = {}) => {
62
+ const {
63
+ preferExisting = true
64
+ } = options;
65
+ const basicFields = {
66
+ customer_name: userInfo.name || userInfo.fullName,
67
+ customer_email: userInfo.email,
68
+ customer_phone: (0, _phoneValidator.formatPhone)(userInfo.phone)
69
+ };
70
+ const addressFields = {
71
+ "billing_address.state": userInfo.address?.state || userInfo.address?.province,
72
+ "billing_address.line1": userInfo.address?.line1,
73
+ "billing_address.line2": userInfo.address?.line2,
74
+ "billing_address.city": userInfo.address?.city,
75
+ "billing_address.postal_code": userInfo.address?.postal_code || userInfo.address?.postalCode,
76
+ "billing_address.country": userInfo.address?.country
77
+ };
78
+ if (options.showPhone) {
79
+ addressFields["billing_address.country"] = userInfo.metadata?.phone?.country || userInfo.address?.country;
80
+ }
81
+ const allFields = {
82
+ ...addressFields,
83
+ ...basicFields
84
+ };
85
+ Object.entries(allFields).forEach(([field, value]) => {
86
+ if (!preferExisting || !currentValues[field.split(".")[0]]) {
87
+ setValue(field, value);
88
+ }
89
+ });
90
+ };
61
91
  PaymentForm.defaultProps = {
62
92
  onlyShowBtn: false,
63
93
  isDonation: false
@@ -148,22 +178,28 @@ function PaymentForm({
148
178
  (0, _react.useEffect)(() => {
149
179
  if (session?.user) {
150
180
  const values = getValues();
151
- if (!values.customer_name) {
152
- setValue("customer_name", session.user.fullName);
153
- }
154
- if (!values.customer_email) {
155
- setValue("customer_email", session.user.email);
156
- }
157
- if (!values.customer_phone) {
158
- setValue("customer_phone", session.user.phone);
159
- }
160
- }
161
- if (!session?.user) {
162
- setValue("customer_name", "");
163
- setValue("customer_email", "");
164
- setValue("customer_phone", "");
181
+ setUserFormValues(session.user, values, setValue, {
182
+ preferExisting: false,
183
+ showPhone: checkoutSession.phone_number_collection?.enabled
184
+ });
185
+ } else {
186
+ setUserFormValues({
187
+ name: "",
188
+ email: "",
189
+ phone: "",
190
+ address: {
191
+ state: "",
192
+ line1: "",
193
+ line2: "",
194
+ city: "",
195
+ postal_code: ""
196
+ }
197
+ }, {}, setValue, {
198
+ preferExisting: false,
199
+ showPhone: checkoutSession.phone_number_collection?.enabled
200
+ });
165
201
  }
166
- }, [session?.user, getValues, setValue]);
202
+ }, [session?.user, getValues, setValue, checkoutSession.phone_number_collection?.enabled]);
167
203
  (0, _react.useEffect)(() => {
168
204
  setValue("payment_method", currencies[paymentCurrencyIndex]?.method?.id);
169
205
  setValue("payment_currency", currencies[paymentCurrencyIndex]?.id);
@@ -199,7 +235,7 @@ function PaymentForm({
199
235
  });
200
236
  const method = paymentMethods.find(x => x.id === paymentMethod);
201
237
  const isDonationMode = checkoutSession?.submit_type === "donate" && isDonation;
202
- const showForm = session?.user;
238
+ const showForm = !!session?.user;
203
239
  const skipBindWallet = method.type === "stripe";
204
240
  const handleConnected = async () => {
205
241
  try {
@@ -232,33 +268,9 @@ function PaymentForm({
232
268
  } = await _api.default.get("/api/customers/me?fallback=1");
233
269
  if (profile) {
234
270
  const values = getValues();
235
- if (!values.customer_name) {
236
- setValue("customer_name", profile.name);
237
- }
238
- if (!values.customer_email) {
239
- setValue("customer_email", profile.email);
240
- }
241
- if (!values.customer_phone) {
242
- setValue("customer_phone", profile.phone);
243
- }
244
- if (profile.address?.country) {
245
- setValue("billing_address.country", profile.address.country);
246
- }
247
- if (profile.address?.state) {
248
- setValue("billing_address.state", profile.address.state);
249
- }
250
- if (profile.address?.line1) {
251
- setValue("billing_address.line1", profile.address.line1);
252
- }
253
- if (profile.address?.line2) {
254
- setValue("billing_address.line2", profile.address.line2);
255
- }
256
- if (profile.address?.city) {
257
- setValue("billing_address.city", profile.address.city);
258
- }
259
- if (profile.address?.postal_code) {
260
- setValue("billing_address.postal_code", profile.address.postal_code);
261
- }
271
+ setUserFormValues(profile, values, setValue, {
272
+ showPhone: checkoutSession.phone_number_collection?.enabled
273
+ });
262
274
  }
263
275
  };
264
276
  const onFormSubmit = async data => {
@@ -445,15 +457,31 @@ function PaymentForm({
445
457
  }
446
458
  }), state.submitting || state.paying ? t("payment.checkout.processing") : buttonText]
447
459
  })
448
- }), state.customerLimited && /* @__PURE__ */(0, _jsxRuntime.jsx)(_confirm.default, {
449
- onConfirm: () => window.open((0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/invoice/past-due?referer=${encodeURIComponent(window.location.href)}`), "_self"),
450
- onCancel: () => setState({
451
- customerLimited: false
452
- }),
453
- confirm: t("payment.customer.pastDue.alert.confirm"),
454
- title: t("payment.customer.pastDue.alert.title"),
455
- message: t("payment.customer.pastDue.alert.description"),
456
- color: "primary"
460
+ }), state.customerLimited && /* @__PURE__ */(0, _jsxRuntime.jsx)(_overDueInvoicePayment.default, {
461
+ customerId: customer?.id || session?.user?.did,
462
+ onPaid: () => {
463
+ setState({
464
+ customerLimited: false
465
+ });
466
+ onAction();
467
+ },
468
+ alertMessage: t("payment.customer.pastDue.alert.customMessage"),
469
+ detailLinkOptions: {
470
+ enabled: true,
471
+ onClick: () => {
472
+ setState({
473
+ customerLimited: false
474
+ });
475
+ window.open((0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/invoice/past-due?referer=${encodeURIComponent(window.location.href)}`), "_self");
476
+ }
477
+ },
478
+ dialogProps: {
479
+ open: state.customerLimited,
480
+ onClose: () => setState({
481
+ customerLimited: false
482
+ }),
483
+ title: t("payment.customer.pastDue.alert.title")
484
+ }
457
485
  })]
458
486
  });
459
487
  }
@@ -600,15 +628,31 @@ function PaymentForm({
600
628
  })
601
629
  })]
602
630
  })
603
- }), state.customerLimited && /* @__PURE__ */(0, _jsxRuntime.jsx)(_confirm.default, {
604
- onConfirm: () => window.open((0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/invoice/past-due?referer=${encodeURIComponent(window.location.href)}`), "_self"),
605
- onCancel: () => setState({
606
- customerLimited: false
607
- }),
608
- confirm: t("payment.customer.pastDue.alert.confirm"),
609
- title: t("payment.customer.pastDue.alert.title"),
610
- message: t("payment.customer.pastDue.alert.description"),
611
- color: "primary"
631
+ }), state.customerLimited && /* @__PURE__ */(0, _jsxRuntime.jsx)(_overDueInvoicePayment.default, {
632
+ customerId: customer?.id || session?.user?.didssion?.user?.did,
633
+ onPaid: () => {
634
+ setState({
635
+ customerLimited: false
636
+ });
637
+ onAction();
638
+ },
639
+ alertMessage: t("payment.customer.pastDue.alert.customMessage"),
640
+ detailLinkOptions: {
641
+ enabled: true,
642
+ onClick: () => {
643
+ setState({
644
+ customerLimited: false
645
+ });
646
+ window.open((0, _ufo.joinURL)((0, _util.getPrefix)(), `/customer/invoice/past-due?referer=${encodeURIComponent(window.location.href)}`), "_self");
647
+ }
648
+ },
649
+ dialogProps: {
650
+ open: state.customerLimited,
651
+ onClose: () => setState({
652
+ customerLimited: false
653
+ }),
654
+ title: t("payment.customer.pastDue.alert.title")
655
+ }
612
656
  })]
613
657
  });
614
658
  }
@@ -24,6 +24,18 @@ function PhoneInput({
24
24
  setValue
25
25
  } = (0, _reactHookForm.useFormContext)();
26
26
  const values = getValues();
27
+ const isUpdatingRef = (0, _react.useRef)(false);
28
+ const safeUpdate = (0, _react.useCallback)(callback => {
29
+ if (isUpdatingRef.current) return;
30
+ try {
31
+ isUpdatingRef.current = true;
32
+ callback();
33
+ } finally {
34
+ requestAnimationFrame(() => {
35
+ isUpdatingRef.current = false;
36
+ });
37
+ }
38
+ }, []);
27
39
  const {
28
40
  phone,
29
41
  handlePhoneValueChange,
@@ -35,8 +47,10 @@ function PhoneInput({
35
47
  value: values[props.name] || "",
36
48
  countries: _reactInternationalPhone.defaultCountries,
37
49
  onChange: data => {
38
- setValue(props.name, data.phone);
39
- setValue(countryFieldName, data.country);
50
+ safeUpdate(() => {
51
+ setValue(props.name, data.phone);
52
+ setValue(countryFieldName, data.country);
53
+ });
40
54
  }
41
55
  });
42
56
  const userCountry = (0, _reactHookForm.useWatch)({
@@ -44,14 +58,16 @@ function PhoneInput({
44
58
  name: countryFieldName
45
59
  });
46
60
  (0, _react.useEffect)(() => {
47
- if (userCountry !== country) {
61
+ if (!userCountry || userCountry === country) return;
62
+ safeUpdate(() => {
48
63
  setCountry(userCountry);
49
- }
50
- }, [userCountry]);
51
- const onCountryChange = v => {
52
- setCountry(v);
53
- setValue(countryFieldName, v);
54
- };
64
+ });
65
+ }, [userCountry, country, setCountry, safeUpdate]);
66
+ const onCountryChange = (0, _react.useCallback)(v => {
67
+ safeUpdate(() => {
68
+ setCountry(v);
69
+ });
70
+ }, [setCountry, safeUpdate]);
55
71
  return (
56
72
  // @ts-ignore
57
73
  /* @__PURE__ */
@@ -28,6 +28,7 @@ var _payment2 = _interopRequireDefault(require("./skeleton/payment"));
28
28
  var _success = _interopRequireDefault(require("./success"));
29
29
  var _summary = _interopRequireDefault(require("./summary"));
30
30
  var _mobile = require("../hooks/mobile");
31
+ var _phoneValidator = require("../libs/phone-validator");
31
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33
  PaymentInner.defaultProps = {
33
34
  completed: false,
@@ -68,18 +69,18 @@ function PaymentInner({
68
69
  defaultValues: {
69
70
  customer_name: customer?.name || session?.user?.fullName || "",
70
71
  customer_email: customer?.email || session?.user?.email || "",
71
- customer_phone: customer?.phone || session?.user?.phone || "",
72
+ customer_phone: (0, _phoneValidator.formatPhone)(customer?.phone || session?.user?.phone || ""),
72
73
  payment_method: defaultMethodId,
73
74
  payment_currency: defaultCurrencyId,
74
75
  billing_address: Object.assign({
75
- country: "",
76
- state: "",
77
- city: "",
78
- line1: "",
79
- line2: "",
80
- postal_code: ""
76
+ country: session?.user?.address?.country || "",
77
+ state: session?.user?.address?.province || "",
78
+ city: session?.user?.address?.city || "",
79
+ line1: session?.user?.address?.line1 || "",
80
+ line2: session?.user?.address?.line2 || "",
81
+ postal_code: session?.user?.address?.postalCode || ""
81
82
  }, customer?.address || {}, {
82
- country: (0, _util2.isValidCountry)(customer?.address?.country || "") ? customer?.address?.country : "us"
83
+ country: (0, _util2.isValidCountry)(customer?.address?.country || session?.user?.address?.country || "") ? customer?.address?.country : "us"
83
84
  })
84
85
  }
85
86
  });
@@ -47,9 +47,6 @@ function ProductDonation({
47
47
  }
48
48
  };
49
49
  const getDefaultPreset = () => {
50
- if (settings?.amount?.preset) {
51
- return formatAmount(settings.amount.preset);
52
- }
53
50
  try {
54
51
  const savedPreset = localStorage.getItem(getUserStorageKey(DONATION_PRESET_KEY_BASE));
55
52
  if (savedPreset) {
@@ -65,7 +62,13 @@ function ProductDonation({
65
62
  }
66
63
  if (presets.length > 0) {
67
64
  const middleIndex = Math.floor(presets.length / 2);
68
- return presets[middleIndex] || presets[0];
65
+ return presets[middleIndex];
66
+ }
67
+ if (settings?.amount?.preset) {
68
+ return formatAmount(settings.amount.preset);
69
+ }
70
+ if (presets.length > 0) {
71
+ return presets[0];
69
72
  }
70
73
  return "0";
71
74
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-react",
3
- "version": "1.18.20",
3
+ "version": "1.18.22",
4
4
  "description": "Reusable react components for payment kit v2",
5
5
  "keywords": [
6
6
  "react",
@@ -54,10 +54,10 @@
54
54
  }
55
55
  },
56
56
  "dependencies": {
57
- "@arcblock/did-connect": "^2.12.36",
58
- "@arcblock/ux": "^2.12.36",
57
+ "@arcblock/did-connect": "^2.12.43",
58
+ "@arcblock/ux": "^2.12.43",
59
59
  "@arcblock/ws": "^1.19.15",
60
- "@blocklet/ui-react": "^2.12.36",
60
+ "@blocklet/ui-react": "^2.12.43",
61
61
  "@mui/icons-material": "^5.16.6",
62
62
  "@mui/lab": "^5.0.0-alpha.173",
63
63
  "@mui/material": "^5.16.6",
@@ -93,7 +93,7 @@
93
93
  "@babel/core": "^7.25.2",
94
94
  "@babel/preset-env": "^7.25.2",
95
95
  "@babel/preset-react": "^7.24.7",
96
- "@blocklet/payment-types": "1.18.20",
96
+ "@blocklet/payment-types": "1.18.22",
97
97
  "@storybook/addon-essentials": "^7.6.20",
98
98
  "@storybook/addon-interactions": "^7.6.20",
99
99
  "@storybook/addon-links": "^7.6.20",
@@ -124,5 +124,5 @@
124
124
  "vite-plugin-babel": "^1.2.0",
125
125
  "vite-plugin-node-polyfills": "^0.21.0"
126
126
  },
127
- "gitHead": "f71a0013f704d58e4c04ebfcc5a35480a0942bcc"
127
+ "gitHead": "844883f56da79c1222fc5aa1b445abb2859e3230"
128
128
  }