payment-kit 1.13.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +15 -0
- package/README.md +3 -0
- package/api/dev.ts +6 -0
- package/api/hooks/pre-start.js +12 -0
- package/api/src/hooks/pre-start.ts +21 -0
- package/api/src/index.ts +92 -0
- package/api/src/jobs/event.ts +72 -0
- package/api/src/jobs/invoice.ts +148 -0
- package/api/src/jobs/payment.ts +208 -0
- package/api/src/jobs/subscription.ts +301 -0
- package/api/src/jobs/webhook.ts +113 -0
- package/api/src/libs/audit.ts +73 -0
- package/api/src/libs/auth.ts +40 -0
- package/api/src/libs/chain/arcblock.ts +13 -0
- package/api/src/libs/dayjs.ts +17 -0
- package/api/src/libs/env.ts +5 -0
- package/api/src/libs/hooks.ts +42 -0
- package/api/src/libs/logger.ts +27 -0
- package/api/src/libs/middleware.ts +12 -0
- package/api/src/libs/payment.ts +53 -0
- package/api/src/libs/queue/index.ts +263 -0
- package/api/src/libs/queue/store.ts +47 -0
- package/api/src/libs/security.ts +95 -0
- package/api/src/libs/session.ts +164 -0
- package/api/src/libs/util.ts +93 -0
- package/api/src/locales/en.ts +3 -0
- package/api/src/locales/index.ts +37 -0
- package/api/src/locales/zh.ts +3 -0
- package/api/src/routes/checkout-sessions.ts +536 -0
- package/api/src/routes/connect/collect.ts +109 -0
- package/api/src/routes/connect/pay.ts +116 -0
- package/api/src/routes/connect/setup.ts +121 -0
- package/api/src/routes/connect/shared.ts +410 -0
- package/api/src/routes/connect/subscribe.ts +128 -0
- package/api/src/routes/customers.ts +70 -0
- package/api/src/routes/events.ts +76 -0
- package/api/src/routes/index.ts +59 -0
- package/api/src/routes/invoices.ts +126 -0
- package/api/src/routes/payment-currencies.ts +38 -0
- package/api/src/routes/payment-intents.ts +122 -0
- package/api/src/routes/payment-links.ts +221 -0
- package/api/src/routes/payment-methods.ts +39 -0
- package/api/src/routes/prices.ts +134 -0
- package/api/src/routes/products.ts +191 -0
- package/api/src/routes/settings.ts +33 -0
- package/api/src/routes/subscription-items.ts +148 -0
- package/api/src/routes/subscriptions.ts +254 -0
- package/api/src/routes/usage-records.ts +120 -0
- package/api/src/routes/webhook-attempts.ts +57 -0
- package/api/src/routes/webhook-endpoints.ts +105 -0
- package/api/src/store/migrate.ts +16 -0
- package/api/src/store/migrations/20230905-genesis.ts +52 -0
- package/api/src/store/migrations/20230911-seeding.ts +145 -0
- package/api/src/store/models/checkout-session.ts +395 -0
- package/api/src/store/models/coupon.ts +137 -0
- package/api/src/store/models/customer.ts +199 -0
- package/api/src/store/models/discount.ts +116 -0
- package/api/src/store/models/event.ts +111 -0
- package/api/src/store/models/index.ts +165 -0
- package/api/src/store/models/invoice-item.ts +185 -0
- package/api/src/store/models/invoice.ts +492 -0
- package/api/src/store/models/job.ts +75 -0
- package/api/src/store/models/payment-currency.ts +139 -0
- package/api/src/store/models/payment-intent.ts +282 -0
- package/api/src/store/models/payment-link.ts +219 -0
- package/api/src/store/models/payment-method.ts +169 -0
- package/api/src/store/models/price.ts +266 -0
- package/api/src/store/models/product.ts +162 -0
- package/api/src/store/models/promotion-code.ts +112 -0
- package/api/src/store/models/setup-intent.ts +206 -0
- package/api/src/store/models/subscription-item.ts +103 -0
- package/api/src/store/models/subscription-schedule.ts +157 -0
- package/api/src/store/models/subscription.ts +307 -0
- package/api/src/store/models/types.ts +406 -0
- package/api/src/store/models/usage-record.ts +132 -0
- package/api/src/store/models/webhook-attempt.ts +96 -0
- package/api/src/store/models/webhook-endpoint.ts +96 -0
- package/api/src/store/sequelize.ts +15 -0
- package/api/third.d.ts +28 -0
- package/blocklet.md +3 -0
- package/blocklet.yml +89 -0
- package/index.html +14 -0
- package/logo.png +0 -0
- package/package.json +133 -0
- package/public/.gitkeep +0 -0
- package/screenshots/.gitkeep +0 -0
- package/screenshots/1-subscription.png +0 -0
- package/screenshots/2-customer-1.png +0 -0
- package/screenshots/3-customer-2.png +0 -0
- package/screenshots/4-admin-3.png +0 -0
- package/screenshots/5-admin-4.png +0 -0
- package/scripts/build-clean.js +6 -0
- package/scripts/bump-version.mjs +35 -0
- package/src/app.tsx +68 -0
- package/src/components/actions.tsx +85 -0
- package/src/components/blockchain/tx.tsx +29 -0
- package/src/components/checkout/amount.tsx +24 -0
- package/src/components/checkout/error.tsx +30 -0
- package/src/components/checkout/footer.tsx +12 -0
- package/src/components/checkout/form/address.tsx +38 -0
- package/src/components/checkout/form/index.tsx +295 -0
- package/src/components/checkout/header.tsx +23 -0
- package/src/components/checkout/pay.tsx +222 -0
- package/src/components/checkout/product-card.tsx +56 -0
- package/src/components/checkout/product-item.tsx +37 -0
- package/src/components/checkout/skeleton/overview.tsx +21 -0
- package/src/components/checkout/skeleton/payment.tsx +35 -0
- package/src/components/checkout/success.tsx +183 -0
- package/src/components/checkout/summary.tsx +34 -0
- package/src/components/collapse.tsx +50 -0
- package/src/components/confirm.tsx +55 -0
- package/src/components/copyable.tsx +38 -0
- package/src/components/currency.tsx +15 -0
- package/src/components/customer/actions.tsx +73 -0
- package/src/components/data.tsx +20 -0
- package/src/components/drawer-form.tsx +77 -0
- package/src/components/error-fallback.tsx +7 -0
- package/src/components/error.tsx +39 -0
- package/src/components/event/list.tsx +217 -0
- package/src/components/info-card.tsx +40 -0
- package/src/components/info-metric.tsx +35 -0
- package/src/components/info-row.tsx +28 -0
- package/src/components/input.tsx +40 -0
- package/src/components/invoice/action.tsx +94 -0
- package/src/components/invoice/list.tsx +225 -0
- package/src/components/invoice/table.tsx +110 -0
- package/src/components/layout.tsx +70 -0
- package/src/components/livemode.tsx +23 -0
- package/src/components/metadata/editor.tsx +57 -0
- package/src/components/metadata/form.tsx +45 -0
- package/src/components/payment-intent/actions.tsx +81 -0
- package/src/components/payment-intent/list.tsx +204 -0
- package/src/components/payment-link/actions.tsx +114 -0
- package/src/components/payment-link/after-pay.tsx +87 -0
- package/src/components/payment-link/before-pay.tsx +175 -0
- package/src/components/payment-link/item.tsx +135 -0
- package/src/components/payment-link/product-select.tsx +66 -0
- package/src/components/payment-link/rename.tsx +64 -0
- package/src/components/portal/invoice/list.tsx +110 -0
- package/src/components/portal/subscription/cancel.tsx +83 -0
- package/src/components/portal/subscription/list.tsx +232 -0
- package/src/components/price/actions.tsx +21 -0
- package/src/components/price/form.tsx +292 -0
- package/src/components/product/actions.tsx +125 -0
- package/src/components/product/add-price.tsx +59 -0
- package/src/components/product/create.tsx +97 -0
- package/src/components/product/edit-price.tsx +75 -0
- package/src/components/product/edit.tsx +67 -0
- package/src/components/product/features.tsx +32 -0
- package/src/components/product/form.tsx +76 -0
- package/src/components/relative-time.tsx +41 -0
- package/src/components/section/header.tsx +29 -0
- package/src/components/status.tsx +12 -0
- package/src/components/subscription/actions/cancel.tsx +66 -0
- package/src/components/subscription/actions/index.tsx +172 -0
- package/src/components/subscription/actions/pause.tsx +83 -0
- package/src/components/subscription/items/actions.tsx +31 -0
- package/src/components/subscription/items/index.tsx +107 -0
- package/src/components/subscription/list.tsx +200 -0
- package/src/components/switch.tsx +48 -0
- package/src/components/table.tsx +66 -0
- package/src/components/uploader.tsx +81 -0
- package/src/components/webhook/attempts.tsx +149 -0
- package/src/contexts/products.tsx +42 -0
- package/src/contexts/session.ts +10 -0
- package/src/contexts/settings.tsx +54 -0
- package/src/env.d.ts +17 -0
- package/src/global.css +97 -0
- package/src/hooks/mobile.ts +15 -0
- package/src/index.tsx +6 -0
- package/src/libs/api.ts +19 -0
- package/src/libs/dayjs.ts +17 -0
- package/src/libs/util.ts +474 -0
- package/src/locales/en.tsx +395 -0
- package/src/locales/index.tsx +8 -0
- package/src/locales/zh.tsx +389 -0
- package/src/pages/admin/billing/index.tsx +56 -0
- package/src/pages/admin/billing/invoices/detail.tsx +215 -0
- package/src/pages/admin/billing/invoices/index.tsx +5 -0
- package/src/pages/admin/billing/subscriptions/detail.tsx +237 -0
- package/src/pages/admin/billing/subscriptions/index.tsx +5 -0
- package/src/pages/admin/customers/customers/detail.tsx +209 -0
- package/src/pages/admin/customers/customers/index.tsx +109 -0
- package/src/pages/admin/customers/index.tsx +47 -0
- package/src/pages/admin/developers/events/detail.tsx +77 -0
- package/src/pages/admin/developers/events/index.tsx +5 -0
- package/src/pages/admin/developers/index.tsx +60 -0
- package/src/pages/admin/developers/logs.tsx +3 -0
- package/src/pages/admin/developers/overview.tsx +3 -0
- package/src/pages/admin/developers/webhooks/detail.tsx +109 -0
- package/src/pages/admin/developers/webhooks/index.tsx +102 -0
- package/src/pages/admin/index.tsx +120 -0
- package/src/pages/admin/overview.tsx +3 -0
- package/src/pages/admin/payments/index.tsx +65 -0
- package/src/pages/admin/payments/intents/detail.tsx +205 -0
- package/src/pages/admin/payments/intents/index.tsx +5 -0
- package/src/pages/admin/payments/links/create.tsx +141 -0
- package/src/pages/admin/payments/links/detail.tsx +318 -0
- package/src/pages/admin/payments/links/index.tsx +167 -0
- package/src/pages/admin/products/coupons/index.tsx +3 -0
- package/src/pages/admin/products/index.tsx +81 -0
- package/src/pages/admin/products/prices/actions.tsx +151 -0
- package/src/pages/admin/products/prices/detail.tsx +203 -0
- package/src/pages/admin/products/prices/list.tsx +95 -0
- package/src/pages/admin/products/pricing-tables.tsx +3 -0
- package/src/pages/admin/products/products/create.tsx +105 -0
- package/src/pages/admin/products/products/detail.tsx +246 -0
- package/src/pages/admin/products/products/index.tsx +154 -0
- package/src/pages/admin/settings/branding.tsx +3 -0
- package/src/pages/admin/settings/business.tsx +3 -0
- package/src/pages/admin/settings/index.tsx +47 -0
- package/src/pages/admin/settings/payment-methods.tsx +80 -0
- package/src/pages/checkout/index.tsx +38 -0
- package/src/pages/checkout/pay.tsx +89 -0
- package/src/pages/customer/index.tsx +93 -0
- package/src/pages/customer/invoice.tsx +147 -0
- package/src/pages/home.tsx +9 -0
- package/tsconfig.api.json +9 -0
- package/tsconfig.eslint.json +7 -0
- package/tsconfig.json +99 -0
- package/tsconfig.types.json +11 -0
- package/vite.config.ts +19 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/indent */
|
|
2
|
+
import Dialog from '@arcblock/ux/lib/Dialog';
|
|
3
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
4
|
+
import type { TProduct } from '@did-pay/types';
|
|
5
|
+
import { Button, CircularProgress, Stack } from '@mui/material';
|
|
6
|
+
import { isEmpty } from 'lodash';
|
|
7
|
+
import type { EventHandler } from 'react';
|
|
8
|
+
import { FormProvider, useForm } from 'react-hook-form';
|
|
9
|
+
|
|
10
|
+
import type { Product } from './form';
|
|
11
|
+
import ProductForm from './form';
|
|
12
|
+
|
|
13
|
+
export default function EditProduct({
|
|
14
|
+
product,
|
|
15
|
+
loading,
|
|
16
|
+
onSave,
|
|
17
|
+
onCancel,
|
|
18
|
+
}: {
|
|
19
|
+
product: TProduct;
|
|
20
|
+
loading: boolean;
|
|
21
|
+
onSave: EventHandler<any>;
|
|
22
|
+
onCancel: EventHandler<any>;
|
|
23
|
+
}) {
|
|
24
|
+
const { t } = useLocaleContext();
|
|
25
|
+
const methods = useForm<Product>({
|
|
26
|
+
defaultValues: {
|
|
27
|
+
...product,
|
|
28
|
+
metadata: isEmpty(product.metadata)
|
|
29
|
+
? []
|
|
30
|
+
: // @ts-ignore
|
|
31
|
+
Object.keys(product.metadata).map((x) => ({ key: x, value: product.metadata[x] })),
|
|
32
|
+
prices: [],
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const { handleSubmit, reset } = methods;
|
|
37
|
+
const onSubmit = async (data: any) => {
|
|
38
|
+
await handleSubmit(onSave)(data);
|
|
39
|
+
reset();
|
|
40
|
+
onCancel(null);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<Dialog
|
|
45
|
+
open
|
|
46
|
+
disableEscapeKeyDown
|
|
47
|
+
fullWidth
|
|
48
|
+
maxWidth="sm"
|
|
49
|
+
onClose={() => onCancel(null)}
|
|
50
|
+
showCloseButton={false}
|
|
51
|
+
title={t('admin.product.edit')}
|
|
52
|
+
actions={
|
|
53
|
+
<Stack direction="row">
|
|
54
|
+
<Button size="small" sx={{ mr: 2 }} onClick={onCancel}>
|
|
55
|
+
{t('common.cancel')}
|
|
56
|
+
</Button>
|
|
57
|
+
<Button variant="contained" color="primary" size="small" disabled={loading} onClick={onSubmit}>
|
|
58
|
+
{loading && <CircularProgress size="small" />} {t('common.save')}
|
|
59
|
+
</Button>
|
|
60
|
+
</Stack>
|
|
61
|
+
}>
|
|
62
|
+
<FormProvider {...methods}>
|
|
63
|
+
<ProductForm />
|
|
64
|
+
</FormProvider>
|
|
65
|
+
</Dialog>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import { AddOutlined, DeleteOutlineOutlined } from '@mui/icons-material';
|
|
3
|
+
import { Box, Button, IconButton, TextField, Typography } from '@mui/material';
|
|
4
|
+
import { Controller, useFieldArray, useFormContext } from 'react-hook-form';
|
|
5
|
+
|
|
6
|
+
export default function MetadataForm() {
|
|
7
|
+
const { t } = useLocaleContext();
|
|
8
|
+
const { control } = useFormContext();
|
|
9
|
+
const features = useFieldArray({ control, name: 'features' });
|
|
10
|
+
return (
|
|
11
|
+
<Box sx={{ width: 1 }}>
|
|
12
|
+
<Typography>{t('admin.product.features.label')}</Typography>
|
|
13
|
+
{features.fields.map((feature, index) => (
|
|
14
|
+
<Box key={feature.id} mt={2} sx={{ width: 1 }}>
|
|
15
|
+
<Controller
|
|
16
|
+
render={({ field }) => <TextField {...field} sx={{ width: '80%' }} size="small" />}
|
|
17
|
+
name={`features.${index}.name`}
|
|
18
|
+
control={control}
|
|
19
|
+
/>
|
|
20
|
+
<IconButton size="small" sx={{ ml: 1 }} onClick={() => features.remove(index)}>
|
|
21
|
+
<DeleteOutlineOutlined color="error" sx={{ opacity: 0.75 }} />
|
|
22
|
+
</IconButton>
|
|
23
|
+
</Box>
|
|
24
|
+
))}
|
|
25
|
+
<Box mt={features.fields.length ? 2 : 1}>
|
|
26
|
+
<Button size="small" variant="outlined" color="inherit" onClick={() => features.append({ name: '' })}>
|
|
27
|
+
<AddOutlined fontSize="small" /> {t('admin.product.features.add')}
|
|
28
|
+
</Button>
|
|
29
|
+
</Box>
|
|
30
|
+
</Box>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* eslint-disable no-nested-ternary */
|
|
2
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
3
|
+
import type { InferFormType, TProduct } from '@did-pay/types';
|
|
4
|
+
import { Box, Stack, Typography } from '@mui/material';
|
|
5
|
+
import { useFormContext, useWatch } from 'react-hook-form';
|
|
6
|
+
|
|
7
|
+
import Collapse from '../collapse';
|
|
8
|
+
import TextInput from '../input';
|
|
9
|
+
import MetadataForm from '../metadata/form';
|
|
10
|
+
import type { Price } from '../price/form';
|
|
11
|
+
import Uploader from '../uploader';
|
|
12
|
+
import ProductFeatures from './features';
|
|
13
|
+
|
|
14
|
+
export type Product = InferFormType<TProduct> & {
|
|
15
|
+
prices: Price[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type Props = {
|
|
19
|
+
simple?: boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default function ProductForm(props: Props) {
|
|
23
|
+
const { t } = useLocaleContext();
|
|
24
|
+
const { control, setValue, formState } = useFormContext();
|
|
25
|
+
const images = useWatch({ control, name: 'images' });
|
|
26
|
+
|
|
27
|
+
const onUploaded = (result: any) => {
|
|
28
|
+
const tmp = new URL(result.url);
|
|
29
|
+
setValue('images', [tmp.pathname]);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<Stack spacing={2} mb={3} direction="row" alignItems="flex-start">
|
|
34
|
+
<Stack spacing={2} flex={2} alignItems="flex-start">
|
|
35
|
+
<TextInput
|
|
36
|
+
name="name"
|
|
37
|
+
rules={{ required: t('admin.product.name.required') }}
|
|
38
|
+
label={t('admin.product.name.label')}
|
|
39
|
+
placeholder={t('admin.product.name.placeholder')}
|
|
40
|
+
error={!!formState.errors.name}
|
|
41
|
+
helperText={formState.errors.name?.message as string}
|
|
42
|
+
autoFocus
|
|
43
|
+
/>
|
|
44
|
+
<TextInput
|
|
45
|
+
name="description"
|
|
46
|
+
rules={{ required: t('admin.product.description.required') }}
|
|
47
|
+
label={t('admin.product.description.label')}
|
|
48
|
+
placeholder={t('admin.product.description.placeholder')}
|
|
49
|
+
error={!!formState.errors.description}
|
|
50
|
+
helperText={formState.errors.description?.message as string}
|
|
51
|
+
multiline
|
|
52
|
+
minRows={2}
|
|
53
|
+
maxRows={4}
|
|
54
|
+
/>
|
|
55
|
+
<Collapse trigger={t('admin.product.additional')}>
|
|
56
|
+
<Stack spacing={2} alignItems="flex-start">
|
|
57
|
+
<TextInput name="statement_descriptor" label={t('admin.product.statement_descriptor.label')} />
|
|
58
|
+
<TextInput name="unit_label" label={t('admin.product.unit_label.label')} />
|
|
59
|
+
{!props.simple && <ProductFeatures />}
|
|
60
|
+
{!props.simple && <MetadataForm title={t('common.metadata.label')} />}
|
|
61
|
+
</Stack>
|
|
62
|
+
</Collapse>
|
|
63
|
+
</Stack>
|
|
64
|
+
<Box flex={1}>
|
|
65
|
+
<Stack direction="column">
|
|
66
|
+
<Typography mb={1}>{t('admin.product.image.label')}</Typography>
|
|
67
|
+
<Uploader onUploaded={onUploaded} preview={images[0]} />
|
|
68
|
+
</Stack>
|
|
69
|
+
</Box>
|
|
70
|
+
</Stack>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
ProductForm.defaultProps = {
|
|
75
|
+
simple: false,
|
|
76
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import { Tooltip, Typography } from '@mui/material';
|
|
3
|
+
import prettyMs from 'pretty-ms-i18n';
|
|
4
|
+
|
|
5
|
+
import { formatPrettyMsLocale, formatToDatetime } from '../libs/util';
|
|
6
|
+
|
|
7
|
+
export default function RelativeTime({ value, ...rest }: { value: string | number }) {
|
|
8
|
+
const { t, locale } = useLocaleContext();
|
|
9
|
+
if (!value) {
|
|
10
|
+
return '-';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const prettyOption = {
|
|
14
|
+
locale: formatPrettyMsLocale(locale),
|
|
15
|
+
compact: true,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const valTime = new Date(value).getTime();
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
|
|
21
|
+
if (valTime > now) {
|
|
22
|
+
const time = prettyMs(valTime - now, prettyOption);
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<Tooltip title={formatToDatetime(value, locale)} placement="top-end" enterTouchDelay={0}>
|
|
26
|
+
<Typography component="span" {...rest}>
|
|
27
|
+
{t('common.afterTime', { time })}
|
|
28
|
+
</Typography>
|
|
29
|
+
</Tooltip>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const time = prettyMs(now - valTime, prettyOption);
|
|
34
|
+
return (
|
|
35
|
+
<Tooltip title={formatToDatetime(value, locale)} placement="top-end" enterTouchDelay={0}>
|
|
36
|
+
<Typography component="span" sx={{ cursor: 'pointer' }} {...rest}>
|
|
37
|
+
{t('common.timeAgo', { time })}
|
|
38
|
+
</Typography>
|
|
39
|
+
</Tooltip>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Stack, Typography } from '@mui/material';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type Props = {
|
|
5
|
+
title: string;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
mb?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default function SectionHeader(props: Props) {
|
|
11
|
+
return (
|
|
12
|
+
<Stack
|
|
13
|
+
className="section-header"
|
|
14
|
+
direction="row"
|
|
15
|
+
justifyContent="space-between"
|
|
16
|
+
alignItems="center"
|
|
17
|
+
sx={{ mb: props.mb, pb: 1, width: 1, borderBottom: '1px solid #eee' }}>
|
|
18
|
+
<Typography variant="h6" sx={{ fontWeight: 600 }}>
|
|
19
|
+
{props.title}
|
|
20
|
+
</Typography>
|
|
21
|
+
{props.children}
|
|
22
|
+
</Stack>
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
SectionHeader.defaultProps = {
|
|
27
|
+
children: null,
|
|
28
|
+
mb: 1,
|
|
29
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Chip, ChipProps } from '@mui/material';
|
|
2
|
+
|
|
3
|
+
export default function Status(props: ChipProps) {
|
|
4
|
+
return (
|
|
5
|
+
<Chip
|
|
6
|
+
{...props}
|
|
7
|
+
size="small"
|
|
8
|
+
variant="outlined"
|
|
9
|
+
sx={{ ...(props.sx || {}), borderRadius: '4px', textTransform: 'capitalize' }}
|
|
10
|
+
/>
|
|
11
|
+
);
|
|
12
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import type { TSubscriptionExpanded } from '@did-pay/types';
|
|
3
|
+
import { Box, FormControlLabel, Radio, RadioGroup, Stack, TextField, Typography } from '@mui/material';
|
|
4
|
+
import { Controller, useFormContext, useWatch } from 'react-hook-form';
|
|
5
|
+
|
|
6
|
+
import { formatTime } from '../../../libs/util';
|
|
7
|
+
|
|
8
|
+
export default function SubscriptionCancelForm({ data }: { data: TSubscriptionExpanded }) {
|
|
9
|
+
const { t } = useLocaleContext();
|
|
10
|
+
const { control, formState } = useFormContext();
|
|
11
|
+
const isCustom = useWatch({ control, name: 'cancel.at' }) === 'custom';
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<Box sx={{ width: 400 }}>
|
|
15
|
+
<Stack direction="row" spacing={3} alignItems="flex-start">
|
|
16
|
+
<Typography>{t('admin.subscription.cancel.at.title')}</Typography>
|
|
17
|
+
<Stack>
|
|
18
|
+
<Controller
|
|
19
|
+
name="cancel.at"
|
|
20
|
+
control={control}
|
|
21
|
+
render={({ field }) => (
|
|
22
|
+
<RadioGroup {...field}>
|
|
23
|
+
<FormControlLabel
|
|
24
|
+
value="now"
|
|
25
|
+
control={<Radio checked={field.value === 'now'} />}
|
|
26
|
+
label={t('admin.subscription.cancel.at.now', { date: formatTime(new Date()) })}
|
|
27
|
+
/>
|
|
28
|
+
<FormControlLabel
|
|
29
|
+
value="current_period_end"
|
|
30
|
+
control={<Radio checked={field.value === 'current_period_end'} />}
|
|
31
|
+
label={t('admin.subscription.cancel.at.current_period_end', {
|
|
32
|
+
date: formatTime(new Date(data.current_period_end * 1000)),
|
|
33
|
+
})}
|
|
34
|
+
/>
|
|
35
|
+
<FormControlLabel
|
|
36
|
+
value="custom"
|
|
37
|
+
control={<Radio checked={field.value === 'custom'} />}
|
|
38
|
+
label={t('admin.subscription.cancel.at.custom')}
|
|
39
|
+
/>
|
|
40
|
+
</RadioGroup>
|
|
41
|
+
)}
|
|
42
|
+
/>
|
|
43
|
+
{isCustom && (
|
|
44
|
+
<Controller
|
|
45
|
+
name="cancel.time"
|
|
46
|
+
rules={{ required: isCustom }}
|
|
47
|
+
control={control}
|
|
48
|
+
render={({ field }) => (
|
|
49
|
+
<TextField
|
|
50
|
+
size="small"
|
|
51
|
+
variant="outlined"
|
|
52
|
+
type="datetime-local"
|
|
53
|
+
{...field}
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
error={!!formState.errors.cancel?.time}
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
helperText={formState.errors.cancel?.time?.message as string}
|
|
58
|
+
/>
|
|
59
|
+
)}
|
|
60
|
+
/>
|
|
61
|
+
)}
|
|
62
|
+
</Stack>
|
|
63
|
+
</Stack>
|
|
64
|
+
</Box>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import Toast from '@arcblock/ux/lib/Toast';
|
|
3
|
+
import type { TSubscriptionExpanded } from '@did-pay/types';
|
|
4
|
+
import { useSetState } from 'ahooks';
|
|
5
|
+
import { FormProvider, useForm, useFormContext } from 'react-hook-form';
|
|
6
|
+
import { useNavigate } from 'react-router-dom';
|
|
7
|
+
import type { LiteralUnion } from 'type-fest';
|
|
8
|
+
|
|
9
|
+
import api from '../../../libs/api';
|
|
10
|
+
import { formatError } from '../../../libs/util';
|
|
11
|
+
import Actions from '../../actions';
|
|
12
|
+
import ConfirmDialog from '../../confirm';
|
|
13
|
+
import SubscriptionCancelForm from './cancel';
|
|
14
|
+
import SubscriptionPauseForm from './pause';
|
|
15
|
+
|
|
16
|
+
type Props = {
|
|
17
|
+
data: TSubscriptionExpanded;
|
|
18
|
+
variant?: LiteralUnion<'compact' | 'normal', string>;
|
|
19
|
+
onChange: (action: string) => void;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
SubscriptionActionsInner.defaultProps = {
|
|
23
|
+
variant: 'compact',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function SubscriptionActionsInner({ data, variant, onChange }: Props) {
|
|
27
|
+
const { t } = useLocaleContext();
|
|
28
|
+
const navigate = useNavigate();
|
|
29
|
+
const { reset, getValues, setError } = useFormContext();
|
|
30
|
+
const [state, setState] = useSetState({
|
|
31
|
+
action: '',
|
|
32
|
+
loading: false,
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const createHandler = (action: string) => {
|
|
36
|
+
return async () => {
|
|
37
|
+
const values = getValues();
|
|
38
|
+
if (action === 'cancel') {
|
|
39
|
+
if (values.cancel.at === 'custom' && !values.cancel.time) {
|
|
40
|
+
setError('cancel.time', { type: 'required', message: t('admin.subscription.cancel.required') });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (action === 'pause') {
|
|
45
|
+
if (values.pause.type === 'custom' && !values.pause.resumesAt) {
|
|
46
|
+
setError('pause.resumesAt', { type: 'required', message: t('admin.subscription.pause.required') });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
setState({ loading: true });
|
|
53
|
+
await api.put(`/api/subscriptions/${data.id}/${action}`, values[action] || {}).then((res) => res.data);
|
|
54
|
+
Toast.success(t('common.saved'));
|
|
55
|
+
onChange(state.action);
|
|
56
|
+
} catch (err) {
|
|
57
|
+
console.error(err);
|
|
58
|
+
Toast.error(formatError(err));
|
|
59
|
+
} finally {
|
|
60
|
+
setState({ loading: false, action: '' });
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const handleCancel = () => {
|
|
66
|
+
setState({ action: '' });
|
|
67
|
+
reset();
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const resume = {
|
|
71
|
+
label: t('admin.subscription.resume'),
|
|
72
|
+
handler: () => setState({ action: 'resume' }),
|
|
73
|
+
disabled: data.status === 'canceled',
|
|
74
|
+
color: 'primary',
|
|
75
|
+
};
|
|
76
|
+
const pause = {
|
|
77
|
+
label: t('admin.subscription.pause.title'),
|
|
78
|
+
handler: () => setState({ action: 'pause' }),
|
|
79
|
+
disabled: data.status === 'canceled',
|
|
80
|
+
color: 'primary',
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const actions = [
|
|
84
|
+
{
|
|
85
|
+
label: t('admin.subscription.update'),
|
|
86
|
+
handler: () => setState({ action: 'update' }),
|
|
87
|
+
color: 'primary',
|
|
88
|
+
disabled: data.status === 'canceled',
|
|
89
|
+
},
|
|
90
|
+
data.status === 'paused' ? resume : pause,
|
|
91
|
+
{
|
|
92
|
+
label: t('admin.subscription.cancel.title'),
|
|
93
|
+
handler: () => setState({ action: 'cancel' }),
|
|
94
|
+
disabled: data.status === 'canceled',
|
|
95
|
+
color: 'error',
|
|
96
|
+
divider: true,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
label: t('admin.customer.view'),
|
|
100
|
+
handler: () => navigate(`/admin/customers/${data.customer_id}`),
|
|
101
|
+
color: 'primary',
|
|
102
|
+
},
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
if (variant === 'compact') {
|
|
106
|
+
actions.push({
|
|
107
|
+
label: t('admin.subscription.view'),
|
|
108
|
+
handler: () => navigate(`/admin/billing/${data.id}`),
|
|
109
|
+
color: 'primary',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<>
|
|
115
|
+
<Actions variant={variant} actions={actions} />
|
|
116
|
+
{state.action === 'cancel' && (
|
|
117
|
+
<ConfirmDialog
|
|
118
|
+
onConfirm={createHandler('cancel')}
|
|
119
|
+
onCancel={handleCancel}
|
|
120
|
+
title={t('admin.subscription.cancel.title')}
|
|
121
|
+
message={<SubscriptionCancelForm data={data} />}
|
|
122
|
+
loading={state.loading}
|
|
123
|
+
/>
|
|
124
|
+
)}
|
|
125
|
+
{state.action === 'pause' && (
|
|
126
|
+
<ConfirmDialog
|
|
127
|
+
onConfirm={createHandler('pause')}
|
|
128
|
+
onCancel={handleCancel}
|
|
129
|
+
title={t('admin.subscription.pause.title')}
|
|
130
|
+
message={<SubscriptionPauseForm />}
|
|
131
|
+
loading={state.loading}
|
|
132
|
+
/>
|
|
133
|
+
)}
|
|
134
|
+
{state.action === 'resume' && (
|
|
135
|
+
<ConfirmDialog
|
|
136
|
+
onConfirm={createHandler('resume')}
|
|
137
|
+
onCancel={handleCancel}
|
|
138
|
+
title={t('admin.subscription.resume')}
|
|
139
|
+
message={t('admin.subscription.resumeTip')}
|
|
140
|
+
loading={state.loading}
|
|
141
|
+
/>
|
|
142
|
+
)}
|
|
143
|
+
</>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export default function SubscriptionActions(props: Props) {
|
|
148
|
+
const methods = useForm({
|
|
149
|
+
defaultValues: {
|
|
150
|
+
cancel: {
|
|
151
|
+
at: 'now',
|
|
152
|
+
time: '',
|
|
153
|
+
refund: 'none',
|
|
154
|
+
},
|
|
155
|
+
pause: {
|
|
156
|
+
type: 'never',
|
|
157
|
+
resumesAt: '',
|
|
158
|
+
behavior: 'void',
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<FormProvider {...methods}>
|
|
165
|
+
<SubscriptionActionsInner {...props} />
|
|
166
|
+
</FormProvider>
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
SubscriptionActions.defaultProps = {
|
|
171
|
+
variant: 'compact',
|
|
172
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import { FormControlLabel, Radio, RadioGroup, Stack, TextField, Typography } from '@mui/material';
|
|
3
|
+
import { Controller, useFormContext, useWatch } from 'react-hook-form';
|
|
4
|
+
|
|
5
|
+
export default function SubscriptionPauseForm() {
|
|
6
|
+
const { t } = useLocaleContext();
|
|
7
|
+
const { control, formState } = useFormContext();
|
|
8
|
+
const isCustom = useWatch({ control, name: 'pause.type' }) === 'custom';
|
|
9
|
+
|
|
10
|
+
return (
|
|
11
|
+
<Stack direction="column" spacing={3} sx={{ width: 400 }}>
|
|
12
|
+
<Stack direction="row" spacing={3} alignItems="flex-start">
|
|
13
|
+
<Typography flex={1}>{t('admin.subscription.pause.type.title')}</Typography>
|
|
14
|
+
<Stack flex={2}>
|
|
15
|
+
<Controller
|
|
16
|
+
name="pause.type"
|
|
17
|
+
control={control}
|
|
18
|
+
render={({ field }) => (
|
|
19
|
+
<RadioGroup {...field}>
|
|
20
|
+
<FormControlLabel
|
|
21
|
+
value="never"
|
|
22
|
+
control={<Radio checked={field.value === 'never'} />}
|
|
23
|
+
label={t('admin.subscription.pause.type.never')}
|
|
24
|
+
/>
|
|
25
|
+
<FormControlLabel
|
|
26
|
+
value="custom"
|
|
27
|
+
control={<Radio checked={field.value === 'custom'} />}
|
|
28
|
+
label={t('admin.subscription.pause.type.custom')}
|
|
29
|
+
/>
|
|
30
|
+
</RadioGroup>
|
|
31
|
+
)}
|
|
32
|
+
/>
|
|
33
|
+
{isCustom && (
|
|
34
|
+
<Controller
|
|
35
|
+
name="pause.resumesAt"
|
|
36
|
+
control={control}
|
|
37
|
+
render={({ field }) => (
|
|
38
|
+
<TextField
|
|
39
|
+
size="small"
|
|
40
|
+
variant="outlined"
|
|
41
|
+
type="datetime-local"
|
|
42
|
+
{...field}
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
error={!!formState.errors.pause?.resumesAt}
|
|
45
|
+
// @ts-ignore
|
|
46
|
+
helperText={formState.errors.pause?.resumesAt?.message as string}
|
|
47
|
+
/>
|
|
48
|
+
)}
|
|
49
|
+
/>
|
|
50
|
+
)}
|
|
51
|
+
</Stack>
|
|
52
|
+
</Stack>
|
|
53
|
+
<Stack direction="row" spacing={3} alignItems="flex-start">
|
|
54
|
+
<Typography flex={1}>{t('admin.subscription.pause.behavior.title')}</Typography>
|
|
55
|
+
<Stack flex={2}>
|
|
56
|
+
<Controller
|
|
57
|
+
name="pause.behavior"
|
|
58
|
+
control={control}
|
|
59
|
+
render={({ field }) => (
|
|
60
|
+
<RadioGroup {...field}>
|
|
61
|
+
<FormControlLabel
|
|
62
|
+
value="keep_as_draft"
|
|
63
|
+
control={<Radio checked={field.value === 'keep_as_draft'} />}
|
|
64
|
+
label={t('admin.subscription.pause.behavior.keep_as_draft')}
|
|
65
|
+
/>
|
|
66
|
+
<FormControlLabel
|
|
67
|
+
value="mark_uncollectible"
|
|
68
|
+
control={<Radio checked={field.value === 'mark_uncollectible'} />}
|
|
69
|
+
label={t('admin.subscription.pause.behavior.mark_uncollectible')}
|
|
70
|
+
/>
|
|
71
|
+
<FormControlLabel
|
|
72
|
+
value="void"
|
|
73
|
+
control={<Radio checked={field.value === 'void'} />}
|
|
74
|
+
label={t('admin.subscription.pause.behavior.void')}
|
|
75
|
+
/>
|
|
76
|
+
</RadioGroup>
|
|
77
|
+
)}
|
|
78
|
+
/>
|
|
79
|
+
</Stack>
|
|
80
|
+
</Stack>
|
|
81
|
+
</Stack>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
|
|
2
|
+
import type { TLineItemExpanded } from '@did-pay/types';
|
|
3
|
+
import { useNavigate } from 'react-router-dom';
|
|
4
|
+
|
|
5
|
+
import Actions from '../../actions';
|
|
6
|
+
|
|
7
|
+
type Props = {
|
|
8
|
+
data: TLineItemExpanded;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export default function LineItemActions(props: Props) {
|
|
12
|
+
const { t } = useLocaleContext();
|
|
13
|
+
const navigate = useNavigate();
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<Actions
|
|
17
|
+
actions={[
|
|
18
|
+
{
|
|
19
|
+
label: t('admin.price.view'),
|
|
20
|
+
handler: () => navigate(`/admin/products/${props.data.price_id}`),
|
|
21
|
+
color: 'primary',
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
label: t('admin.product.view'),
|
|
25
|
+
handler: () => navigate(`/admin/products/${props.data.price.product_id}`),
|
|
26
|
+
color: 'primary',
|
|
27
|
+
},
|
|
28
|
+
]}
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
}
|