payment-kit 1.16.16 → 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.
Files changed (66) hide show
  1. package/api/src/crons/index.ts +1 -1
  2. package/api/src/hooks/pre-start.ts +2 -0
  3. package/api/src/index.ts +2 -0
  4. package/api/src/integrations/arcblock/stake.ts +7 -1
  5. package/api/src/integrations/stripe/resource.ts +1 -1
  6. package/api/src/libs/env.ts +12 -0
  7. package/api/src/libs/event.ts +8 -0
  8. package/api/src/libs/invoice.ts +585 -3
  9. package/api/src/libs/notification/template/subscription-succeeded.ts +1 -2
  10. package/api/src/libs/notification/template/subscription-trial-will-end.ts +2 -2
  11. package/api/src/libs/notification/template/subscription-will-renew.ts +6 -2
  12. package/api/src/libs/notification/template/subscription.overdraft-protection.exhausted.ts +139 -0
  13. package/api/src/libs/overdraft-protection.ts +85 -0
  14. package/api/src/libs/payment.ts +1 -65
  15. package/api/src/libs/queue/index.ts +0 -1
  16. package/api/src/libs/subscription.ts +532 -2
  17. package/api/src/libs/util.ts +4 -0
  18. package/api/src/locales/en.ts +5 -0
  19. package/api/src/locales/zh.ts +5 -0
  20. package/api/src/queues/event.ts +3 -2
  21. package/api/src/queues/invoice.ts +28 -3
  22. package/api/src/queues/notification.ts +25 -3
  23. package/api/src/queues/payment.ts +154 -3
  24. package/api/src/queues/refund.ts +2 -2
  25. package/api/src/queues/subscription.ts +215 -4
  26. package/api/src/queues/webhook.ts +1 -0
  27. package/api/src/routes/connect/change-payment.ts +1 -1
  28. package/api/src/routes/connect/change-plan.ts +1 -1
  29. package/api/src/routes/connect/overdraft-protection.ts +120 -0
  30. package/api/src/routes/connect/recharge.ts +2 -1
  31. package/api/src/routes/connect/setup.ts +1 -1
  32. package/api/src/routes/connect/shared.ts +117 -350
  33. package/api/src/routes/connect/subscribe.ts +1 -1
  34. package/api/src/routes/customers.ts +2 -2
  35. package/api/src/routes/invoices.ts +9 -4
  36. package/api/src/routes/subscriptions.ts +172 -2
  37. package/api/src/store/migrate.ts +9 -10
  38. package/api/src/store/migrations/20240905-index.ts +95 -60
  39. package/api/src/store/migrations/20241203-overdraft-protection.ts +25 -0
  40. package/api/src/store/migrations/20241216-update-overdraft-protection.ts +30 -0
  41. package/api/src/store/models/customer.ts +2 -2
  42. package/api/src/store/models/invoice.ts +7 -0
  43. package/api/src/store/models/lock.ts +7 -0
  44. package/api/src/store/models/subscription.ts +15 -0
  45. package/api/src/store/sequelize.ts +6 -1
  46. package/blocklet.yml +1 -1
  47. package/package.json +23 -23
  48. package/src/components/customer/overdraft-protection.tsx +367 -0
  49. package/src/components/event/list.tsx +3 -4
  50. package/src/components/subscription/actions/cancel.tsx +3 -0
  51. package/src/components/subscription/portal/actions.tsx +324 -77
  52. package/src/components/uploader.tsx +31 -26
  53. package/src/env.d.ts +1 -0
  54. package/src/hooks/subscription.ts +30 -0
  55. package/src/libs/env.ts +4 -0
  56. package/src/locales/en.tsx +41 -0
  57. package/src/locales/zh.tsx +37 -0
  58. package/src/pages/admin/billing/invoices/detail.tsx +16 -15
  59. package/src/pages/customer/index.tsx +7 -2
  60. package/src/pages/customer/invoice/detail.tsx +29 -5
  61. package/src/pages/customer/invoice/past-due.tsx +18 -4
  62. package/src/pages/customer/recharge.tsx +2 -4
  63. package/src/pages/customer/subscription/change-payment.tsx +7 -1
  64. package/src/pages/customer/subscription/detail.tsx +69 -51
  65. package/tsconfig.json +0 -5
  66. package/api/tests/libs/payment.spec.ts +0 -168
@@ -97,7 +97,7 @@ function init() {
97
97
  name: 'metering.subscription.detection',
98
98
  time: meteringSubscriptionDetectionCronTime,
99
99
  fn: () => createMeteringSubscriptionDetection(),
100
- options: { runOnInit: true },
100
+ options: { runOnInit: false },
101
101
  },
102
102
  ],
