Showing
5 changed files
with
137 additions
and
45 deletions
@@ -8,10 +8,12 @@ | @@ -8,10 +8,12 @@ | ||
8 | 8 | ||
9 | const mRoot = '../models'; | 9 | const mRoot = '../models'; |
10 | const payModel = require(`${mRoot}/pay`); | 10 | const payModel = require(`${mRoot}/pay`); |
11 | +const payTool = payModel.payTool; | ||
11 | const headerModel = require('../../../doraemon/models/header'); // 头部model | 12 | const headerModel = require('../../../doraemon/models/header'); // 头部model |
12 | const co = require('bluebird').coroutine; | 13 | const co = require('bluebird').coroutine; |
13 | const helpers = global.yoho.helpers; | 14 | const helpers = global.yoho.helpers; |
14 | const Payment = require('../helpers/payment'); | 15 | const Payment = require('../helpers/payment'); |
16 | +const WxPay = require('../helpers/pay/wechat'); | ||
15 | 17 | ||
16 | /** | 18 | /** |
17 | * 支付中心 | 19 | * 支付中心 |
@@ -19,10 +21,12 @@ const Payment = require('../helpers/payment'); | @@ -19,10 +21,12 @@ const Payment = require('../helpers/payment'); | ||
19 | * @param res | 21 | * @param res |
20 | * @param next | 22 | * @param next |
21 | */ | 23 | */ |
22 | -const payCenter = (req, res, next) => { | 24 | +const payCenter = (req, res) => { |
23 | let orderCode = req.query.order_code; | 25 | let orderCode = req.query.order_code; |
24 | let uid = req.user.uid; | 26 | let uid = req.user.uid; |
25 | let sessionKey = req.session.TOKEN; | 27 | let sessionKey = req.session.TOKEN; |
28 | + let userAgent = req.get('User-Agent'); | ||
29 | + let hasWxShare = Boolean(userAgent.match(/MicroMessenger/i) && userAgent.match(/MicroMessenger/i).length > 0); | ||
26 | 30 | ||
27 | if (!orderCode || !uid) { | 31 | if (!orderCode || !uid) { |
28 | res.redirect('/'); | 32 | res.redirect('/'); |
@@ -32,28 +36,51 @@ const payCenter = (req, res, next) => { | @@ -32,28 +36,51 @@ const payCenter = (req, res, next) => { | ||
32 | sessionKey = sessionKey.substr(0, sessionKey.length - 8); | 36 | sessionKey = sessionKey.substr(0, sessionKey.length - 8); |
33 | } | 37 | } |
34 | 38 | ||
35 | - let headerData = headerModel.setNav({ | ||
36 | - navTitle: '支付中心' | ||
37 | - }); | 39 | + co(function* () { |
40 | + let orderDetail = yield payModel.payCenter({ | ||
41 | + orderCode: orderCode, | ||
42 | + uid: uid | ||
43 | + }); | ||
38 | 44 | ||
39 | - let responseData = { | ||
40 | - pageHeader: headerData, | ||
41 | - module: 'cart', | ||
42 | - page: 'pay', | ||
43 | - title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停' | ||
44 | - }; | 45 | + if (hasWxShare) { |
46 | + let openId = req.session['weixinOpenId' + orderCode]; | ||
45 | 47 | ||
46 | - payModel.payCenter({ | ||
47 | - orderCode: orderCode, | ||
48 | - uid: uid, | ||
49 | - sessionKey: sessionKey | ||
50 | - }).then(result => { | ||
51 | - res.render('pay/pay-center', Object.assign(responseData, result, { | ||
52 | - isOldUser: Boolean(req.cookies._isOldUser && req.cookies._isOldUser === '4'), | ||
53 | - })); | ||
54 | - }).catch(next); | 48 | + if (!openId) { |
49 | + let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl); | ||
50 | + | ||
51 | + if (getOpenidResult.openid) { | ||
52 | + openId = getOpenidResult.openid; | ||
53 | + } | ||
54 | + | ||
55 | + if (getOpenidResult.redirectUrl) { | ||
56 | + return res.redirect(getOpenidResult.redirectUrl); | ||
57 | + } | ||
55 | 58 | ||
59 | + if (openId) { | ||
60 | + req.session['weixinOpenId' + orderCode] = openId; | ||
61 | + } | ||
62 | + } | ||
63 | + } | ||
56 | 64 | ||
65 | + let responseData = { | ||
66 | + pageHeader: headerModel.setNav({ | ||
67 | + navTitle: '支付中心' | ||
68 | + }), | ||
69 | + module: 'cart', | ||
70 | + page: 'pay', | ||
71 | + title: '支付中心 | Yoho!Buy有货 | 潮流购物逛不停', | ||
72 | + payAppInfo: payTool.payAppInfo(orderCode), | ||
73 | + orderCode: orderCode, | ||
74 | + hasWxShare: hasWxShare, | ||
75 | + orderTotal: orderDetail.payment_amount || 0, | ||
76 | + orderTotalFormat: parseInt(orderDetail.payment_amount, 10), | ||
77 | + orderCount: payTool.calBuyNumCount(orderDetail.order_goods), | ||
78 | + uid: uid, | ||
79 | + isOldUser: Boolean(req.cookies._isOldUser && req.cookies._isOldUser === '4') | ||
80 | + }; | ||
81 | + | ||
82 | + res.render('pay/pay-center', responseData); | ||
83 | + })(); | ||
57 | }; | 84 | }; |
58 | 85 | ||
59 | /** | 86 | /** |
@@ -72,7 +99,7 @@ const goAlipay = (req, res, next) => { | @@ -72,7 +99,7 @@ const goAlipay = (req, res, next) => { | ||
72 | res.redirect('/'); | 99 | res.redirect('/'); |
73 | } | 100 | } |
74 | 101 | ||
75 | - co(function*() { | 102 | + co(function* () { |
76 | let orderDetail = yield payModel.getOtherDetail({ | 103 | let orderDetail = yield payModel.getOtherDetail({ |
77 | uid: uid, | 104 | uid: uid, |
78 | orderCode: orderCode, | 105 | orderCode: orderCode, |
1 | /* | 1 | /* |
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: mikey.zhaopeng | ||
5 | - * @Last Modified time: 2017-01-03 18:07:44 | 4 | + * @Last Modified by: Targaryen |
5 | + * @Last Modified time: 2017-01-04 14:54:47 | ||
6 | */ | 6 | */ |
7 | 7 | ||
8 | 'use strict'; | 8 | 'use strict'; |
9 | +const config = global.yoho.config; | ||
10 | +const WxPayConfig = config.WxPayConfig; | ||
11 | +const _ = require('lodash'); | ||
12 | +const logger = global.yoho.logger; | ||
13 | +const rp = require('request-promise'); | ||
14 | +const Promise = require('bluebird'); | ||
9 | 15 | ||
10 | /** | 16 | /** |
11 | * 微信支付相关工具类 | 17 | * 微信支付相关工具类 |
12 | */ | 18 | */ |
13 | const tools = { | 19 | const tools = { |
20 | + | ||
21 | + /** | ||
22 | + * 拼接签名字符串 | ||
23 | + */ | ||
24 | + toUrlParams(urlObj) { | ||
25 | + let buff = ''; | ||
26 | + | ||
27 | + _.forEach(urlObj, (value, key) => { | ||
28 | + if (key !== 'sign') { | ||
29 | + buff += key + '=' + value + '&'; | ||
30 | + } | ||
31 | + }); | ||
32 | + | ||
33 | + buff = _.trimEnd(buff, '&'); | ||
34 | + | ||
35 | + return buff; | ||
36 | + }, | ||
37 | + | ||
38 | + /** | ||
39 | + * 构造获取code的url连接 | ||
40 | + */ | ||
14 | createOauthUrlForCode(redirectUrl) { | 41 | createOauthUrlForCode(redirectUrl) { |
15 | - return redirectUrl; | 42 | + let urlObj = { |
43 | + appid: WxPayConfig.APPID, | ||
44 | + redirect_uri: redirectUrl, | ||
45 | + response_type: 'code', | ||
46 | + scope: 'snsapi_base', | ||
47 | + state: 'STATE#wechat_redirect', | ||
48 | + }; | ||
49 | + | ||
50 | + let bizString = tools.toUrlParams(urlObj); | ||
51 | + | ||
52 | + return 'https://open.weixin.qq.com/connect/oauth2/authorize?' + bizString; | ||
16 | }, | 53 | }, |
54 | + | ||
55 | + /** | ||
56 | + * 构造获取open和access_toke的url地址 | ||
57 | + */ | ||
58 | + createOauthUrlForOpenid(code) { | ||
59 | + let urlObj = { | ||
60 | + appid: WxPayConfig.APPID, | ||
61 | + secret: WxPayConfig.APPSECRET, | ||
62 | + code: code, | ||
63 | + grant_type: 'authorization_code' | ||
64 | + }; | ||
65 | + | ||
66 | + let bizString = tools.toUrlParams(urlObj); | ||
67 | + | ||
68 | + return 'https://api.weixin.qq.com/sns/oauth2/access_token?' + bizString; | ||
69 | + }, | ||
70 | + | ||
71 | + /** | ||
72 | + * 通过code从工作平台获取openid机器access_token | ||
73 | + */ | ||
17 | getOpenidFromMp(code) { | 74 | getOpenidFromMp(code) { |
18 | - return code; | 75 | + let uri = tools.createOauthUrlForOpenid(code); |
76 | + | ||
77 | + let rpOption = { | ||
78 | + method: 'POST', | ||
79 | + uri: uri, | ||
80 | + body: { | ||
81 | + some: 'payload' | ||
82 | + }, | ||
83 | + json: true | ||
84 | + }; | ||
85 | + | ||
86 | + return rp(rpOption).then(resultFromWx => { | ||
87 | + return resultFromWx && resultFromWx.openid; | ||
88 | + }).catch(err => { | ||
89 | + logger.error(err); | ||
90 | + }); | ||
19 | } | 91 | } |
20 | }; | 92 | }; |
21 | 93 | ||
22 | // TODO 微信支付 | 94 | // TODO 微信支付 |
23 | const Wechat = { | 95 | const Wechat = { |
24 | - GetOpenid(req, res) { | ||
25 | - let code = req.query.code; | ||
26 | - | 96 | + getOpenid(code, originalUrl) { |
27 | if (!code) { | 97 | if (!code) { |
28 | - let baseUrl = 'http://m.yohobuy.com' + req.originalUrl; | ||
29 | - let url = tools.createOauthUrlForCode(baseUrl); | 98 | + let baseUrl = 'http://m.yohobuy.com' + originalUrl; |
99 | + let redirectUrl = tools.createOauthUrlForCode(baseUrl); | ||
30 | 100 | ||
31 | - res.redirectUrl(url); | 101 | + return Promise.resolve({redirectUrl: redirectUrl}); |
32 | } else { | 102 | } else { |
33 | - let openid = tools.getOpenidFromMp(code); | ||
34 | - | ||
35 | - return openid; | 103 | + return tools.getOpenidFromMp(code).then(openid => { |
104 | + return {openid: openid}; | ||
105 | + }); | ||
36 | } | 106 | } |
37 | } | 107 | } |
38 | }; | 108 | }; |
@@ -301,17 +301,7 @@ const payCenter = (params) => { | @@ -301,17 +301,7 @@ const payCenter = (params) => { | ||
301 | uid: params.uid, | 301 | uid: params.uid, |
302 | orderCode: params.orderCode | 302 | orderCode: params.orderCode |
303 | }).then(result => { | 303 | }).then(result => { |
304 | - let resultData = _.get(result, 'data', {}); | ||
305 | - | ||
306 | - return { | ||
307 | - payAppInfo: payTool.payAppInfo(params.orderCode), | ||
308 | - orderCode: params.orderCode, | ||
309 | - hasWxShare: true, | ||
310 | - orderTotal: resultData.payment_amount || 0, | ||
311 | - orderTotalFormat: parseInt(resultData.payment_amount, 10), | ||
312 | - orderCount: payTool.calBuyNumCount(resultData.order_goods), | ||
313 | - uid: params.uid | ||
314 | - }; | 304 | + return _.get(result, 'data', {}); |
315 | }); | 305 | }); |
316 | }; | 306 | }; |
317 | 307 | ||
@@ -398,6 +388,7 @@ module.exports = { | @@ -398,6 +388,7 @@ module.exports = { | ||
398 | sendPayConfirm, | 388 | sendPayConfirm, |
399 | procOrderData, | 389 | procOrderData, |
400 | payCenter, | 390 | payCenter, |
391 | + payTool, | ||
401 | getPayCod, | 392 | getPayCod, |
402 | getPayAli | 393 | getPayAli |
403 | }; | 394 | }; |
@@ -107,6 +107,10 @@ module.exports = { | @@ -107,6 +107,10 @@ module.exports = { | ||
107 | alipayKey: 'kcxawi9bb07mzh0aq2wcirsf9znusobw', | 107 | alipayKey: 'kcxawi9bb07mzh0aq2wcirsf9znusobw', |
108 | sellerMail: 'zfb@yoho.cn', | 108 | sellerMail: 'zfb@yoho.cn', |
109 | merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code=' | 109 | merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code=' |
110 | + }, | ||
111 | + WxPayConfig: { | ||
112 | + APPID: 'wx75e5a7c0c88e45c2', | ||
113 | + APPSECRET: 'ce21ae4a3f93852279175a167e54509b' | ||
110 | } | 114 | } |
111 | }; | 115 | }; |
112 | 116 |
-
Please register or login to post a comment