q-koa 10.8.0 → 10.8.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.
@@ -1,6 +1,11 @@
1
1
  const Alipay = require('../../services/alipay')
2
2
 
3
3
  const { getAppByCtx, getConfig } = require('q-koa')
4
+
5
+ const path = require('path')
6
+ const fs = require('fs')
7
+ const AlipaySdk = require('alipay-sdk').default
8
+
4
9
  exports.h5_pay = async (ctx) => {
5
10
  const { app, appName } = getAppByCtx(ctx)
6
11
 
@@ -66,3 +71,67 @@ exports.notify = async (ctx) => {
66
71
  ctx.body = 'success'
67
72
  }
68
73
  }
74
+
75
+ exports.mp_pay = async (ctx) => {
76
+ const { app, appName } = getAppByCtx(ctx)
77
+ const appConfig = getConfig(app)
78
+ const { is_dev, site_host } = await appConfig.getObject('base')
79
+ const {
80
+ app_id,
81
+ alipay_public_key,
82
+ key,
83
+ is_sandbox,
84
+ ...rest
85
+ } = await appConfig.getObject('alipay')
86
+
87
+ const {
88
+ name,
89
+ out_trade_no: _out_trade_no,
90
+ prefix = '',
91
+ order_id,
92
+ type,
93
+ price,
94
+ is_admin = false,
95
+ buyer_id = '2088722007804916',
96
+ } = ctx.request.body
97
+
98
+ const gateway = is_sandbox
99
+ ? `https://openapi.alipaydev.com/gateway.do`
100
+ : 'https://openapi.alipay.com/gateway.do'
101
+
102
+ const notify_url = `https://${
103
+ site_host || 'api.kuashou.com'
104
+ }/${appName}/alipay/notify`
105
+
106
+ const alipaySdk = new AlipaySdk({
107
+ appId: app_id,
108
+ signType: 'RSA2', // 签名算法,默认 RSA2
109
+ gateway, // 支付宝网关地址 ,沙箱环境下使用时需要修改
110
+ alipayPublicKey: alipay_public_key,
111
+ privateKey: fs.readFileSync(
112
+ path.resolve(__dirname, 'private-key.pem'),
113
+ 'ascii'
114
+ ),
115
+ notify_url,
116
+ })
117
+
118
+ const out_trade_no = _out_trade_no
119
+ ? _out_trade_no
120
+ : prefix
121
+ ? `${key.length > 10 ? appName : key}_${prefix}-${order_id}_${type}`
122
+ : `${key.length > 10 ? appName : key}_${order_id}_${type}`
123
+
124
+ const res = await alipaySdk.exec('alipay.trade.create', {
125
+ bizContent: {
126
+ out_trade_no,
127
+ total_amount: is_admin || is_dev ? 1 : Math.round(price * 100),
128
+ subject: name,
129
+ buyer_id,
130
+ },
131
+ })
132
+ if (res.code !== '10000') {
133
+ throw new Error(res.subMsg)
134
+ }
135
+ const { code, msg, tradeNo, ...other } = res
136
+ return ctx.SUCCESS(tradeNo)
137
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "10.8.0",
3
+ "version": "10.8.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,6 +16,7 @@
16
16
  "author": "",
17
17
  "license": "ISC",
18
18
  "dependencies": {
19
+ "alipay-sdk": "^3.2.0",
19
20
  "@alicloud/pop-core": "^1.7.9",
20
21
  "@sigodenjs/wechatpay": "^2.1.1",
21
22
  "ali-oss": "^6.11.2",