Authored by 郭成尧

wechat-pay-params-add

@@ -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-02-20 15:20:23 6 + * @Last Modified time: 2017-02-20 17:47:03
7 */ 7 */
8 8
9 'use strict'; 9 'use strict';
@@ -114,6 +114,11 @@ const pay = (req, res, next) => { @@ -114,6 +114,11 @@ const pay = (req, res, next) => {
114 let payment = req.query.payment; 114 let payment = req.query.payment;
115 let paymentCode = common.getPaymentCode(payment); 115 let paymentCode = common.getPaymentCode(payment);
116 let openId = req.cookies['weixinOpenId' + orderCode]; 116 let openId = req.cookies['weixinOpenId' + orderCode];
  117 + let ip = req.ip;
  118 +
  119 + if (ip.substr(0, 7) === '::ffff:') {
  120 + ip = ip.substr(7);
  121 + }
117 122
118 if (!orderCode || !uid) { 123 if (!orderCode || !uid) {
119 return res.redirect('/'); 124 return res.redirect('/');
@@ -144,7 +149,8 @@ const pay = (req, res, next) => { @@ -144,7 +149,8 @@ const pay = (req, res, next) => {
144 149
145 Payment.pay(user, orderDetail.data, payment, { 150 Payment.pay(user, orderDetail.data, payment, {
146 protocol: req.protocol, 151 protocol: req.protocol,
147 - openId: openId 152 + openId: openId,
  153 + ip: ip
148 }).then(result => { 154 }).then(result => {
149 if (result && paymentCode === payModel.payments.wechat) { 155 if (result && paymentCode === payModel.payments.wechat) {
150 return res.json(result); 156 return res.json(result);
@@ -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-02-20 10:15:03 5 + * @Last Modified time: 2017-02-20 17:46:16
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 moment = require('moment');
18 const common = require('./common'); 19 const common = require('./common');
19 20
20 /** 21 /**
@@ -108,9 +109,13 @@ const tools = { @@ -108,9 +109,13 @@ const tools = {
108 out_trade_no: 'YOHOBuy_' + params.orderCode, 109 out_trade_no: 'YOHOBuy_' + params.orderCode,
109 total_fee: params.totalFee * 100, 110 total_fee: params.totalFee * 100,
110 trade_type: 'JSAPI', 111 trade_type: 'JSAPI',
  112 + time_start: moment().format('YYYYMMDDHHmmss'),
  113 + time_expire: moment().add(2, 'hours').format('YYYYMMDDHHmmss'),
  114 + spbill_create_ip: params.ip,
111 openid: params.openId, 115 openid: params.openId,
112 sign_type: 'MD5' 116 sign_type: 'MD5'
113 }; 117 };
  118 +
114 let signStr = md5(sign.raw(unifiedParams) + '&key=' + WxPayConfig.key).toUpperCase(); 119 let signStr = md5(sign.raw(unifiedParams) + '&key=' + WxPayConfig.key).toUpperCase();
115 120
116 _.assign(unifiedParams, { sign: signStr }); 121 _.assign(unifiedParams, { sign: signStr });
@@ -158,13 +163,14 @@ const Wechat = { @@ -158,13 +163,14 @@ const Wechat = {
158 /** 163 /**
159 * 异步拉起微信支付相关处理 164 * 异步拉起微信支付相关处理
160 */ 165 */
161 - pay(user, order, openId) { 166 + pay(user, order, openId, ip) {
162 167
163 return co(function* () { 168 return co(function* () {
164 let unifiedOrderResult = yield tools.unifiedOrder({ 169 let unifiedOrderResult = yield tools.unifiedOrder({
165 orderCode: order.order_code, 170 orderCode: order.order_code,
166 totalFee: parseFloat(order.payment_amount), 171 totalFee: parseFloat(order.payment_amount),
167 - openId: openId 172 + openId: openId,
  173 + ip: ip
168 }); 174 });
169 175
170 if (unifiedOrderResult) { 176 if (unifiedOrderResult) {
@@ -20,7 +20,7 @@ const Payment = { @@ -20,7 +20,7 @@ const Payment = {
20 20
21 /** 21 /**
22 * 统一支付入口 22 * 统一支付入口
23 - * reqParams: 需要从 controller 传递的参数,支付宝需要 req.protocol,微信需要 openId 23 + * reqParams: 需要从 controller 传递的参数,支付宝需要 req.protocol,微信需要 openId、ip
24 */ 24 */
25 pay(user, order, payType, reqParams) { 25 pay(user, order, payType, reqParams) {
26 return co(function*() { 26 return co(function*() {
@@ -54,7 +54,7 @@ const Payment = { @@ -54,7 +54,7 @@ const Payment = {
54 } 54 }
55 55
56 if (method === PayModel.payments.wechat) { 56 if (method === PayModel.payments.wechat) {
57 - result = yield Wechat.pay(user, order, reqParams.openId); 57 + result = yield Wechat.pay(user, order, reqParams.openId, reqParams.ip);
58 } else { 58 } else {
59 payInfo = yield PayModel.getPaymentInfo(method); 59 payInfo = yield PayModel.getPaymentInfo(method);
60 60