payment-kit 1.19.0 → 1.19.1

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 (133) hide show
  1. package/api/src/crons/index.ts +8 -0
  2. package/api/src/index.ts +4 -0
  3. package/api/src/libs/credit-grant.ts +146 -0
  4. package/api/src/libs/env.ts +1 -0
  5. package/api/src/libs/invoice.ts +4 -3
  6. package/api/src/libs/notification/template/base.ts +388 -2
  7. package/api/src/libs/notification/template/customer-credit-grant-granted.ts +149 -0
  8. package/api/src/libs/notification/template/customer-credit-grant-low-balance.ts +151 -0
  9. package/api/src/libs/notification/template/customer-credit-insufficient.ts +254 -0
  10. package/api/src/libs/notification/template/subscription-canceled.ts +193 -202
  11. package/api/src/libs/notification/template/subscription-refund-succeeded.ts +215 -237
  12. package/api/src/libs/notification/template/subscription-renewed.ts +130 -200
  13. package/api/src/libs/notification/template/subscription-succeeded.ts +100 -202
  14. package/api/src/libs/notification/template/subscription-trial-start.ts +142 -188
  15. package/api/src/libs/notification/template/subscription-trial-will-end.ts +146 -174
  16. package/api/src/libs/notification/template/subscription-upgraded.ts +96 -192
  17. package/api/src/libs/notification/template/subscription-will-canceled.ts +94 -135
  18. package/api/src/libs/notification/template/subscription-will-renew.ts +220 -245
  19. package/api/src/libs/payment.ts +69 -0
  20. package/api/src/libs/queue/index.ts +3 -2
  21. package/api/src/libs/session.ts +8 -0
  22. package/api/src/libs/subscription.ts +74 -3
  23. package/api/src/libs/ws.ts +23 -1
  24. package/api/src/locales/en.ts +33 -0
  25. package/api/src/locales/zh.ts +31 -0
  26. package/api/src/queues/credit-consume.ts +715 -0
  27. package/api/src/queues/credit-grant.ts +572 -0
  28. package/api/src/queues/notification.ts +173 -128
  29. package/api/src/queues/payment.ts +210 -122
  30. package/api/src/queues/subscription.ts +179 -0
  31. package/api/src/routes/checkout-sessions.ts +157 -9
  32. package/api/src/routes/connect/shared.ts +3 -2
  33. package/api/src/routes/credit-grants.ts +241 -0
  34. package/api/src/routes/credit-transactions.ts +208 -0
  35. package/api/src/routes/index.ts +8 -0
  36. package/api/src/routes/meter-events.ts +347 -0
  37. package/api/src/routes/meters.ts +219 -0
  38. package/api/src/routes/payment-currencies.ts +14 -2
  39. package/api/src/routes/payment-links.ts +1 -1
  40. package/api/src/routes/payment-methods.ts +14 -2
  41. package/api/src/routes/prices.ts +43 -0
  42. package/api/src/routes/pricing-table.ts +13 -7
  43. package/api/src/routes/products.ts +63 -4
  44. package/api/src/routes/settings.ts +1 -1
  45. package/api/src/routes/subscriptions.ts +4 -0
  46. package/api/src/store/migrations/20250610-billing-credit.ts +43 -0
  47. package/api/src/store/models/credit-grant.ts +486 -0
  48. package/api/src/store/models/credit-transaction.ts +268 -0
  49. package/api/src/store/models/customer.ts +8 -0
  50. package/api/src/store/models/index.ts +52 -1
  51. package/api/src/store/models/meter-event.ts +423 -0
  52. package/api/src/store/models/meter.ts +176 -0
  53. package/api/src/store/models/payment-currency.ts +66 -14
  54. package/api/src/store/models/price.ts +6 -0
  55. package/api/src/store/models/product.ts +2 -2
  56. package/api/src/store/models/subscription.ts +24 -0
  57. package/api/src/store/models/types.ts +28 -2
  58. package/api/tests/libs/subscription.spec.ts +53 -0
  59. package/blocklet.yml +9 -1
  60. package/package.json +4 -4
  61. package/scripts/sdk.js +233 -1
  62. package/src/app.tsx +10 -0
  63. package/src/components/collapse.tsx +11 -1
  64. package/src/components/customer/credit-grant-item-list.tsx +99 -0
  65. package/src/components/customer/credit-overview.tsx +233 -0
  66. package/src/components/customer/form.tsx +5 -2
  67. package/src/components/invoice/list.tsx +19 -1
  68. package/src/components/metadata/form.tsx +286 -90
  69. package/src/components/meter/actions.tsx +101 -0
  70. package/src/components/meter/add-usage-dialog.tsx +239 -0
  71. package/src/components/meter/events-list.tsx +657 -0
  72. package/src/components/meter/form.tsx +245 -0
  73. package/src/components/meter/products.tsx +264 -0
  74. package/src/components/meter/usage-guide.tsx +174 -0
  75. package/src/components/payment-currency/form.tsx +2 -0
  76. package/src/components/payment-intent/list.tsx +19 -1
  77. package/src/components/payment-link/preview.tsx +1 -1
  78. package/src/components/payment-link/product-select.tsx +52 -12
  79. package/src/components/payment-method/arcblock.tsx +2 -0
  80. package/src/components/payment-method/base.tsx +2 -0
  81. package/src/components/payment-method/bitcoin.tsx +2 -0
  82. package/src/components/payment-method/ethereum.tsx +2 -0
  83. package/src/components/payment-method/stripe.tsx +2 -0
  84. package/src/components/payouts/list.tsx +19 -1
  85. package/src/components/price/currency-select.tsx +51 -31
  86. package/src/components/price/form.tsx +881 -407
  87. package/src/components/pricing-table/preview.tsx +1 -1
  88. package/src/components/product/add-price.tsx +9 -7
  89. package/src/components/product/create.tsx +7 -4
  90. package/src/components/product/edit-price.tsx +21 -12
  91. package/src/components/product/features.tsx +17 -7
  92. package/src/components/product/form.tsx +104 -89
  93. package/src/components/refund/list.tsx +19 -1
  94. package/src/components/section/header.tsx +5 -18
  95. package/src/components/subscription/items/index.tsx +1 -1
  96. package/src/components/subscription/metrics.tsx +37 -5
  97. package/src/components/subscription/portal/actions.tsx +2 -1
  98. package/src/contexts/products.tsx +26 -9
  99. package/src/hooks/subscription.ts +34 -0
  100. package/src/libs/meter-utils.ts +196 -0
  101. package/src/libs/util.ts +4 -0
  102. package/src/locales/en.tsx +385 -4
  103. package/src/locales/zh.tsx +364 -0
  104. package/src/pages/admin/billing/index.tsx +61 -33
  105. package/src/pages/admin/billing/invoices/detail.tsx +1 -1
  106. package/src/pages/admin/billing/meters/create.tsx +60 -0
  107. package/src/pages/admin/billing/meters/detail.tsx +435 -0
  108. package/src/pages/admin/billing/meters/index.tsx +210 -0
  109. package/src/pages/admin/billing/meters/meter-event.tsx +346 -0
  110. package/src/pages/admin/billing/subscriptions/detail.tsx +47 -14
  111. package/src/pages/admin/customers/customers/credit-grant/detail.tsx +391 -0
  112. package/src/pages/admin/customers/customers/detail.tsx +22 -10
  113. package/src/pages/admin/customers/index.tsx +5 -0
  114. package/src/pages/admin/developers/events/detail.tsx +1 -1
  115. package/src/pages/admin/developers/index.tsx +1 -1
  116. package/src/pages/admin/payments/intents/detail.tsx +1 -1
  117. package/src/pages/admin/payments/payouts/detail.tsx +1 -1
  118. package/src/pages/admin/payments/refunds/detail.tsx +1 -1
  119. package/src/pages/admin/products/index.tsx +3 -2
  120. package/src/pages/admin/products/links/detail.tsx +1 -1
  121. package/src/pages/admin/products/prices/actions.tsx +16 -4
  122. package/src/pages/admin/products/prices/detail.tsx +30 -3
  123. package/src/pages/admin/products/prices/list.tsx +8 -1
  124. package/src/pages/admin/products/pricing-tables/detail.tsx +1 -1
  125. package/src/pages/admin/products/products/create.tsx +233 -57
  126. package/src/pages/admin/products/products/detail.tsx +2 -1
  127. package/src/pages/admin/settings/payment-methods/index.tsx +3 -0
  128. package/src/pages/customer/credit-grant/detail.tsx +308 -0
  129. package/src/pages/customer/index.tsx +35 -2
  130. package/src/pages/customer/recharge/account.tsx +5 -5
  131. package/src/pages/customer/subscription/change-payment.tsx +4 -2
  132. package/src/pages/customer/subscription/detail.tsx +48 -14
  133. package/src/pages/customer/subscription/embed.tsx +1 -1
