|
@@ -2,7 +2,7 @@ |
|
@@ -2,7 +2,7 @@ |
2
|
* @Author: Targaryen
|
2
|
* @Author: Targaryen
|
3
|
* @Date: 2017-01-03 17:42:41
|
3
|
* @Date: 2017-01-03 17:42:41
|
4
|
* @Last Modified by: Targaryen
|
4
|
* @Last Modified by: Targaryen
|
5
|
- * @Last Modified time: 2017-01-05 13:52:47
|
5
|
+ * @Last Modified time: 2017-01-09 16:12:07
|
6
|
*/
|
6
|
*/
|
7
|
|
7
|
|
8
|
'use strict';
|
8
|
'use strict';
|
|
@@ -15,6 +15,7 @@ const Promise = require('bluebird'); |
|
@@ -15,6 +15,7 @@ const Promise = require('bluebird'); |
15
|
const co = Promise.coroutine;
|
15
|
const co = Promise.coroutine;
|
16
|
const sign = require('./sign');
|
16
|
const sign = require('./sign');
|
17
|
const md5 = require('md5');
|
17
|
const md5 = require('md5');
|
|
|
18
|
+const common = require('./common');
|
18
|
|
19
|
|
19
|
/**
|
20
|
/**
|
20
|
* 微信支付相关工具类
|
21
|
* 微信支付相关工具类
|
|
@@ -39,28 +40,11 @@ const tools = { |
|
@@ -39,28 +40,11 @@ const tools = { |
39
|
},
|
40
|
},
|
40
|
|
41
|
|
41
|
/**
|
42
|
/**
|
42
|
- * 生成指定长度的随机字符串
|
|
|
43
|
- */
|
|
|
44
|
- createRandomString(len) {
|
|
|
45
|
- let strlen = len || 32;
|
|
|
46
|
- let originString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
47
|
- let resultString = '';
|
|
|
48
|
-
|
|
|
49
|
- for (let i = 0; i < strlen; i++) {
|
|
|
50
|
- let position = parseInt(100 * Math.random(), 0) % 62;
|
|
|
51
|
-
|
|
|
52
|
- resultString += originString.slice(position - 1, position);
|
|
|
53
|
- }
|
|
|
54
|
-
|
|
|
55
|
- return resultString;
|
|
|
56
|
- },
|
|
|
57
|
-
|
|
|
58
|
- /**
|
|
|
59
|
* 构造获取code的url连接
|
43
|
* 构造获取code的url连接
|
60
|
*/
|
44
|
*/
|
61
|
createOauthUrlForCode(redirectUrl) {
|
45
|
createOauthUrlForCode(redirectUrl) {
|
62
|
let urlObj = {
|
46
|
let urlObj = {
|
63
|
- appid: WxPayConfig.APPID,
|
47
|
+ appid: WxPayConfig.appId,
|
64
|
redirect_uri: redirectUrl,
|
48
|
redirect_uri: redirectUrl,
|
65
|
response_type: 'code',
|
49
|
response_type: 'code',
|
66
|
scope: 'snsapi_base',
|
50
|
scope: 'snsapi_base',
|
|
@@ -77,8 +61,8 @@ const tools = { |
|
@@ -77,8 +61,8 @@ const tools = { |
77
|
*/
|
61
|
*/
|
78
|
createOauthUrlForOpenid(code) {
|
62
|
createOauthUrlForOpenid(code) {
|
79
|
let urlObj = {
|
63
|
let urlObj = {
|
80
|
- appid: WxPayConfig.APPID,
|
|
|
81
|
- secret: WxPayConfig.APPSECRET,
|
64
|
+ appid: WxPayConfig.appId,
|
|
|
65
|
+ secret: WxPayConfig.appSecret,
|
82
|
code: code,
|
66
|
code: code,
|
83
|
grant_type: 'authorization_code'
|
67
|
grant_type: 'authorization_code'
|
84
|
};
|
68
|
};
|
|
@@ -115,21 +99,38 @@ const tools = { |
|
@@ -115,21 +99,38 @@ const tools = { |
115
|
*/
|
99
|
*/
|
116
|
unifiedOrder(params) {
|
100
|
unifiedOrder(params) {
|
117
|
let unifiedParams = {
|
101
|
let unifiedParams = {
|
118
|
- appid: WxPayConfig.APPID,
|
|
|
119
|
- mch_id: WxPayConfig.MCHID,
|
102
|
+ appid: WxPayConfig.appId,
|
|
|
103
|
+ mch_id: WxPayConfig.mchId,
|
|
|
104
|
+ notify_url: WxPayConfig.notifyUrl,
|
120
|
device_info: 'WEB',
|
105
|
device_info: 'WEB',
|
121
|
- nonce_str: tools.createRandomString(),
|
106
|
+ nonce_str: common.nonceStr(),
|
122
|
body: '有货订单号:' + params.orderCode,
|
107
|
body: '有货订单号:' + params.orderCode,
|
123
|
out_trade_no: 'YOHOBuy_' + params.orderCode,
|
108
|
out_trade_no: 'YOHOBuy_' + params.orderCode,
|
124
|
- total_fee: params.totalFee,
|
109
|
+ total_fee: params.totalFee * 100,
|
125
|
trade_type: 'JSAPI',
|
110
|
trade_type: 'JSAPI',
|
126
|
openid: params.openId,
|
111
|
openid: params.openId,
|
127
|
- sign_type: 'MD5',
|
112
|
+ sign_type: 'MD5'
|
128
|
};
|
113
|
};
|
129
|
-
|
|
|
130
|
- let signStr = md5(sign.raw(unifiedParams) + WxPayConfig.KEY).toUpperCase();
|
114
|
+ let signStr = md5(sign.raw(unifiedParams) + '&key=' + WxPayConfig.key).toUpperCase();
|
131
|
|
115
|
|
132
|
_.assign(unifiedParams, {sign: signStr});
|
116
|
_.assign(unifiedParams, {sign: signStr});
|
|
|
117
|
+
|
|
|
118
|
+ let xml = common.toXml(unifiedParams);
|
|
|
119
|
+
|
|
|
120
|
+ let xmlParams = {
|
|
|
121
|
+ method: 'POST',
|
|
|
122
|
+ uri: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
|
|
|
123
|
+ headers: {
|
|
|
124
|
+ 'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
125
|
+ },
|
|
|
126
|
+ body: xml,
|
|
|
127
|
+ timeout: 1000
|
|
|
128
|
+ };
|
|
|
129
|
+
|
|
|
130
|
+ return rp(xmlParams).then(resResult => {
|
|
|
131
|
+ console.log(resResult);
|
|
|
132
|
+ return common.xml2Obj(resResult);
|
|
|
133
|
+ });
|
133
|
}
|
134
|
}
|
134
|
};
|
135
|
};
|
135
|
|
136
|
|
|
@@ -156,23 +157,27 @@ const Wechat = { |
|
@@ -156,23 +157,27 @@ const Wechat = { |
156
|
*/
|
157
|
*/
|
157
|
pay(user, order, openId) {
|
158
|
pay(user, order, openId) {
|
158
|
co(function* () {
|
159
|
co(function* () {
|
159
|
- let unifiedOrderResult = yield tools.unifiedorder({
|
160
|
+ let unifiedOrderResult = yield tools.unifiedOrder({
|
160
|
orderCode: order.order_code,
|
161
|
orderCode: order.order_code,
|
161
|
totalFee: parseFloat(order.payment_amount),
|
162
|
totalFee: parseFloat(order.payment_amount),
|
162
|
openId: openId
|
163
|
openId: openId
|
163
|
});
|
164
|
});
|
164
|
|
165
|
|
165
|
if (unifiedOrderResult) {
|
166
|
if (unifiedOrderResult) {
|
166
|
- let nonceStr = tools.createRandomString();
|
167
|
+ let nonceStr = common.nonceStr();
|
167
|
|
168
|
|
168
|
- return {
|
169
|
+ let resParams = {
|
169
|
appId: unifiedOrderResult.appid,
|
170
|
appId: unifiedOrderResult.appid,
|
170
|
timeStamp: Date.parse(new Date()),
|
171
|
timeStamp: Date.parse(new Date()),
|
171
|
nonceStr: nonceStr,
|
172
|
nonceStr: nonceStr,
|
172
|
package: 'prepay_id=' + unifiedOrderResult.prepay_id,
|
173
|
package: 'prepay_id=' + unifiedOrderResult.prepay_id,
|
173
|
signType: 'MD5',
|
174
|
signType: 'MD5',
|
174
|
- paySign: '' // TODO 签名算法
|
|
|
175
|
};
|
175
|
};
|
|
|
176
|
+
|
|
|
177
|
+ let paySign = md5(sign.raw(resParams) + '&key=' + WxPayConfig.key).toUpperCase();
|
|
|
178
|
+
|
|
|
179
|
+ _.assign(resParams, {paySign: paySign});
|
|
|
180
|
+ return resParams;
|
176
|
} else {
|
181
|
} else {
|
177
|
return {};
|
182
|
return {};
|
178
|
}
|
183
|
}
|