payment-kit 1.13.38 → 1.13.39

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.
@@ -1,5 +1,8 @@
1
+ import { toBN } from '@ocap/util';
2
+
1
3
  import { wallet } from '../libs/auth';
2
4
  import dayjs from '../libs/dayjs';
5
+ import CustomError from '../libs/error';
3
6
  import logger from '../libs/logger';
4
7
  import createQueue from '../libs/queue';
5
8
  import { MAX_RETRY_COUNT, getNextRetry } from '../libs/util';
@@ -59,8 +62,18 @@ export const handlePayment = async (job: PaymentJob) => {
59
62
  // try payment capture and reschedule on error
60
63
  logger.info(`PaymentIntent capture attempt: ${paymentIntent.id}`);
61
64
  try {
62
- await paymentIntent.update({ status: 'processing' });
63
65
  const client = paymentMethod.getOcapClient();
66
+ const payer = paymentSettings?.payment_method_options.arcblock?.payer;
67
+
68
+ // check balance before capture
69
+ const result = await client.getAccountTokens({ address: payer, token: paymentCurrency.contract });
70
+ const balance = result.tokens.find((x: any) => x.address === paymentCurrency.contract)?.balance;
71
+ if (balance === undefined || toBN(balance).lt(toBN(paymentIntent.amount))) {
72
+ throw new CustomError('INSUFFICIENT_BALANCE', 'payer balance not enough for this payment');
73
+ }
74
+
75
+ // do the capture
76
+ await paymentIntent.update({ status: 'processing' });
64
77
  const txHash = await client.sendTransferV2Tx({
65
78
  tx: {
66
79
  itx: {
@@ -78,7 +91,7 @@ export const handlePayment = async (job: PaymentJob) => {
78
91
  },
79
92
  },
80
93
  },
81
- delegator: paymentSettings?.payment_method_options.arcblock?.payer,
94
+ delegator: payer,
82
95
  wallet,
83
96
  });
84
97
  logger.info(`PaymentIntent capture done: ${paymentIntent.id} with tx ${txHash}`);
@@ -89,7 +102,7 @@ export const handlePayment = async (job: PaymentJob) => {
89
102
  payment_details: {
90
103
  arcblock: {
91
104
  tx_hash: txHash,
92
- payer: paymentSettings?.payment_method_options.arcblock?.payer as string,
105
+ payer: payer as string,
93
106
  },
94
107
  },
95
108
  });
@@ -129,7 +142,7 @@ export const handlePayment = async (job: PaymentJob) => {
129
142
  payment_details: {
130
143
  arcblock: {
131
144
  tx_hash: txHash,
132
- payer: paymentSettings?.payment_method_options.arcblock?.payer as string,
145
+ payer: payer as string,
133
146
  },
134
147
  },
135
148
  });
@@ -0,0 +1,13 @@
1
+ export default class CustomError extends Error {
2
+ code: string;
3
+
4
+ constructor(code = 'GENERIC', ...params: any[]) {
5
+ super(...params);
6
+
7
+ if (Error.captureStackTrace) {
8
+ Error.captureStackTrace(this, CustomError);
9
+ }
10
+
11
+ this.code = code;
12
+ }
13
+ }
@@ -1,7 +1,7 @@
1
1
  import { Op } from 'sequelize';
2
2
 
3
3
  import { Job, TJob } from '../../store/models/job';
4
- import { CustomError } from '../util';
4
+ import CustomError from '../error';
5
5
 
6
6
  export default function createQueueStore(queue: string) {
7
7
  return {
@@ -119,20 +119,6 @@ export function tryWithTimeout(asyncFn: Function, timeout = 5000) {
119
119
  });
120
120
  }
121
121
 
122
- export class CustomError extends Error {
123
- code: string;
124
-
125
- constructor(code = 'GENERIC', ...params: any) {
126
- super(...params);
127
-
128
- if (Error.captureStackTrace) {
129
- Error.captureStackTrace(this, CustomError);
130
- }
131
-
132
- this.code = code;
133
- }
134
- }
135
-
136
122
  // simple exponential delay: 2^retryCount
137
123
  export const getNextRetry = (retryCount: number) => {
138
124
  const delay = 2 ** retryCount;
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.13.38
17
+ version: 1.13.39
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.13.38",
3
+ "version": "1.13.39",
4
4
  "scripts": {
5
5
  "dev": "blocklet dev",
6
6
  "eject": "vite eject",
@@ -103,7 +103,7 @@
103
103
  "@abtnode/types": "1.16.17-beta-952ef53d",
104
104
  "@arcblock/eslint-config": "^0.2.4",
105
105
  "@arcblock/eslint-config-ts": "^0.2.4",
106
- "@did-pay/types": "1.13.38",
106
+ "@did-pay/types": "1.13.39",
107
107
  "@types/cookie-parser": "^1.4.5",
108
108
  "@types/cors": "^2.8.15",
109
109
  "@types/dotenv-flow": "^3.3.2",
@@ -140,5 +140,5 @@
140
140
  "parser": "typescript"
141
141
  }
142
142
  },
143
- "gitHead": "961b898e9ee66e55ffcbd9247987812f6ed61fe3"
143
+ "gitHead": "d50fe71988ab503ca4f0adf0eef4293008a3ae4f"
144
144
  }