q-koa 10.6.9 → 10.7.1

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.
@@ -5,8 +5,7 @@ const axios = require('axios')
5
5
  const qr = require('qr-image')
6
6
  const path = require('path')
7
7
  const TOUTIAO = getService('toutiao')
8
- const md5 = require('md5')
9
- const utils = require('../../utils')
8
+ const { signResult, hashFn } = require('../../utils')
10
9
 
11
10
  exports.initData = async (ctx) => {
12
11
  const { app, appName } = getAppByCtx(ctx)
@@ -36,7 +35,7 @@ exports.qr_code = async (ctx) => {
36
35
  const result = await toutiaoService.createQR({
37
36
  path: encodeURIComponent(page),
38
37
  appname: type,
39
- filename: utils.hashFn(item.key + page + type),
38
+ filename: hashFn(item.key + page + type),
40
39
  })
41
40
  console.log(`qr,${item.key},${page},${type}`)
42
41
  const qr_image = `https://${site_host || 'api.kuashou.com'}/upload/${result}`
@@ -195,24 +194,6 @@ exports.login = async (ctx) => {
195
194
  })
196
195
  }
197
196
 
198
- const signResult = (params, salt) => {
199
- var skip_arr = ['thirdparty_id', 'app_id', 'sign']
200
- var paramArray = new Array()
201
- for (var k in params) {
202
- if (skip_arr.indexOf(k) != -1) {
203
- continue
204
- }
205
- if (params[k] == '') {
206
- continue
207
- }
208
- paramArray.push(params[k])
209
- }
210
- paramArray.push(salt)
211
- paramArray.sort()
212
- var signStr = paramArray.join('&')
213
- return md5(signStr)
214
- }
215
-
216
197
  exports.pay = async (ctx) => {
217
198
  const { app, appName } = getAppByCtx(ctx)
218
199
  const appConfig = getConfig(app)
@@ -384,3 +365,20 @@ exports.checkPay = async (ctx) => {
384
365
  order_id: res.order_id,
385
366
  })
386
367
  }
368
+
369
+ exports.refund = async (ctx) => {
370
+ const { app, appName } = getAppByCtx(ctx)
371
+ const appConfig = getConfig(app)
372
+ const { id, prefix = '', price, out_trade_no, type } = ctx.request.body
373
+
374
+ if (!app.service.toutiao) throw new Error(`toutiao可能没有service.js`)
375
+ const res = await app.service.toutiao.refund({
376
+ ctx,
377
+ id,
378
+ prefix,
379
+ price,
380
+ out_trade_no,
381
+ type,
382
+ })
383
+ ctx.SUCCESS(res)
384
+ }
@@ -0,0 +1,78 @@
1
+ const { getAppByCtx, getUserByCtx, getConfig } = require('q-koa')
2
+ const axios = require('axios')
3
+ const { signResult } = require('../../utils')
4
+
5
+ exports.refund = async ({
6
+ ctx,
7
+ id,
8
+ prefix = '',
9
+ total_fee,
10
+ refund_fee,
11
+ price,
12
+ out_trade_no: _out_trade_no,
13
+ type = 'TOUTIAO-DOUYIN',
14
+ ...rest
15
+ }) => {
16
+ if (!ctx) throw new Error('?ctx')
17
+ const { app, appName } = getAppByCtx(ctx)
18
+ const appConfig = getConfig(app)
19
+ const { app_id, salt, key, is_sandbox } = await appConfig.getObject('toutiao')
20
+ const { site_host } = await appConfig.getObject('base')
21
+
22
+ const orderModel = prefix ? `${prefix}_order` : 'order'
23
+ const out_trade_no = _out_trade_no
24
+ ? _out_trade_no
25
+ : prefix
26
+ ? `${key}_${prefix}-${id}_${type}`
27
+ : `${key}_${id}_${type}`
28
+ const orderDetail = await app.model[orderModel].findOne({
29
+ where: {
30
+ id,
31
+ },
32
+ })
33
+ if (!orderDetail) throw new Error('不存在该订单')
34
+
35
+ const url = `https://${
36
+ is_sandbox ? 'open-sandbox.douyin.com' : 'developer.toutiao.com'
37
+ }/api/apps/ecpay/v1/create_refund`
38
+
39
+ const data = {
40
+ app_id,
41
+ out_order_no: out_trade_no,
42
+ out_refund_no: `${prefix}-${id}_${type}`,
43
+ reason: rest.remark || '退款',
44
+ refund_amount: Math.round((refund_fee || price) * 100),
45
+ notify_url: `https://${
46
+ site_host || 'api.kuashou.com'
47
+ }/${appName}/toutiao/refund_notify`,
48
+ }
49
+
50
+ const signResult = (params, salt) => {
51
+ var skip_arr = ['thirdparty_id', 'app_id', 'sign']
52
+ var paramArray = new Array()
53
+ for (var k in params) {
54
+ if (skip_arr.indexOf(k) != -1) {
55
+ continue
56
+ }
57
+ if (params[k] == '') {
58
+ continue
59
+ }
60
+ paramArray.push(params[k])
61
+ }
62
+ paramArray.push(salt)
63
+ paramArray.sort()
64
+ var signStr = paramArray.join('&')
65
+ return md5(signStr)
66
+ }
67
+ const res = await axios
68
+ .post(url, {
69
+ ...data,
70
+ sign: signResult(data, salt),
71
+ })
72
+ .then((res) => res.data)
73
+ if (res.err_no) {
74
+ throw new Error(res.err_tips)
75
+ }
76
+
77
+ return res
78
+ }
@@ -51,29 +51,21 @@ exports.refund = async ({
51
51
  })
