@blocklet/payment-react 1.15.26 → 1.15.28
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/components/lazy-loader.d.ts +13 -0
- package/es/components/lazy-loader.js +44 -0
- package/es/index.d.ts +2 -1
- package/es/index.js +3 -1
- package/es/payment/form/index.js +1 -1
- package/es/payment/form/{stripe.d.ts → stripe/form.d.ts} +9 -1
- package/es/payment/form/{stripe.js → stripe/form.js} +1 -2
- package/es/payment/form/stripe/index.d.ts +15 -0
- package/es/payment/form/stripe/index.js +13 -0
- package/lib/components/lazy-loader.d.ts +13 -0
- package/lib/components/lazy-loader.js +57 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +8 -0
- package/lib/payment/form/{stripe.d.ts → stripe/form.d.ts} +9 -1
- package/lib/payment/form/{stripe.js → stripe/form.js} +12 -7
- package/lib/payment/form/stripe/index.d.ts +15 -0
- package/lib/payment/form/stripe/index.js +21 -0
- package/package.json +3 -3
- package/src/components/lazy-loader.tsx +64 -0
- package/src/index.ts +2 -0
- package/src/payment/form/{stripe.tsx → stripe/form.tsx} +4 -5
- package/src/payment/form/stripe/index.tsx +29 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type ComponentType<P = any> = React.ComponentType<P>;
|
|
3
|
+
type LoaderFunction<T extends ComponentType> = () => Promise<{
|
|
4
|
+
default: T;
|
|
5
|
+
} | T>;
|
|
6
|
+
interface LazyComponentOptions {
|
|
7
|
+
LoadingComponent?: React.ComponentType;
|
|
8
|
+
ErrorComponent?: React.ComponentType<{
|
|
9
|
+
error: Error;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
export declare function createLazyComponent<T extends ComponentType, P extends React.ComponentProps<T>>(loader: LoaderFunction<T>, options?: LazyComponentOptions): (props: P) => JSX.Element;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState, useEffect } from "react";
|
|
3
|
+
import Center from "@arcblock/ux/lib/Center";
|
|
4
|
+
import { CircularProgress } from "@mui/material";
|
|
5
|
+
export function createLazyComponent(loader, options = {}) {
|
|
6
|
+
let loadPromise = null;
|
|
7
|
+
const {
|
|
8
|
+
LoadingComponent = () => /* @__PURE__ */ jsx(Center, { children: /* @__PURE__ */ jsx(CircularProgress, {}) }),
|
|
9
|
+
ErrorComponent = ({ error }) => /* @__PURE__ */ jsxs("div", { children: [
|
|
10
|
+
"Error: ",
|
|
11
|
+
error.message
|
|
12
|
+
] })
|
|
13
|
+
} = options;
|
|
14
|
+
const loadComponent = async () => {
|
|
15
|
+
if (loadPromise)
|
|
16
|
+
return loadPromise;
|
|
17
|
+
try {
|
|
18
|
+
loadPromise = loader().then((result) => "default" in result ? result.default : result);
|
|
19
|
+
return await loadPromise;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
loadPromise = null;
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
return function WrappedComponent(props) {
|
|
26
|
+
const [Component, setComponent] = useState(null);
|
|
27
|
+
const [error, setError] = useState(null);
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
loadComponent().then((comp) => {
|
|
30
|
+
setComponent(() => comp);
|
|
31
|
+
}).catch((err) => {
|
|
32
|
+
setError(err);
|
|
33
|
+
console.error("Failed to load component:", err);
|
|
34
|
+
});
|
|
35
|
+
}, []);
|
|
36
|
+
if (error) {
|
|
37
|
+
return /* @__PURE__ */ jsx(ErrorComponent, { error });
|
|
38
|
+
}
|
|
39
|
+
if (!Component) {
|
|
40
|
+
return /* @__PURE__ */ jsx(LoadingComponent, {});
|
|
41
|
+
}
|
|
42
|
+
return /* @__PURE__ */ jsx(Component, { ...props });
|
|
43
|
+
};
|
|
44
|
+
}
|
package/es/index.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ import PricingItem from './components/pricing-item';
|
|
|
27
27
|
import CountrySelect from './components/country-select';
|
|
28
28
|
import TruncatedText from './components/truncated-text';
|
|
29
29
|
import Link from './components/link';
|
|
30
|
+
import { createLazyComponent } from './components/lazy-loader';
|
|
30
31
|
export { PaymentThemeProvider } from './theme';
|
|
31
32
|
export * from './libs/util';
|
|
32
33
|
export * from './libs/connect';
|
|
@@ -36,4 +37,4 @@ export * from './hooks/subscription';
|
|
|
36
37
|
export * from './hooks/mobile';
|
|
37
38
|
export * from './hooks/table';
|
|
38
39
|
export { translations, createTranslator } from './locales';
|
|
39
|
-
export { api, dayjs, FormInput, PhoneInput, AddressForm, StripeForm, Status, Livemode, Switch, ConfirmDialog, CheckoutForm, CheckoutTable, CheckoutDonate, CurrencySelector, Payment, PaymentSummary, PricingTable, ProductSkeleton, Amount, CustomerInvoiceList, CustomerPaymentList, TxLink, TxGas, SafeGuard, PricingItem, CountrySelect, Table, TruncatedText, Link, };
|
|
40
|
+
export { createLazyComponent, api, dayjs, FormInput, PhoneInput, AddressForm, StripeForm, Status, Livemode, Switch, ConfirmDialog, CheckoutForm, CheckoutTable, CheckoutDonate, CurrencySelector, Payment, PaymentSummary, PricingTable, ProductSkeleton, Amount, CustomerInvoiceList, CustomerPaymentList, TxLink, TxGas, SafeGuard, PricingItem, CountrySelect, Table, TruncatedText, Link, };
|
package/es/index.js
CHANGED
|
@@ -19,7 +19,7 @@ import Amount from "./payment/amount.js";
|
|
|
19
19
|
import AddressForm from "./payment/form/address.js";
|
|
20
20
|
import CurrencySelector from "./payment/form/currency.js";
|
|
21
21
|
import PhoneInput from "./payment/form/phone.js";
|
|
22
|
-
import StripeForm from "./payment/form/stripe.js";
|
|
22
|
+
import StripeForm from "./payment/form/stripe/index.js";
|
|
23
23
|
import Payment from "./payment/index.js";
|
|
24
24
|
import ProductSkeleton from "./payment/product-skeleton.js";
|
|
25
25
|
import PaymentSummary from "./payment/summary.js";
|
|
@@ -27,6 +27,7 @@ import PricingItem from "./components/pricing-item.js";
|
|
|
27
27
|
import CountrySelect from "./components/country-select.js";
|
|
28
28
|
import TruncatedText from "./components/truncated-text.js";
|
|
29
29
|
import Link from "./components/link.js";
|
|
30
|
+
import { createLazyComponent } from "./components/lazy-loader.js";
|
|
30
31
|
export { PaymentThemeProvider } from "./theme/index.js";
|
|
31
32
|
export * from "./libs/util.js";
|
|
32
33
|
export * from "./libs/connect.js";
|
|
@@ -37,6 +38,7 @@ export * from "./hooks/mobile.js";
|
|
|
37
38
|
export * from "./hooks/table.js";
|
|
38
39
|
export { translations, createTranslator } from "./locales/index.js";
|
|
39
40
|
export {
|
|
41
|
+
createLazyComponent,
|
|
40
42
|
api,
|
|
41
43
|
dayjs,
|
|
42
44
|
FormInput,
|
package/es/payment/form/index.js
CHANGED
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
import AddressForm from "./address.js";
|
|
29
29
|
import CurrencySelector from "./currency.js";
|
|
30
30
|
import PhoneInput from "./phone.js";
|
|
31
|
-
import StripeCheckout from "./stripe.js";
|
|
31
|
+
import StripeCheckout from "./stripe/index.js";
|
|
32
32
|
import { useMobile } from "../../hooks/mobile.js";
|
|
33
33
|
import { validatePhoneNumber } from "../../libs/phone-validator.js";
|
|
34
34
|
const waitForCheckoutComplete = async (sessionId) => {
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import type { TCustomer } from '@blocklet/payment-types';
|
|
3
|
-
type
|
|
3
|
+
export type StripeCheckoutFormProps = {
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
intentType: string;
|
|
6
|
+
customer: TCustomer;
|
|
7
|
+
mode: string;
|
|
8
|
+
onConfirm: Function;
|
|
9
|
+
returnUrl: string;
|
|
10
|
+
};
|
|
11
|
+
export type StripeCheckoutProps = {
|
|
4
12
|
clientSecret: string;
|
|
5
13
|
intentType: string;
|
|
6
14
|
publicKey: string;
|
|
@@ -5,10 +5,9 @@ import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
|
|
|
5
5
|
import { LoadingButton } from "@mui/lab";
|
|
6
6
|
import { CircularProgress, Typography } from "@mui/material";
|
|
7
7
|
import { styled } from "@mui/system";
|
|
8
|
-
import { Elements, PaymentElement, useElements, useStripe } from "@stripe/react-stripe-js";
|
|
9
|
-
import { loadStripe } from "@stripe/stripe-js";
|
|
10
8
|
import { useSetState } from "ahooks";
|
|
11
9
|
import { useEffect, useCallback } from "react";
|
|
10
|
+
const { Elements, PaymentElement, useElements, useStripe, loadStripe } = globalThis.__STRIPE_COMPONENTS__;
|
|
12
11
|
function StripeCheckoutForm({
|
|
13
12
|
clientSecret,
|
|
14
13
|
intentType,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { StripeCheckoutProps } from './form';
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
__STRIPE_COMPONENTS__?: {
|
|
6
|
+
Elements: any;
|
|
7
|
+
PaymentElement: any;
|
|
8
|
+
useElements: () => any;
|
|
9
|
+
useStripe: () => any;
|
|
10
|
+
loadStripe: (key: string) => Promise<any>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
declare const _default: (props: StripeCheckoutProps) => JSX.Element;
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createLazyComponent } from "../../../components/lazy-loader.js";
|
|
2
|
+
export default createLazyComponent(async () => {
|
|
3
|
+
const [stripeReact, stripe] = await Promise.all([import("@stripe/react-stripe-js"), import("@stripe/stripe-js")]);
|
|
4
|
+
window.__STRIPE_COMPONENTS__ = {
|
|
5
|
+
Elements: stripeReact.Elements,
|
|
6
|
+
PaymentElement: stripeReact.PaymentElement,
|
|
7
|
+
useElements: stripeReact.useElements,
|
|
8
|
+
useStripe: stripeReact.useStripe,
|
|
9
|
+
loadStripe: stripe.loadStripe
|
|
10
|
+
};
|
|
11
|
+
const { default: Component } = await import("./form.js");
|
|
12
|
+
return Component;
|
|
13
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
type ComponentType<P = any> = React.ComponentType<P>;
|
|
3
|
+
type LoaderFunction<T extends ComponentType> = () => Promise<{
|
|
4
|
+
default: T;
|
|
5
|
+
} | T>;
|
|
6
|
+
interface LazyComponentOptions {
|
|
7
|
+
LoadingComponent?: React.ComponentType;
|
|
8
|
+
ErrorComponent?: React.ComponentType<{
|
|
9
|
+
error: Error;
|
|
10
|
+
}>;
|
|
11
|
+
}
|
|
12
|
+
export declare function createLazyComponent<T extends ComponentType, P extends React.ComponentProps<T>>(loader: LoaderFunction<T>, options?: LazyComponentOptions): (props: P) => JSX.Element;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createLazyComponent = createLazyComponent;
|
|
7
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
var _Center = _interopRequireDefault(require("@arcblock/ux/lib/Center"));
|
|
10
|
+
var _material = require("@mui/material");
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
function createLazyComponent(loader, options = {}) {
|
|
13
|
+
let loadPromise = null;
|
|
14
|
+
const {
|
|
15
|
+
LoadingComponent = () => /* @__PURE__ */(0, _jsxRuntime.jsx)(_Center.default, {
|
|
16
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.CircularProgress, {})
|
|
17
|
+
}),
|
|
18
|
+
ErrorComponent = ({
|
|
19
|
+
error
|
|
20
|
+
}) => /* @__PURE__ */(0, _jsxRuntime.jsxs)("div", {
|
|
21
|
+
children: ["Error: ", error.message]
|
|
22
|
+
})
|
|
23
|
+
} = options;
|
|
24
|
+
const loadComponent = async () => {
|
|
25
|
+
if (loadPromise) return loadPromise;
|
|
26
|
+
try {
|
|
27
|
+
loadPromise = loader().then(result => "default" in result ? result.default : result);
|
|
28
|
+
return await loadPromise;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
loadPromise = null;
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return function WrappedComponent(props) {
|
|
35
|
+
const [Component, setComponent] = (0, _react.useState)(null);
|
|
36
|
+
const [error, setError] = (0, _react.useState)(null);
|
|
37
|
+
(0, _react.useEffect)(() => {
|
|
38
|
+
loadComponent().then(comp => {
|
|
39
|
+
setComponent(() => comp);
|
|
40
|
+
}).catch(err => {
|
|
41
|
+
setError(err);
|
|
42
|
+
console.error("Failed to load component:", err);
|
|
43
|
+
});
|
|
44
|
+
}, []);
|
|
45
|
+
if (error) {
|
|
46
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(ErrorComponent, {
|
|
47
|
+
error
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (!Component) {
|
|
51
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(LoadingComponent, {});
|
|
52
|
+
}
|
|
53
|
+
return /* @__PURE__ */(0, _jsxRuntime.jsx)(Component, {
|
|
54
|
+
...props
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ import PricingItem from './components/pricing-item';
|
|
|
27
27
|
import CountrySelect from './components/country-select';
|
|
28
28
|
import TruncatedText from './components/truncated-text';
|
|
29
29
|
import Link from './components/link';
|
|
30
|
+
import { createLazyComponent } from './components/lazy-loader';
|
|
30
31
|
export { PaymentThemeProvider } from './theme';
|
|
31
32
|
export * from './libs/util';
|
|
32
33
|
export * from './libs/connect';
|
|
@@ -36,4 +37,4 @@ export * from './hooks/subscription';
|
|
|
36
37
|
export * from './hooks/mobile';
|
|
37
38
|
export * from './hooks/table';
|
|
38
39
|
export { translations, createTranslator } from './locales';
|
|
39
|
-
export { api, dayjs, FormInput, PhoneInput, AddressForm, StripeForm, Status, Livemode, Switch, ConfirmDialog, CheckoutForm, CheckoutTable, CheckoutDonate, CurrencySelector, Payment, PaymentSummary, PricingTable, ProductSkeleton, Amount, CustomerInvoiceList, CustomerPaymentList, TxLink, TxGas, SafeGuard, PricingItem, CountrySelect, Table, TruncatedText, Link, };
|
|
40
|
+
export { createLazyComponent, api, dayjs, FormInput, PhoneInput, AddressForm, StripeForm, Status, Livemode, Switch, ConfirmDialog, CheckoutForm, CheckoutTable, CheckoutDonate, CurrencySelector, Payment, PaymentSummary, PricingTable, ProductSkeleton, Amount, CustomerInvoiceList, CustomerPaymentList, TxLink, TxGas, SafeGuard, PricingItem, CountrySelect, Table, TruncatedText, Link, };
|
package/lib/index.js
CHANGED
|
@@ -33,6 +33,7 @@ var _exportNames = {
|
|
|
33
33
|
CountrySelect: true,
|
|
34
34
|
TruncatedText: true,
|
|
35
35
|
Link: true,
|
|
36
|
+
createLazyComponent: true,
|
|
36
37
|
PaymentThemeProvider: true,
|
|
37
38
|
translations: true,
|
|
38
39
|
createTranslator: true
|
|
@@ -211,6 +212,12 @@ Object.defineProperty(exports, "api", {
|
|
|
211
212
|
return _api.default;
|
|
212
213
|
}
|
|
213
214
|
});
|
|
215
|
+
Object.defineProperty(exports, "createLazyComponent", {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
get: function () {
|
|
218
|
+
return _lazyLoader.createLazyComponent;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
214
221
|
Object.defineProperty(exports, "createTranslator", {
|
|
215
222
|
enumerable: true,
|
|
216
223
|
get: function () {
|
|
@@ -258,6 +265,7 @@ var _pricingItem = _interopRequireDefault(require("./components/pricing-item"));
|
|
|
258
265
|
var _countrySelect = _interopRequireDefault(require("./components/country-select"));
|
|
259
266
|
var _truncatedText = _interopRequireDefault(require("./components/truncated-text"));
|
|
260
267
|
var _link = _interopRequireDefault(require("./components/link"));
|
|
268
|
+
var _lazyLoader = require("./components/lazy-loader");
|
|
261
269
|
var _theme = require("./theme");
|
|
262
270
|
var _util = require("./libs/util");
|
|
263
271
|
Object.keys(_util).forEach(function (key) {
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import type { TCustomer } from '@blocklet/payment-types';
|
|
3
|
-
type
|
|
3
|
+
export type StripeCheckoutFormProps = {
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
intentType: string;
|
|
6
|
+
customer: TCustomer;
|
|
7
|
+
mode: string;
|
|
8
|
+
onConfirm: Function;
|
|
9
|
+
returnUrl: string;
|
|
10
|
+
};
|
|
11
|
+
export type StripeCheckoutProps = {
|
|
4
12
|
clientSecret: string;
|
|
5
13
|
intentType: string;
|
|
6
14
|
publicKey: string;
|
|
@@ -11,11 +11,16 @@ var _context = require("@arcblock/ux/lib/Locale/context");
|
|
|
11
11
|
var _lab = require("@mui/lab");
|
|
12
12
|
var _material = require("@mui/material");
|
|
13
13
|
var _system = require("@mui/system");
|
|
14
|
-
var _reactStripeJs = require("@stripe/react-stripe-js");
|
|
15
|
-
var _stripeJs = require("@stripe/stripe-js");
|
|
16
14
|
var _ahooks = require("ahooks");
|
|
17
15
|
var _react = require("react");
|
|
18
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
const {
|
|
18
|
+
Elements,
|
|
19
|
+
PaymentElement,
|
|
20
|
+
useElements,
|
|
21
|
+
useStripe,
|
|
22
|
+
loadStripe
|
|
23
|
+
} = globalThis.__STRIPE_COMPONENTS__;
|
|
19
24
|
function StripeCheckoutForm({
|
|
20
25
|
clientSecret,
|
|
21
26
|
intentType,
|
|
@@ -24,8 +29,8 @@ function StripeCheckoutForm({
|
|
|
24
29
|
onConfirm,
|
|
25
30
|
returnUrl = ""
|
|
26
31
|
}) {
|
|
27
|
-
const stripe =
|
|
28
|
-
const elements =
|
|
32
|
+
const stripe = useStripe();
|
|
33
|
+
const elements = useElements();
|
|
29
34
|
const {
|
|
30
35
|
t
|
|
31
36
|
} = (0, _context.useLocaleContext)();
|
|
@@ -117,7 +122,7 @@ function StripeCheckoutForm({
|
|
|
117
122
|
|
|
118
123
|
return /* @__PURE__ */(0, _jsxRuntime.jsxs)(Content, {
|
|
119
124
|
onSubmit: handleSubmit,
|
|
120
|
-
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(
|
|
125
|
+
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(PaymentElement, {
|
|
121
126
|
options: {
|
|
122
127
|
layout: "auto",
|
|
123
128
|
fields: {
|
|
@@ -177,7 +182,7 @@ function StripeCheckout({
|
|
|
177
182
|
onCancel,
|
|
178
183
|
returnUrl = ""
|
|
179
184
|
}) {
|
|
180
|
-
const stripePromise =
|
|
185
|
+
const stripePromise = loadStripe(publicKey);
|
|
181
186
|
const {
|
|
182
187
|
t
|
|
183
188
|
} = (0, _context.useLocaleContext)();
|
|
@@ -202,7 +207,7 @@ function StripeCheckout({
|
|
|
202
207
|
open: state.open,
|
|
203
208
|
onClose: handleClose,
|
|
204
209
|
disableEscapeKeyDown: true,
|
|
205
|
-
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(
|
|
210
|
+
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(Elements, {
|
|
206
211
|
options: {
|
|
207
212
|
clientSecret
|
|
208
213
|
},
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { StripeCheckoutProps } from './form';
|
|
3
|
+
declare global {
|
|
4
|
+
interface Window {
|
|
5
|
+
__STRIPE_COMPONENTS__?: {
|
|
6
|
+
Elements: any;
|
|
7
|
+
PaymentElement: any;
|
|
8
|
+
useElements: () => any;
|
|
9
|
+
useStripe: () => any;
|
|
10
|
+
loadStripe: (key: string) => Promise<any>;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
declare const _default: (props: StripeCheckoutProps) => JSX.Element;
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _lazyLoader = require("../../../components/lazy-loader");
|
|
8
|
+
module.exports = (0, _lazyLoader.createLazyComponent)(async () => {
|
|
9
|
+
const [stripeReact, stripe] = await Promise.all([Promise.resolve().then(() => require("@stripe/react-stripe-js")), Promise.resolve().then(() => require("@stripe/stripe-js"))]);
|
|
10
|
+
window.__STRIPE_COMPONENTS__ = {
|
|
11
|
+
Elements: stripeReact.Elements,
|
|
12
|
+
PaymentElement: stripeReact.PaymentElement,
|
|
13
|
+
useElements: stripeReact.useElements,
|
|
14
|
+
useStripe: stripeReact.useStripe,
|
|
15
|
+
loadStripe: stripe.loadStripe
|
|
16
|
+
};
|
|
17
|
+
const {
|
|
18
|
+
default: Component
|
|
19
|
+
} = await Promise.resolve().then(() => require("./form"));
|
|
20
|
+
return Component;
|
|
21
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/payment-react",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.28",
|
|
4
4
|
"description": "Reusable react components for payment kit v2",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -92,7 +92,7 @@
|
|
|
92
92
|
"@babel/core": "^7.25.2",
|
|
93
93
|
"@babel/preset-env": "^7.25.2",
|
|
94
94
|
"@babel/preset-react": "^7.24.7",
|
|
95
|
-
"@blocklet/payment-types": "1.15.
|
|
95
|
+
"@blocklet/payment-types": "1.15.28",
|
|
96
96
|
"@storybook/addon-essentials": "^7.6.20",
|
|
97
97
|
"@storybook/addon-interactions": "^7.6.20",
|
|
98
98
|
"@storybook/addon-links": "^7.6.20",
|
|
@@ -122,5 +122,5 @@
|
|
|
122
122
|
"vite-plugin-babel": "^1.2.0",
|
|
123
123
|
"vite-plugin-node-polyfills": "^0.21.0"
|
|
124
124
|
},
|
|
125
|
-
"gitHead": "
|
|
125
|
+
"gitHead": "0978937b91bc34a90d0b41af665999d471cec63e"
|
|
126
126
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
|
+
import Center from '@arcblock/ux/lib/Center';
|
|
3
|
+
import { CircularProgress } from '@mui/material';
|
|
4
|
+
|
|
5
|
+
type ComponentType<P = any> = React.ComponentType<P>;
|
|
6
|
+
type LoaderFunction<T extends ComponentType> = () => Promise<{ default: T } | T>;
|
|
7
|
+
|
|
8
|
+
interface LazyComponentOptions {
|
|
9
|
+
LoadingComponent?: React.ComponentType;
|
|
10
|
+
ErrorComponent?: React.ComponentType<{ error: Error }>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createLazyComponent<T extends ComponentType, P extends React.ComponentProps<T>>(
|
|
14
|
+
loader: LoaderFunction<T>,
|
|
15
|
+
options: LazyComponentOptions = {}
|
|
16
|
+
) {
|
|
17
|
+
let loadPromise: Promise<T> | null = null;
|
|
18
|
+
const {
|
|
19
|
+
LoadingComponent = () => (
|
|
20
|
+
<Center>
|
|
21
|
+
<CircularProgress />
|
|
22
|
+
</Center>
|
|
23
|
+
),
|
|
24
|
+
ErrorComponent = ({ error }) => <div>Error: {error.message}</div>,
|
|
25
|
+
} = options;
|
|
26
|
+
|
|
27
|
+
const loadComponent = async () => {
|
|
28
|
+
if (loadPromise) return loadPromise;
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
loadPromise = loader().then((result) => ('default' in result ? result.default : result));
|
|
32
|
+
return await loadPromise;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
loadPromise = null;
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return function WrappedComponent(props: P) {
|
|
40
|
+
const [Component, setComponent] = useState<T | null>(null);
|
|
41
|
+
const [error, setError] = useState<Error | null>(null);
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
loadComponent()
|
|
45
|
+
.then((comp) => {
|
|
46
|
+
setComponent(() => comp);
|
|
47
|
+
})
|
|
48
|
+
.catch((err) => {
|
|
49
|
+
setError(err);
|
|
50
|
+
console.error('Failed to load component:', err);
|
|
51
|
+
});
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
if (error) {
|
|
55
|
+
return <ErrorComponent error={error} />;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!Component) {
|
|
59
|
+
return <LoadingComponent />;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return <Component {...props} />;
|
|
63
|
+
};
|
|
64
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -27,6 +27,7 @@ import PricingItem from './components/pricing-item';
|
|
|
27
27
|
import CountrySelect from './components/country-select';
|
|
28
28
|
import TruncatedText from './components/truncated-text';
|
|
29
29
|
import Link from './components/link';
|
|
30
|
+
import { createLazyComponent } from './components/lazy-loader';
|
|
30
31
|
|
|
31
32
|
export { PaymentThemeProvider } from './theme';
|
|
32
33
|
|
|
@@ -41,6 +42,7 @@ export * from './hooks/table';
|
|
|
41
42
|
export { translations, createTranslator } from './locales';
|
|
42
43
|
|
|
43
44
|
export {
|
|
45
|
+
createLazyComponent,
|
|
44
46
|
api,
|
|
45
47
|
dayjs,
|
|
46
48
|
FormInput,
|
|
@@ -5,12 +5,12 @@ import type { TCustomer } from '@blocklet/payment-types';
|
|
|
5
5
|
import { LoadingButton } from '@mui/lab';
|
|
6
6
|
import { CircularProgress, Typography } from '@mui/material';
|
|
7
7
|
import { styled } from '@mui/system';
|
|
8
|
-
import { Elements, PaymentElement, useElements, useStripe } from '@stripe/react-stripe-js';
|
|
9
|
-
import { loadStripe } from '@stripe/stripe-js';
|
|
10
8
|
import { useSetState } from 'ahooks';
|
|
11
9
|
import { useEffect, useCallback } from 'react';
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
const { Elements, PaymentElement, useElements, useStripe, loadStripe } = (globalThis as any).__STRIPE_COMPONENTS__;
|
|
12
|
+
|
|
13
|
+
export type StripeCheckoutFormProps = {
|
|
14
14
|
clientSecret: string;
|
|
15
15
|
intentType: string;
|
|
16
16
|
customer: TCustomer;
|
|
@@ -150,7 +150,7 @@ const Content = styled('form')`
|
|
|
150
150
|
min-height: 320px;
|
|
151
151
|
`;
|
|
152
152
|
|
|
153
|
-
type StripeCheckoutProps = {
|
|
153
|
+
export type StripeCheckoutProps = {
|
|
154
154
|
clientSecret: string;
|
|
155
155
|
intentType: string;
|
|
156
156
|
publicKey: string;
|
|
@@ -160,7 +160,6 @@ type StripeCheckoutProps = {
|
|
|
160
160
|
onCancel: Function;
|
|
161
161
|
returnUrl?: string;
|
|
162
162
|
};
|
|
163
|
-
|
|
164
163
|
export default function StripeCheckout({
|
|
165
164
|
clientSecret,
|
|
166
165
|
intentType,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createLazyComponent } from '../../../components/lazy-loader';
|
|
2
|
+
import type { StripeCheckoutProps } from './form';
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
interface Window {
|
|
6
|
+
__STRIPE_COMPONENTS__?: {
|
|
7
|
+
Elements: any;
|
|
8
|
+
PaymentElement: any;
|
|
9
|
+
useElements: () => any;
|
|
10
|
+
useStripe: () => any;
|
|
11
|
+
loadStripe: (key: string) => Promise<any>;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default createLazyComponent<React.ComponentType<StripeCheckoutProps>, StripeCheckoutProps>(async () => {
|
|
17
|
+
const [stripeReact, stripe] = await Promise.all([import('@stripe/react-stripe-js'), import('@stripe/stripe-js')]);
|
|
18
|
+
|
|
19
|
+
window.__STRIPE_COMPONENTS__ = {
|
|
20
|
+
Elements: stripeReact.Elements,
|
|
21
|
+
PaymentElement: stripeReact.PaymentElement,
|
|
22
|
+
useElements: stripeReact.useElements,
|
|
23
|
+
useStripe: stripeReact.useStripe,
|
|
24
|
+
loadStripe: stripe.loadStripe,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const { default: Component } = await import('./form');
|
|
28
|
+
return Component;
|
|
29
|
+
});
|