Authored by 郭成尧

wechat-backend

... ... @@ -8,10 +8,12 @@
const mRoot = '../models';
const payModel = require(`${mRoot}/pay`);
const payTool = payModel.payTool;
const headerModel = require('../../../doraemon/models/header'); // 头部model
const co = require('bluebird').coroutine;
const helpers = global.yoho.helpers;
const Payment = require('../helpers/payment');
const WxPay = require('../helpers/pay/wechat');
/**
* 支付中心
... ... @@ -19,10 +21,12 @@ const Payment = require('../helpers/payment');
* @param res
* @param next
*/
const payCenter = (req, res, next) => {
const payCenter = (req, res) => {
let orderCode = req.query.order_code;
let uid = req.user.uid;
let sessionKey = req.session.TOKEN;
let userAgent = req.get('User-Agent');
let hasWxShare = Boolean(userAgent.match(/MicroMessenger/i) && userAgent.match(/MicroMessenger/i).length > 0);
if (!orderCode || !uid) {
res.redirect('/');
... ... @@ -32,28 +36,51 @@ const payCenter = (req, res, next) => {
sessionKey = sessionKey.substr(0, sessionKey.length - 8);
}
let headerData = headerModel.setNav({
navTitle: '支付中心'
});
co(function* () {
let orderDetail = yield payModel.payCenter({
orderCode: orderCode,
uid: uid
});
let responseData = {
pageHeader: headerData,
module: 'cart',
page: 'pay',
title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停'
};
if (hasWxShare) {
let openId = req.session['weixinOpenId' + orderCode];
payModel.payCenter({
orderCode: orderCode,
uid: uid,
sessionKey: sessionKey
}).then(result => {
res.render('pay/pay-center', Object.assign(responseData, result, {
isOldUser: Boolean(req.cookies._isOldUser && req.cookies._isOldUser === '4'),
}));
}).catch(next);
if (!openId) {
let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl);
if (getOpenidResult.openid) {
openId = getOpenidResult.openid;
}
if (getOpenidResult.redirectUrl) {
return res.redirect(getOpenidResult.redirectUrl);
}
if (openId) {
req.session['weixinOpenId' + orderCode] = openId;
}
}
}
let responseData = {
pageHeader: headerModel.setNav({
navTitle: '支付中心'
}),
module: 'cart',
page: 'pay',
title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停',
payAppInfo: payTool.payAppInfo(orderCode),
orderCode: orderCode,
hasWxShare: hasWxShare,
orderTotal: orderDetail.payment_amount || 0,
orderTotalFormat: parseInt(orderDetail.payment_amount, 10),
orderCount: payTool.calBuyNumCount(orderDetail.order_goods),
uid: uid,
isOldUser: Boolean(req.cookies._isOldUser && req.cookies._isOldUser === '4')
};
res.render('pay/pay-center', responseData);
})();
};
/**
... ... @@ -72,7 +99,7 @@ const goAlipay = (req, res, next) => {
res.redirect('/');
}
co(function*() {
co(function* () {
let orderDetail = yield payModel.getOtherDetail({
uid: uid,
orderCode: orderCode,
... ...
/*
* @Author: Targaryen
* @Date: 2017-01-03 17:42:41
* @Last Modified by: mikey.zhaopeng
* @Last Modified time: 2017-01-03 18:07:44
* @Last Modified by: Targaryen
* @Last Modified time: 2017-01-04 14:54:47
*/
'use strict';
const config = global.yoho.config;
const WxPayConfig = config.WxPayConfig;
const _ = require('lodash');
const logger = global.yoho.logger;
const rp = require('request-promise');
const Promise = require('bluebird');
/**
* 微信支付相关工具类
*/
const tools = {
/**
* 拼接签名字符串
*/
toUrlParams(urlObj) {
let buff = '';
_.forEach(urlObj, (value, key) => {
if (key !== 'sign') {
buff += key + '=' + value + '&';
}
});
buff = _.trimEnd(buff, '&');
return buff;
},
/**
* 构造获取code的url连接
*/
createOauthUrlForCode(redirectUrl) {
return redirectUrl;
let urlObj = {
appid: WxPayConfig.APPID,
redirect_uri: redirectUrl,
response_type: 'code',
scope: 'snsapi_base',
state: 'STATE#wechat_redirect',
};
let bizString = tools.toUrlParams(urlObj);
return 'https://open.weixin.qq.com/connect/oauth2/authorize?' + bizString;
},
/**
* 构造获取open和access_toke的url地址
*/
createOauthUrlForOpenid(code) {
let urlObj = {
appid: WxPayConfig.APPID,
secret: WxPayConfig.APPSECRET,
code: code,
grant_type: 'authorization_code'
};
let bizString = tools.toUrlParams(urlObj);
return 'https://api.weixin.qq.com/sns/oauth2/access_token?' + bizString;
},
/**
* 通过code从工作平台获取openid机器access_token
*/
getOpenidFromMp(code) {
return code;
let uri = tools.createOauthUrlForOpenid(code);
let rpOption = {
method: 'POST',
uri: uri,
body: {
some: 'payload'
},
json: true
};
return rp(rpOption).then(resultFromWx => {
return resultFromWx && resultFromWx.openid;
}).catch(err => {
logger.error(err);
});
}
};
// TODO 微信支付
const Wechat = {
GetOpenid(req, res) {
let code = req.query.code;
getOpenid(code, originalUrl) {
if (!code) {
let baseUrl = 'http://m.yohobuy.com' + req.originalUrl;
let url = tools.createOauthUrlForCode(baseUrl);
let baseUrl = 'http://m.yohobuy.com' + originalUrl;
let redirectUrl = tools.createOauthUrlForCode(baseUrl);
res.redirectUrl(url);
return Promise.resolve({redirectUrl: redirectUrl});
} else {
let openid = tools.getOpenidFromMp(code);
return openid;
return tools.getOpenidFromMp(code).then(openid => {
return {openid: openid};
});
}
}
};
... ...
/**
* 各种支付的入口
* TAR NOTE: 本项目中仅支持支付宝和微信支付,其他支付方式需调试
* TAR NOTE: 本项目中仅支持支付宝支付,其他支付方式需调试
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/7/22
*/
... ...
... ... @@ -301,17 +301,7 @@ const payCenter = (params) => {
uid: params.uid,
orderCode: params.orderCode
}).then(result => {
let resultData = _.get(result, 'data', {});
return {
payAppInfo: payTool.payAppInfo(params.orderCode),
orderCode: params.orderCode,
hasWxShare: true,
orderTotal: resultData.payment_amount || 0,
orderTotalFormat: parseInt(resultData.payment_amount, 10),
orderCount: payTool.calBuyNumCount(resultData.order_goods),
uid: params.uid
};
return _.get(result, 'data', {});
});
};
... ... @@ -398,6 +388,7 @@ module.exports = {
sendPayConfirm,
procOrderData,
payCenter,
payTool,
getPayCod,
getPayAli
};
... ...
... ... @@ -107,6 +107,10 @@ module.exports = {
alipayKey: 'kcxawi9bb07mzh0aq2wcirsf9znusobw',
sellerMail: 'zfb@yoho.cn',
merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code='
},
WxPayConfig: {
APPID: 'wx75e5a7c0c88e45c2',
APPSECRET: 'ce21ae4a3f93852279175a167e54509b'
}
};
... ...