...
|
...
|
@@ -2,7 +2,7 @@ |
|
|
* @Author: Targaryen
|
|
|
* @Date: 2017-01-03 17:42:41
|
|
|
* @Last Modified by: Targaryen
|
|
|
* @Last Modified time: 2017-01-05 13:52:47
|
|
|
* @Last Modified time: 2017-01-09 16:12:07
|
|
|
*/
|
|
|
|
|
|
'use strict';
|
...
|
...
|
@@ -15,6 +15,7 @@ const Promise = require('bluebird'); |
|
|
const co = Promise.coroutine;
|
|
|
const sign = require('./sign');
|
|
|
const md5 = require('md5');
|
|
|
const common = require('./common');
|
|
|
|
|
|
/**
|
|
|
* 微信支付相关工具类
|
...
|
...
|
@@ -39,28 +40,11 @@ const tools = { |
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 生成指定长度的随机字符串
|
|
|
*/
|
|
|
createRandomString(len) {
|
|
|
let strlen = len || 32;
|
|
|
let originString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
let resultString = '';
|
|
|
|
|
|
for (let i = 0; i < strlen; i++) {
|
|
|
let position = parseInt(100 * Math.random(), 0) % 62;
|
|
|
|
|
|
resultString += originString.slice(position - 1, position);
|
|
|
}
|
|
|
|
|
|
return resultString;
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 构造获取code的url连接
|
|
|
*/
|
|
|
createOauthUrlForCode(redirectUrl) {
|
|
|
let urlObj = {
|
|
|
appid: WxPayConfig.APPID,
|
|
|
appid: WxPayConfig.appId,
|
|
|
redirect_uri: redirectUrl,
|
|
|
response_type: 'code',
|
|
|
scope: 'snsapi_base',
|
...
|
...
|
@@ -77,8 +61,8 @@ const tools = { |
|
|
*/
|
|
|
createOauthUrlForOpenid(code) {
|
|
|
let urlObj = {
|
|
|
appid: WxPayConfig.APPID,
|
|
|
secret: WxPayConfig.APPSECRET,
|
|
|
appid: WxPayConfig.appId,
|
|
|
secret: WxPayConfig.appSecret,
|
|
|
code: code,
|
|
|
grant_type: 'authorization_code'
|
|
|
};
|
...
|
...
|
@@ -115,21 +99,38 @@ const tools = { |
|
|
*/
|
|
|
unifiedOrder(params) {
|
|
|
let unifiedParams = {
|
|
|
appid: WxPayConfig.APPID,
|
|
|
mch_id: WxPayConfig.MCHID,
|
|
|
appid: WxPayConfig.appId,
|
|
|
mch_id: WxPayConfig.mchId,
|
|
|
notify_url: WxPayConfig.notifyUrl,
|
|
|
device_info: 'WEB',
|
|
|
nonce_str: tools.createRandomString(),
|
|
|
nonce_str: common.nonceStr(),
|
|
|
body: '有货订单号:' + params.orderCode,
|
|
|
out_trade_no: 'YOHOBuy_' + params.orderCode,
|
|
|
total_fee: params.totalFee,
|
|
|
total_fee: params.totalFee * 100,
|
|
|
trade_type: 'JSAPI',
|
|
|
openid: params.openId,
|
|
|
sign_type: 'MD5',
|
|
|
sign_type: 'MD5'
|
|
|
};
|
|
|
|
|
|
let signStr = md5(sign.raw(unifiedParams) + WxPayConfig.KEY).toUpperCase();
|
|
|
let signStr = md5(sign.raw(unifiedParams) + '&key=' + WxPayConfig.key).toUpperCase();
|
|
|
|
|
|
_.assign(unifiedParams, {sign: signStr});
|
|
|
|
|
|
let xml = common.toXml(unifiedParams);
|
|
|
|
|
|
let xmlParams = {
|
|
|
method: 'POST',
|
|
|
uri: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
},
|
|
|
body: xml,
|
|
|
timeout: 1000
|
|
|
};
|
|
|
|
|
|
return rp(xmlParams).then(resResult => {
|
|
|
console.log(resResult);
|
|
|
return common.xml2Obj(resResult);
|
|
|
});
|
|
|
}
|
|
|
};
|
|
|
|
...
|
...
|
@@ -156,23 +157,27 @@ const Wechat = { |
|
|
*/
|
|
|
pay(user, order, openId) {
|
|
|
co(function* () {
|
|
|
let unifiedOrderResult = yield tools.unifiedorder({
|
|
|
let unifiedOrderResult = yield tools.unifiedOrder({
|
|
|
orderCode: order.order_code,
|
|
|
totalFee: parseFloat(order.payment_amount),
|
|
|
openId: openId
|
|
|
});
|
|
|
|
|
|
if (unifiedOrderResult) {
|
|
|
let nonceStr = tools.createRandomString();
|
|
|
let nonceStr = common.nonceStr();
|
|
|
|
|
|
return {
|
|
|
let resParams = {
|
|
|
appId: unifiedOrderResult.appid,
|
|
|
timeStamp: Date.parse(new Date()),
|
|
|
nonceStr: nonceStr,
|
|
|
package: 'prepay_id=' + unifiedOrderResult.prepay_id,
|
|
|
signType: 'MD5',
|
|
|
paySign: '' // TODO 签名算法
|
|
|
};
|
|
|
|
|
|
let paySign = md5(sign.raw(resParams) + '&key=' + WxPayConfig.key).toUpperCase();
|
|
|
|
|
|
_.assign(resParams, {paySign: paySign});
|
|
|
return resParams;
|
|
|
} else {
|
|
|
return {};
|
|
|
}
|
...
|
...
|
|