Authored by 郭成尧

unified-order-post-ok

... ... @@ -3,7 +3,7 @@
* @Author: Targaryen
* @Date: 2017-01-04 15:17:51
* @Last Modified by: Targaryen
* @Last Modified time: 2017-01-04 17:59:37
* @Last Modified time: 2017-01-09 15:42:43
*/
'use strict';
... ... @@ -45,7 +45,7 @@ const payCenter = (req, res) => {
});
if (hasWxShare) {
let openId = req.session['weixinOpenId' + orderCode];
let openId = req.cookies['weixinOpenId' + orderCode];
if (!openId) {
let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl);
... ... @@ -59,7 +59,10 @@ const payCenter = (req, res) => {
}
if (openId) {
req.session['weixinOpenId' + orderCode] = openId;
res.cookie('weixinOpenId' + orderCode, openId, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 24 * 60 * 60 * 1000)
});
}
}
}
... ... @@ -96,7 +99,7 @@ const goAlipay = (req, res, next) => {
let uid = req.user.uid;
let sessionKey = req.session.TOKEN;
let payment = req.query.payment;
let openId = req.session['weixinOpenId' . orderCode];
let openId = req.cookies['weixinOpenId' + orderCode];
if (!orderCode || !uid || !sessionKey) {
res.redirect('/');
... ...
... ... @@ -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 {};
}
... ...
... ... @@ -53,7 +53,7 @@ const Payment = {
let method = paymentPars[0] * 1;
if (method === PayModel.payments.wechat) {
result = yield Wechat.pay(user, order);
result = yield Wechat.pay(user, order, reqParams.openId);
} else {
payInfo = yield PayModel.getPaymentInfo(method);
... ...
... ... @@ -19,7 +19,7 @@ const logger = global.yoho.logger;
// 支付方式
const payments = {
alipay: 33,
wechat: 36,
wechat: 22,
alibank: 42
};
... ...
... ... @@ -109,10 +109,11 @@ module.exports = {
merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code='
},
WxPayConfig: {
APPID: 'wx75e5a7c0c88e45c2',
MCHID: '1227694201',
KEY: '7e6f3307b64cc87c79c472814b88f7fb',
APPSECRET: 'ce21ae4a3f93852279175a167e54509b'
appId: 'wx75e5a7c0c88e45c2',
mchId: '1227694201',
key: '7e6f3307b64cc87c79c472814b88f7fb',
appSecret: 'ce21ae4a3f93852279175a167e54509b',
notifyUrl: 'payment/alipay_notify',
}
};
... ...