103
103
  onError: (error: Error, name: string) => {
@@ -7,6 +7,7 @@ import { initPaywallResources } from '../libs/resource';
7
7
  import { initialize } from '../store/models';
8
8
  import { sequelize } from '../store/sequelize';
9
9
  import { syncCurrencyLogo } from '../crons/currency';
10
+ import { ensureCreateOverdraftProtectionPrices } from '../libs/overdraft-protection';
10
11
 
11
12
  dotenv.config({ silent: true });
12
13
 
@@ -16,6 +17,7 @@ dotenv.config({ silent: true });
16
17
  await initPaywallResources();
17
18
  await ensurePaymentStats();
18
19
  await syncCurrencyLogo();
20
+ await ensureCreateOverdraftProtectionPrices();
19
21
  process.exit(0);
20
22
  } catch (err) {
21
23
  console.error('pre-start error', err);
package/api/src/index.ts CHANGED
@@ -38,6 +38,7 @@ import payHandlers from './routes/connect/pay';
38
38
  import setupHandlers from './routes/connect/setup';
39
39
  import subscribeHandlers from './routes/connect/subscribe';
40
40
  import delegationHandlers from './routes/connect/delegation';
41
+ import overdraftProtectionHandlers from './routes/connect/overdraft-protection';
41
42
  import { initialize } from './store/models';
42
43
  import { sequelize } from './store/sequelize';
43
44
  import { initUserHandler } from './integrations/blocklet/user';
@@ -73,6 +74,7 @@ handlers.attach(Object.assign({ app: router }, changePaymentHandlers));
73
74
  handlers.attach(Object.assign({ app: router }, changePlanHandlers));
74
75
  handlers.attach(Object.assign({ app: router }, rechargeHandlers));
75
76
  handlers.attach(Object.assign({ app: router }, delegationHandlers));
77
+ handlers.attach(Object.assign({ app: router }, overdraftProtectionHandlers));
76
78
  router.use('/api', routes);
77
79
 
78
80
  const isProduction = process.env.BLOCKLET_MODE === 'production';
@@ -7,6 +7,7 @@ import { toStakeAddress } from '@arcblock/did-util';
7
7
  import env from '@blocklet/sdk/lib/env';
8
8
  import { BN, fromUnitToToken, toBN } from '@ocap/util';
9
9
 
10
+ import { Op } from 'sequelize';
10
11
  import { wallet } from '../../libs/auth';
11
12
  import { events } from '../../libs/event';
12
13
  import logger from '../../libs/logger';
@@ -147,7 +148,12 @@ export async function checkStakeRevokeTx() {
147
148
 
148
149
  // Check related subscriptions in the stake
149
150
  const subscriptions = await Subscription.findAll({
150
- where: { 'payment_details.arcblock.staking.address': address },
151
+ where: {
152
+ [Op.or]: [
153
+ { 'payment_details.arcblock.staking.address': address },
154
+ { 'overdraft_protection.payment_details.arcblock.staking.address': address },
155
+ ],
156
+ },
151
157
  });
152
158
  if (subscriptions.length === 0) {
153
159
  return;
@@ -400,7 +400,7 @@ export async function batchHandleStripeSubscriptions() {
400
400
 
401
401
  const subscriptions = await Subscription.findAll({
402
402
  where: {
403
- status: { [Op.not]: ['canceled', 'incomplete', 'incomplete_expired'] },
403
+ status: { [Op.notIn]: ['canceled', 'incomplete', 'incomplete_expired'] },
404
404
  'payment_details.stripe.subscription_id': { [Op.not]: null },
405
405
  },
406
406
  });
@@ -1,4 +1,5 @@
1
1
  import env from '@blocklet/sdk/lib/env';
2
+ import { env as configEnv } from '@blocklet/sdk/lib/config';
2
3
 
3
4
  export const paymentStatCronTime: string = '0 1 0 * * *'; // 默认每天一次,计算前一天的
4
5
  export const subscriptionCronTime: string = process.env.SUBSCRIPTION_CRON_TIME || '0 */30 * * * *'; // 默认每 30 min 行一次
@@ -12,7 +13,18 @@ export const revokeStakeCronTime: string = process.env.REVOKE_STAKE_CRON_TIME ||
12
13
  export const daysUntilCancel: string | undefined = process.env.DAYS_UNTIL_CANCEL;
13
14
  export const meteringSubscriptionDetectionCronTime: string =
14
15
  process.env.METERING_SUBSCRIPTION_DETECTION_CRON_TIME || '0 0 10 * * *'; // 默认每天 10:00 执行
16
+ export const overdraftProtectionMaxCount: number = Number(configEnv?.preferences?.overdraftProtectionMaxCount || 10); // 透支保护次数上限
15
17
 
18
+ // sequelize 配置相关
19
+ export const sequelizeOptionsPoolMin: number = process.env.SEQUELIZE_OPTIONS_POOL_MIN
20
+ ? +process.env.SEQUELIZE_OPTIONS_POOL_MIN
21
+ : 0;
22
+ export const sequelizeOptionsPoolMax: number = process.env.SEQUELIZE_OPTIONS_POOL_MAX
23
+ ? +process.env.SEQUELIZE_OPTIONS_POOL_MAX
24
+ : 5;
25
+ export const sequelizeOptionsPoolIdle: number = process.env.SEQUELIZE_OPTIONS_POOL_IDLE
26
+ ? +process.env.SEQUELIZE_OPTIONS_POOL_IDLE
27
+ : 10 * 1000;
16
28
  export default {
17
29
  ...env,
18
30
  };
@@ -10,3 +10,11 @@ interface MyEventType extends EventEmitter {
10
10
  }
11
11
 
12
12
  export const events = new EventEmitter() as MyEventType;
13
+
14
+ export const emitAsync = (event: string, ...args: any[]) => {
15
+ return new Promise((resolve, reject) => {
16
+ events.emit(event, ...args);
17
+ events.once(`${event}.done`, resolve);
18
+ events.once(`${event}.error`, reject);
19
+ });
20
+ };