@@ -0,0 +1,233 @@
1
+ import { formatBNStr, CreditGrantsList, CreditTransactionsList, api } from '@blocklet/payment-react';
2
+ import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
3
+ import { Avatar, Box, Card, CardContent, Stack, Typography, Tabs, Tab } from '@mui/material';
4
+ import { useState } from 'react';
5
+ import type { TPaymentCurrency } from '@blocklet/payment-types';
6
+ import { useRequest } from 'ahooks';
7
+
8
+ enum CreditTab {
9
+ OVERVIEW = 'overview',
10
+ GRANTS = 'grants',
11
+ TRANSACTIONS = 'transactions',
12
+ }
13
+
14
+ interface CreditOverviewProps {
15
+ customerId: string;
16
+ settings: any;
17
+ mode?: 'portal' | 'dashboard';
18
+ }
19
+
20
+ const fetchCreditSummary = async (customerId: string) => {
21
+ try {
22
+ const [grantsResponse, pendingAmountResponse] = await Promise.all([
23
+ api.get(`/api/credit-grants/summary?customer_id=${customerId}`),
24
+ api.get(`/api/meter-events/pending-amount?customer_id=${customerId}`),
25
+ // api.get(`/api/credit-transactions/summary?customer_id=${customerId}`),
26
+ ]);
27
+
28
+ return {
29
+ grants: grantsResponse.data,
30
+ // transactions: transactionsResponse.data,
31
+ pendingAmount: pendingAmountResponse.data,
32
+ };
33
+ } catch (error) {
34
+ console.error('Failed to fetch credit summary:', error);
35
+ return {
36
+ grants: null,
37
+ transactions: null,
38
+ pendingAmount: null,
39
+ };
40
+ }
41
+ };
42
+
43
+ export default function CreditOverview({ customerId, settings, mode = 'portal' }: CreditOverviewProps) {
44
+ const { t } = useLocaleContext();
45
+ const [creditTab, setCreditTab] = useState<CreditTab>(CreditTab.OVERVIEW);
46
+ const { data: creditSummary } = useRequest(fetchCreditSummary, {
47
+ defaultParams: [customerId],
48
+ refreshDeps: [creditTab === CreditTab.OVERVIEW],
49
+ });
50
+
51
+ // 渲染信用概览卡片
52
+ const renderCreditOverviewCard = (currency: any) => {
53
+ const method = settings?.paymentMethods?.find((m: any) => m.id === currency.payment_method_id);
54
+ if (method?.type !== 'arcblock') {
55
+ return null;
56
+ }
57
+
58
+ const currencyId = currency.id as string;
59
+ const grantData = creditSummary?.grants?.[currencyId];
60
+ const pendingAmount = creditSummary?.pendingAmount?.[currencyId] || '0';
61
+
62
+ if (!grantData) {
63
+ return null;
64
+ }
65
+
66
+ const totalAmount = grantData.totalAmount || '0';
67
+ const remainingAmount = grantData.remainingAmount || '0';
68
+
69
+ return (
70
+ <Card
71
+ key={currency.id}
72
+ sx={{
73
+ height: '100%',
74
+ display: 'flex',
75
+ flexDirection: 'column',
76
+ border: '1px solid',
77
+ borderColor: 'divider',
78
+ boxShadow: 1,
79
+ borderRadius: 1,
80
+ }}>
81
+ <CardContent sx={{ flexGrow: 1 }}>
82
+ <Stack spacing={2}>
83
+ {/* 货币信息 */}
84
+ <Stack
85
+ direction="row"
86
+ spacing={1}
87
+ sx={{
88
+ alignItems: 'center',
89
+ }}>
90
+ <Avatar src={currency.logo} alt={currency.symbol} sx={{ width: 24, height: 24 }} />
91
+ <Typography variant="h6" component="div">
92
+ {currency.name}
93
+ </Typography>
94
+ </Stack>
95
+
96
+ {/* 可用额度 / 总额度 */}
97
+ <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
98
+ <Typography
99
+ variant="body2"
100
+ gutterBottom
101
+ sx={{
102
+ color: 'text.secondary',
103
+ }}>
104
+ {t('admin.customer.creditGrants.creditBalance')}
105
+ </Typography>
106
+ <Typography variant="h5" component="div" sx={{ fontWeight: 'normal' }}>
107
+ {totalAmount === '0' && remainingAmount === '0' ? (
108
+ <>0 {currency.symbol}</>
109
+ ) : (
110
+ <>
111
+ {formatBNStr(remainingAmount, currency.decimal, 6, true)} /{' '}
112
+ {formatBNStr(totalAmount, currency.decimal, 6, true)} {currency.symbol}
113
+ </>
114
+ )}
115
+ </Typography>
116
+ </Box>
117
+
118
+ {/* 欠费额度 */}
119
+ {pendingAmount !== '0' && (
120
+ <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
121
+ <Typography
122
+ variant="body2"
123
+ gutterBottom
124
+ sx={{
125
+ color: 'text.secondary',
126
+ }}>
127
+ {t('admin.customer.creditGrants.pendingAmount')}
128
+ </Typography>
129
+ <Typography
130
+ variant="body1"
131
+ sx={{
132
+ color: 'error.main',
133
+ }}>
134
+ {formatBNStr(pendingAmount, currency.decimal, 6, true)} {currency.symbol}
135
+ </Typography>
136
+ </Box>
137
+ )}
138
+ </Stack>
139
+ </CardContent>
140
+ </Card>
141
+ );
142
+ };
143
+
144
+ // 获取有信用额度的货币
145
+ const creditCurrencies =
146
+ settings?.paymentMethods
147
+ ?.filter((method: any) => method.type === 'arcblock')
148
+ ?.flatMap((method: any) => method.payment_currencies)
149
+ ?.filter((currency: TPaymentCurrency) => {
150
+ const currencyId = currency.id as string;
151
+ const grantData = creditSummary?.grants?.[currencyId];
152
+ return grantData;
153
+ }) || [];
154
+
155
+ if (creditCurrencies.length === 0) {
156
+ return null;
157
+ }
158
+
159
+ return (
160
+ <Stack sx={{ width: '100%' }}>
161
+ <Tabs
162
+ value={creditTab}
163
+ onChange={(_, newValue) => setCreditTab(newValue as CreditTab)}
164
+ // sx={{ borderBottom: 1, borderColor: 'divider', }}
165
+ sx={{
166
+ flex: '1 0 auto',
167
+ maxWidth: '100%',
168
+ fontSize: 14,
169
+ borderBottom: 1,
170
+ borderColor: 'divider',
171
+ '.Mui-selected': {
172
+ fontSize: '14px !important',
173
+ color: 'primary.main',
174
+ },
175
+ }}>
176
+ <Tab label={t('admin.creditGrants.overview')} value={CreditTab.OVERVIEW} />
177
+ <Tab label={t('admin.creditGrants.title')} value={CreditTab.GRANTS} />
178
+ <Tab label={t('admin.creditTransactions.title')} value={CreditTab.TRANSACTIONS} />
179
+ </Tabs>
180
+ {/* 概览标签页 */}
181
+ {creditTab === CreditTab.OVERVIEW && (
182
+ <Box sx={{ width: '100%', mt: 1 }} key={creditTab}>
183
+ <Box
184
+ sx={{
185
+ display: 'grid',
186
+ gridTemplateColumns: {
187
+ xs: 'repeat(1, 1fr)',
188
+ md: 'repeat(2, 1fr)',
189
+ },
190
+ gap: 2,
191
+ '@container (max-width: 600px)': {
192
+ gridTemplateColumns: 'repeat(1, 1fr)',
193
+ },
194
+ }}>
195
+ {creditCurrencies.map(renderCreditOverviewCard)}
196
+ </Box>
197
+
198
+ {creditCurrencies.length === 0 && (
199
+ <Box
200
+ sx={{
201
+ display: 'flex',
202
+ flexDirection: 'column',
203
+ alignItems: 'center',
204
+ justifyContent: 'center',
205
+ py: 8,
206
+ textAlign: 'center',
207
+ }}>
208
+ <Typography
209
+ variant="h6"
210
+ gutterBottom
211
+ sx={{
212
+ color: 'text.secondary',
213
+ }}>
214
+ {t('admin.customer.creditGrants.noGrants')}
215
+ </Typography>
216
+ <Typography
217
+ variant="body2"
218
+ sx={{
219
+ color: 'text.secondary',
220
+ }}>
221
+ {t('admin.customer.creditGrants.noGrantsDescription')}
222
+ </Typography>
223
+ </Box>
224
+ )}
225
+ </Box>
226
+ )}
227
+ {creditTab === CreditTab.GRANTS && <CreditGrantsList customer_id={customerId} mode={mode} key={creditTab} />}
228
+ {creditTab === CreditTab.TRANSACTIONS && (
229
+ <CreditTransactionsList customer_id={customerId} mode={mode} key={creditTab} />
230
+ )}
231
+ </Stack>
232
+ );
233
+ }
@@ -81,13 +81,16 @@ export default function CustomerForm() {
81
81
  },
