Authored by 郭成尧

unified-order-post-ok

@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 * @Author: Targaryen 3 * @Author: Targaryen
4 * @Date: 2017-01-04 15:17:51 4 * @Date: 2017-01-04 15:17:51
5 * @Last Modified by: Targaryen 5 * @Last Modified by: Targaryen
6 - * @Last Modified time: 2017-01-04 17:59:37 6 + * @Last Modified time: 2017-01-09 15:42:43
7 */ 7 */
8 8
9 'use strict'; 9 'use strict';
@@ -45,7 +45,7 @@ const payCenter = (req, res) => { @@ -45,7 +45,7 @@ const payCenter = (req, res) => {
45 }); 45 });
46 46
47 if (hasWxShare) { 47 if (hasWxShare) {
48 - let openId = req.session['weixinOpenId' + orderCode]; 48 + let openId = req.cookies['weixinOpenId' + orderCode];
49 49
50 if (!openId) { 50 if (!openId) {
51 let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl); 51 let getOpenidResult = yield WxPay.getOpenid(req.query.code, req.originalUrl);
@@ -59,7 +59,10 @@ const payCenter = (req, res) => { @@ -59,7 +59,10 @@ const payCenter = (req, res) => {
59 } 59 }
60 60
61 if (openId) { 61 if (openId) {
62 - req.session['weixinOpenId' + orderCode] = openId; 62 + res.cookie('weixinOpenId' + orderCode, openId, {
  63 + domain: 'yohobuy.com',
  64 + expires: new Date(Date.now() + 24 * 60 * 60 * 1000)
  65 + });
63 } 66 }
64 } 67 }
65 } 68 }
@@ -96,7 +99,7 @@ const goAlipay = (req, res, next) => { @@ -96,7 +99,7 @@ const goAlipay = (req, res, next) => {
96 let uid = req.user.uid; 99 let uid = req.user.uid;
97 let sessionKey = req.session.TOKEN; 100 let sessionKey = req.session.TOKEN;
98 let payment = req.query.payment; 101 let payment = req.query.payment;
99 - let openId = req.session['weixinOpenId' . orderCode]; 102 + let openId = req.cookies['weixinOpenId' + orderCode];
100 103
101 if (!orderCode || !uid || !sessionKey) { 104 if (!orderCode || !uid || !sessionKey) {
102 res.redirect('/'); 105 res.redirect('/');
@@ -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 }
@@ -53,7 +53,7 @@ const Payment = { @@ -53,7 +53,7 @@ const Payment = {
53 let method = paymentPars[0] * 1; 53 let method = paymentPars[0] * 1;
54 54
55 if (method === PayModel.payments.wechat) { 55 if (method === PayModel.payments.wechat) {
56 - result = yield Wechat.pay(user, order); 56 + result = yield Wechat.pay(user, order, reqParams.openId);
57 } else { 57 } else {
58 payInfo = yield PayModel.getPaymentInfo(method); 58 payInfo = yield PayModel.getPaymentInfo(method);
59 59
@@ -19,7 +19,7 @@ const logger = global.yoho.logger; @@ -19,7 +19,7 @@ const logger = global.yoho.logger;
19 // 支付方式 19 // 支付方式
20 const payments = { 20 const payments = {
21 alipay: 33, 21 alipay: 33,
22 - wechat: 36, 22 + wechat: 22,
23 alibank: 42 23 alibank: 42
24 }; 24 };
25 25
@@ -109,10 +109,11 @@ module.exports = { @@ -109,10 +109,11 @@ module.exports = {
109 merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code=' 109 merchantUrl: 'http://m.yohobuy.com/home/orders/detail?order_code='
110 }, 110 },
111 WxPayConfig: { 111 WxPayConfig: {
112 - APPID: 'wx75e5a7c0c88e45c2',  
113 - MCHID: '1227694201',  
114 - KEY: '7e6f3307b64cc87c79c472814b88f7fb',  
115 - APPSECRET: 'ce21ae4a3f93852279175a167e54509b' 112 + appId: 'wx75e5a7c0c88e45c2',
  113 + mchId: '1227694201',
  114 + key: '7e6f3307b64cc87c79c472814b88f7fb',
  115 + appSecret: 'ce21ae4a3f93852279175a167e54509b',
  116 + notifyUrl: 'payment/alipay_notify',
116 } 117 }
117 }; 118 };
118 119