q-koa 13.8.43 → 13.8.45

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.
@@ -269,9 +269,7 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
269
269
  if (obj.setting && apiVersion && cacheVersion) {
270
270
  let target = null
271
271
  if (config) {
272
- const application_key = `${app.appConfig.appName}-application`
273
-
274
- const application = app.cache.get(application_key)
272
+ const application = app.cache.get('application')
275
273
  if (application) {
276
274
  target = application.find((a) => a.config === config)
277
275
  }
@@ -2361,6 +2361,39 @@ exports.union_checkPay = async (ctx) => {
2361
2361
  ctx.SUCCESS({ prefix, ...payResult })
2362
2362
  }
2363
2363
 
2364
+ exports.union_check_balance = async (ctx) => {
2365
+ const { app } = getAppByCtx(ctx)
2366
+
2367
+ const {
2368
+ pay_config = 'weixin_pay',
2369
+ accttype = '01', // 01:余额户 02:冻结户
2370
+ mch_id = '',
2371
+ } = ctx.request.body
2372
+
2373
+ const appConfig = getConfig(app)
2374
+ const {
2375
+ union_mchId,
2376
+ union_appid,
2377
+ union_privateKey,
2378
+ union_publicKey,
2379
+ } = await appConfig.getObject(pay_config)
2380
+
2381
+ const allinpay = new AllinPay({
2382
+ cusid: mch_id || union_mchId,
2383
+ appid: union_appid,
2384
+ isTest: false,
2385
+ privateKey: union_privateKey,
2386
+ publicKey: union_publicKey,
2387
+ })
2388
+
2389
+ const result = await allinpay.checkBalance(accttype)
2390
+
2391
+ // amount 单位为分,转换为元便于前端展示
2392
+ const balance = result.amount ? Number(result.amount) / 100 : 0
2393
+
2394
+ ctx.SUCCESS({ ...result, balance })
2395
+ }
2396
+
2364
2397
  exports.b2b_checkPay = async (ctx) => {
2365
2398
  const { app, appName, controller } = getAppByCtx(ctx)
2366
2399
 
@@ -39,9 +39,12 @@ class AllinPay {
39
39
  options.isTest !== undefined ? options.isTest : config.allinpay.isTest
40
40
  this.signType = options.signType || config.allinpay.signType
41
41
  this.privateKey = options.privateKey || config.allinpay.privateKey
42
- this.publicKey = this.isTest
43
- ? config.allinpay.publicKey.test
44
- : config.allinpay.publicKey.production
42
+ // 优先使用外部传入的公钥(如商户配置的 union_publicKey),未传则用内置平台公钥
43
+ this.publicKey =
44
+ options.publicKey ||
45
+ (this.isTest
46
+ ? config.allinpay.publicKey.test
47
+ : config.allinpay.publicKey.production)
45
48
  this.baseUrl = this.isTest
46
49
  ? config.allinpay.apiUrl.test
47
50
  : config.allinpay.apiUrl.production
@@ -105,25 +108,52 @@ class AllinPay {
105
108
  const signStr = this.buildSignStr(paramsCopy)
106
109
 
107
110
  // PEM 标准化:无头尾则包 SPKI 头尾 + 内容单换行;已有头尾若一行无换行补换行
108
- let pem = String(this.publicKey)
109
- if (!pem.includes('-----BEGIN')) {
110
- pem = `-----BEGIN PUBLIC KEY-----\n${pem}\n-----END PUBLIC KEY-----`
111
- } else if (!pem.includes('\n')) {
112
- pem = pem
113
- .replace('-----BEGIN PUBLIC KEY-----', '-----BEGIN PUBLIC KEY-----\n')
114
- .replace('-----END PUBLIC KEY-----', '\n-----END PUBLIC KEY-----')
111
+ const normalizePem = (key) => {
112
+ let pem = String(key)
113
+ if (!pem.includes('-----BEGIN')) {
114
+ pem = `-----BEGIN PUBLIC KEY-----\n${pem}\n-----END PUBLIC KEY-----`
115
+ } else if (!pem.includes('\n')) {
116
+ pem = pem
117
+ .replace('-----BEGIN PUBLIC KEY-----', '-----BEGIN PUBLIC KEY-----\n')
118
+ .replace('-----END PUBLIC KEY-----', '\n-----END PUBLIC KEY-----')
119
+ }
120
+ return pem
115
121
  }
116
- // RSA 双算法兜底:先 SHA256(新 2048-bit 证书),失败再 SHA1(老 1024-bit 证书)
117
- for (const alg of ['RSA-SHA256', 'RSA-SHA1']) {
118
- try {
119
- const verify = crypto.createVerify(alg)
120
- verify.update(signStr, 'utf8')
121
- if (verify.verify(pem, sign, 'base64')) return true
122
- } catch (e) {}
122
+
123
+ // 候选公钥:先用实例公钥(可能是商户配置的 union_publicKey),失败再用内置平台公钥兜底。
124
+ // 通联平台响应用平台私钥签名,商户配置的应用公钥验不过时用平台公钥补一轮。
125
+ const candidates = [this.publicKey]
126
+ const platformKey = this.isTest
127
+ ? config.allinpay.publicKey.test
128
+ : config.allinpay.publicKey.production
129
+ if (platformKey !== this.publicKey) candidates.push(platformKey)
130
+
131
+ for (const pem of candidates) {
132
+ // RSA 双算法兜底:先 SHA256(新 2048-bit 证书),失败再 SHA1(老 1024-bit 证书)
133
+ for (const alg of ['RSA-SHA256', 'RSA-SHA1']) {
134
+ try {
135
+ const verify = crypto.createVerify(alg)
136
+ verify.update(signStr, 'utf8')
137
+ if (verify.verify(normalizePem(pem), sign, 'base64')) return true
138
+ } catch (e) {}
139
+ }
123
140
  }
124
141
  return false
125
142
  }
126
143
 
144
+ // 统一响应处理:retcode=SUCCESS 时验签(失败仅告警),否则抛出业务错误
145
+ _handleResult(result) {
146
+ if (result && result.retcode === 'SUCCESS') {
147
+ if (result.sign && !this.verifySign(result)) {
148
+ console.warn(
149
+ 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
150
+ )
151
+ }
152
+ return result
153
+ }
154
+ throw new Error(result.retmsg || 'Unknown error')
155
+ }
156
+
127
157
  async request(url, params) {
128
158
  const _params = new URLSearchParams()
129
159
  Object.keys(params).forEach((key) => {
@@ -156,7 +186,8 @@ class AllinPay {
156
186
  console.error('Response status:', error.response.status)
157
187
  console.error('Response data:', JSON.stringify(error.response.data))
158
188
  }
159
- throw new Error(error.message)
189
+ // 直接抛出原始错误,保留错误栈便于排查
190
+ throw error
160
191
  }
161
192
  }
162
193
 
@@ -216,17 +247,7 @@ class AllinPay {
216
247
  const url = `${this.baseUrl}/pay`
217
248
  const result = await this.request(url, params)
218
249
 
219
- if (result && result.retcode === 'SUCCESS') {
220
- if (result.sign && this.verifySign(result)) {
221
- return result
222
- } else {
223
- console.warn(
224
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
225
- )
226
- return result
227
- }
228
- }
229
- throw new Error(result.retmsg || 'Unknown error')
250
+ return this._handleResult(result)
230
251
  }
231
252
 
232
253
  async queryOrder(reqsn) {
@@ -243,17 +264,7 @@ class AllinPay {
243
264
  const url = `${this.baseUrl}/query`
244
265
  const result = await this.request(url, params)
245
266
 
246
- if (result && result.retcode === 'SUCCESS') {
247
- if (result.sign && this.verifySign(result)) {
248
- return result
249
- } else {
250
- console.warn(
251
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
252
- )
253
- return result
254
- }
255
- }
256
- throw new Error(result.retmsg || 'Unknown error')
267
+ return this._handleResult(result)
257
268
  }
258
269
 
259
270
  async checkOrder(reqsn) {
@@ -273,17 +284,28 @@ class AllinPay {
273
284
  : 'https://vsp.allinpay.com/apiweb/tranx/query'
274
285
  const result = await this.request(url, params)
275
286
 
276
- if (result && result.retcode === 'SUCCESS') {
277
- if (result.sign && this.verifySign(result)) {
278
- return result
279
- } else {
280
- console.warn(
281
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
282
- )
283
- return result
284
- }
287
+ return this._handleResult(result)
288
+ }
289
+
290
+ async checkBalance(accttype = '01') {
291
+ const params = {
292
+ cusid: this.cusid,
293
+ appid: this.appid,
294
+ version: '12',
295
+ randomstr: this.generateRandomStr(),
296
+ accttype, // 01:余额户查询 02:冻结户查询
297
+ signtype: this.signType,
285
298
  }
286
- throw new Error(result.retmsg || 'Unknown error')
299
+ if (this.orgid) params.orgid = this.orgid
300
+
301
+ params.sign = this.sign(params)
302
+
303
+ const url = this.isTest
304
+ ? 'https://syb-test.allinpay.com/vsppcusapi/cusacct/querybalance'
305
+ : 'https://cus.allinpay.com/cusapi/cusacct/querybalance'
306
+ const result = await this.request(url, params)
307
+
308
+ return this._handleResult(result)
287
309
  }
288
310
 
289
311
  async refundOrder(orderData) {
@@ -300,7 +322,6 @@ class AllinPay {
300
322
  }
301
323
  if (remark) params.remark = remark
302
324
  if (acct) params.acct = acct
303
- params.signtype = this.signType
304
325
  if (this.notifyUrl) params.notify_url = this.notifyUrl
305
326
 
306
327
  params.sign = this.sign(params)
@@ -310,17 +331,7 @@ class AllinPay {
310
331
  : 'https://vsp.allinpay.com/apiweb/tranx/refund'
311
332
  const result = await this.request(url, params)
312
333
 
313
- if (result && result.retcode === 'SUCCESS') {
314
- if (result.sign && this.verifySign(result)) {
315
- return result
316
- } else {
317
- console.warn(
318
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
319
- )
320
- return result
321
- }
322
- }
323
- throw new Error(result.retmsg || 'Unknown error')
334
+ return this._handleResult(result)
324
335
  }
325
336
 
326
337
  async cancelOrder(reqsn) {
@@ -337,17 +348,7 @@ class AllinPay {
337
348
  const url = `${this.baseUrl}/cancel`
338
349
  const result = await this.request(url, params)
339
350
 
340
- if (result && result.retcode === 'SUCCESS') {
341
- if (result.sign && this.verifySign(result)) {
342
- return result
343
- } else {
344
- console.warn(
345
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
346
- )
347
- return result
348
- }
349
- }
350
- throw new Error(result.retmsg || 'Unknown error')
351
+ return this._handleResult(result)
351
352
  }
352
353
 
353
354
  async downloadBill({ date }) {
@@ -359,7 +360,6 @@ class AllinPay {
359
360
  filetype: '1',
360
361
  signtype: this.signType,
361
362
  }
362
- params.signtype = this.signType
363
363
 
364
364
  params.sign = this.sign(params)
365
365
 
@@ -368,17 +368,7 @@ class AllinPay {
368
368
  : 'https://cus.allinpay.com/cusapi/trxfile/get'
369
369
  const result = await this.request(url, params)
370
370
 
371
- if (result && result.retcode === 'SUCCESS') {
372
- if (result.sign && this.verifySign(result)) {
373
- return result
374
- } else {
375
- console.warn(
376
- 'Sign verification failed, but retcode is SUCCESS. Returning result anyway.'
377
- )
378
- return result
379
- }
380
- }
381
- throw new Error(result.retmsg || 'Unknown error')
371
+ return this._handleResult(result)
382
372
  }
383
373
 
384
374
  generateRandomStr(length = 32) {
@@ -60,8 +60,7 @@ exports.getObject = (config, app) => async (type) => {
60
60
  }
61
61
 
62
62
  if (app) {
63
- const application_key = `${app.appConfig.appName}-application`
64
- const applicationList = app.cache.get(application_key)
63
+ const applicationList = app.cache.get('application')
65
64
  const target = applicationList && applicationList.find(findConfig(type))
66
65
  if (target) {
67
66
  flag = true
@@ -77,7 +76,7 @@ exports.getObject = (config, app) => async (type) => {
77
76
 
78
77
  if (result.length > 0) {
79
78
  const formatData = JSON.parse(JSON.stringify(result))
80
- app.cache.set(application_key, formatData)
79
+ app.cache.set('application', formatData)
81
80
 
82
81
  const target = formatData.find(findConfig(type))
83
82
  if (target) {
@@ -189,11 +188,8 @@ exports.getImageType = (fileBuffer) => {
189
188
 
190
189
  exports.getAppConfig = async ({ app, config, key, app_id, id }) => {
191
190
  if (!(app && (config || key || app_id || id))) return null
192
-
193
- const application_key = `${app.appConfig.appName}-application`
194
-
195
191
  const application =
196
- app.cache.get(application_key) || (await app.model.application.findAll())
192
+ app.cache.get('application') || (await app.model.application.findAll())
197
193
  const target = application.find(findConfig(config || key || app_id || id))
198
194
 
199
195
  return target || null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.8.43",
3
+ "version": "13.8.45",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {