@blocklet/payment-react 1.13.182 → 1.13.183
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/checkout/form.d.ts +1 -1
- package/es/checkout/form.js +2 -1
- package/es/checkout/table.d.ts +1 -1
- package/es/checkout/table.js +12 -2
- package/es/contexts/payment.d.ts +1 -0
- package/es/contexts/payment.js +5 -0
- package/es/history/mini-invoice/list.js +1 -1
- package/es/payment/index.d.ts +2 -2
- package/es/payment/index.js +9 -1
- package/es/types/index.d.ts +16 -0
- package/lib/checkout/form.d.ts +1 -1
- package/lib/checkout/form.js +3 -1
- package/lib/checkout/table.d.ts +1 -1
- package/lib/checkout/table.js +4 -2
- package/lib/contexts/payment.d.ts +1 -0
- package/lib/contexts/payment.js +5 -0
- package/lib/history/mini-invoice/list.js +1 -1
- package/lib/payment/index.d.ts +2 -2
- package/lib/payment/index.js +13 -0
- package/lib/types/index.d.ts +16 -0
- package/package.json +3 -3
- package/src/checkout/form.tsx +2 -1
- package/src/checkout/table.tsx +9 -2
- package/src/contexts/payment.tsx +7 -0
- package/src/history/mini-invoice/list.tsx +1 -1
- package/src/payment/index.tsx +12 -3
- package/src/types/index.ts +17 -0
package/es/checkout/form.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutProps } from '../types';
|
|
3
|
-
declare function CheckoutForm({ id, onPaid, onError,
|
|
3
|
+
declare function CheckoutForm({ id, mode, onPaid, onError, onChange, goBack, extraParams }: CheckoutProps): import("react").JSX.Element;
|
|
4
4
|
declare namespace CheckoutForm {
|
|
5
5
|
var defaultProps: {
|
|
6
6
|
onPaid: any;
|
package/es/checkout/form.js
CHANGED
|
@@ -14,7 +14,7 @@ const fetchCheckoutSession = async (id) => {
|
|
|
14
14
|
const { data } = await api.get(`/api/checkout-sessions/retrieve/${id}`);
|
|
15
15
|
return data;
|
|
16
16
|
};
|
|
17
|
-
export default function CheckoutForm({ id, onPaid, onError,
|
|
17
|
+
export default function CheckoutForm({ id, mode, onPaid, onError, onChange, goBack, extraParams }) {
|
|
18
18
|
if (!id.startsWith("plink_") && !id.startsWith("cs_")) {
|
|
19
19
|
throw new Error("Either a checkoutSession or a paymentLink id is required.");
|
|
20
20
|
}
|
|
@@ -49,6 +49,7 @@ export default function CheckoutForm({ id, onPaid, onError, mode, goBack, extraP
|
|
|
49
49
|
error: apiError || state.appError,
|
|
50
50
|
onPaid: handlePaid,
|
|
51
51
|
onError: handleError,
|
|
52
|
+
onChange,
|
|
52
53
|
goBack,
|
|
53
54
|
mode
|
|
54
55
|
}
|
package/es/checkout/table.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutProps } from '../types';
|
|
3
|
-
export default function CheckoutTable({ id, onPaid, onError,
|
|
3
|
+
export default function CheckoutTable({ id, mode, onPaid, onError, onChange, extraParams, goBack }: CheckoutProps): import("react").JSX.Element;
|
package/es/checkout/table.js
CHANGED
|
@@ -14,7 +14,7 @@ const fetchData = async (id) => {
|
|
|
14
14
|
const { data } = await api.get(`/api/pricing-tables/${id}`);
|
|
15
15
|
return data;
|
|
16
16
|
};
|
|
17
|
-
export default function CheckoutTable({ id, onPaid, onError,
|
|
17
|
+
export default function CheckoutTable({ id, mode, onPaid, onError, onChange, extraParams, goBack }) {
|
|
18
18
|
if (!id.startsWith("prctbl_")) {
|
|
19
19
|
throw new Error("A valid pricing table id is required.");
|
|
20
20
|
}
|
|
@@ -66,5 +66,15 @@ export default function CheckoutTable({ id, onPaid, onError, mode, extraParams,
|
|
|
66
66
|
}
|
|
67
67
|
return /* @__PURE__ */ jsx(PricingTable, { mode: "select", table: data, onSelect: handleSelect });
|
|
68
68
|
}
|
|
69
|
-
return /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(
|
|
69
|
+
return /* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(
|
|
70
|
+
CheckoutForm,
|
|
71
|
+
{
|
|
72
|
+
id: hashSessionId || sessionId,
|
|
73
|
+
mode,
|
|
74
|
+
onPaid,
|
|
75
|
+
onError,
|
|
76
|
+
onChange,
|
|
77
|
+
...prop
|
|
78
|
+
}
|
|
79
|
+
) });
|
|
70
80
|
}
|
package/es/contexts/payment.d.ts
CHANGED
package/es/contexts/payment.js
CHANGED
|
@@ -11,6 +11,10 @@ const getSettings = async () => {
|
|
|
11
11
|
const { data } = await api.get("/api/settings");
|
|
12
12
|
return data;
|
|
13
13
|
};
|
|
14
|
+
const getCurrency = (currencyId, methods) => {
|
|
15
|
+
const currencies = methods.reduce((acc, x) => acc.concat(x.payment_currencies), []);
|
|
16
|
+
return currencies.find((x) => x.id === currencyId);
|
|
17
|
+
};
|
|
14
18
|
function PaymentProvider({ children, session, connect }) {
|
|
15
19
|
const { data, error, run, loading } = useRequest(getSettings);
|
|
16
20
|
const [livemode, setLivemode] = useLocalStorageState("livemode", { defaultValue: true });
|
|
@@ -29,6 +33,7 @@ function PaymentProvider({ children, session, connect }) {
|
|
|
29
33
|
prefix,
|
|
30
34
|
livemode: !!livemode,
|
|
31
35
|
settings: data,
|
|
36
|
+
getCurrency: (currencyId) => getCurrency(currencyId, data.paymentMethods),
|
|
32
37
|
refresh: run,
|
|
33
38
|
setLivemode,
|
|
34
39
|
api
|
package/es/payment/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ type Props = CheckoutContext & CheckoutCallbacks & {
|
|
|
5
5
|
completed?: boolean;
|
|
6
6
|
error?: any;
|
|
7
7
|
};
|
|
8
|
-
declare function Payment({ checkoutSession, paymentMethods, paymentIntent, paymentLink, customer, completed, error, mode, onPaid, onError, goBack, }: Props): import("react").JSX.Element;
|
|
8
|
+
declare function Payment({ checkoutSession, paymentMethods, paymentIntent, paymentLink, customer, completed, error, mode, onPaid, onError, onChange, goBack, }: Props): import("react").JSX.Element;
|
|
9
9
|
declare namespace Payment {
|
|
10
10
|
var defaultProps: {
|
|
11
11
|
completed: boolean;
|
|
@@ -16,7 +16,7 @@ export default Payment;
|
|
|
16
16
|
type MainProps = CheckoutContext & CheckoutCallbacks & {
|
|
17
17
|
completed?: boolean;
|
|
18
18
|
};
|
|
19
|
-
export declare function PaymentInner({ checkoutSession, paymentMethods, paymentLink, paymentIntent, customer, completed, mode, onPaid, onError, goBack, }: MainProps): import("react").JSX.Element;
|
|
19
|
+
export declare function PaymentInner({ checkoutSession, paymentMethods, paymentLink, paymentIntent, customer, completed, mode, onPaid, onError, onChange, goBack, }: MainProps): import("react").JSX.Element;
|
|
20
20
|
export declare namespace PaymentInner {
|
|
21
21
|
var defaultProps: {
|
|
22
22
|
completed: boolean;
|
package/es/payment/index.js
CHANGED
|
@@ -33,6 +33,7 @@ export default function Payment({
|
|
|
33
33
|
mode,
|
|
34
34
|
onPaid,
|
|
35
35
|
onError,
|
|
36
|
+
onChange,
|
|
36
37
|
goBack
|
|
37
38
|
}) {
|
|
38
39
|
const { t } = useLocaleContext();
|
|
@@ -57,7 +58,7 @@ export default function Payment({
|
|
|
57
58
|
return /* @__PURE__ */ jsx(PaymentError, { title: "Oops", description: formatError(error) });
|
|
58
59
|
}
|
|
59
60
|
if (!checkoutSession || !delay) {
|
|
60
|
-
return /* @__PURE__ */ jsx(Root, { mode, children: /* @__PURE__ */ jsxs(Stack, { className: "cko-container", children: [
|
|
61
|
+
return /* @__PURE__ */ jsx(Root, { mode, children: /* @__PURE__ */ jsxs(Stack, { className: "cko-container", sx: { gap: { sm: mode === "standalone" ? 0 : 8 } }, children: [
|
|
61
62
|
/* @__PURE__ */ jsx(Stack, { className: "cko-overview", children: /* @__PURE__ */ jsx(OverviewSkeleton, {}) }),
|
|
62
63
|
/* @__PURE__ */ jsx(Stack, { className: "cko-payment", children: /* @__PURE__ */ jsx(PaymentSkeleton, {}) }),
|
|
63
64
|
/* @__PURE__ */ jsx(CheckoutFooter, { className: "cko-footer" })
|
|
@@ -92,6 +93,7 @@ export default function Payment({
|
|
|
92
93
|
customer,
|
|
93
94
|
onPaid,
|
|
94
95
|
onError,
|
|
96
|
+
onChange,
|
|
95
97
|
goBack,
|
|
96
98
|
mode
|
|
97
99
|
}
|
|
@@ -110,6 +112,7 @@ export function PaymentInner({
|
|
|
110
112
|
mode,
|
|
111
113
|
onPaid,
|
|
112
114
|
onError,
|
|
115
|
+
onChange,
|
|
113
116
|
goBack
|
|
114
117
|
}) {
|
|
115
118
|
const { t } = useLocaleContext();
|
|
@@ -140,6 +143,11 @@ export function PaymentInner({
|
|
|
140
143
|
});
|
|
141
144
|
const currencyId = useWatch({ control: methods.control, name: "payment_currency", defaultValue: defaultCurrencyId });
|
|
142
145
|
const currency = findCurrency(paymentMethods, currencyId) || settings.baseCurrency;
|
|
146
|
+
useEffect(() => {
|
|
147
|
+
if (onChange) {
|
|
148
|
+
onChange(methods.getValues());
|
|
149
|
+
}
|
|
150
|
+
}, [currencyId]);
|
|
143
151
|
const onUpsell = async (from, to) => {
|
|
144
152
|
try {
|
|
145
153
|
const { data } = await api.put(`/api/checkout-sessions/${state.checkoutSession.id}/upsell`, { from, to });
|
package/es/types/index.d.ts
CHANGED
|
@@ -8,6 +8,21 @@ export type CheckoutContext = {
|
|
|
8
8
|
customer?: TCustomer;
|
|
9
9
|
mode: LiteralUnion<'standalone' | 'inline' | 'popup', string>;
|
|
10
10
|
};
|
|
11
|
+
export type CheckoutFormData = {
|
|
12
|
+
customer_name: string;
|
|
13
|
+
customer_email?: string;
|
|
14
|
+
customer_phone?: string;
|
|
15
|
+
payment_method: string;
|
|
16
|
+
payment_currency: string;
|
|
17
|
+
billing_address?: {
|
|
18
|
+
country: string;
|
|
19
|
+
state?: string;
|
|
20
|
+
city?: string;
|
|
21
|
+
line1?: string;
|
|
22
|
+
line2?: string;
|
|
23
|
+
postal_code: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
11
26
|
export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
12
27
|
id: string;
|
|
13
28
|
extraParams?: Record<string, any>;
|
|
@@ -16,5 +31,6 @@ export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
|
16
31
|
export type CheckoutCallbacks = {
|
|
17
32
|
onPaid: (res: CheckoutContext) => void;
|
|
18
33
|
onError: (err: Error) => void;
|
|
34
|
+
onChange?: (data: CheckoutFormData) => void;
|
|
19
35
|
goBack?: () => void;
|
|
20
36
|
};
|
package/lib/checkout/form.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutProps } from '../types';
|
|
3
|
-
declare function CheckoutForm({ id, onPaid, onError,
|
|
3
|
+
declare function CheckoutForm({ id, mode, onPaid, onError, onChange, goBack, extraParams }: CheckoutProps): import("react").JSX.Element;
|
|
4
4
|
declare namespace CheckoutForm {
|
|
5
5
|
var defaultProps: {
|
|
6
6
|
onPaid: any;
|
package/lib/checkout/form.js
CHANGED
|
@@ -27,9 +27,10 @@ const fetchCheckoutSession = async id => {
|
|
|
27
27
|
};
|
|
28
28
|
function CheckoutForm({
|
|
29
29
|
id,
|
|
30
|
+
mode,
|
|
30
31
|
onPaid,
|
|
31
32
|
onError,
|
|
32
|
-
|
|
33
|
+
onChange,
|
|
33
34
|
goBack,
|
|
34
35
|
extraParams
|
|
35
36
|
}) {
|
|
@@ -73,6 +74,7 @@ function CheckoutForm({
|
|
|
73
74
|
error: apiError || state.appError,
|
|
74
75
|
onPaid: handlePaid,
|
|
75
76
|
onError: handleError,
|
|
77
|
+
onChange,
|
|
76
78
|
goBack,
|
|
77
79
|
mode
|
|
78
80
|
});
|
package/lib/checkout/table.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { CheckoutProps } from '../types';
|
|
3
|
-
export default function CheckoutTable({ id, onPaid, onError,
|
|
3
|
+
export default function CheckoutTable({ id, mode, onPaid, onError, onChange, extraParams, goBack }: CheckoutProps): import("react").JSX.Element;
|
package/lib/checkout/table.js
CHANGED
|
@@ -25,9 +25,10 @@ const fetchData = async id => {
|
|
|
25
25
|
};
|
|
26
26
|
function CheckoutTable({
|
|
27
27
|
id,
|
|
28
|
+
mode,
|
|
28
29
|
onPaid,
|
|
29
30
|
onError,
|
|
30
|
-
|
|
31
|
+
onChange,
|
|
31
32
|
extraParams,
|
|
32
33
|
goBack
|
|
33
34
|
}) {
|
|
@@ -108,9 +109,10 @@ function CheckoutTable({
|
|
|
108
109
|
return /* @__PURE__ */(0, _jsxRuntime.jsx)(_system.Box, {
|
|
109
110
|
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_form.default, {
|
|
110
111
|
id: hashSessionId || sessionId,
|
|
112
|
+
mode,
|
|
111
113
|
onPaid,
|
|
112
114
|
onError,
|
|
113
|
-
|
|
115
|
+
onChange,
|
|
114
116
|
...prop
|
|
115
117
|
})
|
|
116
118
|
});
|
package/lib/contexts/payment.js
CHANGED
|
@@ -29,6 +29,10 @@ const getSettings = async () => {
|
|
|
29
29
|
} = await _api.default.get("/api/settings");
|
|
30
30
|
return data;
|
|
31
31
|
};
|
|
32
|
+
const getCurrency = (currencyId, methods) => {
|
|
33
|
+
const currencies = methods.reduce((acc, x) => acc.concat(x.payment_currencies), []);
|
|
34
|
+
return currencies.find(x => x.id === currencyId);
|
|
35
|
+
};
|
|
32
36
|
function PaymentProvider({
|
|
33
37
|
children,
|
|
34
38
|
session,
|
|
@@ -59,6 +63,7 @@ function PaymentProvider({
|
|
|
59
63
|
prefix,
|
|
60
64
|
livemode: !!livemode,
|
|
61
65
|
settings: data,
|
|
66
|
+
getCurrency: currencyId => getCurrency(currencyId, data.paymentMethods),
|
|
62
67
|
refresh: run,
|
|
63
68
|
setLivemode,
|
|
64
69
|
api: _api.default
|
|
@@ -67,7 +67,7 @@ function MiniInvoiceList() {
|
|
|
67
67
|
name: t("payment.customer.subscriptions.nextInvoice"),
|
|
68
68
|
value: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
|
|
69
69
|
sx: {
|
|
70
|
-
color: "
|
|
70
|
+
color: "success.main",
|
|
71
71
|
fontWeight: "bold",
|
|
72
72
|
marginRight: "10px"
|
|
73
73
|
},
|
package/lib/payment/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ type Props = CheckoutContext & CheckoutCallbacks & {
|
|
|
5
5
|
completed?: boolean;
|
|
6
6
|
error?: any;
|
|
7
7
|
};
|
|
8
|
-
declare function Payment({ checkoutSession, paymentMethods, paymentIntent, paymentLink, customer, completed, error, mode, onPaid, onError, goBack, }: Props): import("react").JSX.Element;
|
|
8
|
+
declare function Payment({ checkoutSession, paymentMethods, paymentIntent, paymentLink, customer, completed, error, mode, onPaid, onError, onChange, goBack, }: Props): import("react").JSX.Element;
|
|
9
9
|
declare namespace Payment {
|
|
10
10
|
var defaultProps: {
|
|
11
11
|
completed: boolean;
|
|
@@ -16,7 +16,7 @@ export default Payment;
|
|
|
16
16
|
type MainProps = CheckoutContext & CheckoutCallbacks & {
|
|
17
17
|
completed?: boolean;
|
|
18
18
|
};
|
|
19
|
-
export declare function PaymentInner({ checkoutSession, paymentMethods, paymentLink, paymentIntent, customer, completed, mode, onPaid, onError, goBack, }: MainProps): import("react").JSX.Element;
|
|
19
|
+
export declare function PaymentInner({ checkoutSession, paymentMethods, paymentLink, paymentIntent, customer, completed, mode, onPaid, onError, onChange, goBack, }: MainProps): import("react").JSX.Element;
|
|
20
20
|
export declare namespace PaymentInner {
|
|
21
21
|
var defaultProps: {
|
|
22
22
|
completed: boolean;
|
package/lib/payment/index.js
CHANGED
|
@@ -42,6 +42,7 @@ function Payment({
|
|
|
42
42
|
mode,
|
|
43
43
|
onPaid,
|
|
44
44
|
onError,
|
|
45
|
+
onChange,
|
|
45
46
|
goBack
|
|
46
47
|
}) {
|
|
47
48
|
const {
|
|
@@ -79,6 +80,11 @@ function Payment({
|
|
|
79
80
|
mode,
|
|
80
81
|
children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
|
|
81
82
|
className: "cko-container",
|
|
83
|
+
sx: {
|
|
84
|
+
gap: {
|
|
85
|
+
sm: mode === "standalone" ? 0 : 8
|
|
86
|
+
}
|
|
87
|
+
},
|
|
82
88
|
children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Stack, {
|
|
83
89
|
className: "cko-overview",
|
|
84
90
|
children: /* @__PURE__ */(0, _jsxRuntime.jsx)(_overview.default, {})
|
|
@@ -112,6 +118,7 @@ function Payment({
|
|
|
112
118
|
customer,
|
|
113
119
|
onPaid,
|
|
114
120
|
onError,
|
|
121
|
+
onChange,
|
|
115
122
|
goBack,
|
|
116
123
|
mode
|
|
117
124
|
});
|
|
@@ -129,6 +136,7 @@ function PaymentInner({
|
|
|
129
136
|
mode,
|
|
130
137
|
onPaid,
|
|
131
138
|
onError,
|
|
139
|
+
onChange,
|
|
132
140
|
goBack
|
|
133
141
|
}) {
|
|
134
142
|
const {
|
|
@@ -168,6 +176,11 @@ function PaymentInner({
|
|
|
168
176
|
defaultValue: defaultCurrencyId
|
|
169
177
|
});
|
|
170
178
|
const currency = (0, _util.findCurrency)(paymentMethods, currencyId) || settings.baseCurrency;
|
|
179
|
+
(0, _react.useEffect)(() => {
|
|
180
|
+
if (onChange) {
|
|
181
|
+
onChange(methods.getValues());
|
|
182
|
+
}
|
|
183
|
+
}, [currencyId]);
|
|
171
184
|
const onUpsell = async (from, to) => {
|
|
172
185
|
try {
|
|
173
186
|
const {
|
package/lib/types/index.d.ts
CHANGED
|
@@ -8,6 +8,21 @@ export type CheckoutContext = {
|
|
|
8
8
|
customer?: TCustomer;
|
|
9
9
|
mode: LiteralUnion<'standalone' | 'inline' | 'popup', string>;
|
|
10
10
|
};
|
|
11
|
+
export type CheckoutFormData = {
|
|
12
|
+
customer_name: string;
|
|
13
|
+
customer_email?: string;
|
|
14
|
+
customer_phone?: string;
|
|
15
|
+
payment_method: string;
|
|
16
|
+
payment_currency: string;
|
|
17
|
+
billing_address?: {
|
|
18
|
+
country: string;
|
|
19
|
+
state?: string;
|
|
20
|
+
city?: string;
|
|
21
|
+
line1?: string;
|
|
22
|
+
line2?: string;
|
|
23
|
+
postal_code: string;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
11
26
|
export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
12
27
|
id: string;
|
|
13
28
|
extraParams?: Record<string, any>;
|
|
@@ -16,5 +31,6 @@ export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
|
16
31
|
export type CheckoutCallbacks = {
|
|
17
32
|
onPaid: (res: CheckoutContext) => void;
|
|
18
33
|
onError: (err: Error) => void;
|
|
34
|
+
onChange?: (data: CheckoutFormData) => void;
|
|
19
35
|
goBack?: () => void;
|
|
20
36
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blocklet/payment-react",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.183",
|
|
4
4
|
"description": "Reusable react components for payment kit v2",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -90,7 +90,7 @@
|
|
|
90
90
|
"@babel/core": "^7.23.9",
|
|
91
91
|
"@babel/preset-env": "^7.23.9",
|
|
92
92
|
"@babel/preset-react": "^7.23.3",
|
|
93
|
-
"@blocklet/payment-types": "1.13.
|
|
93
|
+
"@blocklet/payment-types": "1.13.183",
|
|
94
94
|
"@storybook/addon-essentials": "^7.6.13",
|
|
95
95
|
"@storybook/addon-interactions": "^7.6.13",
|
|
96
96
|
"@storybook/addon-links": "^7.6.13",
|
|
@@ -119,5 +119,5 @@
|
|
|
119
119
|
"vite-plugin-babel": "^1.2.0",
|
|
120
120
|
"vite-plugin-node-polyfills": "^0.19.0"
|
|
121
121
|
},
|
|
122
|
-
"gitHead": "
|
|
122
|
+
"gitHead": "8cf0deb151d24b4a1d58a18dd225d9ef8a8faecb"
|
|
123
123
|
}
|
package/src/checkout/form.tsx
CHANGED
|
@@ -21,7 +21,7 @@ const fetchCheckoutSession = async (id: string): Promise<CheckoutContext> => {
|
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
// FIXME: @wangshijun support popup
|
|
24
|
-
export default function CheckoutForm({ id, onPaid, onError,
|
|
24
|
+
export default function CheckoutForm({ id, mode, onPaid, onError, onChange, goBack, extraParams }: CheckoutProps) {
|
|
25
25
|
if (!id.startsWith('plink_') && !id.startsWith('cs_')) {
|
|
26
26
|
throw new Error('Either a checkoutSession or a paymentLink id is required.');
|
|
27
27
|
}
|
|
@@ -62,6 +62,7 @@ export default function CheckoutForm({ id, onPaid, onError, mode, goBack, extraP
|
|
|
62
62
|
error={apiError || state.appError}
|
|
63
63
|
onPaid={handlePaid}
|
|
64
64
|
onError={handleError}
|
|
65
|
+
onChange={onChange}
|
|
65
66
|
goBack={goBack}
|
|
66
67
|
mode={mode as string}
|
|
67
68
|
/>
|
package/src/checkout/table.tsx
CHANGED
|
@@ -18,7 +18,7 @@ const fetchData = async (id: string): Promise<TPricingTableExpanded> => {
|
|
|
18
18
|
return data;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
export default function CheckoutTable({ id, onPaid, onError,
|
|
21
|
+
export default function CheckoutTable({ id, mode, onPaid, onError, onChange, extraParams, goBack }: CheckoutProps) {
|
|
22
22
|
if (!id.startsWith('prctbl_')) {
|
|
23
23
|
throw new Error('A valid pricing table id is required.');
|
|
24
24
|
}
|
|
@@ -88,7 +88,14 @@ export default function CheckoutTable({ id, onPaid, onError, mode, extraParams,
|
|
|
88
88
|
|
|
89
89
|
return (
|
|
90
90
|
<Box>
|
|
91
|
-
<CheckoutForm
|
|
91
|
+
<CheckoutForm
|
|
92
|
+
id={hashSessionId || sessionId}
|
|
93
|
+
mode={mode}
|
|
94
|
+
onPaid={onPaid}
|
|
95
|
+
onError={onError}
|
|
96
|
+
onChange={onChange}
|
|
97
|
+
{...prop}
|
|
98
|
+
/>
|
|
92
99
|
</Box>
|
|
93
100
|
);
|
|
94
101
|
}
|
package/src/contexts/payment.tsx
CHANGED
|
@@ -21,6 +21,7 @@ export type PaymentContextType = {
|
|
|
21
21
|
prefix: string;
|
|
22
22
|
settings: Settings;
|
|
23
23
|
refresh: () => void;
|
|
24
|
+
getCurrency: (currencyId: string) => TPaymentCurrency | undefined;
|
|
24
25
|
setLivemode: (livemode: boolean) => void;
|
|
25
26
|
api: Axios;
|
|
26
27
|
};
|
|
@@ -34,6 +35,11 @@ const getSettings = async () => {
|
|
|
34
35
|
return data;
|
|
35
36
|
};
|
|
36
37
|
|
|
38
|
+
const getCurrency = (currencyId: string, methods: TPaymentMethodExpanded[]) => {
|
|
39
|
+
const currencies = methods.reduce((acc, x) => acc.concat(x.payment_currencies), [] as TPaymentCurrency[]);
|
|
40
|
+
return currencies.find((x) => x.id === currencyId);
|
|
41
|
+
};
|
|
42
|
+
|
|
37
43
|
// eslint-disable-next-line react/prop-types
|
|
38
44
|
function PaymentProvider({ children, session, connect }: { children: any; session: any; connect: any }): JSX.Element {
|
|
39
45
|
const { data, error, run, loading } = useRequest(getSettings);
|
|
@@ -55,6 +61,7 @@ function PaymentProvider({ children, session, connect }: { children: any; sessio
|
|
|
55
61
|
prefix,
|
|
56
62
|
livemode: !!livemode,
|
|
57
63
|
settings: data,
|
|
64
|
+
getCurrency: (currencyId: string) => getCurrency(currencyId, data.paymentMethods),
|
|
58
65
|
refresh: run,
|
|
59
66
|
setLivemode,
|
|
60
67
|
api,
|
package/src/payment/index.tsx
CHANGED
|
@@ -17,7 +17,7 @@ import { LiteralUnion } from 'type-fest';
|
|
|
17
17
|
|
|
18
18
|
import api from '../api';
|
|
19
19
|
import { usePaymentContext } from '../contexts/payment';
|
|
20
|
-
import { CheckoutCallbacks, CheckoutContext } from '../types';
|
|
20
|
+
import { CheckoutCallbacks, CheckoutContext, CheckoutFormData } from '../types';
|
|
21
21
|
import { findCurrency, formatError, getStatementDescriptor, isValidCountry } from '../util';
|
|
22
22
|
import PaymentError from './error';
|
|
23
23
|
import CheckoutFooter from './footer';
|
|
@@ -46,6 +46,7 @@ export default function Payment({
|
|
|
46
46
|
mode,
|
|
47
47
|
onPaid,
|
|
48
48
|
onError,
|
|
49
|
+
onChange,
|
|
49
50
|
goBack,
|
|
50
51
|
}: Props) {
|
|
51
52
|
const { t } = useLocaleContext();
|
|
@@ -77,7 +78,7 @@ export default function Payment({
|
|
|
77
78
|
if (!checkoutSession || !delay) {
|
|
78
79
|
return (
|
|
79
80
|
<Root mode={mode}>
|
|
80
|
-
<Stack className="cko-container">
|
|
81
|
+
<Stack className="cko-container" sx={{ gap: { sm: mode === 'standalone' ? 0 : 8 } }}>
|
|
81
82
|
<Stack className="cko-overview">
|
|
82
83
|
<OverviewSkeleton />
|
|
83
84
|
</Stack>
|
|
@@ -120,6 +121,7 @@ export default function Payment({
|
|
|
120
121
|
customer={customer as TCustomer}
|
|
121
122
|
onPaid={onPaid}
|
|
122
123
|
onError={onError}
|
|
124
|
+
onChange={onChange}
|
|
123
125
|
goBack={goBack}
|
|
124
126
|
mode={mode}
|
|
125
127
|
/>
|
|
@@ -142,6 +144,7 @@ export function PaymentInner({
|
|
|
142
144
|
mode,
|
|
143
145
|
onPaid,
|
|
144
146
|
onError,
|
|
147
|
+
onChange,
|
|
145
148
|
goBack,
|
|
146
149
|
}: MainProps) {
|
|
147
150
|
const { t } = useLocaleContext();
|
|
@@ -151,7 +154,7 @@ export function PaymentInner({
|
|
|
151
154
|
const defaultCurrencyId = state.checkoutSession.currency_id || state.checkoutSession.line_items[0]?.price.currency_id;
|
|
152
155
|
const defaultMethodId = paymentMethods.find((m) => m.payment_currencies.some((c) => c.id === defaultCurrencyId))?.id;
|
|
153
156
|
|
|
154
|
-
const methods = useForm({
|
|
157
|
+
const methods = useForm<CheckoutFormData>({
|
|
155
158
|
defaultValues: {
|
|
156
159
|
customer_name: customer?.name || session?.user?.fullName || '',
|
|
157
160
|
customer_email: customer?.email || session?.user?.email || '',
|
|
@@ -178,6 +181,12 @@ export function PaymentInner({
|
|
|
178
181
|
(findCurrency(paymentMethods as TPaymentMethodExpanded[], currencyId as string) as TPaymentCurrency) ||
|
|
179
182
|
settings.baseCurrency;
|
|
180
183
|
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
if (onChange) {
|
|
186
|
+
onChange(methods.getValues());
|
|
187
|
+
}
|
|
188
|
+
}, [currencyId]); // eslint-disable-line
|
|
189
|
+
|
|
181
190
|
const onUpsell = async (from: string, to: string) => {
|
|
182
191
|
try {
|
|
183
192
|
const { data } = await api.put(`/api/checkout-sessions/${state.checkoutSession.id}/upsell`, { from, to });
|
package/src/types/index.ts
CHANGED
|
@@ -17,6 +17,22 @@ export type CheckoutContext = {
|
|
|
17
17
|
mode: LiteralUnion<'standalone' | 'inline' | 'popup', string>;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
export type CheckoutFormData = {
|
|
21
|
+
customer_name: string;
|
|
22
|
+
customer_email?: string;
|
|
23
|
+
customer_phone?: string;
|
|
24
|
+
payment_method: string;
|
|
25
|
+
payment_currency: string;
|
|
26
|
+
billing_address?: {
|
|
27
|
+
country: string;
|
|
28
|
+
state?: string;
|
|
29
|
+
city?: string;
|
|
30
|
+
line1?: string;
|
|
31
|
+
line2?: string;
|
|
32
|
+
postal_code: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
20
36
|
export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
21
37
|
id: string;
|
|
22
38
|
extraParams?: Record<string, any>;
|
|
@@ -26,5 +42,6 @@ export type CheckoutProps = Partial<CheckoutCallbacks> & {
|
|
|
26
42
|
export type CheckoutCallbacks = {
|
|
27
43
|
onPaid: (res: CheckoutContext) => void;
|
|
28
44
|
onError: (err: Error) => void;
|
|
45
|
+
onChange?: (data: CheckoutFormData) => void;
|
|
29
46
|
goBack?: () => void;
|
|
30
47
|
};
|