@blocklet/payment-react 1.14.21 → 1.14.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. package/es/checkout/donate.d.ts +2 -1
  2. package/es/checkout/donate.js +9 -10
  3. package/es/checkout/form.d.ts +1 -1
  4. package/es/checkout/form.js +23 -1
  5. package/es/checkout/table.d.ts +1 -1
  6. package/es/checkout/table.js +8 -1
  7. package/es/components/blockchain/tx.js +2 -1
  8. package/es/components/country-select.d.ts +16 -0
  9. package/es/components/country-select.js +82 -0
  10. package/es/components/input.d.ts +21 -21
  11. package/es/components/input.js +43 -42
  12. package/es/components/livemode.js +1 -0
  13. package/es/components/pricing-table.js +0 -2
  14. package/es/components/status.js +2 -3
  15. package/es/components/table.d.ts +2 -0
  16. package/es/components/table.js +186 -0
  17. package/es/contexts/payment.d.ts +2 -0
  18. package/es/contexts/payment.js +5 -2
  19. package/es/history/invoice/list.d.ts +3 -1
  20. package/es/history/invoice/list.js +215 -48
  21. package/es/hooks/mobile.d.ts +4 -0
  22. package/es/hooks/mobile.js +10 -0
  23. package/es/index.d.ts +5 -1
  24. package/es/index.js +7 -1
  25. package/es/libs/util.d.ts +15 -2
  26. package/es/libs/util.js +92 -28
  27. package/es/locales/en.js +22 -7
  28. package/es/locales/index.d.ts +0 -1
  29. package/es/locales/index.js +10 -1
  30. package/es/locales/zh.js +21 -6
  31. package/es/payment/error.js +2 -2
  32. package/es/payment/footer.js +1 -1
  33. package/es/payment/form/address.d.ts +9 -2
  34. package/es/payment/form/address.js +69 -69
  35. package/es/payment/form/currency.js +39 -25
  36. package/es/payment/form/index.d.ts +1 -1
  37. package/es/payment/form/index.js +83 -81
  38. package/es/payment/form/phone.js +15 -51
  39. package/es/payment/index.d.ts +1 -10
  40. package/es/payment/index.js +274 -219
  41. package/es/payment/product-card.js +4 -4
  42. package/es/payment/product-donation.js +7 -2
  43. package/es/payment/product-item.d.ts +2 -2
  44. package/es/payment/product-item.js +120 -81
  45. package/es/payment/summary.js +188 -118
  46. package/es/theme/index.css +240 -0
  47. package/es/theme/index.d.ts +9 -0
  48. package/es/theme/index.js +243 -0
  49. package/es/theme/typography.d.ts +2 -0
  50. package/es/theme/typography.js +53 -0
  51. package/es/types/index.d.ts +11 -0
  52. package/lib/checkout/donate.d.ts +2 -1
  53. package/lib/checkout/donate.js +14 -2
  54. package/lib/checkout/form.d.ts +1 -1
  55. package/lib/checkout/form.js +22 -1
  56. package/lib/checkout/table.d.ts +1 -1
  57. package/lib/checkout/table.js +14 -1
  58. package/lib/components/blockchain/tx.js +4 -1
  59. package/lib/components/country-select.d.ts +16 -0
  60. package/lib/components/country-select.js +115 -0
  61. package/lib/components/input.d.ts +21 -21
  62. package/lib/components/input.js +21 -12
  63. package/lib/components/livemode.js +1 -0
  64. package/lib/components/pricing-table.js +0 -2
  65. package/lib/components/status.js +2 -3
  66. package/lib/components/table.d.ts +2 -0
  67. package/lib/components/table.js +220 -0
  68. package/lib/contexts/payment.d.ts +2 -0
  69. package/lib/contexts/payment.js +4 -1
  70. package/lib/history/invoice/list.d.ts +3 -1
  71. package/lib/history/invoice/list.js +290 -62
  72. package/lib/hooks/mobile.d.ts +4 -0
  73. package/lib/hooks/mobile.js +17 -0
  74. package/lib/index.d.ts +5 -1
  75. package/lib/index.js +36 -0
  76. package/lib/libs/util.d.ts +15 -2
  77. package/lib/libs/util.js +115 -37
  78. package/lib/locales/en.js +22 -7
  79. package/lib/locales/index.d.ts +0 -1
  80. package/lib/locales/index.js +14 -3
  81. package/lib/locales/zh.js +21 -6
  82. package/lib/payment/error.js +5 -1
  83. package/lib/payment/footer.js +1 -1
  84. package/lib/payment/form/address.d.ts +9 -2
  85. package/lib/payment/form/address.js +67 -59
  86. package/lib/payment/form/currency.js +31 -24
  87. package/lib/payment/form/index.d.ts +1 -1
  88. package/lib/payment/form/index.js +92 -93
  89. package/lib/payment/form/phone.js +11 -59
  90. package/lib/payment/index.d.ts +1 -10
  91. package/lib/payment/index.js +291 -219
  92. package/lib/payment/product-card.js +5 -4
  93. package/lib/payment/product-donation.js +9 -2
  94. package/lib/payment/product-item.d.ts +2 -2
  95. package/lib/payment/product-item.js +38 -19
  96. package/lib/payment/summary.js +219 -127
  97. package/lib/theme/index.css +240 -0
  98. package/lib/theme/index.d.ts +9 -0
  99. package/lib/theme/index.js +259 -0
  100. package/lib/theme/typography.d.ts +2 -0
  101. package/lib/theme/typography.js +59 -0
  102. package/lib/types/index.d.ts +11 -0
  103. package/package.json +14 -11
  104. package/src/checkout/donate.tsx +16 -10
  105. package/src/checkout/form.tsx +23 -0
  106. package/src/checkout/table.tsx +13 -1
  107. package/src/components/blockchain/tx.tsx +2 -1
  108. package/src/components/country-select.tsx +93 -0
  109. package/src/components/input.tsx +49 -46
  110. package/src/components/livemode.tsx +1 -0
  111. package/src/components/pricing-table.tsx +0 -2
  112. package/src/components/status.tsx +1 -2
  113. package/src/components/table.tsx +200 -0
  114. package/src/contexts/payment.tsx +6 -1
  115. package/src/history/invoice/list.tsx +254 -49
  116. package/src/hooks/mobile.ts +13 -0
  117. package/src/index.ts +7 -0
  118. package/src/libs/util.ts +120 -31
  119. package/src/locales/en.tsx +18 -4
  120. package/src/locales/index.tsx +10 -3
  121. package/src/locales/zh.tsx +17 -3
  122. package/src/payment/error.tsx +2 -2
  123. package/src/payment/footer.tsx +1 -1
  124. package/src/payment/form/address.tsx +56 -47
  125. package/src/payment/form/currency.tsx +29 -23
  126. package/src/payment/form/index.tsx +89 -76
  127. package/src/payment/form/phone.tsx +14 -51
  128. package/src/payment/index.tsx +294 -242
  129. package/src/payment/product-card.tsx +4 -4
  130. package/src/payment/product-donation.tsx +7 -3
  131. package/src/payment/product-item.tsx +49 -20
  132. package/src/payment/summary.tsx +191 -108
  133. package/src/theme/index.css +240 -0
  134. package/src/theme/index.tsx +250 -0
  135. package/src/theme/typography.ts +56 -0
  136. package/src/types/index.ts +12 -0
