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,59 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
|
|
3
|
+
import { PaymentCurrency } from '../store/models/payment-currency';
|
|
4
|
+
import checkoutSessions from './checkout-sessions';
|
|
5
|
+
import customers from './customers';
|
|
6
|
+
import events from './events';
|
|
7
|
+
import invoices from './invoices';
|
|
8
|
+
import paymentCurrencies from './payment-currencies';
|
|
9
|
+
import paymentIntents from './payment-intents';
|
|
10
|
+
import paymentLinks from './payment-links';
|
|
11
|
+
import paymentMethods from './payment-methods';
|
|
12
|
+
import prices from './prices';
|
|
13
|
+
import products from './products';
|
|
14
|
+
import settings from './settings';
|
|
15
|
+
import subscriptionItems from './subscription-items';
|
|
16
|
+
import subscriptions from './subscriptions';
|
|
17
|
+
import usageRecords from './usage-records';
|
|
18
|
+
import webhookAttempts from './webhook-attempts';
|
|
19
|
+
import webhookEndpoints from './webhook-endpoints';
|
|
20
|
+
|
|
21
|
+
const router = Router();
|
|
22
|
+
|
|
23
|
+
router.use((req, _, next) => {
|
|
24
|
+
if (req.query.livemode) {
|
|
25
|
+
try {
|
|
26
|
+
req.livemode = !!JSON.parse(String(req.query.livemode));
|
|
27
|
+
} catch {
|
|
28
|
+
req.livemode = true;
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
req.livemode = true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
next();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
router.use(async (req, _, next) => {
|
|
38
|
+
req.currency = await PaymentCurrency.findOne({ where: { is_base_currency: true, livemode: req.livemode } });
|
|
39
|
+
next();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
router.use('/checkout-sessions', checkoutSessions);
|
|
43
|
+
router.use('/customers', customers);
|
|
44
|
+
router.use('/events', events);
|
|
45
|
+
router.use('/invoices', invoices);
|
|
46
|
+
router.use('/payment-intents', paymentIntents);
|
|
47
|
+
router.use('/payment-links', paymentLinks);
|
|
48
|
+
router.use('/payment-methods', paymentMethods);
|
|
49
|
+
router.use('/payment-currencies', paymentCurrencies);
|
|
50
|
+
router.use('/prices', prices);
|
|
51
|
+
router.use('/products', products);
|
|
52
|
+
router.use('/settings', settings);
|
|
53
|
+
router.use('/subscription-items', subscriptionItems);
|
|
54
|
+
router.use('/subscriptions', subscriptions);
|
|
55
|
+
router.use('/usage-records', usageRecords);
|
|
56
|
+
router.use('/webhook-attempts', webhookAttempts);
|
|
57
|
+
router.use('/webhook-endpoints', webhookEndpoints);
|
|
58
|
+
|
|
59
|
+
export default router;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { isValid } from '@arcblock/did';
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
import type { WhereOptions } from 'sequelize';
|
|
5
|
+
|
|
6
|
+
import { authenticate } from '../libs/security';
|
|
7
|
+
import { expandLineItems } from '../libs/session';
|
|
8
|
+
import { Customer } from '../store/models/customer';
|
|
9
|
+
import { Invoice } from '../store/models/invoice';
|
|
10
|
+
import { InvoiceItem } from '../store/models/invoice-item';
|
|
11
|
+
import { PaymentCurrency } from '../store/models/payment-currency';
|
|
12
|
+
import { PaymentIntent } from '../store/models/payment-intent';
|
|
13
|
+
import { PaymentMethod } from '../store/models/payment-method';
|
|
14
|
+
import { Price } from '../store/models/price';
|
|
15
|
+
import { Product } from '../store/models/product';
|
|
16
|
+
import { Subscription } from '../store/models/subscription';
|
|
17
|
+
|
|
18
|
+
const router = Router();
|
|
19
|
+
const authMine = authenticate<Subscription>({ component: true, roles: ['owner', 'admin'], mine: true });
|
|
20
|
+
const authPortal = authenticate<Invoice>({
|
|
21
|
+
component: true,
|
|
22
|
+
roles: ['owner', 'admin'],
|
|
23
|
+
record: {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
model: Invoice,
|
|
26
|
+
field: 'customer_id',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const schema = Joi.object<{
|
|
31
|
+
page: number;
|
|
32
|
+
size: number;
|
|
33
|
+
livemode?: boolean;
|
|
34
|
+
status?: string;
|
|
35
|
+
customer_id?: string;
|
|
36
|
+
customer_did?: string;
|
|
37
|
+
subscription_id?: string;
|
|
38
|
+
}>({
|
|
39
|
+
page: Joi.number().integer().min(1).default(1),
|
|
40
|
+
size: Joi.number().integer().min(1).max(100).default(20),
|
|
41
|
+
livemode: Joi.boolean().empty(''),
|
|
42
|
+
status: Joi.string().empty(''),
|
|
43
|
+
customer_id: Joi.string().empty(''),
|
|
44
|
+
customer_did: Joi.string().empty(''),
|
|
45
|
+
subscription_id: Joi.string().empty(''),
|
|
46
|
+
});
|
|
47
|
+
router.get('/', authMine, async (req, res) => {
|
|
48
|
+
const { page, size, ...query } = await schema.validateAsync(req.query, { stripUnknown: true });
|
|
49
|
+
const where: WhereOptions<Invoice> = {};
|
|
50
|
+
|
|
51
|
+
if (query.status) {
|
|
52
|
+
where.status = query.status
|
|
53
|
+
.split(',')
|
|
54
|
+
.map((x) => x.trim())
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
}
|
|
57
|
+
if (query.customer_id) {
|
|
58
|
+
where.customer_id = query.customer_id;
|
|
59
|
+
}
|
|
60
|
+
if (query.customer_did && isValid(query.customer_did)) {
|
|
61
|
+
const customer = await Customer.findOne({ where: { did: query.customer_did } });
|
|
62
|
+
if (customer) {
|
|
63
|
+
where.customer_id = customer.id;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (query.subscription_id) {
|
|
67
|
+
where.subscription_id = query.subscription_id;
|
|
68
|
+
}
|
|
69
|
+
if (typeof query.livemode === 'boolean') {
|
|
70
|
+
where.livemode = query.livemode;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const { rows: list, count } = await Invoice.findAndCountAll({
|
|
75
|
+
where,
|
|
76
|
+
order: [['created_at', 'DESC']],
|
|
77
|
+
offset: (page - 1) * size,
|
|
78
|
+
limit: size,
|
|
79
|
+
include: [
|
|
80
|
+
{ model: PaymentCurrency, as: 'paymentCurrency' },
|
|
81
|
+
// { model: PaymentMethod, as: 'paymentMethod' },
|
|
82
|
+
// { model: PaymentIntent, as: 'paymentIntent' },
|
|
83
|
+
// { model: Subscription, as: 'subscription' },
|
|
84
|
+
{ model: Customer, as: 'customer' },
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
res.json({ count, list });
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error(err);
|
|
91
|
+
res.json({ count: 0, list: [] });
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// FIXME: exclude some sensitive fields from PaymentMethod
|
|
96
|
+
router.get('/:id', authPortal, async (req, res) => {
|
|
97
|
+
try {
|
|
98
|
+
const doc = await Invoice.findOne({
|
|
99
|
+
where: { id: req.params.id },
|
|
100
|
+
include: [
|
|
101
|
+
{ model: PaymentCurrency, as: 'paymentCurrency' },
|
|
102
|
+
{ model: PaymentMethod, as: 'paymentMethod' },
|
|
103
|
+
{ model: PaymentIntent, as: 'paymentIntent' },
|
|
104
|
+
{ model: Subscription, as: 'subscription' },
|
|
105
|
+
{ model: InvoiceItem, as: 'lines' },
|
|
106
|
+
{ model: Customer, as: 'customer' },
|
|
107
|
+
],
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (doc) {
|
|
111
|
+
const json = doc.toJSON();
|
|
112
|
+
const products = (await Product.findAll()).map((x) => x.toJSON());
|
|
113
|
+
const prices = (await Price.findAll()).map((x) => x.toJSON());
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
expandLineItems(json.lines, products, prices);
|
|
116
|
+
res.json(json);
|
|
117
|
+
} else {
|
|
118
|
+
res.json(null);
|
|
119
|
+
}
|
|
120
|
+
} catch (err) {
|
|
121
|
+
console.error(err);
|
|
122
|
+
res.json(null);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
export default router;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { InferAttributes, Op, WhereOptions } from 'sequelize';
|
|
3
|
+
|
|
4
|
+
import { authenticate } from '../libs/security';
|
|
5
|
+
import { PaymentCurrency } from '../store/models/payment-currency';
|
|
6
|
+
|
|
7
|
+
const router = Router();
|
|
8
|
+
|
|
9
|
+
const auth = authenticate<PaymentCurrency>({ component: true, roles: ['owner', 'admin'] });
|
|
10
|
+
|
|
11
|
+
router.get('/', auth, async (req, res) => {
|
|
12
|
+
const { query } = req;
|
|
13
|
+
const where: WhereOptions<InferAttributes<PaymentCurrency>> = {};
|
|
14
|
+
|
|
15
|
+
if (typeof query.active === 'string') {
|
|
16
|
+
where.active = JSON.parse(query.active);
|
|
17
|
+
}
|
|
18
|
+
if (typeof query.livemode === 'string') {
|
|
19
|
+
where.livemode = JSON.parse(query.livemode);
|
|
20
|
+
}
|
|
21
|
+
const list = await PaymentCurrency.findAll({
|
|
22
|
+
where,
|
|
23
|
+
order: [['created_at', 'DESC']],
|
|
24
|
+
include: [],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
res.json(list);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
router.get('/:id', auth, async (req, res) => {
|
|
31
|
+
const doc = await PaymentCurrency.findOne({
|
|
32
|
+
where: { [Op.or]: [{ id: req.params.id }, { symbol: req.params.id }] },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
res.json(doc);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
export default router;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isValid } from '@arcblock/did';
|
|
2
|
+
import { Router } from 'express';
|
|
3
|
+
import Joi from 'joi';
|
|
4
|
+
import type { WhereOptions } from 'sequelize';
|
|
5
|
+
|
|
6
|
+
import { authenticate } from '../libs/security';
|
|
7
|
+
import { CheckoutSession } from '../store/models/checkout-session';
|
|
8
|
+
import { Customer } from '../store/models/customer';
|
|
9
|
+
import { Invoice } from '../store/models/invoice';
|
|
10
|
+
import { PaymentCurrency } from '../store/models/payment-currency';
|
|
11
|
+
import { PaymentIntent } from '../store/models/payment-intent';
|
|
12
|
+
import { PaymentMethod } from '../store/models/payment-method';
|
|
13
|
+
import { Subscription } from '../store/models/subscription';
|
|
14
|
+
|
|
15
|
+
const router = Router();
|
|
16
|
+
const authMine = authenticate<Subscription>({ component: true, roles: ['owner', 'admin'], mine: true });
|
|
17
|
+
const authPortal = authenticate<PaymentIntent>({
|
|
18
|
+
component: true,
|
|
19
|
+
roles: ['owner', 'admin'],
|
|
20
|
+
record: {
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
model: PaymentIntent,
|
|
23
|
+
field: 'customer_id',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// list payment links
|
|
28
|
+
const paginationSchema = Joi.object<{
|
|
29
|
+
page: number;
|
|
30
|
+
size: number;
|
|
31
|
+
status?: string;
|
|
32
|
+
livemode?: boolean;
|
|
33
|
+
invoice_id?: string;
|
|
34
|
+
customer_id?: string;
|
|
35
|
+
customer_did?: string;
|
|
36
|
+
}>({
|
|
37
|
+
page: Joi.number().integer().min(1).default(1),
|
|
38
|
+
size: Joi.number().integer().min(1).max(100).default(20),
|
|
39
|
+
status: Joi.string().empty(''),
|
|
40
|
+
livemode: Joi.boolean().empty(''),
|
|
41
|
+
invoice_id: Joi.string().empty(''),
|
|
42
|
+
customer_id: Joi.string().empty(''),
|
|
43
|
+
customer_did: Joi.string().empty(''),
|
|
44
|
+
});
|
|
45
|
+
router.get('/', authMine, async (req, res) => {
|
|
46
|
+
const { page, size, ...query } = await paginationSchema.validateAsync(req.query, { stripUnknown: true });
|
|
47
|
+
const where: WhereOptions<PaymentIntent> = {};
|
|
48
|
+
|
|
49
|
+
if (query.status) {
|
|
50
|
+
where.status = query.status
|
|
51
|
+
.split(',')
|
|
52
|
+
.map((x) => x.trim())
|
|
53
|
+
.filter(Boolean);
|
|
54
|
+
}
|
|
55
|
+
if (query.customer_id) {
|
|
56
|
+
where.customer_id = query.customer_id;
|
|
57
|
+
}
|
|
58
|
+
if (query.customer_did && isValid(query.customer_did)) {
|
|
59
|
+
const customer = await Customer.findOne({ where: { did: query.customer_did } });
|
|
60
|
+
if (customer) {
|
|
61
|
+
where.customer_id = customer.id;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (query.invoice_id) {
|
|
65
|
+
where.invoice_id = query.invoice_id;
|
|
66
|
+
}
|
|
67
|
+
if (typeof query.livemode === 'boolean') {
|
|
68
|
+
where.livemode = query.livemode;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
const { rows: list, count } = await PaymentIntent.findAndCountAll({
|
|
73
|
+
where,
|
|
74
|
+
order: [['created_at', 'DESC']],
|
|
75
|
+
offset: (page - 1) * size,
|
|
76
|
+
limit: size,
|
|
77
|
+
include: [
|
|
78
|
+
{ model: PaymentCurrency, as: 'paymentCurrency' },
|
|
79
|
+
{ model: PaymentMethod, as: 'paymentMethod' },
|
|
80
|
+
{ model: Customer, as: 'customer' },
|
|
81
|
+
],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
res.json({ count, list });
|
|
85
|
+
} catch (err) {
|
|
86
|
+
console.error(err);
|
|
87
|
+
res.json({ count: 0, list: [] });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// FIXME: exclude some sensitive fields from PaymentMethod
|
|
92
|
+
router.get('/:id', authPortal, async (req, res) => {
|
|
93
|
+
try {
|
|
94
|
+
const doc = await PaymentIntent.findOne({
|
|
95
|
+
where: { id: req.params.id },
|
|
96
|
+
include: [
|
|
97
|
+
{ model: PaymentCurrency, as: 'paymentCurrency' },
|
|
98
|
+
{ model: PaymentMethod, as: 'paymentMethod' },
|
|
99
|
+
{ model: Customer, as: 'customer' },
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
let checkoutSession;
|
|
104
|
+
let invoice;
|
|
105
|
+
let subscription;
|
|
106
|
+
|
|
107
|
+
if (doc) {
|
|
108
|
+
checkoutSession = await CheckoutSession.findOne({ where: { payment_intent_id: doc.id } });
|
|
109
|
+
invoice = await Invoice.findByPk(doc.invoice_id);
|
|
110
|
+
if (invoice && invoice.subscription_id) {
|
|
111
|
+
subscription = await Subscription.findByPk(invoice.subscription_id);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
res.json({ ...doc?.toJSON(), checkoutSession, invoice, subscription });
|
|
116
|
+
} catch (err) {
|
|
117
|
+
console.error(err);
|
|
118
|
+
res.json(null);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
export default router;
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import Joi from 'joi';
|
|
3
|
+
// import isEmpty from 'lodash/isEmpty';
|
|
4
|
+
import pick from 'lodash/pick';
|
|
5
|
+
import type { WhereOptions } from 'sequelize';
|
|
6
|
+
|
|
7
|
+
import { authenticate } from '../libs/security';
|
|
8
|
+
import { formatMetadata } from '../libs/util';
|
|
9
|
+
import { PaymentLink } from '../store/models/payment-link';
|
|
10
|
+
import { Price } from '../store/models/price';
|
|
11
|
+
import { Product } from '../store/models/product';
|
|
12
|
+
|
|
13
|
+
const router = Router();
|
|
14
|
+
const auth = authenticate<PaymentLink>({ component: true, roles: ['owner', 'admin'] });
|
|
15
|
+
|
|
16
|
+
const formatBeforeSave = (payload: any) => {
|
|
17
|
+
const raw: Partial<PaymentLink> = Object.assign(
|
|
18
|
+
{
|
|
19
|
+
after_completion: {
|
|
20
|
+
type: 'hosted_confirmation',
|
|
21
|
+
hosted_confirmation: {
|
|
22
|
+
custom_message: '',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
allow_promotion_codes: false,
|
|
26
|
+
customer_creation: 'always',
|
|
27
|
+
consent_collection: {
|
|
28
|
+
promotions: 'none',
|
|
29
|
+
terms_of_service: 'none',
|
|
30
|
+
},
|
|
31
|
+
invoice_creation: {
|
|
32
|
+
// FIXME: force to true if we are subscription
|
|
33
|
+
enabled: false,
|
|
34
|
+
},
|
|
35
|
+
phone_number_collection: {
|
|
36
|
+
enabled: false,
|
|
37
|
+
},
|
|
38
|
+
billing_address_collection: 'auto',
|
|
39
|
+
subscription_data: {
|
|
40
|
+
description: '',
|
|
41
|
+
trial_period_days: 0,
|
|
42
|
+
},
|
|
43
|
+
submit_type: 'pay',
|
|
44
|
+
},
|
|
45
|
+
pick(payload, [
|
|
46
|
+
'name',
|
|
47
|
+
'line_items',
|
|
48
|
+
'currency_id',
|
|
49
|
+
'after_completion',
|
|
50
|
+
'allow_promotion_codes',
|
|
51
|
+
'consent_collection',
|
|
52
|
+
'custom_fields',
|
|
53
|
+
'customer_creation',
|
|
54
|
+
'invoice_creation',
|
|
55
|
+
'phone_number_collection',
|
|
56
|
+
'billing_address_collection',
|
|
57
|
+
'submit_type',
|
|
58
|
+
'subscription_data',
|
|
59
|
+
'metadata',
|
|
60
|
+
])
|
|
61
|
+
);
|
|
62
|
+
if (raw.after_completion?.type === 'hosted_confirmation') {
|
|
63
|
+
delete raw.after_completion?.redirect;
|
|
64
|
+
}
|
|
65
|
+
if (raw.after_completion?.type === 'redirect') {
|
|
66
|
+
delete raw.after_completion?.hosted_confirmation;
|
|
67
|
+
}
|
|
68
|
+
if (!payload.include_free_trial) {
|
|
69
|
+
delete raw.subscription_data;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
raw.line_items?.forEach((x) => {
|
|
73
|
+
if (x.adjustable_quantity?.enabled) {
|
|
74
|
+
x.adjustable_quantity.minimum = Number(x.adjustable_quantity?.minimum);
|
|
75
|
+
x.adjustable_quantity.maximum = Number(x.adjustable_quantity?.maximum);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
raw.metadata = formatMetadata(raw.metadata);
|
|
80
|
+
|
|
81
|
+
return raw;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// create payment link
|
|
85
|
+
// FIXME: @wangshijun use schema validation
|
|
86
|
+
router.post('/', auth, async (req, res) => {
|
|
87
|
+
const raw: Partial<PaymentLink> = formatBeforeSave(req.body);
|
|
88
|
+
raw.active = true;
|
|
89
|
+
raw.livemode = !!req.livemode;
|
|
90
|
+
raw.created_via = req.user?.via;
|
|
91
|
+
raw.currency_id = raw.currency_id || req.currency.id;
|
|
92
|
+
|
|
93
|
+
const link = await PaymentLink.create(raw as PaymentLink);
|
|
94
|
+
|
|
95
|
+
// lock prices
|
|
96
|
+
await Promise.all(link.line_items.map((x) => Price.update({ locked: true }, { where: { id: x.price_id } })));
|
|
97
|
+
|
|
98
|
+
res.json(link);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// list payment links
|
|
102
|
+
const paginationSchema = Joi.object<{
|
|
103
|
+
page: number;
|
|
104
|
+
size: number;
|
|
105
|
+
active?: boolean;
|
|
106
|
+
livemode?: boolean;
|
|
107
|
+
}>({
|
|
108
|
+
page: Joi.number().integer().min(1).default(1),
|
|
109
|
+
size: Joi.number().integer().min(1).max(100).default(20),
|
|
110
|
+
active: Joi.boolean().empty(''),
|
|
111
|
+
livemode: Joi.boolean().empty(''),
|
|
112
|
+
});
|
|
113
|
+
router.get('/', auth, async (req, res) => {
|
|
114
|
+
const { page, size, ...query } = await paginationSchema.validateAsync(req.query, { stripUnknown: true });
|
|
115
|
+
const where: WhereOptions<PaymentLink> = {};
|
|
116
|
+
|
|
117
|
+
if (typeof query.active === 'boolean') {
|
|
118
|
+
where.active = query.active;
|
|
119
|
+
}
|
|
120
|
+
if (typeof query.livemode === 'boolean') {
|
|
121
|
+
where.livemode = query.livemode;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
const { rows: list, count } = await PaymentLink.findAndCountAll({
|
|
126
|
+
where,
|
|
127
|
+
order: [['created_at', 'DESC']],
|
|
128
|
+
offset: (page - 1) * size,
|
|
129
|
+
limit: size,
|
|
130
|
+
include: [],
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const priceIds: string[] = list.reduce((acc: string[], x) => acc.concat(x.line_items.map((i) => i.price_id)), []);
|
|
134
|
+
const prices = await Price.findAll({ where: { id: priceIds }, include: [{ model: Product, as: 'product' }] });
|
|
135
|
+
|
|
136
|
+
list.forEach((x) => {
|
|
137
|
+
x.line_items.forEach((i) => {
|
|
138
|
+
// @ts-ignore
|
|
139
|
+
i.price = prices.find((p) => p.id === i.price_id);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
res.json({ count, list });
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.error(err);
|
|
146
|
+
res.json({ count: 0, list: [] });
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
router.get('/:id', auth, async (req, res) => {
|
|
151
|
+
const doc = await PaymentLink.findByPk(req.params.id);
|
|
152
|
+
|
|
153
|
+
if (doc) {
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
doc.line_items = await Price.expand(doc.line_items);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
res.json(doc);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// update
|
|
162
|
+
// eslint-disable-next-line consistent-return
|
|
163
|
+
router.put('/:id', auth, async (req, res) => {
|
|
164
|
+
const doc = await PaymentLink.findByPk(req.params.id);
|
|
165
|
+
|
|
166
|
+
if (!doc) {
|
|
167
|
+
return res.status(404).json({ error: 'payment link not found' });
|
|
168
|
+
}
|
|
169
|
+
if (doc.active === false) {
|
|
170
|
+
return res.status(403).json({ error: 'payment link archived' });
|
|
171
|
+
}
|
|
172
|
+
// if (doc.locked) {
|
|
173
|
+
// return res.status(403).json({ error: 'payment link locked' });
|
|
174
|
+
// }
|
|
175
|
+
|
|
176
|
+
await doc.update(formatBeforeSave(req.body));
|
|
177
|
+
|
|
178
|
+
res.json(doc);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// archive
|
|
182
|
+
router.put('/:id/archive', auth, async (req, res) => {
|
|
183
|
+
const doc = await PaymentLink.findByPk(req.params.id);
|
|
184
|
+
|
|
185
|
+
if (!doc) {
|
|
186
|
+
return res.status(404).json({ error: 'payment link not found' });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (doc.active === false) {
|
|
190
|
+
return res.status(403).json({ error: 'payment link already archived' });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// if (doc.locked) {
|
|
194
|
+
// return res.status(403).json({ error: 'payment link locked' });
|
|
195
|
+
// }
|
|
196
|
+
|
|
197
|
+
await doc.update({ active: false });
|
|
198
|
+
return res.json(doc);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// delete
|
|
202
|
+
router.delete('/:id', auth, async (req, res) => {
|
|
203
|
+
const doc = await PaymentLink.findByPk(req.params.id);
|
|
204
|
+
|
|
205
|
+
if (!doc) {
|
|
206
|
+
return res.status(404).json({ error: 'payment link not found' });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (doc.active === false) {
|
|
210
|
+
return res.status(403).json({ error: 'payment link archived' });
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// if (doc.locked) {
|
|
214
|
+
// return res.status(403).json({ error: 'payment link locked' });
|
|
215
|
+
// }
|
|
216
|
+
|
|
217
|
+
await doc.destroy();
|
|
218
|
+
return res.json(doc);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
export default router;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { InferAttributes, Op, WhereOptions } from 'sequelize';
|
|
3
|
+
|
|
4
|
+
import { authenticate } from '../libs/security';
|
|
5
|
+
import { PaymentCurrency } from '../store/models/payment-currency';
|
|
6
|
+
import { PaymentMethod } from '../store/models/payment-method';
|
|
7
|
+
|
|
8
|
+
const router = Router();
|
|
9
|
+
|
|
10
|
+
const auth = authenticate<PaymentMethod>({ component: true, roles: ['owner', 'admin'] });
|
|
11
|
+
|
|
12
|
+
router.get('/', auth, async (req, res) => {
|
|
13
|
+
const { query } = req;
|
|
14
|
+
const where: WhereOptions<InferAttributes<PaymentMethod>> = {};
|
|
15
|
+
|
|
16
|
+
if (typeof query.active === 'string') {
|
|
17
|
+
where.active = JSON.parse(query.active);
|
|
18
|
+
}
|
|
19
|
+
if (typeof query.livemode === 'string') {
|
|
20
|
+
where.livemode = JSON.parse(query.livemode);
|
|
21
|
+
}
|
|
22
|
+
const list = await PaymentMethod.findAll({
|
|
23
|
+
where,
|
|
24
|
+
order: [['created_at', 'DESC']],
|
|
25
|
+
include: [{ model: PaymentCurrency, as: 'payment_currencies' }],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
res.json(list);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
router.get('/:id', auth, async (req, res) => {
|
|
32
|
+
const doc = await PaymentMethod.findOne({
|
|
33
|
+
where: { [Op.or]: [{ id: req.params.id }, { name: req.params.id }] },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
res.json(doc);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export default router;
|