payment-kit 1.13.255 → 1.13.256

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.
@@ -116,57 +116,61 @@ router.get('/', authMine, async (req, res) => {
116
116
 
117
117
  // push staking info as first invoice if we are on the last page
118
118
  let subscription;
119
- if (where.subscription_id && include_staking) {
120
- subscription = await Subscription.findByPk(where.subscription_id);
121
- if (subscription?.payment_details?.arcblock?.staking?.tx_hash && page === Math.ceil((count || 1) / pageSize)) {
122
- const method = await PaymentMethod.findOne({ where: { type: 'arcblock', livemode: subscription.livemode } });
123
- if (method) {
124
- const client = method.getOcapClient();
125
- const { address } = subscription.payment_details.arcblock.staking;
126
- const { state } = await client.getStakeState({ address });
127
- const last =
128
- list[list.length - 1] ||
129
- (await Invoice.findOne({ where: { subscription_id: subscription.id }, order: [['created_at', 'ASC']] }));
119
+ if (where.subscription_id && include_staking && page === Math.ceil((count || 1) / pageSize)) {
120
+ try {
121
+ subscription = await Subscription.findByPk(where.subscription_id);
122
+ if (subscription?.payment_details?.arcblock?.staking?.tx_hash) {
123
+ const method = await PaymentMethod.findOne({ where: { type: 'arcblock', livemode: subscription.livemode } });
124
+ if (method) {
125
+ const client = method.getOcapClient();
126
+ const { address } = subscription.payment_details.arcblock.staking;
127
+ const { state } = await client.getStakeState({ address });
128
+ const last =
129
+ list[list.length - 1] ||
130
+ (await Invoice.findOne({ where: { subscription_id: subscription.id }, order: [['created_at', 'ASC']] }));
130
131
 
131
- if (state && last) {
132
- const data = JSON.parse(state.data?.value || '{}');
133
- list.push({
134
- id: address as string,
135
- status: 'paid',
136
- description: 'Subscription staking',
137
- billing_reason: 'subscription_create',
138
- total: data[subscription.id],
139
- amount_due: '0',
140
- amount_paid: data[subscription.id],
141
- amount_remaining: '0',
142
- ...pick(last, [
143
- 'number',
144
- 'paid',
145
- 'auto_advance',
146
- 'currency_id',
147
- 'customer_id',
148
- 'subscription_id',
149
- 'period_start',
150
- 'period_end',
151
- 'created_at',
152
- 'updated_at',
153
- ]),
154
- // @ts-ignore
155
- paymentCurrency: await PaymentCurrency.findByPk(last.currency_id),
156
- paymentMethod: method,
157
- // @ts-ignore
158
- customer: await Customer.findByPk(last.customer_id),
159
- metadata: {
160
- payment_details: {
161
- arcblock: {
162
- tx_hash: subscription.payment_details.arcblock.staking.tx_hash,
163
- payer: subscription.payment_details.arcblock.payer,
132
+ if (state && last) {
133
+ const data = JSON.parse(state.data?.value || '{}');
134
+ list.push({
135
+ id: address as string,
136
+ status: 'paid',
137
+ description: 'Subscription staking',
138
+ billing_reason: 'subscription_create',
139
+ total: data[subscription.id],
140
+ amount_due: '0',
141
+ amount_paid: data[subscription.id],
142
+ amount_remaining: '0',
143
+ ...pick(last, [
144
+ 'number',
145
+ 'paid',
146
+ 'auto_advance',
147
+ 'currency_id',
148
+ 'customer_id',
149
+ 'subscription_id',
150
+ 'period_start',
151
+ 'period_end',
152
+ 'created_at',
153
+ 'updated_at',
154
+ ]),
155
+ // @ts-ignore
156
+ paymentCurrency: await PaymentCurrency.findByPk(last.currency_id),
157
+ paymentMethod: method,
158
+ // @ts-ignore
159
+ customer: await Customer.findByPk(last.customer_id),
160
+ metadata: {
161
+ payment_details: {
162
+ arcblock: {
163
+ tx_hash: subscription.payment_details.arcblock.staking.tx_hash,
164
+ payer: subscription.payment_details.arcblock.payer,
165
+ },
164
166
  },
165
167
  },
166
- },
167
- });
168
+ });
169
+ }
168
170
  }
169
171
  }
172
+ } catch (err) {
173
+ console.error('Failed to include staking record in invoice list', err);
170
174
  }
171
175
  }
172
176
 
package/blocklet.yml CHANGED
@@ -14,7 +14,7 @@ repository:
14
14
  type: git
15
15
  url: git+https://github.com/blocklet/payment-kit.git
16
16
  specVersion: 1.2.8
17
- version: 1.13.255
17
+ version: 1.13.256
18
18
  logo: logo.png
19
19
  files:
20
20
  - dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payment-kit",
3
- "version": "1.13.255",
3
+ "version": "1.13.256",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev --open",
6
6
  "eject": "vite eject",
@@ -51,7 +51,7 @@
51
51
  "@arcblock/ux": "^2.9.79",
52
52
  "@arcblock/validator": "^1.18.117",
53
53
  "@blocklet/logger": "1.16.26",
54
- "@blocklet/payment-react": "1.13.255",
54
+ "@blocklet/payment-react": "1.13.256",
55
55
  "@blocklet/sdk": "1.16.26",
56
56
  "@blocklet/ui-react": "^2.9.79",
57
57
  "@blocklet/uploader": "^0.1.6",
@@ -116,7 +116,7 @@
116
116
  "devDependencies": {
117
117
  "@abtnode/types": "1.16.26",
118
118
  "@arcblock/eslint-config-ts": "^0.3.0",
119
- "@blocklet/payment-types": "1.13.255",
119
+ "@blocklet/payment-types": "1.13.256",
120
120
  "@types/cookie-parser": "^1.4.7",
121
121
  "@types/cors": "^2.8.17",
122
122
  "@types/dotenv-flow": "^3.3.3",
@@ -155,5 +155,5 @@
155
155
  "parser": "typescript"
156
156
  }
157
157
  },
158
- "gitHead": "79416ada68496df6d233a59244a0cec245414813"
158
+ "gitHead": "5534c9d95122867a83c0d67eb3d1a27675c370b7"
159
159
  }
package/src/app.tsx CHANGED
@@ -4,7 +4,6 @@ import Center from '@arcblock/ux/lib/Center';
4
4
  import { LocaleProvider } from '@arcblock/ux/lib/Locale/context';
5
5
  import { ThemeProvider, createTheme } from '@arcblock/ux/lib/Theme';
6
6
  import { ToastProvider } from '@arcblock/ux/lib/Toast';
7
- import { MiniInvoiceList } from '@blocklet/payment-react';
8
7
  import { CircularProgress } from '@mui/material';
9
8
  import React, { Suspense } from 'react';
10
9
  import { ErrorBoundary } from 'react-error-boundary';
@@ -24,6 +23,7 @@ const CustomerHome = React.lazy(() => import('./pages/customer/index'));
24
23
  const CustomerInvoiceDetail = React.lazy(() => import('./pages/customer/invoice/detail'));
25
24
  const CustomerInvoicePastDue = React.lazy(() => import('./pages/customer/invoice/past-due'));
26
25
  const CustomerSubscriptionDetail = React.lazy(() => import('./pages/customer/subscription/detail'));
26
+ const CustomerSubscriptionEmbed = React.lazy(() => import('./pages/customer/subscription/embed'));
27
27
  const CustomerSubscriptionChangePlan = React.lazy(() => import('./pages/customer/subscription/change-plan'));
28
28
  const CustomerSubscriptionChangePayment = React.lazy(() => import('./pages/customer/subscription/change-payment'));
29
29
 
@@ -91,8 +91,18 @@ function App() {
91
91
  </Layout>
92
92
  }
93
93
  />
94
- <Route key="customer-embed" path="/customer/embed/subscription" element={<MiniInvoiceList />} />,
95
- <Route key="subscription-embed" path="/embed/customer/subscription" element={<MiniInvoiceList />} />,
94
+ <Route
95
+ key="customer-embed"
96
+ path="/customer/embed/subscription"
97
+ element={<CustomerSubscriptionEmbed />}
98
+ />
99
+ ,
100
+ <Route
101
+ key="subscription-embed"
102
+ path="/embed/customer/subscription"
103
+ element={<CustomerSubscriptionEmbed />}
104
+ />
105
+ ,
96
106
  <Route
97
107
  key="customer-due"
98
108
  path="/customer/invoice/past-due"
@@ -0,0 +1,232 @@
1
+ /* eslint-disable react/no-unstable-nested-components */
2
+ import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
3
+ import {
4
+ Status,
5
+ api,
6
+ formatBNStr,
7
+ formatError,
8
+ formatSubscriptionProduct,
9
+ formatTime,
10
+ formatToDate,
11
+ getDidConnectQueryParams,
12
+ getInvoiceStatusColor,
13
+ getPrefix,
14
+ getSubscriptionStatusColor,
15
+ } from '@blocklet/payment-react';
16
+ import type { Paginated, TInvoiceExpanded, TSubscriptionExpanded } from '@blocklet/payment-types';
17
+ import {
18
+ Alert,
19
+ Box,
20
+ Button,
21
+ CircularProgress,
22
+ Divider,
23
+ Link,
24
+ List,
25
+ ListItem,
26
+ ListSubheader,
27
+ Stack,
28
+ Typography,
29
+ } from '@mui/material';
30
+ import { useRequest } from 'ahooks';
31
+ import { useMemo } from 'react';
32
+ import { joinURL, withQuery } from 'ufo';
33
+
34
+ const fetchInvoiceData = (params: Record<string, any> = {}): Promise<Paginated<TInvoiceExpanded>> => {
35
+ const search = new URLSearchParams();
36
+ Object.keys(params).forEach((key) => {
37
+ search.set(key, String(params[key]));
38
+ });
39
+ return api.get(`/api/invoices?${search.toString()}`).then((res: any) => res.data);
40
+ };
41
+
42
+ const fetchSubscriptionData = (id: string, authToken: string): Promise<TSubscriptionExpanded> => {
43
+ if (!id) {
44
+ throw new Error('Subscription ID is missing');
45
+ }
46
+
47
+ return api.get(`/api/subscriptions/${id}?authToken=${authToken}`).then((res) => res.data);
48
+ };
49
+
50
+ export default function SubscriptionEmbed() {
51
+ const { t, locale } = useLocaleContext();
52
+ const params = new URL(window.location.href).searchParams;
53
+
54
+ const subscriptionId = params.get('id') || '';
55
+ const authToken = params.get('authToken') || '';
56
+
57
+ const { data: subscription, error, loading } = useRequest(() => fetchSubscriptionData(subscriptionId, authToken));
58
+ const { data } = useRequest(() =>
59
+ fetchInvoiceData({
60
+ page: 1,
61
+ pageSize: 20,
62
+ status: 'open,paid,uncollectible',
63
+ subscription_id: subscriptionId,
64
+ authToken,
65
+ ignore_zero: true,
66
+ include_staking: true,
67
+ })
68
+ );
69
+
70
+ const subscriptionPageUrl: string = useMemo(() => {
71
+ if (!subscription) {
72
+ return '#';
73
+ }
74
+
75
+ const pageUrl: string = joinURL(getPrefix(), `/customer/subscription/${subscription.id}`);
76
+
77
+ return withQuery(pageUrl, {
78
+ source: 'embed',
79
+ ...getDidConnectQueryParams({ forceConnected: subscription.customer.did }),
80
+ });
81
+ }, [subscription]);
82
+
83
+ if (error) {
84
+ return (
85
+ <Position>
86
+ <Alert severity="error">{formatError(error)}</Alert>
87
+ </Position>
88
+ );
89
+ }
90
+ if (!subscription || loading) {
91
+ return (
92
+ <Position>
93
+ <CircularProgress />
94
+ </Position>
95
+ );
96
+ }
97
+
98
+ const invoices = data?.list || [];
99
+
100
+ const infoList = [
101
+ {
102
+ name: t('payment.customer.subscriptions.plan'),
103
+ value: (
104
+ <Typography fontWeight={600} sx={{ marginRight: '10px' }}>
105
+ {formatSubscriptionProduct(subscription.items)}
106
+ </Typography>
107
+ ),
108
+ },
109
+ {
110
+ name: t('common.status'),
111
+ value: <Status label={subscription.status} color={getSubscriptionStatusColor(subscription.status)} />,
112
+ },
113
+ ];
114
+
115
+ if (subscription.status === 'active' || subscription.status === 'trialing') {
116
+ infoList.push({
117
+ name: t('payment.customer.subscriptions.nextInvoice'),
118
+ value: (
119
+ <Typography
120
+ sx={{
121
+ color: 'success.main',
122
+ fontWeight: 'bold',
123
+ marginRight: '10px',
124
+ }}>
125
+ {formatTime(subscription.current_period_end * 1000)}
126
+ </Typography>
127
+ ),
128
+ });
129
+ }
130
+
131
+ return (
132
+ <Position>
133
+ <Typography title={t('payment.checkout.subscription')} />
134
+ <Box
135
+ className="mini-invoice-wrap"
136
+ sx={{
137
+ display: 'flex',
138
+ flexDirection: 'column',
139
+ alignItem: 'center',
140
+ justifyContent: 'flex-start',
141
+ padding: '16px',
142
+ width: '100%',
143
+ height: '100%',
144
+ }}>
145
+ <Typography component="h3" sx={{ textAlign: 'center' }} variant="h5" gutterBottom>
146
+ {t('payment.customer.subscriptions.current')}
147
+ </Typography>
148
+ <Box sx={{ marginTop: '12px' }}>
149
+ <List>
150
+ {infoList.map(({ name, value }) => {
151
+ return (
152
+ <ListItem key={name} disableGutters sx={{ display: 'flex', justifyContent: 'space-between' }}>
153
+ <Typography component="span">{name}</Typography>
154
+ <Typography component="span">{value}</Typography>
155
+ </ListItem>
156
+ );
157
+ })}
158
+ </List>
159
+ </Box>
160
+ <Divider />
161
+ <Box sx={{ marginTop: '12px', flex: 1 }}>
162
+ <List sx={{ overflow: 'auto' }} className="mini-invoice-list">
163
+ <ListSubheader disableGutters sx={{ padding: 0 }}>
164
+ <Typography component="h2" variant="h6" fontSize="16px">
165
+ {t('payment.customer.invoices')}
166
+ </Typography>
167
+ </ListSubheader>
168
+ {(invoices as any).length === 0 ? (
169
+ <Typography color="text.secondary">{t('payment.customer.invoice.empty')}</Typography>
170
+ ) : (
171
+ (invoices as any).map((item: any) => {
172
+ return (
173
+ <ListItem key={item.id} disableGutters sx={{ display: 'flex', justifyContent: 'space-between' }}>
174
+ <Typography component="span" sx={{ flex: 3 }}>
175
+ {formatToDate(item.created_at, locale, 'YYYY-MM-DD')}
176
+ </Typography>
177
+ <Typography component="span" sx={{ flex: 1, textAlign: 'right' }}>
178
+ {formatBNStr(item.total, item.paymentCurrency.decimal)}&nbsp;
179
+ {item.paymentCurrency.symbol}
180
+ </Typography>
181
+ <Typography component="span" sx={{ flex: 2, textAlign: 'right' }}>
182
+ <Status label={item.status} color={getInvoiceStatusColor(item.status)} />
183
+ </Typography>
184
+ </ListItem>
185
+ );
186
+ })
187
+ )}
188
+ </List>
189
+ </Box>
190
+ <Stack direction="row" justifyContent="center" spacing={2} sx={{ mt: 2 }}>
191
+ {subscription.service_actions?.map((x) => (
192
+ // @ts-ignore
193
+ <Button
194
+ component={Link}
195
+ key={x.name}
196
+ variant={x.variant}
197
+ color={x.color}
198
+ href={x.link}
199
+ size="small"
200
+ target="_blank"
201
+ sx={{ textDecoration: 'none !important' }}>
202
+ {x.text[locale] || x.text.en || x.name}
203
+ </Button>
204
+ ))}
205
+ <Button
206
+ variant="contained"
207
+ sx={{ color: '#fff!important', width: subscription.service_actions?.length ? 'auto' : '100%' }}
208
+ target="_blank"
209
+ href={subscriptionPageUrl}>
210
+ {t('payment.customer.subscriptions.view')}
211
+ </Button>
212
+ </Stack>
213
+ </Box>
214
+ </Position>
215
+ );
216
+ }
217
+
218
+ function Position({ children }: any) {
219
+ return (
220
+ <Box
221
+ className="mini-invoice-box" // 预留 class 用于设置定位
222
+ sx={{
223
+ padding: '8px',
224
+ width: '100%',
225
+ maxWidth: '500px',
226
+ background: '#fff',
227
+ margin: '0 auto',
228
+ }}>
229
+ {children}
230
+ </Box>
231
+ );
232
+ }