q-koa 8.8.7 → 8.9.2

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.
@@ -26,6 +26,31 @@ exports.initData = async ({ includes, excludes, app, ctx }) => {
26
26
  excludeInclude,
27
27
  autoInclude = true,
28
28
  } = route
29
+
30
+ if (url.includes('http')) {
31
+ return axios
32
+ .post(url, {
33
+ ...data,
34
+ limit,
35
+ order,
36
+ })
37
+ .then((res) => {
38
+ if (res.data.code !== 200) {
39
+ console.log(`${item}请求失败,${res.data.message}`)
40
+ } else {
41
+ return {
42
+ [item]: res.data.data,
43
+ }
44
+ }
45
+ })
46
+ .catch((e) => {
47
+ console.log(`${item}请求失败,${e.message}`)
48
+ return {
49
+ [item]: [],
50
+ }
51
+ })
52
+ }
53
+
29
54
  const [model, fn] = url.split('/')
30
55
 
31
56
  let myInclude
@@ -907,6 +907,66 @@ exports.mp_pay = async (ctx) => {
907
907
  })
908
908
  }
909
909
 
910
+ exports.mp_pay_new = async (ctx) => {
911
+ const { app, appName } = getAppByCtx(ctx)
912
+ const {
913
+ name,
914
+ order_id,
915
+ price,
916
+ return_url,
917
+ prefix = '',
918
+ type,
919
+ out_trade_no: _out_trade_no,
920
+ is_admin = false,
921
+ config = 'weixin_mp',
922
+ pay_config = 'weixin_pay',
923
+ } = ctx.request.body
924
+
925
+ if (price === 0) throw new Error('价格不能为0')
926
+
927
+ if (
928
+ !ctx.request[`${appName}-user`] ||
929
+ !ctx.request[`${appName}-user`].mp_user
930
+ ) {
931
+ throw new Error('请先小程序登录')
932
+ }
933
+
934
+ const appConfig = getConfig(app)
935
+
936
+ const { app_id, app_secrect } = await appConfig.getObject(config)
937
+ const { mchId } = await appConfig.getObject(pay_config)
938
+ const { is_dev, site_host } = await appConfig.getObject('base')
939
+
940
+ const weixinMp = new WeixinMp({
941
+ appid: app_id,
942
+ secrect: app_secrect,
943
+ })
944
+ weixinMp.init()
945
+
946
+ const userDetail = ctx.request[`${appName}-user`].mp_user
947
+
948
+ const out_trade_no = _out_trade_no
949
+ ? _out_trade_no
950
+ : prefix
951
+ ? `${appName}_${prefix}-${order_id}`
952
+ : `${appName}_${order_id}`
953
+
954
+ const prePayResult = await weixinMp.createOrder({
955
+ openid: userDetail.openid,
956
+ combine_trade_no: out_trade_no,
957
+ sub_orders: [
958
+ {
959
+ mchid: mchId,
960
+ amount: is_admin || is_dev ? 1 : Math.round(price * 100),
961
+ trade_no: out_trade_no,
962
+ description: `订单【${out_trade_no}】`,
963
+ },
964
+ ],
965
+ })
966
+
967
+ return ctx.SUCCESS(prePayResult)
968
+ }
969
+
910
970
  exports.notify = async (ctx) => {
911
971
  const { app } = getAppByCtx(ctx)
912
972
  const result = ctx.request.xmlBody
@@ -1034,13 +1094,22 @@ exports.sendMessage = async (ctx) => {
1034
1094
  exports.refund = async (ctx) => {
1035
1095
  const { app, appName } = getAppByCtx(ctx)
1036
1096
  const appConfig = getConfig(app)
1037
- const { id, prefix = '', price, pay_config = 'weixin_pay' } = ctx.request.body
1097
+ const {
1098
+ id,
1099
+ prefix = '',
1100
+ price,
1101
+ config = 'weixin_mp',
1102
+ pay_config = 'weixin_pay',
1103
+ is_new_pay = false,
1104
+ } = ctx.request.body
1038
1105
 
1039
- await app.service.weixin.refund({
1106
+ const fn = is_new_pay ? 'refund_new' : 'refund'
1107
+ await app.service.weixin[fn]({
1040
1108
  ctx,
1041
1109
  id,
1042
1110
  prefix,
1043
1111
  price,
1112
+ config,
1044
1113
  pay_config,
1045
1114
  })
1046
1115
  ctx.SUCCESS('ok')
@@ -1188,7 +1257,57 @@ exports.checkPay = async (ctx) => {
1188
1257
  order: orderModel,
1189
1258
  order_id: id,
1190
1259
  order_price,
1191
- transaction_id: payResult.transaction_id,
1260
+ transactionid: payResult.transaction_id,
1261
+ })
1262
+ }
1263
+
1264
+ ctx.SUCCESS({ prefix, out_trade_no, ...payResult })
1265
+ }
1266
+
1267
+ exports.checkPayNew = async (ctx) => {
1268
+ const { app, appName, controller } = getAppByCtx(ctx)
1269
+
1270
+ const {
1271
+ id,
1272
+ prefix,
1273
+ config = 'weixin_mp',
1274
+ pay_config = 'weixin_pay',
1275
+ } = ctx.request.body
1276
+
1277
+ const orderModel = prefix ? `${prefix}_order` : 'order'
1278
+ const result = await app.model[orderModel].findOne({
1279
+ where: {
1280
+ id,
1281
+ },
1282
+ })
1283
+ if (!result) return ctx.SUCCESS('找不到')
1284
+
1285
+ const appConfig = getConfig(app)
1286
+ const { app_id, app_secrect } = await appConfig.getObject(config)
1287
+ const { mchId } = await appConfig.getObject(pay_config)
1288
+
1289
+ const weixinMp = new WeixinMp({
1290
+ appid: app_id,
1291
+ secrect: app_secrect,
1292
+ })
1293
+ weixinMp.init()
1294
+
1295
+ const orderPrefix = prefix ? `${prefix}-` : ''
1296
+ const out_trade_no = `${appName}_${orderPrefix}${id}`
1297
+
1298
+ const payResult = await weixinMp.getOrder({
1299
+ trade_no: out_trade_no,
1300
+ })
1301
+
1302
+ if (payResult) {
1303
+ console.log('payResult', payResult)
1304
+ const order_price = Number(payResult.amount) / 100
1305
+ await app.service[orderModel].notify({
1306
+ app,
1307
+ order: orderModel,
1308
+ order_id: id,
1309
+ order_price,
1310
+ transactionid: payResult.transaction_id,
1192
1311
  })
1193
1312
  }
1194
1313
 
@@ -13,6 +13,7 @@ exports.refund = async ({
13
13
  refund_fee,
14
14
  price,
15
15
  type = '',
16
+ config = 'weixin_mp',
16
17
  pay_config = 'weixin_pay',
17
18
  ...rest
18
19
  }) => {
@@ -65,6 +66,78 @@ exports.refund = async ({
65
66
  }
66
67
  }
67
68
 
69
+ exports.refund_new = async ({
70
+ ctx,
71
+ id,
72
+ prefix = '',
73
+ total_fee,
74
+ refund_fee,
75
+ price,
76
+ transaction_id = '',
77
+ type = '',
78
+ config = 'weixin_mp',
79
+ pay_config = 'weixin_pay',
80
+ ...rest
81
+ }) => {
82
+ const { app, appName } = getAppByCtx(ctx)
83
+ const appConfig = getConfig(app)
84
+
85
+ const { app_id, app_secrect } = await appConfig.getObject(config)
86
+ const { mchId } = await appConfig.getObject(pay_config)
87
+
88
+ const weixinMp = new WeixinMp({
89
+ appid: app_id,
90
+ secrect: app_secrect,
91
+ })
92
+ weixinMp.init()
93
+
94
+ const orderModel = prefix ? `${prefix}_order` : 'order'
95
+ const out_trade_no = prefix
96
+ ? `${appName}_${prefix}-${id}`
97
+ : `${appName}_${id}`
98
+
99
+ const orderDetail = await app.model[orderModel].findOne({
100
+ where: {
101
+ id,
102
+ },
103
+ include: [
104
+ {
105
+ model: app.model.user,
106
+ },
107
+ ],
108
+ })
109
+
110
+ if (!orderDetail) throw new Error('不存在该订单')
111
+ if (!orderDetail.user || (orderDetail.user && !orderDetail.user.mp_openid)) {
112
+ throw new Error('找不到退款用户')
113
+ }
114
+ try {
115
+ const payResult = await weixinMp.getOrder({
116
+ trade_no: out_trade_no,
117
+ })
118
+
119
+ const { transaction_id } = payResult
120
+
121
+ const result = await weixinMp.refundOrder({
122
+ openid: orderDetail.user.mp_openid,
123
+ trade_no: out_trade_no,
124
+ mchid: mchId,
125
+ transaction_id,
126
+ refund_no: out_trade_no + (type ? `_${type}` : ''),
127
+ total_amount: Math.round((total_fee || refund_fee || price) * 100),
128
+ refund_amount: Math.round((refund_fee || price) * 100),
129
+ })
130
+ return result
131
+ // if (result_code === 'SUCCESS') {
132
+ // return true
133
+ // } else {
134
+ // throw new Error(err_code_des)
135
+ // }
136
+ } catch (e) {
137
+ throw new Error(e)
138
+ }
139
+ }
140
+
68
141
  exports.cash = async ({
69
142
  ctx,
70
143
  id,
@@ -222,3 +295,132 @@ exports.url_link = async ({ app, page, scene, config = 'weixin_mp' }) => {
222
295
  })
223
296
  return result
224
297
  }
298
+
299
+ exports.user_enter_tempsession = async ({ app, result }) => {
300
+ console.log('user_enter_tempsession', result)
301
+ }
302
+
303
+ exports.handleUserMessage = async ({ app, result }) => {
304
+ const appConfig = getConfig(app)
305
+
306
+ if (result.ToUserName) {
307
+ const { app_id, app_secrect } = await appConfig.getObject(result.ToUserName)
308
+ const weixinMp = new WeixinMp({
309
+ appid: app_id,
310
+ secrect: app_secrect,
311
+ })
312
+ weixinMp.init()
313
+ await weixinMp.handleMessage({
314
+ touser: result.FromUserName,
315
+ msgtype: 'text',
316
+ text: {
317
+ content: `你说${result.Content},是吗?`,
318
+ },
319
+ })
320
+ }
321
+ }
322
+
323
+ exports.funds_order_pay = async ({ app, result }) => {
324
+ const appConfig = getConfig(app)
325
+
326
+ if (result.ToUserName) {
327
+ const { app_id, app_secrect, mchId } = await appConfig.getObject(
328
+ result.ToUserName
329
+ )
330
+
331
+ const weixinMp = new WeixinMp({
332
+ appid: app_id,
333
+ secrect: app_secrect,
334
+ })
335
+ weixinMp.init()
336
+
337
+ if (result.order_info) {
338
+ let prefix = ''
339
+ let order_id = result.order_info.trade_no.split('_')[1]
340
+ if (order_id.includes('-')) {
341
+ const arr = order_id.split('-')
342
+ order_id = Number(arr[1])
343
+ ;[prefix] = arr
344
+ } else {
345
+ order_id = Number(order_id)
346
+ }
347
+ const model = prefix ? `${prefix}_order` : 'order'
348
+ let transactionid = ''
349
+ try {
350
+ transactionid = result.order_info.transaction_id
351
+ } catch (e) {}
352
+
353
+ const payResult = await weixinMp.getOrder({
354
+ trade_no: result.order_info.trade_no,
355
+ })
356
+
357
+ const { amount } = payResult
358
+
359
+ if (app.service[model] && app.service[model].notify) {
360
+ await app.service[model].notify({
361
+ app,
362
+ order_id,
363
+ order: model,
364
+ order_price: amount / 100,
365
+ transactionid,
366
+ })
367
+ }
368
+ }
369
+ }
370
+ }
371
+
372
+ exports.funds_order_refund = async ({ app, result }) => {
373
+ const appConfig = getConfig(app)
374
+
375
+ if (result.ToUserName) {
376
+ const { app_id, app_secrect, mchId } = await appConfig.getObject(
377
+ result.ToUserName
378
+ )
379
+
380
+ const weixinMp = new WeixinMp({
381
+ appid: app_id,
382
+ secrect: app_secrect,
383
+ })
384
+ weixinMp.init()
385
+
386
+ if (result.order_info) {
387
+ let prefix = ''
388
+ let order_id = result.order_info.refund_no.split('_')[1]
389
+ if (order_id.includes('-')) {
390
+ const arr = order_id.split('-')
391
+ order_id = Number(arr[1])
392
+ ;[prefix] = arr
393
+ } else {
394
+ order_id = Number(order_id)
395
+ }
396
+ const model = prefix ? `${prefix}_order` : 'order'
397
+
398
+ const trade_no = result.order_info.refund_no
399
+ .split('_')
400
+ .slice(0, 2)
401
+ .join('_')
402
+
403
+ const type = trade_no.includes('_') ? trade_no.split('_')[1] : ''
404
+
405
+ const payResult = await weixinMp.getOrder({
406
+ trade_no,
407
+ })
408
+
409
+ const refundTarget = payResult.refund_list.find(
410
+ (t) => t.refund_no === result.order_info.refund_no
411
+ )
412
+
413
+ if (!refundTarget || refundTarget.result !== 'SUCCESS')
414
+ throw new Error('还没退成功')
415
+ if (app.service[model] && app.service[model].refund_notify) {
416
+ await app.service[model].refund_notify({
417
+ app,
418
+ order_id,
419
+ order: model,
420
+ refund_price: refundTarget.amount / 100,
421
+ type,
422
+ })
423
+ }
424
+ }
425
+ }
426
+ }
@@ -34,6 +34,12 @@ const getExpressUrl =
34
34
  'https://api.weixin.qq.com/cgi-bin/express/business/delivery/getall?access_token=%s'
35
35
  const addOrderUrl =
36
36
  'https://api.weixin.qq.com/cgi-bin/express/business/order/add?access_token=%s'
37
+ const createOrderUrl =
38
+ 'https://api.weixin.qq.com/shop/pay/createorder?access_token=%s'
39
+ const getOrderUrl =
40
+ 'https://api.weixin.qq.com/shop/pay/getorder?access_token=%s'
41
+ const refundOrderUrl =
42
+ 'https://api.weixin.qq.com/shop/pay/refundorder?access_token=%s'
37
43
 
38
44
  const path = require('path')
39
45
  const fs = require('fs')
@@ -501,6 +507,63 @@ module.exports = class Singleton {
501
507
  return result.data
502
508
  }
503
509
 
510
+ async createOrder(payLoad) {
511
+ const access_token = await this.getAccessToken()
512
+ const url = util.format(createOrderUrl, access_token)
513
+ const result = await axios
514
+ .post(url, {
515
+ ...payLoad,
516
+ })
517
+ .then((res) => res.data)
518
+
519
+ if (result.errcode) {
520
+ if (result.errcode === 40001) {
521
+ cache.reset()
522
+ return await this.createOrder(payLoad)
523
+ }
524
+ throw new Error(`${result.errcode};${result.errmsg}`)
525
+ }
526
+ return result.payment_params
527
+ }
528
+
529
+ async getOrder(payLoad) {
530
+ const access_token = await this.getAccessToken()
531
+ const url = util.format(getOrderUrl, access_token)
532
+ const result = await axios
533
+ .post(url, {
534
+ ...payLoad,
535
+ })
536
+ .then((res) => res.data)
537
+
538
+ if (result.errcode) {
539
+ if (result.errcode === 40001) {
540
+ cache.reset()
541
+ return await this.getOrder(payLoad)
542
+ }
543
+ throw new Error(`${result.errcode};${result.errmsg}`)
544
+ }
545
+ return result.order
546
+ }
547
+
548
+ async refundOrder(payLoad) {
549
+ const access_token = await this.getAccessToken()
550
+ const url = util.format(refundOrderUrl, access_token)
551
+ const result = await axios
552
+ .post(url, {
553
+ ...payLoad,
554
+ })
555
+ .then((res) => res.data)
556
+
557
+ if (result.errcode) {
558
+ if (result.errcode === 40001) {
559
+ cache.reset()
560
+ return await this.refundOrder(payLoad)
561
+ }
562
+ throw new Error(`${result.errcode};${result.errmsg}`)
563
+ }
564
+ return result.data
565
+ }
566
+
504
567
  getConfig() {
505
568
  return this.config
506
569
  }
@@ -34,7 +34,10 @@ exports.getObject = (config, app) => async (type) => {
34
34
  flag = true
35
35
  }
36
36
 
37
- const findConfig = (item) => item.config === type || item.pay_config === type
37
+ const findConfig = (item) =>
38
+ item.config === type ||
39
+ item.pay_config === type ||
40
+ (item.ToUserName && item.ToUserName === type)
38
41
  if (app) {
39
42
  const applicationList = app.cache.get('application')
40
43
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "8.8.7",
3
+ "version": "8.9.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {