payment-kit 1.17.2 → 1.17.4

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.
@@ -57,7 +57,7 @@ export async function mintNftForCheckoutSession(id: string) {
57
57
  client.getAccountState({ address: wallet.address }),
58
58
  ]);
59
59
 
60
- const preMint = preMintFromFactory({
60
+ const preMint = await preMintFromFactory({
61
61
  factory: formatFactoryState(factoryState as any),
62
62
  inputs: inputs || {},
63
63
  owner: nftOwner,
@@ -1,5 +1,4 @@
1
1
  import env from '@blocklet/sdk/lib/env';
2
- import { env as configEnv } from '@blocklet/sdk/lib/config';
3
2
 
4
3
  export const paymentStatCronTime: string = '0 1 0 * * *'; // 默认每天一次,计算前一天的
5
4
  export const subscriptionCronTime: string = process.env.SUBSCRIPTION_CRON_TIME || '0 */30 * * * *'; // 默认每 30 min 行一次
@@ -13,7 +12,6 @@ export const revokeStakeCronTime: string = process.env.REVOKE_STAKE_CRON_TIME ||
13
12
  export const daysUntilCancel: string | undefined = process.env.DAYS_UNTIL_CANCEL;
14
13
  export const meteringSubscriptionDetectionCronTime: string =
15
14
  process.env.METERING_SUBSCRIPTION_DETECTION_CRON_TIME || '0 0 10 * * *'; // 默认每天 10:00 执行
16
- export const overdraftProtectionMaxCount: number = Number(configEnv?.preferences?.overdraftProtectionMaxCount || 10); // 透支保护次数上限
17
15
 
18
16
  // sequelize 配置相关
19
17
  export const sequelizeOptionsPoolMin: number = process.env.SEQUELIZE_OPTIONS_POOL_MIN
@@ -12,7 +12,7 @@ export function ensureI18n() {
12
12
  };
13
13
  }
14
14
 
15
- export function contextMiddleware(req: Request, _res: Response, next: NextFunction) {
15
+ export async function contextMiddleware(req: Request, _res: Response, next: NextFunction) {
16
16
  const requestId =
17
17
  (req.headers['x-request-id'] as string) || `req_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
18
18
  let requestedBy = 'system';
@@ -22,7 +22,7 @@ export function contextMiddleware(req: Request, _res: Response, next: NextFuncti
22
22
  const componentDid = req.get('x-component-did');
23
23
  if (sig && componentDid) {
24
24
  const data = typeof req.body === 'undefined' ? {} : req.body;
25
- const verified = verify(data, sig);
25
+ const verified = await verify(data, sig);
26
26
  if (verified) {
27
27
  requestedBy = componentDid;
28
28
  }
@@ -10,6 +10,9 @@ export async function ensureOverdraftProtectionPrice(livemode = true) {
10
10
  const exist = await Price.findOne({ where: { lookup_key: lookUpKey, livemode } });
11
11
  if (exist) {
12
12
  const product = await Product.findByPk(exist.product_id);
13
+ if (product?.name === 'Overdraft Protection') {
14
+ product.update({ name: 'SubGuard Service' });
15
+ }
13
16
  return {
14
17
  product,
15
18
  price: exist,
@@ -27,7 +30,7 @@ export async function ensureOverdraftProtectionPrice(livemode = true) {
27
30
 
28
31
  const result = await createProductAndPrices({
29
32
  type: 'service',
30
- name: 'Overdraft Protection',
33
+ name: 'SubGuard Service',
31
34
  description:
32
35
  'If you purchase this service, the overdue subscription will be protected and not be ended due to overdue',
33
36
  images: [],
@@ -34,7 +34,7 @@ export function authenticate<T extends Model>({ component, roles, record, mine,
34
34
  const sig = req.get('x-component-sig');
35
35
  if (component && sig) {
36
36
  const { data } = getVerifyData(req as any, 'component');
37
- const verified = verify(data, sig);
37
+ const verified = await verify(data, sig);
38
38
  if (!verified) {
39
39
  return res.status(401).json({ error: 'Invalid signature for component call' });
40
40
  }
@@ -56,7 +56,7 @@ export function authenticate<T extends Model>({ component, roles, record, mine,
56
56
  const id = req.params.id || req.query.subscription_id || '';
57
57
  if (embed && token && id) {
58
58
  try {
59
- const verified = wallet.verify(id as string, token as string);
59
+ const verified = await wallet.verify(id as string, token as string);
60
60
  if (!verified) {
61
61
  return res.status(401).json({ error: `Invalid signature for embed: ${id}` });
62
62
  }
@@ -1296,7 +1296,7 @@ export async function returnOverdraftProtectionStake(
1296
1296
  const item = await Refund.create({
1297
1297
  type: 'stake_return',
1298
1298
  amount: unused,
1299
- description: 'overdraft protection_return_for_subscription',
1299
+ description: 'stake_return_for_subscription_guard',
1300
1300
  status: 'pending',
1301
1301
  reason: refundReason || 'requested_by_admin',
1302
1302
  subscription_id: subscription.id,
@@ -194,8 +194,8 @@ export default flat({
194
194
  },
195
195
 
196
196
  overdraftProtectionExhausted: {
197
- title: 'Insufficient Credit for Overdraft Protection',
198
- body: 'Your subscription to {productName} has insufficient staked credit for overdraft protection. Please increase your stake to maintain the service or disable it if no longer needed.',
197
+ title: 'Insufficient Credit for SubGuard™',
198
+ body: 'Your subscription to {productName} has insufficient staked credit for SubGuard™. Please increase your stake to maintain the service or disable it if no longer needed.',
199
199
  },
200
200
  },
201
201
  });
@@ -187,8 +187,8 @@ export default flat({
187
187
  },
188
188
 
189
189
  overdraftProtectionExhausted: {
190
- title: '透支保护额度不足',
191
- body: '您订阅的 {productName} 透支保护额度不足,为了避免影响您的透支保护服务,请及时充值。如不再需要透支保护服务,可关闭该功能。',
190
+ title: '订阅守护服务额度不足',
191
+ body: '您订阅的 {productName} 订阅守护服务额度不足,为了避免影响您的使用,请及时充值。如不再需要订阅守护服务,可关闭该功能。',
192
192
  },
193
193
  },
194
194
  });
@@ -68,7 +68,7 @@ import {
68
68
  import {
69
69
  OverdraftProtectionExhaustedEmailTemplate,
70
70
  OverdraftProtectionExhaustedEmailTemplateOptions,
71
- } from '../libs/notification/template/subscription.overdraft-protection.exhausted';
71
+ } from '../libs/notification/template/subscription-overdraft-protection-exhausted';
72
72
 
73
73
  export type NotificationQueueJobOptions = any;
74
74
 
@@ -1211,7 +1211,7 @@ events.on('customer.stake.revoked', async ({ subscriptionId, tx }: { subscriptio
1211
1211
  feedback: 'other',
1212
1212
  comment: `Revoked by ${tx.tx.from} with tx ${tx.hash}`,
1213
1213
  },
1214
- // 关闭透支保护
1214
+ // 关闭订阅守护
1215
1215
  // @ts-ignore
1216
1216
  overdraft_protection: {
1217
1217
  ...(subscription.overdraft_protection || {}),
@@ -1,6 +1,6 @@
1
1
  import type { CallbackArgs } from '../../libs/auth';
2
2
  import { isSubscriptionOverdraftProtectionEnabled } from '../../libs/subscription';
3
- import { Lock } from '../../store/models';
3
+ import { Lock, TLineItemExpanded } from '../../store/models';
4
4
  import {
5
5
  ensureSubscriptionForOverdraftProtection,
6
6
  executeOcapTransactions,
@@ -35,6 +35,8 @@ export default {
35
35
  );
36
36
  }
37
37
 
38
+ // @ts-ignore
39
+ const items = subscription!.items as TLineItemExpanded[];
38
40
  const claims: { [type: string]: [string, object] } = {};
39
41
 
40
42
  if (paymentMethod.type === 'arcblock') {
@@ -48,6 +50,7 @@ export default {
48
50
  paymentMethod,
49
51
  stakeAmount,
50
52
  subscription,
53
+ items,
51
54
  }),
52
55
  ];
53
56
 
@@ -692,6 +692,7 @@ export async function getOverdraftProtectionStakeTxClaim({
692
692
  paymentCurrency,
693
693
  paymentMethod,
694
694
  stakeAmount,
695
+ items,
695
696
  }: {
696
697
  userDid: string;
697
698
  userPk: string;
@@ -699,6 +700,7 @@ export async function getOverdraftProtectionStakeTxClaim({
699
700
  paymentCurrency: PaymentCurrency;
700
701
  paymentMethod: PaymentMethod;
701
702
  stakeAmount: string;
703
+ items: TLineItemExpanded[];
702
704
  }) {
703
705
  // create staking amount
704
706
  logger.info('getStakeTxClaim', {
@@ -725,9 +727,11 @@ export async function getOverdraftProtectionStakeTxClaim({
725
727
  ),
726
728
  };
727
729
 
730
+ const setup = getSubscriptionCreateSetup(items, paymentCurrency.id, 0, 0);
731
+
728
732
  return {
729
733
  type: 'StakeTx',
730
- description: `Stake to complete subscription ${subscription.id} overdraft protection`,
734
+ description: `Stake to complete subscription ${subscription.id} SubGuard`,
731
735
  partialTx: {
732
736
  from: userDid,
733
737
  pk: userPk,
@@ -735,8 +739,8 @@ export async function getOverdraftProtectionStakeTxClaim({
735
739
  address,
736
740
  receiver: wallet.address,
737
741
  slashers: [wallet.address],
738
- revokeWaitingPeriod: 0,
739
- message: `Stake for subscription ${subscription.id} overdraft protection`,
742
+ revokeWaitingPeriod: setup.cycle.duration / 1000, // wait for at least 1 billing cycle
743
+ message: `Stake for subscription ${subscription.id} SubGuard`,
740
744
  nonce,
741
745
  inputs: [],
742
746
  data,
@@ -946,12 +950,13 @@ export async function ensureSubscriptionForCollectBatch(subscriptionId: string,
946
950
  export async function ensureSubscriptionForOverdraftProtection(subscriptionId: string, amount: string) {
947
951
  const subscription = await Subscription.findByPk(subscriptionId);
948
952
  if (!subscription) {
949
- throw new Error(`Subscription ${subscriptionId} not found when prepare overdraft protection`);
953
+ throw new Error(`Subscription ${subscriptionId} not found when prepare SubGuard`);
950
954
  }
951
-
955
+ // @ts-ignore
956
+ subscription.items = await expandSubscriptionItems(subscription.id);
952
957
  const paymentCurrency = await PaymentCurrency.findByPk(subscription.currency_id);
953
958
  if (!paymentCurrency) {
954
- throw new Error(`PaymentCurrency ${subscription.currency_id} not found when prepare overdraft protection`);
959
+ throw new Error(`PaymentCurrency ${subscription.currency_id} not found when prepare SubGuard`);
955
960
  }
956
961
 
957
962
  const paymentMethod = await PaymentMethod.findByPk(paymentCurrency.payment_method_id);
@@ -960,7 +965,7 @@ export async function ensureSubscriptionForOverdraftProtection(subscriptionId: s
960
965
  throw new Error(`Payment method not found for subscription ${subscriptionId}`);
961
966
  }
962
967
  if (paymentMethod.type !== 'arcblock') {
963
- throw new Error(`Payment method ${paymentMethod.type} not supported for overdraft protection`);
968
+ throw new Error(`Payment method ${paymentMethod.type} not supported for SubGuard`);
964
969
  }
965
970
 
966
971
  const customer = await Customer.findByPk(subscription.customer_id);
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/naming-convention */
1
2
  import { isValid } from '@arcblock/did';
2
3
  import { Router } from 'express';
3
4
  import Joi from 'joi';
@@ -43,6 +44,7 @@ const schema = createListParamSchema<{
43
44
  ignore_zero?: boolean;
44
45
  include_staking?: boolean;
45
46
  include_return_staking?: boolean;
47
+ include_overdraft_protection?: boolean;
46
48
  include_recovered_from?: boolean;
47
49
  }>({
48
50
  status: Joi.string().empty(''),
@@ -53,15 +55,25 @@ const schema = createListParamSchema<{
53
55
  ignore_zero: Joi.boolean().empty(false),
54
56
  include_staking: Joi.boolean().empty(false),
55
57
  include_return_staking: Joi.boolean().empty(false),
58
+ include_overdraft_protection: Joi.boolean().default(true),
56
59
  include_recovered_from: Joi.boolean().empty(false),
57
60
  });
58
61
  router.get('/', authMine, async (req, res) => {
59
62
  // eslint-disable-next-line @typescript-eslint/naming-convention
60
- const { page, pageSize, livemode, status, ignore_zero, include_staking, include_return_staking, ...query } =
61
- await schema.validateAsync(req.query, {
62
- stripUnknown: false,
63
- allowUnknown: true,
64
- });
63
+ const {
64
+ page,
65
+ pageSize,
66
+ livemode,
67
+ status,
68
+ ignore_zero,
69
+ include_staking,
70
+ include_return_staking,
71
+ include_overdraft_protection = true,
72
+ ...query
73
+ } = await schema.validateAsync(req.query, {
74
+ stripUnknown: false,
75
+ allowUnknown: true,
76
+ });
65
77
  const where = getWhereFromKvQuery(query.q);
66
78
 
67
79
  if (status) {
@@ -107,7 +119,11 @@ router.get('/', authMine, async (req, res) => {
107
119
  // @ts-ignore
108
120
  where[key] = query[key];
109
121
  });
122
+
110
123
  const excludeBillingReasons = ['recharge'];
124
+ if (!include_overdraft_protection) {
125
+ excludeBillingReasons.push('stake_overdraft_protection');
126
+ }
111
127
  if (!!(include_staking && query.subscription_id) || !include_staking) {
112
128
  excludeBillingReasons.push('stake');
113
129
  }
@@ -1760,7 +1760,7 @@ router.get('/:id/cycle-amount', authPortal, async (req, res) => {
1760
1760
  const pastMaxAmount = await getPastInvoicesAmount(subscription.id, 'max');
1761
1761
 
1762
1762
  // return max amount
1763
- const nextAmount = new BN(result.amount === '0' ? result.minExpectedAmount : result.amount);
1763
+ const nextAmount = new BN(result.amount === '0' ? result.minExpectedAmount : result.amount).toString();
1764
1764
 
1765
1765
  const maxAmount = new BN(pastMaxAmount.amount).lte(new BN(nextAmount)) ? nextAmount : pastMaxAmount.amount;
1766
1766
 
package/blocklet.yml CHANGED
@@ -14,7 +14,7 @@ repository:
14
14
  type: git
15
15
  url: git+https://github.com/blocklet/payment-kit.git
16
16
  specVersion: 1.2.8
17
- version: 1.17.2
17
+ version: 1.17.4
18
18
  logo: logo.png
19
19
  files:
20
20
  - dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payment-kit",
3
- "version": "1.17.2",
3
+ "version": "1.17.4",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev --open",
6
6
  "eject": "vite eject",
@@ -43,30 +43,30 @@
43
43
  ]
44
44
  },
45
45
  "dependencies": {
46
- "@abtnode/cron": "^1.16.36",
47
- "@arcblock/did": "^1.18.165",
46
+ "@abtnode/cron": "^1.16.37",
47
+ "@arcblock/did": "^1.19.2",
48
48
  "@arcblock/did-auth-storage-nedb": "^1.7.1",
49
- "@arcblock/did-connect": "^2.11.15",
50
- "@arcblock/did-util": "^1.18.165",
51
- "@arcblock/jwt": "^1.18.165",
52
- "@arcblock/ux": "^2.11.15",
53
- "@arcblock/validator": "^1.18.165",
54
- "@blocklet/js-sdk": "^1.16.36",
55
- "@blocklet/logger": "^1.16.36",
56
- "@blocklet/payment-react": "1.17.2",
57
- "@blocklet/sdk": "^1.16.36",
58
- "@blocklet/ui-react": "^2.11.15",
59
- "@blocklet/uploader": "^0.1.60",
60
- "@blocklet/xss": "^0.1.17",
49
+ "@arcblock/did-connect": "^2.11.23",
50
+ "@arcblock/did-util": "^1.19.2",
51
+ "@arcblock/jwt": "^1.19.2",
52
+ "@arcblock/ux": "^2.11.23",
53
+ "@arcblock/validator": "^1.19.2",
54
+ "@blocklet/js-sdk": "^1.16.37",
55
+ "@blocklet/logger": "^1.16.37",
56
+ "@blocklet/payment-react": "1.17.4",
57
+ "@blocklet/sdk": "^1.16.37",
58
+ "@blocklet/ui-react": "^2.11.23",
59
+ "@blocklet/uploader": "^0.1.63",
60
+ "@blocklet/xss": "^0.1.20",
61
61
  "@mui/icons-material": "^5.16.6",
62
62
  "@mui/lab": "^5.0.0-alpha.173",
63
63
  "@mui/material": "^5.16.6",
64
64
  "@mui/system": "^5.16.6",
65
- "@ocap/asset": "^1.18.165",
66
- "@ocap/client": "^1.18.165",
67
- "@ocap/mcrypto": "^1.18.165",
68
- "@ocap/util": "^1.18.165",
69
- "@ocap/wallet": "^1.18.165",
65
+ "@ocap/asset": "^1.19.2",
66
+ "@ocap/client": "^1.19.2",
67
+ "@ocap/mcrypto": "^1.19.2",
68
+ "@ocap/util": "^1.19.2",
69
+ "@ocap/wallet": "^1.19.2",
70
70
  "@stripe/react-stripe-js": "^2.7.3",
71
71
  "@stripe/stripe-js": "^2.4.0",
72
72
  "ahooks": "^3.8.0",
@@ -118,9 +118,9 @@
118
118
  "validator": "^13.12.0"
119
119
  },
120
120
  "devDependencies": {
121
- "@abtnode/types": "^1.16.36",
121
+ "@abtnode/types": "^1.16.37",
122
122
  "@arcblock/eslint-config-ts": "^0.3.3",
123
- "@blocklet/payment-types": "1.17.2",
123
+ "@blocklet/payment-types": "1.17.4",
124
124
  "@types/cookie-parser": "^1.4.7",
125
125
  "@types/cors": "^2.8.17",
126
126
  "@types/debug": "^4.1.12",
@@ -166,5 +166,5 @@
166
166
  "parser": "typescript"
167
167
  }
168
168
  },
169
- "gitHead": "c38e8a29d94254d503aa29084c2b629805a5a7ad"
169
+ "gitHead": "2d25cbcf146ba2195b1c3434ab3f64566f851893"
170
170
  }