@@ -9,8 +9,9 @@ type Props = {
9
9
  pageSize?: number;
10
10
  target?: string;
11
11
  action?: string;
12
+ type?: 'list' | 'table';
12
13
  };
13
- declare function CustomerInvoiceList({ customer_id, subscription_id, currency_id, include_staking, include_recovered_from, status, pageSize, target, action, }: Props): import("react").JSX.Element;
14
+ declare function CustomerInvoiceList(props: Props): JSX.Element;
14
15
  declare namespace CustomerInvoiceList {
15
16
  var defaultProps: {
16
17
  customer_id: string;
@@ -22,6 +23,7 @@ declare namespace CustomerInvoiceList {
22
23
  pageSize: number;
23
24
  target: string;
24
25
  action: string;
26
+ type: string;
25
27
  };
26
28
  }
27
29
  export default CustomerInvoiceList;
@@ -4,8 +4,8 @@ import Toast from "@arcblock/ux/lib/Toast";
4
4
  import { OpenInNewOutlined } from "@mui/icons-material";
5
5
  import { Box, Button, CircularProgress, Hidden, Stack, Typography } from "@mui/material";
6
6
  import { styled } from "@mui/system";
7
- import { useInfiniteScroll, useSetState } from "ahooks";
8
- import { useEffect } from "react";
7
+ import { useInfiniteScroll, useRequest, useSetState } from "ahooks";
8
+ import React, { useEffect, useState } from "react";
9
9
  import { joinURL } from "ufo";
10
10
  import Status from "../../components/status.js";
11
11
  import { usePaymentContext } from "../../contexts/payment.js";
@@ -20,6 +20,7 @@ import {
20
20
  getPrefix,
21
21
  getTxLink
22
22
  } from "../../libs/util.js";
23
+ import Table from "../../components/table.js";
23
24
  const groupByDate = (items) => {
24
25
  const grouped = {};
25
26
  items.forEach((item) => {
@@ -57,22 +58,178 @@ const getInvoiceLink = (invoice, action) => {
57
58
  url: getTxLink(invoice.paymentMethod, invoice.metadata?.payment_details).link
58
59
  };
59
60
  };
60
- export default function CustomerInvoiceList({
61
- customer_id,
62
- subscription_id,
63
- currency_id,
64
- include_staking,
65
- include_recovered_from,
66
- status,
67
- pageSize,
68
- target,
69
- action
70
- }) {
61
+ const InvoiceTable = React.memo((props) => {
62
+ const {
63
+ pageSize,
64
+ target,
65
+ action,
66
+ onPay,
67
+ status,
68
+ customer_id,
69
+ currency_id,
70
+ subscription_id,
71
+ include_staking,
72
+ include_recovered_from
73
+ } = props;
74
+ const listKey = "invoice-table";
75
+ const { t, locale } = useLocaleContext();
76
+ const [search, setSearch] = useState({
77
+ pageSize: pageSize || 10,
78
+ page: 1
79
+ });
80
+ const { loading, data = { list: [], count: 0 } } = useRequest(
81
+ () => fetchData({
82
+ ...search,
83
+ status,
84
+ customer_id,
85
+ currency_id,
86
+ subscription_id,
87
+ include_staking,
88
+ include_recovered_from,
89
+ ignore_zero: true
90
+ }),
91
+ {
92
+ refreshDeps: [search]
93
+ }
94
+ );
95
+ const columns = [
96
+ {
97
+ label: t("payment.customer.invoice.invoiceNumber"),
98
+ name: "number",
99
+ options: {
100
+ customBodyRenderLite: (_, index) => {
101
+ const invoice = data?.list[index];
102
+ const link = getInvoiceLink(invoice, action);
103
+ return /* @__PURE__ */ jsx("a", { href: link.url, target: link.external ? "_blank" : target, rel: "noreferrer", children: invoice?.number });
104
+ }
105
+ }
106
+ },
107
+ {
108
+ label: t("common.amount"),
109
+ name: "total",
110
+ options: {
111
+ customBodyRenderLite: (_, index) => {
112
+ const invoice = data?.list[index];
113
+ const link = getInvoiceLink(invoice, action);
114
+ return /* @__PURE__ */ jsx("a", { href: link.url, target: link.external ? "_blank" : target, rel: "noreferrer", children: /* @__PURE__ */ jsxs(Typography, { children: [
115
+ formatBNStr(invoice.total, invoice.paymentCurrency.decimal),
116
+ "\xA0",
117
+ invoice.paymentCurrency.symbol
118
+ ] }) });
119
+ }
120
+ }
121
+ },
122
+ {
123
+ label: t("common.updatedAt"),
124
+ name: "name",
125
+ options: {
126
+ customBodyRenderLite: (val, index) => {
127
+ const invoice = data?.list[index];
128
+ const link = getInvoiceLink(invoice, action);
129
+ return /* @__PURE__ */ jsx("a", { href: link.url, target: link.external ? "_blank" : target, rel: "noreferrer", children: formatToDate(invoice.created_at, locale, "YYYY-MM-DD HH:mm:ss") });
130
+ }
131
+ }
132
+ },
133
+ {
134
+ label: t("common.description"),
135
+ name: "",
136
+ options: {
137
+ sort: false,
138
+ customBodyRenderLite: (val, index) => {
139
+ const invoice = data?.list[index];
140
+ const link = getInvoiceLink(invoice, action);
141
+ return /* @__PURE__ */ jsx("a", { href: link.url, target: link.external ? "_blank" : target, rel: "noreferrer", children: invoice.description || invoice.id });
142
+ }
143
+ }
144
+ },
145
+ {
146
+ label: t("common.status"),
147
+ name: "created_at",
148
+ options: {
149
+ sort: true,
150
+ customBodyRenderLite: (val, index) => {
151
+ const invoice = data?.list[index];
152
+ const link = getInvoiceLink(invoice, action);
153
+ if (action) {
154
+ return link.connect ? /* @__PURE__ */ jsx(Button, { variant: "text", size: "small", onClick: () => onPay(invoice.id), sx: { color: "text.link" }, children: t("payment.customer.invoice.pay") }) : /* @__PURE__ */ jsx(
155
+ Button,
156
+ {
157
+ component: "a",
158
+ variant: "text",
159
+ size: "small",
160
+ href: link.url,
161
+ target: link.external ? "_blank" : target,
162
+ sx: { color: "var(--foregrounds-fg-interactive, #0086FF) !important" },
163
+ rel: "noreferrer",
164
+ children: t("payment.customer.invoice.pay")
165
+ }
166
+ );
167
+ }
168
+ return /* @__PURE__ */ jsx("a", { href: link.url, target: link.external ? "_blank" : target, rel: "noreferrer", children: /* @__PURE__ */ jsx(Status, { label: invoice.status, color: getInvoiceStatusColor(invoice.status) }) });
169
+ }
170
+ }
171
+ }
172
+ ];
173
+ const onTableChange = ({ page, rowsPerPage }) => {
174
+ if (search.pageSize !== rowsPerPage) {
175
+ setSearch((x) => ({ ...x, pageSize: rowsPerPage, page: 1 }));
176
+ } else if (search.page !== page + 1) {
177
+ setSearch((x) => ({ ...x, page: page + 1 }));
178
+ }
179
+ };
180
+ return /* @__PURE__ */ jsx(InvoiceTableRoot, { children: /* @__PURE__ */ jsx(
181
+ Table,
182
+ {
183
+ hasRowLink: true,
184
+ durable: `__${listKey}__`,
185
+ durableKeys: ["page", "rowsPerPage", "searchText"],
186
+ data: data.list,
187
+ columns,
188
+ options: {
189
+ count: data.count,
190
+ page: search.page - 1,
191
+ rowsPerPage: search.pageSize
192
+ },
193
+ loading,
194
+ onChange: onTableChange,
195
+ toolbar: false,
196
+ sx: { mt: 2 },
197
+ showMobile: false,
198
+ mobileTDFlexDirection: "row",
199
+ emptyNodeText: t("payment.customer.invoice.emptyList")
200
+ }
201
+ ) });
202
+ });
203
+ const InvoiceTableRoot = styled(Box)`
204
+ @media (max-width: ${({ theme }) => theme.breakpoints.values.md}px) {
205
+ .MuiTable-root > .MuiTableBody-root > .MuiTableRow-root > td.MuiTableCell-root {
206
+ > div {
207
+ width: fit-content;
208
+ flex: inherit;
209
+ font-size: 14px;
210
+ }
211
+ }
212
+ .invoice-summary {
213
+ padding-right: 20px;
214
+ }
215
+ }
216
+ `;
217
+ const InvoiceList = React.memo((props) => {
218
+ const {
219
+ customer_id,
220
+ subscription_id,
221
+ include_recovered_from,
222
+ currency_id,
223
+ include_staking,
224
+ status,
225
+ pageSize,
226
+ target,
227
+ action,
228
+ onPay
229
+ } = props;
71
230
  const size = pageSize || 10;
72
231
  const subscription = useSubscription("events");
73
232
  const { t, locale } = useLocaleContext();
74
- const { connect } = usePaymentContext();
75
- const [state, setState] = useSetState({ paying: "" });
76
233
  const { data, loadMore, loadingMore, loading, reloadAsync } = useInfiniteScroll(
77
234
  (d) => {
78
235
  const page = d ? Math.ceil(d.list.length / size) + 1 : 1;
@@ -101,36 +258,6 @@ export default function CustomerInvoiceList({
101
258
  });
102
259
  }
103
260
  }, [subscription]);
104
- const onPay = (invoiceId) => {
105
- if (state.paying) {
106
- return;
107
- }
108
- setState({ paying: invoiceId });
109
- connect.open({
110
- action: "collect",
111
- saveConnect: false,
112
- messages: {
113
- scan: "",
114
- title: t(`payment.customer.invoice.${action || "pay"}`),
115
- success: t(`payment.customer.invoice.${action || "pay"}Success`),
116
- error: t(`payment.customer.invoice.${action || "pay"}Error`),
117
- confirm: ""
118
- },
119
- extraParams: { invoiceId, action },
120
- onSuccess: () => {
121
- connect.close();
122
- setState({ paying: "" });
123
- },
124
- onClose: () => {
125
- connect.close();
126
- setState({ paying: "" });
127
- },
128
- onError: (err) => {
129
- setState({ paying: "" });
130
- Toast.error(formatError(err));
131
- }
132
- });
133
- };
134
261
  if (loading || !data) {
135
262
  return /* @__PURE__ */ jsx(CircularProgress, {});
136
263
  }
@@ -176,7 +303,6 @@ export default function CustomerInvoiceList({
176
303
  {
177
304
  component: "a",
178
305
  variant: "contained",
179
- color: "primary",
180
306
  size: "small",
181
307
  href: link.url,
182
308
  target: link.external ? "_blank" : target,
@@ -195,6 +321,46 @@ export default function CustomerInvoiceList({
195
321
  !hasMore && data.count > size && /* @__PURE__ */ jsx(Typography, { color: "text.secondary", children: t("common.noMore", { resource: t("payment.customer.invoices") }) })
196
322
  ] })
197
323
  ] });
324
+ });
325
+ export default function CustomerInvoiceList(props) {
326
+ const { action, type } = props;
327
+ const { t } = useLocaleContext();
328
+ const { connect } = usePaymentContext();
329
+ const [state, setState] = useSetState({ paying: "" });
330
+ const onPay = (invoiceId) => {
331
+ if (state.paying) {
332
+ return;
333
+ }
334
+ setState({ paying: invoiceId });
335
+ connect.open({
336
+ action: "collect",
337
+ saveConnect: false,
338
+ messages: {
339
+ scan: "",
340
+ title: t(`payment.customer.invoice.${action || "pay"}`),
341
+ success: t(`payment.customer.invoice.${action || "pay"}Success`),
342
+ error: t(`payment.customer.invoice.${action || "pay"}Error`),
343
+ confirm: ""
344
+ },
345
+ extraParams: { invoiceId, action },
346
+ onSuccess: () => {
347
+ connect.close();
348
+ setState({ paying: "" });
349
+ },
350
+ onClose: () => {
351
+ connect.close();
352
+ setState({ paying: "" });
353
+ },
354
+ onError: (err) => {
355
+ setState({ paying: "" });
356
+ Toast.error(formatError(err));
357
+ }
358
+ });
359
+ };
360
+ if (type === "table") {
361
+ return /* @__PURE__ */ jsx(InvoiceTable, { ...props, onPay });
362
+ }
363
+ return /* @__PURE__ */ jsx(InvoiceList, { ...props, onPay });
198
364
  }
199
365
  CustomerInvoiceList.defaultProps = {
200
366
  customer_id: "",
@@ -205,10 +371,11 @@ CustomerInvoiceList.defaultProps = {
205
371
  status: "open,paid,uncollectible",
206
372
  pageSize: 10,
207
373
  target: "_self",
208
- action: ""
374
+ action: "",
375
+ type: "list"
209
376
  };
210
377
  const Root = styled(Stack)`
211
- @media (max-width: 600px) {
378
+ @media (max-width: ${({ theme }) => theme.breakpoints.values.md}px) {
212
379
  .invoice-description {
213
380
  display: none !important;
214
381
  }
@@ -0,0 +1,4 @@
1
+ export declare function useMobile(mobilePoint?: 'md' | 'sm' | 'lg' | 'xl' | 'xs'): {
2
+ isMobile: boolean;
3
+ mobileSize: string;
4
+ };
@@ -0,0 +1,10 @@
1
+ import { useTheme } from "@mui/material/styles";
2
+ import useMediaQuery from "@mui/material/useMediaQuery";
3
+ const MOBILE_POINT = "md";
4
+ export function useMobile(mobilePoint = MOBILE_POINT) {
5
+ const theme = useTheme();
6
+ return {
7
+ isMobile: useMediaQuery(theme.breakpoints.down(mobilePoint)),
8
+ mobileSize: `${theme.breakpoints.values[mobilePoint]}px`
9
+ };
10
+ }
package/es/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import ConfirmDialog from './components/confirm';
7
7
  import FormInput from './components/input';
8
8
  import Livemode from './components/livemode';
9
9
  import PricingTable from './components/pricing-table';
10
+ import Table from './components/table';
10
11
  import SafeGuard from './components/safe-guard';
11
12
  import Status from './components/status';
12
13
  import Switch from './components/switch-button';
@@ -23,9 +24,12 @@ import Payment from './payment/index';
23
24
  import ProductSkeleton from './payment/product-skeleton';
24
25
  import PaymentSummary from './payment/summary';
25
26
  import PricingItem from './components/pricing-item';
27
+ import CountrySelect from './components/country-select';
28
+ export { PaymentThemeProvider } from './theme';
26
29
  export * from './libs/util';
27
30
  export * from './libs/connect';
28
31
  export * from './contexts/payment';
29
32
  export * from './hooks/subscription';
33
+ export * from './hooks/mobile';
30
34
  export { translations, createTranslator } from './locales';
31
- 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, };
35
+ 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, };
package/es/index.js CHANGED
@@ -7,6 +7,7 @@ import ConfirmDialog from "./components/confirm.js";
7
7
  import FormInput from "./components/input.js";
8
8
  import Livemode from "./components/livemode.js";
9
9
  import PricingTable from "./components/pricing-table.js";
10
+ import Table from "./components/table.js";
10
11
  import SafeGuard from "./components/safe-guard.js";
11
12
  import Status from "./components/status.js";
12
13
  import Switch from "./components/switch-button.js";
@@ -23,10 +24,13 @@ import Payment from "./payment/index.js";
23
24
  import ProductSkeleton from "./payment/product-skeleton.js";
24
25
  import PaymentSummary from "./payment/summary.js";
25
26
  import PricingItem from "./components/pricing-item.js";
27
+ import CountrySelect from "./components/country-select.js";
28
+ export { PaymentThemeProvider } from "./theme/index.js";
26
29
  export * from "./libs/util.js";
27
30
  export * from "./libs/connect.js";
28
31
  export * from "./contexts/payment.js";
29
32
  export * from "./hooks/subscription.js";
33
+ export * from "./hooks/mobile.js";
30
34
  export { translations, createTranslator } from "./locales/index.js";
31
35
  export {
32
36
  api,
@@ -53,5 +57,7 @@ export {
53
57
  TxLink,
54
58
  TxGas,
55
59
  SafeGuard,
56
- PricingItem
60
+ PricingItem,
61
+ CountrySelect,
62
+ Table
57
63
  };
package/es/libs/util.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="react" />
2
2
  import type { PaymentDetails, PriceCurrency, PriceRecurring, TLineItemExpanded, TPaymentCurrency, TPaymentCurrencyExpanded, TPaymentMethod, TPaymentMethodExpanded, TPrice, TProductExpanded, TSubscriptionExpanded, TSubscriptionItemExpanded } from '@blocklet/payment-types';
3
- import { PricingRenderProps } from '../types';
3
+ import { ActionProps, PricingRenderProps } from '../types';
4
4
  export declare const PAYMENT_KIT_DID = "z2qaCNvKMv5GjouKdcDWexv6WqtHbpNPQDnAk";
5
5
  export declare const isPaymentKitMounted: () => any;
6
6
  export declare const getPrefix: () => string;
@@ -40,11 +40,21 @@ export declare function getCheckoutAmount(items: TLineItemExpanded[], currency:
40
40
  };
41
41
  export declare function getRecurringPeriod(recurring: PriceRecurring): number;
42
42
  export declare function formatUpsellSaving(items: TLineItemExpanded[], currency: TPaymentCurrency): string;
43
+ export declare function formatMeteredThen(subscription: string, recurring: string, hasMetered: boolean, locale?: string): string;
44
+ export declare function formatPriceDisplay({ amount, then, actualAmount, showThen }: {
45
+ amount: string;
46
+ then?: string;
47
+ actualAmount: string;
48
+ showThen?: boolean;
49
+ }, recurring: string, hasMetered: boolean, locale?: string): string;
43
50
  export declare function formatCheckoutHeadlines(items: TLineItemExpanded[], currency: TPaymentCurrency, trialInDays: number, locale?: string): {
44
51
  action: string;
45
52
  amount: string;
46
53
  then?: string;
47
54
  secondary?: string;
55
+ showThen?: boolean;
56
+ actualAmount: string;
57
+ priceDisplay: string;
48
58
  };
49
59
  export declare function formatAmount(amount: string, decimals: number): string;
50
60
  export declare function findCurrency(methods: TPaymentMethodExpanded[], currencyId: string): TPaymentCurrencyExpanded | null;
@@ -53,11 +63,13 @@ export declare function stopEvent(e: React.SyntheticEvent<any>): void;
53
63
  export declare function sleep(ms: number): Promise<unknown>;
54
64
  export declare function formatSubscriptionProduct(items: TSubscriptionItemExpanded[], maxLength?: number): string;
55
65
  export declare const getSubscriptionTimeSummary: (subscription: TSubscriptionExpanded) => string;
56
- export declare const getSubscriptionAction: (subscription: TSubscriptionExpanded) => {
66
+ export declare const getSubscriptionAction: (subscription: TSubscriptionExpanded, actionProps: ActionProps) => {
57
67
  action: string;
58
68
  variant: string;
59
69
  color: string;
60
70
  canRenew: boolean;
71
+ text?: string;
72
+ sx?: any;
61
73
  } | null;
62
74
  export declare const mergeExtraParams: (extra?: Record<string, any>) => string;
63
75
  export declare const flattenPaymentMethods: (methods?: TPaymentMethodExpanded[]) => import("sequelize").InferAttributes<import("@blocklet/payment-types").PaymentCurrency, {
@@ -77,4 +89,5 @@ export declare function formatTotalPrice({ product, quantity, priceId, locale, }
77
89
  locale: string;
78
90
  }): PricingRenderProps;
79
91
  export declare function formatQuantityInventory(price: TPrice, quantity: string | number, locale?: string): string;
92
+ export declare function formatSubscriptionStatus(status: string): string;
80
93
  export declare function formatAmountPrecisionLimit(amount: string, locale?: string, precision?: number): string;
package/es/libs/util.js CHANGED
@@ -400,24 +400,42 @@ export function formatUpsellSaving(items, currency) {
400
400
  const after = new BN(to.total);
401
401
  return Number(before.sub(after).mul(new BN(100)).div(before).toString()).toFixed(0);
402
402
  }
403
+ export function formatMeteredThen(subscription, recurring, hasMetered, locale = "en") {
404
+ if (hasMetered) {
405
+ return t("payment.checkout.meteredThen", locale, { subscription, recurring });
406
+ }
407
+ return t("payment.checkout.then", locale, { subscription, recurring });
408
+ }
409
+ export function formatPriceDisplay({ amount, then, actualAmount, showThen }, recurring, hasMetered, locale = "en") {
410
+ if (Number(actualAmount) === 0 && hasMetered) {
411
+ return t("payment.checkout.metered", locale, { recurring });
412
+ }
413
+ if (showThen) {
414
+ return [amount, then].filter(Boolean).join(", ");
415
+ }
416
+ return [amount, then].filter(Boolean).join(" ");
417
+ }
403
418
  export function formatCheckoutHeadlines(items, currency, trialInDays, locale = "en") {
404
419
  const brand = getStatementDescriptor(items);
405
420
  const { total } = getCheckoutAmount(items, currency, trialInDays > 0);
421
+ const actualAmount = fromUnitToToken(total, currency.decimal);
406
422
  const amount = `${fromUnitToToken(total, currency.decimal)} ${currency.symbol}`;
407
423
  if (items.length === 0) {
408
424
  return {
409
425
  action: t("payment.checkout.empty", locale),
410
426
  amount: "0",
411
- then: ""
427
+ then: "",
428
+ actualAmount: "0",
429
+ priceDisplay: "0"
412
430
  };
413
431
  }
414
432
  const { name } = items[0]?.price.product || { name: "" };
415
433
  if (items.every((x) => x.price.type === "one_time")) {
416
434
  const action = t("payment.checkout.pay", locale, { payee: brand });
417
435
  if (items.length > 1) {
418
- return { action, amount };
436
+ return { action, amount, actualAmount, priceDisplay: amount };
419
437
  }
420
- return { action, amount, then: "" };
438
+ return { action, amount, then: "", actualAmount, priceDisplay: amount };
421
439
  }
422
440
  const item = items.find((x) => x.price.type === "recurring");
423
441
  const recurring = formatRecurring(
@@ -426,8 +444,8 @@ export function formatCheckoutHeadlines(items, currency, trialInDays, locale = "
426
444
  "per",
427
445
  locale
428
446
  );
447
+ const hasMetered = items.some((x) => x.price.type === "recurring" && x.price.recurring?.usage_type === "metered");
429
448
  if (items.every((x) => x.price.type === "recurring")) {
430
- const hasMetered = items.some((x) => x.price.type === "recurring" && x.price.recurring?.usage_type === "metered");
431
449
  const subscription2 = [
432
450
  hasMetered ? t("payment.checkout.least", locale) : "",
433
451
  fromUnitToToken(
@@ -443,29 +461,53 @@ export function formatCheckoutHeadlines(items, currency, trialInDays, locale = "
443
461
  ].filter(Boolean).join(" ");
444
462
  if (items.length > 1) {
445
463
  if (trialInDays > 0) {
446
- return {
464
+ const result4 = {
447
465
  action: t("payment.checkout.try2", locale, { name, count: items.length - 1 }),
448
466
  amount: t("payment.checkout.free", locale, { count: trialInDays }),
449
- then: t("payment.checkout.then", locale, { subscription: subscription2, recurring })
467
+ then: formatMeteredThen(subscription2, recurring, hasMetered && Number(subscription2) === 0, locale),
468
+ showThen: true,
469
+ actualAmount: "0"
470
+ };
471
+ return {
472
+ ...result4,
473
+ priceDisplay: formatPriceDisplay(result4, recurring, hasMetered, locale)
450
474
  };
451
475
  }
452
- return {
476
+ const result3 = {
453
477
  action: t("payment.checkout.sub2", locale, { name, count: items.length - 1 }),
454
478
  amount,
455
- then: recurring
479
+ then: hasMetered ? t("payment.checkout.meteredThen", locale, { recurring }) : recurring,
480
+ showThen: hasMetered,
481
+ actualAmount
482
+ };
483
+ return {
484
+ ...result3,
485
+ priceDisplay: formatPriceDisplay(result3, recurring, hasMetered, locale)
456
486
  };
457
487
  }
458
488
  if (trialInDays > 0) {
459
- return {
489
+ const result3 = {
460
490
  action: t("payment.checkout.try1", locale, { name }),
461
491
  amount: t("payment.checkout.free", locale, { count: trialInDays }),
462
- then: t("payment.checkout.then", locale, { subscription: subscription2, recurring })
492
+ then: formatMeteredThen(subscription2, recurring, hasMetered && Number(subscription2) === 0, locale),
493
+ showThen: true,
494
+ actualAmount: "0"
495
+ };
496
+ return {
497
+ ...result3,
498
+ priceDisplay: formatPriceDisplay(result3, recurring, hasMetered, locale)
463
499
  };
464
500
  }
465
- return {
501
+ const result2 = {
466
502
  action: t("payment.checkout.sub1", locale, { name }),
467
503
  amount,
468
- then: recurring
504
+ then: hasMetered ? t("payment.checkout.meteredThen", locale, { recurring }) : recurring,
505
+ showThen: hasMetered,
506
+ actualAmount
507
+ };
508
+ return {
509
+ ...result2,
510
+ priceDisplay: formatPriceDisplay(result2, recurring, hasMetered, locale)
469
511
  };
470
512
  }
471
513
  const subscription = fromUnitToToken(
@@ -477,10 +519,21 @@ export function formatCheckoutHeadlines(items, currency, trialInDays, locale = "
477
519
  }, new BN(0)),
478
520
  currency.decimal
479
521
  );
480
- return {
522
+ const result = {
481
523
  action: t("payment.checkout.pay", locale, { payee: brand }),
482
524
  amount,
483
- then: t("payment.checkout.then", locale, { subscription: `${subscription} ${currency.symbol}`, recurring })
525
+ then: formatMeteredThen(
526
+ `${subscription} ${currency.symbol}`,
527
+ recurring,
528
+ hasMetered && Number(subscription) === 0,
529
+ locale
530
+ ),
531
+ showThen: true,
532
+ actualAmount
533
+ };
534
+ return {
535
+ ...result,
536
+ priceDisplay: formatPriceDisplay(result, recurring, hasMetered, locale)
484
537
  };
485
538
  }
486
539
  export function formatAmount(amount, decimals) {
@@ -516,41 +569,46 @@ export function formatSubscriptionProduct(items, maxLength = 2) {
516
569
  return names.slice(0, maxLength).join(", ") + (names.length > maxLength ? ` and ${names.length - maxLength} more` : "");
517
570
  }
518
571
  export const getSubscriptionTimeSummary = (subscription) => {
519
- const lines = [`Started on ${formatToDate(subscription.start_date * 1e3)}`];
572
+ const lines = [`Start on ${formatToDate(subscription.start_date * 1e3, "en", "YYYY-MM-DD")}`];
573
+ const getLineTimeMessage = (time) => {
574
+ const curDay = dayjs().isSame(dayjs(time), "day");
575
+ const timeFormat = curDay ? "HH:mm:ss" : "YYYY-MM-DD";
576
+ return `${curDay ? "in" : "on"} ${formatToDate(time, "en", timeFormat)}`;
577
+ };
520
578
  if (subscription.status === "active" || subscription.status === "trialing") {
521
579
  if (subscription.cancel_at) {
522
- lines.push(`will cancel on ${formatToDate(subscription.cancel_at * 1e3)}`);
580
+ lines.push(`Ended ${getLineTimeMessage(subscription.cancel_at * 1e3)}`);
523
581
  } else if (subscription.cancel_at_period_end) {
524
- lines.push(`will cancel on ${formatToDate(subscription.current_period_end * 1e3)}`);
582
+ lines.push(`Ended ${getLineTimeMessage(subscription.current_period_end * 1e3)}`);
525
583
  } else {
526
- lines.push(`will renew on ${formatToDate(subscription.current_period_end * 1e3)}`);
584
+ lines.push(`Renew ${getLineTimeMessage(subscription.current_period_end * 1e3)}`);
527
585
  }
528
586
  } else if (subscription.status === "past_due") {
529
- lines.push(`will cancel on ${formatToDate((subscription.cancel_at || subscription.current_period_end) * 1e3)}`);
587
+ lines.push(`Ended ${getLineTimeMessage((subscription.cancel_at || subscription.current_period_end) * 1e3)}`);
530
588
  } else if (subscription.status === "canceled") {
531
- lines.push(`canceled on ${formatToDate(subscription.canceled_at * 1e3)}`);
589
+ lines.push(`Ended ${getLineTimeMessage(subscription.canceled_at * 1e3)}`);
532
590
  }
533
- return lines.join(", ");
591
+ return lines.join(",");
534
592
  };
535
- export const getSubscriptionAction = (subscription) => {
593
+ export const getSubscriptionAction = (subscription, actionProps) => {
536
594
  if (subscription.status === "active" || subscription.status === "trialing") {
537
595
  if (subscription.cancel_at_period_end) {
538
596
  if (subscription.cancelation_details?.reason === "payment_failed") {
539
597
  return null;
540
598
  }
541
- return { action: "recover", variant: "contained", color: "primary", canRenew: false };
599
+ return { action: "recover", variant: "contained", color: "primary", canRenew: false, ...actionProps?.recover };
542
600
  }
543
601
  if (subscription.cancel_at && subscription.cancel_at !== subscription.current_period_end) {
544
602
  return null;
545
603
  }
546
- return { action: "cancel", variant: "outlined", color: "inherit", canRenew: false };
604
+ return { action: "cancel", variant: "outlined", color: "inherit", canRenew: false, ...actionProps?.cancel };
547
605
  }
548
606
  if (subscription.status === "past_due") {
549
607
  const canRenew = subscription.cancel_at && subscription.cancel_at !== subscription.current_period_end;
550
- return { action: "pastDue", variant: "contained", color: "primary", canRenew };
608
+ return { action: "pastDue", variant: "contained", color: "primary", canRenew, ...actionProps?.pastDue };
551
609
  }
552
610
  if (subscription.status !== "canceled" && subscription.cancel_at_period_end) {
553
- return { action: "recover", variant: "contained", color: "primary", canRenew: false };
611
+ return { action: "recover", variant: "contained", color: "primary", canRenew: false, ...actionProps?.recover };
554
612
  }
555
613
  return null;
556
614
  };
@@ -657,8 +715,8 @@ export function formatTotalPrice({
657
715
  }
658
716
  const unitValue = new BN(price.custom_unit_amount || price.unit_amount);
659
717
  const currency = price?.currency ?? {};
660
- const total = `${fromUnitToToken(unitValue.mul(new BN(quantity)), currency.decimal)} ${currency.symbol}`;
661
- const unit = `${fromUnitToToken(unitValue, currency.decimal)} ${currency.symbol}`;
718
+ const total = `${fromUnitToToken(unitValue.mul(new BN(quantity)), currency.decimal)} ${currency.symbol} `;
719
+ const unit = `${fromUnitToToken(unitValue, currency.decimal)} ${currency.symbol} `;
662
720
  const appendUnit = (v, alt) => {
663
721
  if (product.unit_label) {
664
722
  return `${v}/${price.product.unit_label}`;
@@ -690,6 +748,12 @@ export function formatQuantityInventory(price, quantity, locale = "en") {
690
748
  }
691
749
  return "";
692
750
  }
751
+ export function formatSubscriptionStatus(status) {
752
+ if (status === "canceled") {
753
+ return "Ended";
754
+ }
755
+ return status;
756
+ }
693
757
  export function formatAmountPrecisionLimit(amount, locale = "en", precision = 6) {
694
758
  if (!amount) {
695
759
  return "";