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.
- package/api/src/jobs/payment.ts +17 -4
- package/api/src/libs/error.ts +13 -0
- package/api/src/libs/queue/store.ts +1 -1
- package/api/src/libs/util.ts +0 -14
- package/blocklet.yml +1 -1
- package/package.json +3 -3
package/api/src/jobs/payment.ts
CHANGED
|
@@ -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:
|
|
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:
|
|
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:
|
|
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
|
+
}
|
package/api/src/libs/util.ts
CHANGED
|
@@ -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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payment-kit",
|
|
3
|
-
"version": "1.13.
|
|
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.
|
|
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": "
|
|
143
|
+
"gitHead": "d50fe71988ab503ca4f0adf0eef4293008a3ae4f"
|
|
144
144
|
}
|