@blocklet/payment-react 1.14.18 → 1.14.20

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/es/libs/util.js CHANGED
@@ -691,7 +691,10 @@ export function formatQuantityInventory(price, quantity, locale = "en") {
691
691
  return "";
692
692
  }
693
693
  export function formatAmountPrecisionLimit(amount, locale = "en", precision = 6) {
694
- const [, decimal] = amount.split(".");
694
+ if (!amount) {
695
+ return "";
696
+ }
697
+ const [, decimal] = String(amount).split(".");
695
698
  if (decimal && decimal.length > precision) {
696
699
  return t("common.amountPrecisionLimit", locale, { precision });
697
700
  }
@@ -176,7 +176,7 @@ export default function PaymentForm({
176
176
  }
177
177
  };
178
178
  const onUserLoggedIn = async () => {
179
- const { data: profile } = await api.get("/api/customers/me");
179
+ const { data: profile } = await api.get("/api/customers/me?fallback=1");
180
180
  if (profile) {
181
181
  const values = getValues();
182
182
  if (!values.customer_name) {
@@ -250,7 +250,9 @@ export default function PaymentForm({
250
250
  if (result.data.stripeContext?.status === "succeeded") {
251
251
  setState({ paying: true });
252
252
  } else {
253
- setState({ stripePaying: true });
253
+ setTimeout(() => {
254
+ setState({ stripePaying: true });
255
+ }, 200);
254
256
  }
255
257
  }
256
258
  } catch (err) {
@@ -8,7 +8,7 @@ import { styled } from "@mui/system";
8
8
  import { Elements, PaymentElement, useElements, useStripe } from "@stripe/react-stripe-js";
9
9
  import { loadStripe } from "@stripe/stripe-js";
10
10
  import { useSetState } from "ahooks";
11
- import { useEffect } from "react";
11
+ import { useEffect, useCallback } from "react";
12
12
  function StripeCheckoutForm({ clientSecret, intentType, customer, mode, onConfirm }) {
13
13
  const stripe = useStripe();
14
14
  const elements = useElements();
@@ -41,42 +41,46 @@ function StripeCheckoutForm({ clientSecret, intentType, customer, mode, onConfir
41
41
  }
42
42
  });
43
43
  }, [stripe, clientSecret]);
44
- const handleSubmit = async (e) => {
45
- e.preventDefault();
46
- if (!stripe || !elements) {
47
- return;
48
- }
49
- try {
50
- setState({ confirming: true });
51
- const method = intentType === "payment_intent" ? "confirmPayment" : "confirmSetup";
52
- const { error } = await stripe[method]({
53
- elements,
54
- redirect: "if_required",
55
- confirmParams: {
56
- payment_method_data: {
57
- billing_details: {
58
- name: customer.name,
59
- phone: customer.phone,
60
- email: customer.email,
61
- address: customer.address
44
+ const handleSubmit = useCallback(
45
+ async (e) => {
46
+ e.preventDefault();
47
+ if (!stripe || !elements) {
48
+ return;
49
+ }
50
+ try {
51
+ setState({ confirming: true });
52
+ const method = intentType === "payment_intent" ? "confirmPayment" : "confirmSetup";
53
+ const { error } = await stripe[method]({
54
+ elements,
55
+ redirect: "if_required",
56
+ confirmParams: {
57
+ payment_method_data: {
58
+ billing_details: {
59
+ name: customer.name,
60
+ phone: customer.phone,
61
+ email: customer.email,
62
+ address: customer.address
63
+ }
62
64
  }
63
65
  }
64
- }
65
- });
66
- setState({ confirming: false });
67
- if (error) {
68
- if (error.type === "validation_error") {
66
+ });
67
+ setState({ confirming: false });
68
+ if (error) {
69
+ if (error.type === "validation_error") {
70
+ return;
71
+ }
72
+ setState({ message: error.message });
69
73
  return;
70
74
  }
71
- setState({ message: error.message });
72
- return;
75
+ onConfirm();
76
+ } catch (err) {
77
+ console.error(err);
78
+ setState({ confirming: false, message: err.message });
73
79
  }
74
- onConfirm();
75
- } catch (err) {
76
- console.error(err);
77
- setState({ confirming: false, message: err.message });
78
- }
79
- };
80
+ },
81
+ [customer, intentType, stripe]
82
+ // eslint-disable-line
83
+ );
80
84
  return /* @__PURE__ */ jsxs(Content, { onSubmit: handleSubmit, children: [
81
85
  /* @__PURE__ */ jsx(
82
86
  PaymentElement,
@@ -213,7 +213,7 @@ export function PaymentInner({
213
213
  if (Number(amount) < min || Number(amount) > max) {
214
214
  return;
215
215
  }
216
- if (formatAmountPrecisionLimit(amount, locale)) {
216
+ if (amount && formatAmountPrecisionLimit(String(amount), locale)) {
217
217
  return;
218
218
  }
219
219
  try {
package/lib/libs/util.js CHANGED
@@ -827,7 +827,10 @@ function formatQuantityInventory(price, quantity, locale = "en") {
827
827
  return "";
828
828
  }
829
829
  function formatAmountPrecisionLimit(amount, locale = "en", precision = 6) {
830
- const [, decimal] = amount.split(".");
830
+ if (!amount) {
831
+ return "";
832
+ }
833
+ const [, decimal] = String(amount).split(".");
831
834
  if (decimal && decimal.length > precision) {
832
835
  return (0, _locales.t)("common.amountPrecisionLimit", locale, {
833
836
  precision
@@ -204,7 +204,7 @@ function PaymentForm({
204
204
  const onUserLoggedIn = async () => {
205
205
  const {
206
206
  data: profile
207
- } = await _api.default.get("/api/customers/me");
207
+ } = await _api.default.get("/api/customers/me?fallback=1");
208
208
  if (profile) {
209
209
  const values = getValues();
210
210
  if (!values.customer_name) {
@@ -292,9 +292,11 @@ function PaymentForm({
292
292
  paying: true
293
293
  });
294
294
  } else {
295
- setState({
296
- stripePaying: true
297
- });
295
+ setTimeout(() => {
296
+ setState({
297
+ stripePaying: true
298
+ });
299
+ }, 200);
298
300
  }
299
301
  }
300
302
  } catch (err) {
@@ -63,7 +63,7 @@ function StripeCheckoutForm({
63
63
  }
64
64
  });
65
65
  }, [stripe, clientSecret]);
66
- const handleSubmit = async e => {
66
+ const handleSubmit = (0, _react.useCallback)(async e => {
67
67
  e.preventDefault();
68
68
  if (!stripe || !elements) {
69
69
  return;
@@ -109,7 +109,10 @@ function StripeCheckoutForm({
109
109
  message: err.message
110
110
  });
111
111
  }
112
- };
112
+ }, [customer, intentType, stripe]
113
+ // eslint-disable-line
114
+ );
115
+
113
116
  return /* @__PURE__ */(0, _jsxRuntime.jsxs)(Content, {
114
117
  onSubmit: handleSubmit,
115
118
  children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_reactStripeJs.PaymentElement, {
@@ -265,7 +265,7 @@ function PaymentInner({
265
265
  if (Number(amount) < min || Number(amount) > max) {
266
266
  return;
267
267
  }
268
- if ((0, _util2.formatAmountPrecisionLimit)(amount, locale)) {
268
+ if (amount && (0, _util2.formatAmountPrecisionLimit)(String(amount), locale)) {
269
269
  return;
270
270
  }
271
271
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-react",
3
- "version": "1.14.18",
3
+ "version": "1.14.20",
4
4
  "description": "Reusable react components for payment kit v2",
5
5
  "keywords": [
6
6
  "react",
@@ -91,7 +91,7 @@
91
91
  "@babel/core": "^7.25.2",
92
92
  "@babel/preset-env": "^7.25.2",
93
93
  "@babel/preset-react": "^7.24.7",
94
- "@blocklet/payment-types": "1.14.18",
94
+ "@blocklet/payment-types": "1.14.20",
95
95
  "@storybook/addon-essentials": "^7.6.20",
96
96
  "@storybook/addon-interactions": "^7.6.20",
97
97
  "@storybook/addon-links": "^7.6.20",
@@ -120,5 +120,5 @@
120
120
  "vite-plugin-babel": "^1.2.0",
121
121
  "vite-plugin-node-polyfills": "^0.21.0"
122
122
  },
123
- "gitHead": "6be8c3dd1c744aca5607bcd6ae1745d6fbae03dd"
123
+ "gitHead": "c226ecda79fb537b2067c636410e4c7ab0f2723d"
124
124
  }
package/src/libs/util.ts CHANGED
@@ -894,7 +894,10 @@ export function formatQuantityInventory(price: TPrice, quantity: string | number
894
894
  }
895
895
 
896
896
  export function formatAmountPrecisionLimit(amount: string, locale = 'en', precision: number = 6) {
897
- const [, decimal] = amount.split('.');
897
+ if (!amount) {
898
+ return '';
899
+ }
900
+ const [, decimal] = String(amount).split('.');
898
901
  if (decimal && decimal.length > precision) {
899
902
  return t('common.amountPrecisionLimit', locale, { precision });
900
903
  }
@@ -238,7 +238,7 @@ export default function PaymentForm({
238
238
  };
239
239
 
240
240
  const onUserLoggedIn = async () => {
241
- const { data: profile } = await api.get('/api/customers/me');
241
+ const { data: profile } = await api.get('/api/customers/me?fallback=1');
242
242
  if (profile) {
243
243
  const values = getValues();
244
244
  if (!values.customer_name) {
@@ -315,7 +315,9 @@ export default function PaymentForm({
315
315
  if (result.data.stripeContext?.status === 'succeeded') {
316
316
  setState({ paying: true });
317
317
  } else {
318
- setState({ stripePaying: true });
318
+ setTimeout(() => {
319
+ setState({ stripePaying: true });
320
+ }, 200);
319
321
  }
320
322
  }
321
323
  } catch (err: any) {
@@ -8,7 +8,7 @@ import { styled } from '@mui/system';
8
8
  import { Elements, PaymentElement, useElements, useStripe } from '@stripe/react-stripe-js';
9
9
  import { loadStripe } from '@stripe/stripe-js';
10
10
  import { useSetState } from 'ahooks';
11
- import { useEffect } from 'react';
11
+ import { useEffect, useCallback } from 'react';
12
12
 
13
13
  type StripeCheckoutFormProps = {
14
14
  clientSecret: string;
@@ -57,47 +57,50 @@ function StripeCheckoutForm({ clientSecret, intentType, customer, mode, onConfir
57
57
  // eslint-disable-next-line react-hooks/exhaustive-deps
58
58
  }, [stripe, clientSecret]);
59
59
 
60
- const handleSubmit = async (e: any) => {
61
- e.preventDefault();
60
+ const handleSubmit = useCallback(
61
+ async (e: any) => {
62
+ e.preventDefault();
62
63
 
63
- if (!stripe || !elements) {
64
- return;
65
- }
64
+ if (!stripe || !elements) {
65
+ return;
66
+ }
66
67
 
67
- try {
68
- setState({ confirming: true });
69
- const method = intentType === 'payment_intent' ? 'confirmPayment' : 'confirmSetup';
70
- const { error } = await stripe[method]({
71
- elements,
72
- redirect: 'if_required',
73
- confirmParams: {
74
- payment_method_data: {
75
- billing_details: {
76
- name: customer.name,
77
- phone: customer.phone,
78
- email: customer.email,
79
- address: customer.address,
68
+ try {
69
+ setState({ confirming: true });
70
+ const method = intentType === 'payment_intent' ? 'confirmPayment' : 'confirmSetup';
71
+ const { error } = await stripe[method]({
72
+ elements,
73
+ redirect: 'if_required',
74
+ confirmParams: {
75
+ payment_method_data: {
76
+ billing_details: {
77
+ name: customer.name,
78
+ phone: customer.phone,
79
+ email: customer.email,
80
+ address: customer.address,
81
+ },
80
82
  },
81
83
  },
82
- },
83
- });
84
+ });
85
+
86
+ setState({ confirming: false });
87
+ if (error) {
88
+ if (error.type === 'validation_error') {
89
+ return;
90
+ }
84
91
 
85
- setState({ confirming: false });
86
- if (error) {
87
- if (error.type === 'validation_error') {
92
+ setState({ message: error.message as string });
88
93
  return;
89
94
  }
90
95
 
91
- setState({ message: error.message as string });
92
- return;
96
+ onConfirm();
97
+ } catch (err: any) {
98
+ console.error(err);
99
+ setState({ confirming: false, message: err.message as string });
93
100
  }
94
-
95
- onConfirm();
96
- } catch (err: any) {
97
- console.error(err);
98
- setState({ confirming: false, message: err.message as string });
99
- }
100
- };
101
+ },
102
+ [customer, intentType, stripe] // eslint-disable-line
103
+ );
101
104
 
102
105
  return (
103
106
  <Content onSubmit={handleSubmit}>
@@ -261,7 +261,7 @@ export function PaymentInner({
261
261
  return;
262
262
  }
263
263
 
264
- if (formatAmountPrecisionLimit(amount, locale)) {
264
+ if (amount && formatAmountPrecisionLimit(String(amount), locale)) {
265
265
  return;
266
266
  }
267
267
  try {