payment-kit 1.16.17 → 1.16.18
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/api/src/crons/index.ts +1 -1
- package/api/src/hooks/pre-start.ts +2 -0
- package/api/src/index.ts +2 -0
- package/api/src/integrations/arcblock/stake.ts +7 -1
- package/api/src/integrations/stripe/resource.ts +1 -1
- package/api/src/libs/env.ts +12 -0
- package/api/src/libs/event.ts +8 -0
- package/api/src/libs/invoice.ts +585 -3
- package/api/src/libs/notification/template/subscription-succeeded.ts +1 -2
- package/api/src/libs/notification/template/subscription-trial-will-end.ts +2 -2
- package/api/src/libs/notification/template/subscription-will-renew.ts +6 -2
- package/api/src/libs/notification/template/subscription.overdraft-protection.exhausted.ts +139 -0
- package/api/src/libs/overdraft-protection.ts +85 -0
- package/api/src/libs/payment.ts +1 -65
- package/api/src/libs/queue/index.ts +0 -1
- package/api/src/libs/subscription.ts +532 -2
- package/api/src/libs/util.ts +4 -0
- package/api/src/locales/en.ts +5 -0
- package/api/src/locales/zh.ts +5 -0
- package/api/src/queues/event.ts +3 -2
- package/api/src/queues/invoice.ts +28 -3
- package/api/src/queues/notification.ts +25 -3
- package/api/src/queues/payment.ts +154 -3
- package/api/src/queues/refund.ts +2 -2
- package/api/src/queues/subscription.ts +215 -4
- package/api/src/queues/webhook.ts +1 -0
- package/api/src/routes/connect/change-payment.ts +1 -1
- package/api/src/routes/connect/change-plan.ts +1 -1
- package/api/src/routes/connect/overdraft-protection.ts +120 -0
- package/api/src/routes/connect/recharge.ts +2 -1
- package/api/src/routes/connect/setup.ts +1 -1
- package/api/src/routes/connect/shared.ts +117 -350
- package/api/src/routes/connect/subscribe.ts +1 -1
- package/api/src/routes/customers.ts +2 -2
- package/api/src/routes/invoices.ts +9 -4
- package/api/src/routes/subscriptions.ts +172 -2
- package/api/src/store/migrate.ts +9 -10
- package/api/src/store/migrations/20240905-index.ts +95 -60
- package/api/src/store/migrations/20241203-overdraft-protection.ts +25 -0
- package/api/src/store/migrations/20241216-update-overdraft-protection.ts +30 -0
- package/api/src/store/models/customer.ts +2 -2
- package/api/src/store/models/invoice.ts +7 -0
- package/api/src/store/models/lock.ts +7 -0
- package/api/src/store/models/subscription.ts +15 -0
- package/api/src/store/sequelize.ts +6 -1
- package/blocklet.yml +1 -1
- package/package.json +23 -23
- package/src/components/customer/overdraft-protection.tsx +367 -0
- package/src/components/event/list.tsx +3 -4
- package/src/components/subscription/actions/cancel.tsx +3 -0
- package/src/components/subscription/portal/actions.tsx +324 -77
- package/src/components/uploader.tsx +31 -26
- package/src/env.d.ts +1 -0
- package/src/hooks/subscription.ts +30 -0
- package/src/libs/env.ts +4 -0
- package/src/locales/en.tsx +41 -0
- package/src/locales/zh.tsx +37 -0
- package/src/pages/admin/billing/invoices/detail.tsx +16 -15
- package/src/pages/customer/index.tsx +7 -2
- package/src/pages/customer/invoice/detail.tsx +29 -5
- package/src/pages/customer/invoice/past-due.tsx +18 -4
- package/src/pages/customer/recharge.tsx +2 -4
- package/src/pages/customer/subscription/change-payment.tsx +7 -1
- package/src/pages/customer/subscription/detail.tsx +69 -51
- package/tsconfig.json +0 -5
- package/api/tests/libs/payment.spec.ts +0 -168
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { getPaymentAmountForCycleSubscription } from '../../src/libs/payment';
|
|
2
|
-
import { SubscriptionItem, Price, UsageRecord } from '../../src/store/models'; // 假设这些模型在 models 文件中
|
|
3
|
-
import { getSubscriptionCycleSetup, getSubscriptionCycleAmount } from '../../src/libs/subscription'; // 假设这些工具函数在 utils 文件中
|
|
4
|
-
|
|
5
|
-
jest.mock('../../src/store/models');
|
|
6
|
-
jest.mock('../../src/libs/subscription');
|
|
7
|
-
|
|
8
|
-
describe('getPaymentAmountForCycleSubscription', () => {
|
|
9
|
-
const subscription = {
|
|
10
|
-
id: 'sub_123456789012345678901234',
|
|
11
|
-
livemode: true,
|
|
12
|
-
currency_id: 'tba',
|
|
13
|
-
customer_id: 'cus_123456789012345678',
|
|
14
|
-
current_period_end: 1627689600,
|
|
15
|
-
current_period_start: 1625097600,
|
|
16
|
-
default_payment_method_id: 'pm_123456789012345678901234',
|
|
17
|
-
description: 'Test subscription',
|
|
18
|
-
latest_invoice_id: 'in_123456789012345678901234',
|
|
19
|
-
metadata: { key: 'value' },
|
|
20
|
-
pending_setup_intent: 'seti_123456789012345678901234',
|
|
21
|
-
pending_update: {
|
|
22
|
-
billing_cycle_anchor: 1627689600,
|
|
23
|
-
expires_at: 1627689600,
|
|
24
|
-
subscription_items: [],
|
|
25
|
-
trial_end: 1627689600,
|
|
26
|
-
},
|
|
27
|
-
status: 'active',
|
|
28
|
-
cancel_at_period_end: false,
|
|
29
|
-
cancel_at: 1627689600,
|
|
30
|
-
canceled_at: 1627689600,
|
|
31
|
-
cancelation_details: {
|
|
32
|
-
comment: 'Customer requested cancellation',
|
|
33
|
-
feedback: 'too_expensive',
|
|
34
|
-
reason: 'cancellation_requested',
|
|
35
|
-
return_stake: true,
|
|
36
|
-
slash_stake: false,
|
|
37
|
-
},
|
|
38
|
-
billing_cycle_anchor: 1625097600,
|
|
39
|
-
billing_thresholds: {
|
|
40
|
-
amount_gte: 1000,
|
|
41
|
-
stake_gte: 100,
|
|
42
|
-
reset_billing_cycle_anchor: true,
|
|
43
|
-
},
|
|
44
|
-
collection_method: 'charge_automatically',
|
|
45
|
-
days_until_due: 30,
|
|
46
|
-
days_until_cancel: 7,
|
|
47
|
-
discount_id: 'di_123456789012345678901234',
|
|
48
|
-
next_pending_invoice_item_invoice: 1627689600,
|
|
49
|
-
pause_collection: {
|
|
50
|
-
behavior: 'keep_as_draft',
|
|
51
|
-
resumes_at: 1627689600,
|
|
52
|
-
},
|
|
53
|
-
payment_settings: {
|
|
54
|
-
payment_method_options: {
|
|
55
|
-
card: {
|
|
56
|
-
request_three_d_secure: 'any',
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
payment_method_types: ['card'],
|
|
60
|
-
},
|
|
61
|
-
pending_invoice_item_interval: {
|
|
62
|
-
interval: 'month',
|
|
63
|
-
interval_count: 1,
|
|
64
|
-
},
|
|
65
|
-
schedule_id: 'sch_123456789012345678901234',
|
|
66
|
-
ended_at: 1627689600,
|
|
67
|
-
start_date: 1625097600,
|
|
68
|
-
trial_end: 1627689600,
|
|
69
|
-
trial_start: 1625097600,
|
|
70
|
-
trial_settings: {
|
|
71
|
-
end_behavior: {
|
|
72
|
-
missing_payment_method: 'cancel',
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
payment_details: {
|
|
76
|
-
tx_hash: '0x1234567890abcdef',
|
|
77
|
-
},
|
|
78
|
-
proration_behavior: 'always_invoice',
|
|
79
|
-
payment_behavior: 'allow_incomplete',
|
|
80
|
-
service_actions: [
|
|
81
|
-
{
|
|
82
|
-
action: 'activate',
|
|
83
|
-
service: 'service_1',
|
|
84
|
-
},
|
|
85
|
-
],
|
|
86
|
-
_from: 'sub_098765432109876543210987',
|
|
87
|
-
created_at: new Date(),
|
|
88
|
-
updated_at: new Date(),
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const paymentCurrency = {
|
|
92
|
-
id: 'usd',
|
|
93
|
-
decimal: 18,
|
|
94
|
-
symbol: 'TBA',
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
const subscriptionItems = [
|
|
98
|
-
{ id: 'item_1', price_id: 'price_1', quantity: 1 },
|
|
99
|
-
{ id: 'item_2', price_id: 'price_2', quantity: 2 },
|
|
100
|
-
];
|
|
101
|
-
|
|
102
|
-
const expandedItems = [
|
|
103
|
-
{
|
|
104
|
-
id: 'item_1',
|
|
105
|
-
// @ts-ignore
|
|
106
|
-
price: new Price({
|
|
107
|
-
id: 'price_1',
|
|
108
|
-
active: true,
|
|
109
|
-
product_id: 'prod_1',
|
|
110
|
-
livemode: false,
|
|
111
|
-
type: 'recurring',
|
|
112
|
-
unit_amount: '100000000000000000',
|
|
113
|
-
currency_id: 'tba',
|
|
114
|
-
recurring: {
|
|
115
|
-
usage_type: 'licensed',
|
|
116
|
-
interval: 'month',
|
|
117
|
-
interval_count: 1,
|
|
118
|
-
},
|
|
119
|
-
}),
|
|
120
|
-
quantity: 1,
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
id: 'item_2',
|
|
124
|
-
// @ts-ignore
|
|
125
|
-
price: new Price({
|
|
126
|
-
id: 'price_2',
|
|
127
|
-
active: true,
|
|
128
|
-
product_id: 'prod_2',
|
|
129
|
-
livemode: false,
|
|
130
|
-
type: 'recurring',
|
|
131
|
-
currency_id: 'tba',
|
|
132
|
-
unit_amount: '100000000000000000',
|
|
133
|
-
recurring: {
|
|
134
|
-
usage_type: 'metered',
|
|
135
|
-
interval: 'month',
|
|
136
|
-
interval_count: 1,
|
|
137
|
-
aggregate_usage: 'sum',
|
|
138
|
-
},
|
|
139
|
-
}),
|
|
140
|
-
quantity: 2,
|
|
141
|
-
},
|
|
142
|
-
];
|
|
143
|
-
|
|
144
|
-
beforeEach(() => {
|
|
145
|
-
(SubscriptionItem.findAll as jest.Mock).mockResolvedValue(subscriptionItems);
|
|
146
|
-
(Price.expand as jest.Mock).mockResolvedValue(expandedItems);
|
|
147
|
-
(UsageRecord.getSummary as jest.Mock).mockResolvedValue(10);
|
|
148
|
-
(getSubscriptionCycleSetup as jest.Mock).mockReturnValue({
|
|
149
|
-
period: { start: 1625097600, end: 1627689600 },
|
|
150
|
-
cycle: 2592000,
|
|
151
|
-
});
|
|
152
|
-
(getSubscriptionCycleAmount as jest.Mock).mockReturnValue({ total: '100000000000000000' });
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('should return the total amount for the subscription cycle', async () => {
|
|
156
|
-
// @ts-ignore
|
|
157
|
-
const result = await getPaymentAmountForCycleSubscription(subscription, paymentCurrency);
|
|
158
|
-
expect(result).toBe(0.1);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
it('should return 0 if there are no subscription items', async () => {
|
|
162
|
-
(SubscriptionItem.findAll as jest.Mock).mockResolvedValue([]);
|
|
163
|
-
(Price.expand as jest.Mock).mockResolvedValue([]); // 确保 Price.expand 返回空数组
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
const result = await getPaymentAmountForCycleSubscription(subscription, paymentCurrency);
|
|
166
|
-
expect(result).toBe(0);
|
|
167
|
-
});
|
|
168
|
-
});
|