82
82
  }}
83
83
  />
84
-
85
84
  <FormLabel className="base-label">{t('payment.checkout.billing.required')}</FormLabel>
86
85
  <Controller
87
86
  name="address.country"
88
87
  control={control}
89
88
  render={({ field }) => (
90
- <CountrySelect {...field} ref={field.ref as unknown as React.RefObject<HTMLDivElement>} sx={{ pl: '6px' }} />
89
+ <CountrySelect
90
+ {...field}
91
+ ref={field.ref as unknown as React.RefObject<HTMLDivElement | null>}
92
+ sx={{ pl: '6px' }}
93
+ />
91
94
  )}
92
95
  />
93
96
  <FormInput
@@ -12,7 +12,7 @@ import {
12
12
  getTxLink,
13
13
  } from '@blocklet/payment-react';
14
14
  import type { TInvoiceExpanded } from '@blocklet/payment-types';
15
- import { CircularProgress, Typography } from '@mui/material';
15
+ import { Avatar, CircularProgress, Typography } from '@mui/material';
16
16
  import { useLocalStorageState } from 'ahooks';
17
17
  import { useEffect, useState } from 'react';
18
18
  import { Link } from 'react-router-dom';
@@ -176,6 +176,24 @@ export default function InvoiceList({
176
176
  },
177
177
  },
178
178
  },
179
+ {
180
+ label: t('common.paymentMethod'),
181
+ name: 'paymentMethod',
182
+ width: 120,
183
+ options: {
184
+ customBodyRenderLite: (_: string, index: number) => {
185
+ const invoice = data?.list[index] as TInvoiceExpanded;
186
+ return (
187
+ <InvoiceLink invoice={invoice}>
188
+ <Typography sx={{ display: 'flex', alignItems: 'center', whiteSpace: 'nowrap' }}>
189
+ <Avatar src={invoice.paymentMethod.logo} sx={{ width: 18, height: 18, mr: 1 }} />
190
+ {invoice.paymentMethod.name}
191
+ </Typography>
192
+ </InvoiceLink>
193
+ );
194
+ },
195
+ },
196
+ },
179
197
  {
180
198
  label: t('common.status'),
181
199
  name: 'status',