@blocklet/payment-react 1.13.113

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.
Files changed (173) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +29 -0
  3. package/babel.config.es.js +8 -0
  4. package/build.config.ts +29 -0
  5. package/es/api.d.ts +2 -0
  6. package/es/api.js +18 -0
  7. package/es/checkout/index.d.ts +15 -0
  8. package/es/checkout/index.js +61 -0
  9. package/es/components/input.d.ts +23 -0
  10. package/es/components/input.js +44 -0
  11. package/es/components/livemode.d.ts +2 -0
  12. package/es/components/livemode.js +24 -0
  13. package/es/components/pricing-table.d.ts +18 -0
  14. package/es/components/pricing-table.js +175 -0
  15. package/es/components/status.d.ts +3 -0
  16. package/es/components/status.js +20 -0
  17. package/es/components/switch.d.ts +6 -0
  18. package/es/components/switch.js +42 -0
  19. package/es/contexts/payment.d.ts +29 -0
  20. package/es/contexts/payment.js +45 -0
  21. package/es/dayjs.d.ts +2 -0
  22. package/es/dayjs.js +14 -0
  23. package/es/index.d.ts +16 -0
  24. package/es/index.js +29 -0
  25. package/es/locales/en.d.ts +2 -0
  26. package/es/locales/en.js +213 -0
  27. package/es/locales/index.d.ts +10 -0
  28. package/es/locales/index.js +20 -0
  29. package/es/locales/zh.d.ts +2 -0
  30. package/es/locales/zh.js +213 -0
  31. package/es/payment/amount.d.ts +12 -0
  32. package/es/payment/amount.js +22 -0
  33. package/es/payment/error.d.ts +13 -0
  34. package/es/payment/error.js +12 -0
  35. package/es/payment/footer.d.ts +4 -0
  36. package/es/payment/footer.js +9 -0
  37. package/es/payment/form/addon.d.ts +2 -0
  38. package/es/payment/form/addon.js +14 -0
  39. package/es/payment/form/address.d.ts +7 -0
  40. package/es/payment/form/address.js +119 -0
  41. package/es/payment/form/index.d.ts +9 -0
  42. package/es/payment/form/index.js +337 -0
  43. package/es/payment/form/phone.d.ts +4 -0
  44. package/es/payment/form/phone.js +97 -0
  45. package/es/payment/form/stripe.d.ts +13 -0
  46. package/es/payment/form/stripe.js +158 -0
  47. package/es/payment/header.d.ts +7 -0
  48. package/es/payment/header.js +29 -0
  49. package/es/payment/index.d.ts +28 -0
  50. package/es/payment/index.js +327 -0
  51. package/es/payment/product-card.d.ts +21 -0
  52. package/es/payment/product-card.js +34 -0
  53. package/es/payment/product-item.d.ts +19 -0
  54. package/es/payment/product-item.js +107 -0
  55. package/es/payment/product-skeleton.d.ts +4 -0
  56. package/es/payment/product-skeleton.js +34 -0
  57. package/es/payment/skeleton/overview.d.ts +2 -0
  58. package/es/payment/skeleton/overview.js +13 -0
  59. package/es/payment/skeleton/payment.d.ts +2 -0
  60. package/es/payment/skeleton/payment.js +19 -0
  61. package/es/payment/success.d.ts +8 -0
  62. package/es/payment/success.js +164 -0
  63. package/es/payment/summary.d.ts +12 -0
  64. package/es/payment/summary.js +178 -0
  65. package/es/theme.d.ts +1 -0
  66. package/es/theme.js +17 -0
  67. package/es/types/index.d.ts +19 -0
  68. package/es/types/index.js +0 -0
  69. package/es/types/shims.d.ts +18 -0
  70. package/es/util.d.ts +52 -0
  71. package/es/util.js +390 -0
  72. package/lib/api.d.ts +2 -0
  73. package/lib/api.js +26 -0
  74. package/lib/checkout/index.d.ts +15 -0
  75. package/lib/checkout/index.js +83 -0
  76. package/lib/components/input.d.ts +23 -0
  77. package/lib/components/input.js +72 -0
  78. package/lib/components/livemode.d.ts +2 -0
  79. package/lib/components/livemode.js +29 -0
  80. package/lib/components/pricing-table.d.ts +18 -0
  81. package/lib/components/pricing-table.js +232 -0
  82. package/lib/components/status.d.ts +3 -0
  83. package/lib/components/status.js +23 -0
  84. package/lib/components/switch.d.ts +6 -0
  85. package/lib/components/switch.js +51 -0
  86. package/lib/contexts/payment.d.ts +29 -0
  87. package/lib/contexts/payment.js +73 -0
  88. package/lib/dayjs.d.ts +2 -0
  89. package/lib/dayjs.js +21 -0
  90. package/lib/index.d.ts +16 -0
  91. package/lib/index.js +143 -0
  92. package/lib/locales/en.d.ts +2 -0
  93. package/lib/locales/en.js +220 -0
  94. package/lib/locales/index.d.ts +10 -0
  95. package/lib/locales/index.js +33 -0
  96. package/lib/locales/zh.d.ts +2 -0
  97. package/lib/locales/zh.js +220 -0
  98. package/lib/payment/amount.d.ts +12 -0
  99. package/lib/payment/amount.js +28 -0
  100. package/lib/payment/error.d.ts +13 -0
  101. package/lib/payment/error.js +52 -0
  102. package/lib/payment/footer.d.ts +4 -0
  103. package/lib/payment/footer.js +25 -0
  104. package/lib/payment/form/addon.d.ts +2 -0
  105. package/lib/payment/form/addon.js +37 -0
  106. package/lib/payment/form/address.d.ts +7 -0
  107. package/lib/payment/form/address.js +152 -0
  108. package/lib/payment/form/index.d.ts +9 -0
  109. package/lib/payment/form/index.js +464 -0
  110. package/lib/payment/form/phone.d.ts +4 -0
  111. package/lib/payment/form/phone.js +133 -0
  112. package/lib/payment/form/stripe.d.ts +13 -0
  113. package/lib/payment/form/stripe.js +213 -0
  114. package/lib/payment/header.d.ts +7 -0
  115. package/lib/payment/header.js +58 -0
  116. package/lib/payment/index.d.ts +28 -0
  117. package/lib/payment/index.js +382 -0
  118. package/lib/payment/product-card.d.ts +21 -0
  119. package/lib/payment/product-card.js +81 -0
  120. package/lib/payment/product-item.d.ts +19 -0
  121. package/lib/payment/product-item.js +160 -0
  122. package/lib/payment/product-skeleton.d.ts +4 -0
  123. package/lib/payment/product-skeleton.js +71 -0
  124. package/lib/payment/skeleton/overview.d.ts +2 -0
  125. package/lib/payment/skeleton/overview.js +48 -0
  126. package/lib/payment/skeleton/payment.d.ts +2 -0
  127. package/lib/payment/skeleton/payment.js +54 -0
  128. package/lib/payment/success.d.ts +8 -0
  129. package/lib/payment/success.js +215 -0
  130. package/lib/payment/summary.d.ts +12 -0
  131. package/lib/payment/summary.js +225 -0
  132. package/lib/theme.d.ts +1 -0
  133. package/lib/theme.js +19 -0
  134. package/lib/types/index.d.ts +19 -0
  135. package/lib/types/index.js +1 -0
  136. package/lib/types/shims.d.ts +18 -0
  137. package/lib/util.d.ts +52 -0
  138. package/lib/util.js +487 -0
  139. package/package.json +104 -0
  140. package/src/api.ts +24 -0
  141. package/src/checkout/index.tsx +74 -0
  142. package/src/components/input.tsx +58 -0
  143. package/src/components/livemode.tsx +23 -0
  144. package/src/components/pricing-table.tsx +207 -0
  145. package/src/components/status.tsx +19 -0
  146. package/src/components/switch.tsx +48 -0
  147. package/src/contexts/payment.tsx +74 -0
  148. package/src/dayjs.ts +17 -0
  149. package/src/index.ts +32 -0
  150. package/src/locales/en.tsx +218 -0
  151. package/src/locales/index.tsx +30 -0
  152. package/src/locales/zh.tsx +214 -0
  153. package/src/payment/amount.tsx +24 -0
  154. package/src/payment/error.tsx +29 -0
  155. package/src/payment/footer.tsx +12 -0
  156. package/src/payment/form/addon.tsx +24 -0
  157. package/src/payment/form/address.tsx +119 -0
  158. package/src/payment/form/index.tsx +401 -0
  159. package/src/payment/form/phone.tsx +103 -0
  160. package/src/payment/form/stripe.tsx +195 -0
  161. package/src/payment/header.tsx +40 -0
  162. package/src/payment/index.tsx +367 -0
  163. package/src/payment/product-card.tsx +55 -0
  164. package/src/payment/product-item.tsx +121 -0
  165. package/src/payment/product-skeleton.tsx +39 -0
  166. package/src/payment/skeleton/overview.tsx +21 -0
  167. package/src/payment/skeleton/payment.tsx +35 -0
  168. package/src/payment/success.tsx +186 -0
  169. package/src/payment/summary.tsx +198 -0
  170. package/src/theme.ts +18 -0
  171. package/src/types/index.ts +29 -0
  172. package/src/types/shims.d.ts +18 -0
  173. package/src/util.ts +543 -0
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ module.exports = AddressForm;
7
+ var _jsxRuntime = require("react/jsx-runtime");
8
+ var _context = require("@arcblock/ux/lib/Locale/context");
9
+ var _material = require("@mui/material");
10
+ var _reactHookForm = require("react-hook-form");
11
+ var _reactInternationalPhone = require("react-international-phone");
12
+ var _input = _interopRequireDefault(require("../../components/input"));
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+ function AddressForm({
15
+ mode,
16
+ stripe
17
+ }) {
18
+ const {
19
+ t
20
+ } = (0, _context.useLocaleContext)();
21
+ const {
22
+ control,
23
+ setValue
24
+ } = (0, _reactHookForm.useFormContext)();
25
+ if (mode === "required") {
26
+ return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Fade, {
27
+ in: true,
28
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
29
+ className: "cko-payment-address cko-payment-form",
30
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
31
+ sx: {
32
+ mb: 1,
33
+ color: "text.primary",
34
+ fontWeight: 600
35
+ },
36
+ children: t(`payment.checkout.billing.${mode}`)
37
+ }), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
38
+ direction: "column",
39
+ className: "cko-payment-form",
40
+ spacing: 0,
41
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
42
+ direction: "row",
43
+ spacing: 0,
44
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
45
+ name: "billing_address.country",
46
+ control,
47
+ render: ({
48
+ field
49
+ }) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactInternationalPhone.CountrySelector, {
50
+ selectedCountry: field.value,
51
+ onSelect: ({
52
+ iso2
53
+ }) => setValue(field.name, iso2),
54
+ buttonStyle: {
55
+ width: "64px",
56
+ height: "40px",
57
+ border: "1px solid #ccc",
58
+ marginLeft: -1,
59
+ marginTop: -1
60
+ }
61
+ })
62
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
63
+ name: "billing_address.postal_code",
64
+ rules: {
65
+ required: t("payment.checkout.required")
66
+ },
67
+ errorPosition: "right",
68
+ variant: "outlined",
69
+ placeholder: t("payment.checkout.billing.postal_code")
70
+ })]
71
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
72
+ name: "billing_address.state",
73
+ rules: {
74
+ required: t("payment.checkout.required")
75
+ },
76
+ errorPosition: "right",
77
+ variant: "outlined",
78
+ placeholder: t("payment.checkout.billing.state")
79
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
80
+ name: "billing_address.line1",
81
+ rules: {
82
+ required: t("payment.checkout.required")
83
+ },
84
+ errorPosition: "right",
85
+ variant: "outlined",
86
+ placeholder: t("payment.checkout.billing.line1")
87
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
88
+ name: "billing_address.city",
89
+ rules: {
90
+ required: t("payment.checkout.required")
91
+ },
92
+ errorPosition: "right",
93
+ variant: "outlined",
94
+ placeholder: t("payment.checkout.billing.city")
95
+ })]
96
+ })]
97
+ })
98
+ });
99
+ }
100
+ if (stripe) {
101
+ return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Fade, {
102
+ in: true,
103
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
104
+ className: "cko-payment-address cko-payment-form",
105
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
106
+ sx: {
107
+ mb: 1,
108
+ color: "text.primary",
109
+ fontWeight: 600
110
+ },
111
+ children: t(`payment.checkout.billing.${mode}`)
112
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Stack, {
113
+ direction: "column",
114
+ className: "cko-payment-form",
115
+ spacing: 0,
116
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
117
+ direction: "row",
118
+ spacing: 0,
119
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
120
+ name: "billing_address.country",
121
+ control,
122
+ render: ({
123
+ field
124
+ }) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactInternationalPhone.CountrySelector, {
125
+ selectedCountry: field.value,
126
+ onSelect: ({
127
+ iso2
128
+ }) => setValue(field.name, iso2),
129
+ buttonStyle: {
130
+ width: "64px",
131
+ height: "40px",
132
+ border: "1px solid #ccc",
133
+ marginLeft: -1,
134
+ marginTop: -1
135
+ }
136
+ })
137
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
138
+ name: "billing_address.postal_code",
139
+ rules: {
140
+ required: t("payment.checkout.required")
141
+ },
142
+ errorPosition: "right",
143
+ variant: "outlined",
144
+ placeholder: t("payment.checkout.billing.postal_code")
145
+ })]
146
+ })
147
+ })]
148
+ })
149
+ });
150
+ }
151
+ return null;
152
+ }
@@ -0,0 +1,9 @@
1
+ /// <reference types="react" />
2
+ import 'react-international-phone/style.css';
3
+ import { CheckoutCallbacks, CheckoutContext } from '../../types';
4
+ type PageData = CheckoutContext & CheckoutCallbacks;
5
+ declare function PaymentForm({ checkoutSession, paymentMethods, paymentIntent, customer, onPaid, onError, mode, }: PageData): import("react").JSX.Element;
6
+ declare namespace PaymentForm {
7
+ var defaultProps: {};
8
+ }
9
+ export default PaymentForm;
@@ -0,0 +1,464 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ module.exports = PaymentForm;
7
+ var _jsxRuntime = require("react/jsx-runtime");
8
+ require("react-international-phone/style.css");
9
+ var _context = require("@arcblock/ux/lib/Locale/context");
10
+ var _Theme = require("@arcblock/ux/lib/Theme");
11
+ var _Toast = _interopRequireDefault(require("@arcblock/ux/lib/Toast"));
12
+ var _lab = require("@mui/lab");
13
+ var _material = require("@mui/material");
14
+ var _ahooks = require("ahooks");
15
+ var _googleLibphonenumber = require("google-libphonenumber");
16
+ var _pWaitFor = _interopRequireDefault(require("p-wait-for"));
17
+ var _react = require("react");
18
+ var _reactHookForm = require("react-hook-form");
19
+ var _useBus = require("use-bus");
20
+ var _isEmail = _interopRequireDefault(require("validator/es/lib/isEmail"));
21
+ var _api = _interopRequireDefault(require("../../api"));
22
+ var _input = _interopRequireDefault(require("../../components/input"));
23
+ var _payment = require("../../contexts/payment");
24
+ var _util = require("../../util");
25
+ var _addon = _interopRequireDefault(require("./addon"));
26
+ var _address = _interopRequireDefault(require("./address"));
27
+ var _phone = _interopRequireDefault(require("./phone"));
28
+ var _stripe = _interopRequireDefault(require("./stripe"));
29
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
+ const phoneUtil = _googleLibphonenumber.PhoneNumberUtil.getInstance();
31
+ const waitForCheckoutComplete = async sessionId => {
32
+ let result;
33
+ await (0, _pWaitFor.default)(async () => {
34
+ const {
35
+ data
36
+ } = await _api.default.get(`/api/checkout-sessions/retrieve/${sessionId}`);
37
+ if (data.paymentIntent && data.paymentIntent.status === "requires_action" && data.paymentIntent.last_payment_error) {
38
+ throw new Error(data.paymentIntent.last_payment_error.message);
39
+ }
40
+ result = data;
41
+ return data.checkoutSession?.status === "complete" && ["paid", "no_payment_required"].includes(data.checkoutSession?.payment_status);
42
+ }, {
43
+ interval: 2e3,
44
+ timeout: 3 * 60 * 1e3
45
+ });
46
+ return result;
47
+ };
48
+ PaymentForm.defaultProps = {};
49
+ function PaymentForm({
50
+ checkoutSession,
51
+ paymentMethods,
52
+ paymentIntent,
53
+ customer,
54
+ onPaid,
55
+ onError,
56
+ mode
57
+ }) {
58
+ const theme = (0, _Theme.useTheme)();
59
+ const {
60
+ t
61
+ } = (0, _context.useLocaleContext)();
62
+ const {
63
+ session,
64
+ connect
65
+ } = (0, _payment.usePaymentContext)();
66
+ const {
67
+ control,
68
+ getValues,
69
+ setValue,
70
+ handleSubmit
71
+ } = (0, _reactHookForm.useFormContext)();
72
+ const [state, setState] = (0, _ahooks.useSetState)({
73
+ submitting: false,
74
+ paying: false,
75
+ paid: false,
76
+ paymentIntent,
77
+ stripeContext: void 0,
78
+ customer,
79
+ stripePaying: false
80
+ });
81
+ (0, _react.useEffect)(() => {
82
+ if (session.user) {
83
+ const values = getValues();
84
+ if (!values.customer_name) {
85
+ setValue("customer_name", session.user.fullName);
86
+ }
87
+ if (!values.customer_email) {
88
+ setValue("customer_email", session.user.email);
89
+ }
90
+ if (!values.customer_phone) {
91
+ setValue("customer_phone", session.user.phone);
92
+ }
93
+ }
94
+ }, [session.user, getValues, setValue]);
95
+ const paymentMethod = (0, _reactHookForm.useWatch)({
96
+ control,
97
+ name: "payment_method"
98
+ });
99
+ const paymentCurrency = (0, _reactHookForm.useWatch)({
100
+ control,
101
+ name: "payment_currency"
102
+ });
103
+ const paymentCurrencies = paymentMethods.find(x => x.id === paymentMethod)?.payment_currencies || [];
104
+ const domSize = (0, _ahooks.useSize)(document.body);
105
+ const isColumnLayout = (0, _ahooks.useCreation)(() => {
106
+ if (domSize) {
107
+ if (domSize?.width <= theme.breakpoints.values.md) {
108
+ return true;
109
+ }
110
+ }
111
+ return false;
112
+ }, [domSize, theme]);
113
+ const payee = (0, _util.getStatementDescriptor)(checkoutSession.line_items);
114
+ const buttonText = session.user ? t(`payment.checkout.${checkoutSession.mode}`) : t("payment.checkout.connect", {
115
+ action: t(`payment.checkout.${checkoutSession.mode}`)
116
+ });
117
+ const method = paymentMethods.find(x => x.id === paymentMethod);
118
+ const handleMethodChange = e => {
119
+ setValue("payment_method", e.target.value);
120
+ const currencies = paymentMethods.find(x => x.id === e.target.value)?.payment_currencies || [];
121
+ if (currencies.some(x => x.id === paymentCurrency) === false) {
122
+ setValue("payment_currency", currencies[0]?.id);
123
+ }
124
+ };
125
+ const handleConnected = async () => {
126
+ try {
127
+ const result = await waitForCheckoutComplete(checkoutSession.id);
128
+ setState({
129
+ paid: true,
130
+ paying: false
131
+ });
132
+ onPaid(result);
133
+ } catch (err) {
134
+ _Toast.default.error((0, _util.formatError)(err));
135
+ } finally {
136
+ setState({
137
+ paying: false
138
+ });
139
+ }
140
+ };
141
+ const onUserLoggedIn = async () => {
142
+ const {
143
+ data: profile
144
+ } = await _api.default.get("/api/customers/me");
145
+ if (profile) {
146
+ const values = getValues();
147
+ if (!values.customer_name) {
148
+ setValue("customer_name", profile.name);
149
+ }
150
+ if (!values.customer_email) {
151
+ setValue("customer_email", profile.email);
152
+ }
153
+ if (!values.customer_phone) {
154
+ setValue("customer_phone", profile.phone);
155
+ }
156
+ if (profile.address?.country) {
157
+ setValue("billing_address.country", profile.address.country);
158
+ }
159
+ if (profile.address?.state) {
160
+ setValue("billing_address.state", profile.address.state);
161
+ }
162
+ if (profile.address?.line1) {
163
+ setValue("billing_address.line1", profile.address.line1);
164
+ }
165
+ if (profile.address?.line2) {
166
+ setValue("billing_address.line2", profile.address.line2);
167
+ }
168
+ if (profile.address?.city) {
169
+ setValue("billing_address.city", profile.address.city);
170
+ }
171
+ if (profile.address?.postal_code) {
172
+ setValue("billing_address.postal_code", profile.address.postal_code);
173
+ }
174
+ }
175
+ };
176
+ const onSubmit = async data => {
177
+ setState({
178
+ submitting: true
179
+ });
180
+ try {
181
+ const result = await _api.default.put(`/api/checkout-sessions/${checkoutSession.id}/submit`, data);
182
+ setState({
183
+ paymentIntent: result.data.paymentIntent,
184
+ stripeContext: result.data.stripeContext,
185
+ customer: result.data.customer,
186
+ submitting: false
187
+ });
188
+ if (["arcblock", "ethereum"].includes(method.type)) {
189
+ setState({
190
+ paying: true
191
+ });
192
+ if (result.data.balance?.sufficient || result.data.delegation?.sufficient) {
193
+ await handleConnected();
194
+ } else {
195
+ connect.open({
196
+ action: checkoutSession.mode,
197
+ timeout: 5 * 60 * 1e3,
198
+ extraParams: {
199
+ checkoutSessionId: checkoutSession.id
200
+ },
201
+ onSuccess: async () => {
202
+ connect.close();
203
+ await handleConnected();
204
+ },
205
+ onClose: () => {
206
+ connect.close();
207
+ setState({
208
+ submitting: false,
209
+ paying: false
210
+ });
211
+ },
212
+ onError: err => {
213
+ setState({
214
+ submitting: false,
215
+ paying: false
216
+ });
217
+ onError(err);
218
+ }
219
+ });
220
+ }
221
+ }
222
+ if (["stripe"].includes(method.type)) {
223
+ if (result.data.stripeContext?.status === "succeeded") {
224
+ setState({
225
+ paying: true
226
+ });
227
+ } else {
228
+ setState({
229
+ stripePaying: true
230
+ });
231
+ }
232
+ }
233
+ } catch (err) {
234
+ if (err.response?.data?.code) {
235
+ (0, _useBus.dispatch)(`error.${err.response?.data?.code}`);
236
+ }
237
+ _Toast.default.error((0, _util.formatError)(err));
238
+ } finally {
239
+ setState({
240
+ submitting: false
241
+ });
242
+ }
243
+ };
244
+ const onAction = () => {
245
+ if (session.user) {
246
+ handleSubmit(onSubmit)();
247
+ } else {
248
+ session.login({
249
+ onSuccess: onUserLoggedIn,
250
+ extraParams: {}
251
+ });
252
+ }
253
+ };
254
+ const onStripeConfirm = async () => {
255
+ setState({
256
+ stripePaying: false,
257
+ paying: true
258
+ });
259
+ await handleConnected();
260
+ };
261
+ const onStripeCancel = () => {
262
+ setState({
263
+ stripePaying: false
264
+ });
265
+ };
266
+ return /* @__PURE__ */(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
267
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Fade, {
268
+ in: true,
269
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
270
+ className: "cko-payment-contact",
271
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
272
+ direction: "row",
273
+ sx: {
274
+ mb: 1
275
+ },
276
+ alignItems: "center",
277
+ justifyContent: "space-between",
278
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
279
+ sx: {
280
+ color: "text.primary",
281
+ fontWeight: 600
282
+ },
283
+ children: t("payment.checkout.contact")
284
+ }), isColumnLayout || mode !== "standalone" ? null : /* @__PURE__ */(0, _jsxRuntime.jsx)(_addon.default, {})]
285
+ }), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
286
+ direction: "column",
287
+ className: "cko-payment-form",
288
+ spacing: 0,
289
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
290
+ name: "customer_name",
291
+ variant: "outlined",
292
+ errorPosition: "right",
293
+ rules: {
294
+ required: t("payment.checkout.required")
295
+ },
296
+ InputProps: {
297
+ startAdornment: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.InputAdornment, {
298
+ position: "start",
299
+ children: t("payment.checkout.customer.name")
300
+ })
301
+ }
302
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_input.default, {
303
+ name: "customer_email",
304
+ variant: "outlined",
305
+ errorPosition: "right",
306
+ rules: {
307
+ required: t("payment.checkout.required"),
308
+ validate: x => (0, _isEmail.default)(x) ? true : t("payment.checkout.invalid")
309
+ },
310
+ InputProps: {
311
+ startAdornment: /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.InputAdornment, {
312
+ position: "start",
313
+ children: t("payment.checkout.customer.email")
314
+ })
315
+ }
316
+ }), checkoutSession.phone_number_collection?.enabled && /* @__PURE__ */(0, _jsxRuntime.jsx)(_phone.default, {
317
+ name: "customer_phone",
318
+ variant: "outlined",
319
+ errorPosition: "right",
320
+ placeholder: "Phone number",
321
+ rules: {
322
+ required: t("payment.checkout.required"),
323
+ validate: x => {
324
+ try {
325
+ const parsed = phoneUtil.parseAndKeepRawInput(x);
326
+ return phoneUtil.isValidNumber(parsed) ? true : t("payment.checkout.invalid");
327
+ } catch {
328
+ return t("payment.checkout.invalid");
329
+ }
330
+ }
331
+ }
332
+ })]
333
+ })]
334
+ })
335
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_address.default, {
336
+ mode: checkoutSession.billing_address_collection,
337
+ stripe: method?.type === "stripe"
338
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Fade, {
339
+ in: true,
340
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
341
+ direction: "column",
342
+ alignItems: "flex-start",
343
+ className: "cko-payment-methods",
344
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
345
+ sx: {
346
+ mb: 2,
347
+ color: "text.primary",
348
+ fontWeight: 600
349
+ },
350
+ children: t("payment.checkout.method")
351
+ }), /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
352
+ direction: "row",
353
+ sx: {
354
+ width: 1
355
+ },
356
+ spacing: 1,
357
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
358
+ name: "payment_method",
359
+ control,
360
+ render: ({
361
+ field
362
+ }) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Select, {
363
+ ...field,
364
+ onChange: handleMethodChange,
365
+ sx: {
366
+ flex: 1
367
+ },
368
+ size: "small",
369
+ children: paymentMethods.map(x => {
370
+ const selected = x.id === paymentMethod;
371
+ return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.MenuItem, {
372
+ value: x.id,
373
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
374
+ direction: "row",
375
+ spacing: 1,
376
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Avatar, {
377
+ src: x.logo,
378
+ alt: x.name,
379
+ sx: {
380
+ width: 20,
381
+ height: 20
382
+ }
383
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
384
+ color: selected ? "text.primary" : "text.secondary",
385
+ children: x.name
386
+ })]
387
+ })
388
+ }, x.id);
389
+ })
390
+ })
391
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactHookForm.Controller, {
392
+ name: "payment_currency",
393
+ control,
394
+ render: ({
395
+ field
396
+ }) => /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Select, {
397
+ ...field,
398
+ sx: {
399
+ flex: 1
400
+ },
401
+ size: "small",
402
+ children: paymentCurrencies.map(x => {
403
+ const selected = x.id === paymentCurrency;
404
+ return /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.MenuItem, {
405
+ value: x.id,
406
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
407
+ direction: "row",
408
+ spacing: 1,
409
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Avatar, {
410
+ src: x.logo,
411
+ alt: x.name,
412
+ sx: {
413
+ width: 20,
414
+ height: 20
415
+ }
416
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
417
+ color: selected ? "text.primary" : "text.secondary",
418
+ children: x.symbol
419
+ })]
420
+ })
421
+ }, x.id);
422
+ })
423
+ })
424
+ })]
425
+ }), state.stripePaying && state.stripeContext && /* @__PURE__ */(0, _jsxRuntime.jsx)(_stripe.default, {
426
+ clientSecret: state.stripeContext.client_secret,
427
+ intentType: state.stripeContext.intent_type,
428
+ publicKey: method.settings.stripe?.publishable_key,
429
+ customer: state.customer,
430
+ mode: checkoutSession.mode,
431
+ onConfirm: onStripeConfirm,
432
+ onCancel: onStripeCancel
433
+ })]
434
+ })
435
+ }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Fade, {
436
+ in: true,
437
+ children: /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
438
+ className: "cko-payment-submit",
439
+ children: [/* @__PURE__ */(0, _jsxRuntime.jsx)(_lab.LoadingButton, {
440
+ variant: "contained",
441
+ color: "primary",
442
+ size: "large",
443
+ onClick: onAction,
444
+ fullWidth: true,
445
+ loadingPosition: "end",
446
+ disabled: state.submitting || state.paying || state.stripePaying,
447
+ loading: state.submitting || state.paying,
448
+ children: state.submitting || state.paying ? t("payment.checkout.processing") : buttonText
449
+ }), ["subscription", "setup"].includes(checkoutSession.mode) && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
450
+ sx: {
451
+ mt: 1,
452
+ color: "text.secondary",
453
+ fontSize: "0.9rem",
454
+ lineHeight: "1.1rem",
455
+ textAlign: "center"
456
+ },
457
+ children: t("payment.checkout.confirm", {
458
+ payee
459
+ })
460
+ })]
461
+ })
462
+ })]
463
+ });
464
+ }
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ export default function PhoneInput({ ...props }: {
3
+ [x: string]: any;
4
+ }): import("react").JSX.Element;