52
52
  if (!orderDetail) throw new Error('不存在该订单')
53
53
  try {
54
- const { result_code, err_code_des } = await payObj.refund({
54
+ const data = {
55
55
  ...rest,
56
56
  out_trade_no,
57
- out_refund_no: id + '_' + type,
57
+ out_refund_no: `${prefix}-${id}_${type}`,
58
58
  total_fee: Math.round((total_fee || refund_fee || price) * 100),
59
59
  refund_fee: Math.round((refund_fee || price) * 100),
60
60
  notify_url: `https://${
61
61
  site_host || 'api.kuashou.com'
62
62
  }/${appName}/weixin/refund_notify/${pay_config}`,
63
- })
63
+ }
64
+ const { result_code, err_code_des } = await payObj.refund(data)
64
65
  if (result_code === 'SUCCESS') {
65
66
  return true
66
67
  } else {
67
- console.error({
68
- ...rest,
69
- out_trade_no,
70
- out_refund_no: id + '_' + type,
71
- total_fee: Math.round((total_fee || refund_fee || price) * 100),
72
- refund_fee: Math.round((refund_fee || price) * 100),
73
- notify_url: `https://${
74
- site_host || 'api.kuashou.com'
75
- }/${appName}/weixin/refund_notify/${pay_config}`,
76
- })
68
+ console.error(data)
77
69
  throw new Error(err_code_des)
78
70
  }
79
71
  } catch (e) {
@@ -1,4 +1,5 @@
1
1
  const { lodash } = require('q-koa')
2
+ const md5 = require('md5')
2
3
 
3
4
  const findConfig = (type) => (item) =>
4
5
  item.key === type ||
@@ -169,3 +170,21 @@ exports.getAppConfig = async ({ app, config, key, app_id }) => {
169
170
 
170
171
  return target || null
171
172
  }
173
+
174
+ exports.signResult = (params, salt) => {
175
+ var skip_arr = ['thirdparty_id', 'app_id', 'sign']
176
+ var paramArray = new Array()
177
+ for (var k in params) {
178
+ if (skip_arr.indexOf(k) != -1) {
179
+ continue
180
+ }
181
+ if (params[k] == '') {
182
+ continue
183
+ }
184
+ paramArray.push(params[k])
185
+ }
186
+ paramArray.push(salt)
187
+ paramArray.sort()
188
+ var signStr = paramArray.join('&')
189
+ return md5(signStr)
190
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "10.6.9",
3
+ "version": "10.7.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {