Showing
6 changed files
with
103 additions
and
33 deletions
1 | -/** | 1 | +/* |
2 | * 支付 | 2 | * 支付 |
3 | - * @author: jing.li<jing.li@yoho.cn> | ||
4 | - * @date: 2016/10/25 | 3 | + * @Author: Targaryen |
4 | + * @Date: 2017-01-04 15:17:51 | ||
5 | + * @Last Modified by: Targaryen | ||
6 | + * @Last Modified time: 2017-01-04 17:59:37 | ||
5 | */ | 7 | */ |
6 | 8 | ||
7 | 'use strict'; | 9 | 'use strict'; |
@@ -94,6 +96,7 @@ const goAlipay = (req, res, next) => { | @@ -94,6 +96,7 @@ const goAlipay = (req, res, next) => { | ||
94 | let uid = req.user.uid; | 96 | let uid = req.user.uid; |
95 | let sessionKey = req.session.TOKEN; | 97 | let sessionKey = req.session.TOKEN; |
96 | let payment = req.query.payment; | 98 | let payment = req.query.payment; |
99 | + let openId = req.session['weixinOpenId' . orderCode]; | ||
97 | 100 | ||
98 | if (!orderCode || !uid || !sessionKey) { | 101 | if (!orderCode || !uid || !sessionKey) { |
99 | res.redirect('/'); | 102 | res.redirect('/'); |
@@ -122,7 +125,10 @@ const goAlipay = (req, res, next) => { | @@ -122,7 +125,10 @@ const goAlipay = (req, res, next) => { | ||
122 | }).redirect(url); | 125 | }).redirect(url); |
123 | } | 126 | } |
124 | 127 | ||
125 | - Payment.pay(user, orderDetail.data, payment, req.protocol).then(result => { | 128 | + Payment.pay(user, orderDetail.data, payment, { |
129 | + protocol: req.protocol, | ||
130 | + openId: openId | ||
131 | + }).then(result => { | ||
126 | if (result && result.data && result.data.href) { | 132 | if (result && result.data && result.data.href) { |
127 | res.redirect(result.data.href); | 133 | res.redirect(result.data.href); |
128 | } else { | 134 | } else { |
@@ -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-04 14:54:47 | 5 | + * @Last Modified time: 2017-01-04 18:19:06 |
6 | */ | 6 | */ |
7 | 7 | ||
8 | 'use strict'; | 8 | 'use strict'; |
@@ -12,6 +12,7 @@ const _ = require('lodash'); | @@ -12,6 +12,7 @@ const _ = require('lodash'); | ||
12 | const logger = global.yoho.logger; | 12 | const logger = global.yoho.logger; |
13 | const rp = require('request-promise'); | 13 | const rp = require('request-promise'); |
14 | const Promise = require('bluebird'); | 14 | const Promise = require('bluebird'); |
15 | +const co = Promise.coroutine; | ||
15 | 16 | ||
16 | /** | 17 | /** |
17 | * 微信支付相关工具类 | 18 | * 微信支付相关工具类 |
@@ -36,6 +37,23 @@ const tools = { | @@ -36,6 +37,23 @@ const tools = { | ||
36 | }, | 37 | }, |
37 | 38 | ||
38 | /** | 39 | /** |
40 | + * 生成指定长度的随机字符串 | ||
41 | + */ | ||
42 | + createRandomString(len) { | ||
43 | + let strlen = len || 32; | ||
44 | + let originString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
45 | + let resultString = ''; | ||
46 | + | ||
47 | + for (let i = 0; i < strlen; i++) { | ||
48 | + let position = parseInt(100 * Math.random(), 0) % 62; | ||
49 | + | ||
50 | + resultString += originString.slice(position - 1, position); | ||
51 | + } | ||
52 | + | ||
53 | + return resultString; | ||
54 | + }, | ||
55 | + | ||
56 | + /** | ||
39 | * 构造获取code的url连接 | 57 | * 构造获取code的url连接 |
40 | */ | 58 | */ |
41 | createOauthUrlForCode(redirectUrl) { | 59 | createOauthUrlForCode(redirectUrl) { |
@@ -88,22 +106,72 @@ const tools = { | @@ -88,22 +106,72 @@ const tools = { | ||
88 | }).catch(err => { | 106 | }).catch(err => { |
89 | logger.error(err); | 107 | logger.error(err); |
90 | }); | 108 | }); |
109 | + }, | ||
110 | + | ||
111 | + /** | ||
112 | + * 微信统一下单接口 | ||
113 | + */ | ||
114 | + unifiedOrder(params) { | ||
115 | + let unifiedParams = { | ||
116 | + appid: WxPayConfig.APPID, | ||
117 | + mch_id: WxPayConfig.MCHID, | ||
118 | + device_info: 'WEB', | ||
119 | + nonce_str: tools.createRandomString(), | ||
120 | + body: '有货订单号:' + params.orderCode, | ||
121 | + out_trade_no: 'YOHOBuy_' + params.orderCode, | ||
122 | + total_fee: params.totalFee, | ||
123 | + trade_type: 'JSAPI', | ||
124 | + openid: params.openId, | ||
125 | + sign: '', // TODO 签名算法 | ||
126 | + sign_type: 'MD5', | ||
127 | + }; | ||
91 | } | 128 | } |
92 | }; | 129 | }; |
93 | 130 | ||
94 | // TODO 微信支付 | 131 | // TODO 微信支付 |
95 | const Wechat = { | 132 | const Wechat = { |
133 | + /** | ||
134 | + * 支付中心微信支付相关处理 | ||
135 | + */ | ||
96 | getOpenid(code, originalUrl) { | 136 | getOpenid(code, originalUrl) { |
97 | if (!code) { | 137 | if (!code) { |
98 | let baseUrl = 'http://m.yohobuy.com' + originalUrl; | 138 | let baseUrl = 'http://m.yohobuy.com' + originalUrl; |
99 | let redirectUrl = tools.createOauthUrlForCode(baseUrl); | 139 | let redirectUrl = tools.createOauthUrlForCode(baseUrl); |
100 | 140 | ||
101 | - return Promise.resolve({redirectUrl: redirectUrl}); | 141 | + return Promise.resolve({ redirectUrl: redirectUrl }); |
102 | } else { | 142 | } else { |
103 | return tools.getOpenidFromMp(code).then(openid => { | 143 | return tools.getOpenidFromMp(code).then(openid => { |
104 | - return {openid: openid}; | 144 | + return { openid: openid }; |
105 | }); | 145 | }); |
106 | } | 146 | } |
147 | + }, | ||
148 | + | ||
149 | + /** | ||
150 | + * 异步拉起微信支付相关处理 | ||
151 | + */ | ||
152 | + pay(user, order, openId) { | ||
153 | + co(function* () { | ||
154 | + let unifiedOrderResult = yield tools.unifiedorder({ | ||
155 | + orderCode: order.order_code, | ||
156 | + totalFee: parseFloat(order.payment_amount), | ||
157 | + openId: openId | ||
158 | + }); | ||
159 | + | ||
160 | + if (unifiedOrderResult) { | ||
161 | + let nonceStr = tools.createRandomString(); | ||
162 | + | ||
163 | + return { | ||
164 | + appId: unifiedOrderResult.appid, | ||
165 | + timeStamp: Date.parse(new Date()), | ||
166 | + nonceStr: nonceStr, | ||
167 | + package: 'prepay_id=' + unifiedOrderResult.prepay_id, | ||
168 | + signType: 'MD5', | ||
169 | + paySign: '' // TODO 签名算法 | ||
170 | + }; | ||
171 | + } else { | ||
172 | + return {}; | ||
173 | + } | ||
174 | + })(); | ||
107 | } | 175 | } |
108 | }; | 176 | }; |
109 | 177 |
@@ -16,7 +16,12 @@ const co = Promise.coroutine; | @@ -16,7 +16,12 @@ const co = Promise.coroutine; | ||
16 | const logger = global.yoho.logger; | 16 | const logger = global.yoho.logger; |
17 | 17 | ||
18 | const Payment = { | 18 | const Payment = { |
19 | - pay(user, order, payType, protocol) { | 19 | + |
20 | + /** | ||
21 | + * 统一支付入口 | ||
22 | + * reqParams: 需要从 controller 传递的参数,支付宝需要 req.protocol,微信需要 openId | ||
23 | + */ | ||
24 | + pay(user, order, payType, reqParams) { | ||
20 | return co(function*() { | 25 | return co(function*() { |
21 | let result = { | 26 | let result = { |
22 | code: 400, | 27 | code: 400, |
@@ -48,8 +53,7 @@ const Payment = { | @@ -48,8 +53,7 @@ const Payment = { | ||
48 | let method = paymentPars[0] * 1; | 53 | let method = paymentPars[0] * 1; |
49 | 54 | ||
50 | if (method === PayModel.payments.wechat) { | 55 | if (method === PayModel.payments.wechat) { |
51 | - // 如果是微信支付,不需要调用获取支付方式详情接口 | ||
52 | - result = yield Wechat.pay(user, order, { id: PayModel.payments.wechat }); | 56 | + result = yield Wechat.pay(user, order); |
53 | } else { | 57 | } else { |
54 | payInfo = yield PayModel.getPaymentInfo(method); | 58 | payInfo = yield PayModel.getPaymentInfo(method); |
55 | 59 | ||
@@ -59,12 +63,12 @@ const Payment = { | @@ -59,12 +63,12 @@ const Payment = { | ||
59 | 63 | ||
60 | switch (payInfo.id) { | 64 | switch (payInfo.id) { |
61 | case PayModel.payments.alipay: | 65 | case PayModel.payments.alipay: |
62 | - result = Alipay.pay(user, order, payInfo, protocol); | 66 | + result = Alipay.pay(user, order, payInfo, reqParams.protocol); |
63 | break; | 67 | break; |
64 | case PayModel.payments.alibank: | 68 | case PayModel.payments.alibank: |
65 | bankCode = paymentPars[1]; | 69 | bankCode = paymentPars[1]; |
66 | payInfo.bankCode = bankCode; // 设置默认银行 | 70 | payInfo.bankCode = bankCode; // 设置默认银行 |
67 | - result = Alibank.pay(user, order, payInfo, protocol); | 71 | + result = Alibank.pay(user, order, payInfo, reqParams.protocol); |
68 | break; | 72 | break; |
69 | default: | 73 | default: |
70 | break; | 74 | break; |
@@ -27,10 +27,7 @@ const payments = { | @@ -27,10 +27,7 @@ const payments = { | ||
27 | const _getBanner = (param) => { | 27 | const _getBanner = (param) => { |
28 | return serviceAPI.get('operations/api/v5/resource/get', { | 28 | return serviceAPI.get('operations/api/v5/resource/get', { |
29 | content_code: param.contentCode | 29 | content_code: param.contentCode |
30 | - }, { | ||
31 | - code: 200 | ||
32 | - }).then((result) => { | ||
33 | - | 30 | + }, { code: 200 }).then((result) => { |
34 | result = result.data; | 31 | result = result.data; |
35 | 32 | ||
36 | return result; | 33 | return result; |
@@ -46,10 +43,7 @@ const _getOthersBuy2 = (param) => { | @@ -46,10 +43,7 @@ const _getOthersBuy2 = (param) => { | ||
46 | rec_pos: '100005', | 43 | rec_pos: '100005', |
47 | limit: 2, | 44 | limit: 2, |
48 | client_id: param.client_id | 45 | client_id: param.client_id |
49 | - }, { | ||
50 | - code: 200 | ||
51 | - }).then((result) => { | ||
52 | - | 46 | + }, { code: 200 }).then((result) => { |
53 | if (result && result.data && result.data.product_list) { | 47 | if (result && result.data && result.data.product_list) { |
54 | return productProcess.processProductList(result.data.product_list); | 48 | return productProcess.processProductList(result.data.product_list); |
55 | } | 49 | } |
@@ -64,12 +58,8 @@ const _getOtherDetail = (param) => { | @@ -64,12 +58,8 @@ const _getOtherDetail = (param) => { | ||
64 | uid: param.uid, | 58 | uid: param.uid, |
65 | order_code: param.orderCode, | 59 | order_code: param.orderCode, |
66 | session_key: param.sessionKey | 60 | session_key: param.sessionKey |
67 | - }, { | ||
68 | - code: 200 | ||
69 | - }).then((result) => { | ||
70 | - | 61 | + }, { code: 200 }).then(result => { |
71 | return result; | 62 | return result; |
72 | - | ||
73 | }); | 63 | }); |
74 | }; | 64 | }; |
75 | 65 | ||
@@ -98,7 +88,7 @@ const _getOthersBuy = (param) => { | @@ -98,7 +88,7 @@ const _getOthersBuy = (param) => { | ||
98 | * @param id | 88 | * @param id |
99 | */ | 89 | */ |
100 | const getBankByOrder = (id) => { | 90 | const getBankByOrder = (id) => { |
101 | - return co(function*() { | 91 | + return co(function* () { |
102 | let result = yield payApi.getBankByOrder(id); | 92 | let result = yield payApi.getBankByOrder(id); |
103 | 93 | ||
104 | if (result && result.code === 200 && result.data) { | 94 | if (result && result.code === 200 && result.data) { |
@@ -115,7 +105,7 @@ const getBankByOrder = (id) => { | @@ -115,7 +105,7 @@ const getBankByOrder = (id) => { | ||
115 | * @param bankCode | 105 | * @param bankCode |
116 | */ | 106 | */ |
117 | const setOrderPayBank = (code, payment, bankCode) => { | 107 | const setOrderPayBank = (code, payment, bankCode) => { |
118 | - return co(function*() { | 108 | + return co(function* () { |
119 | let data = yield payApi.setOrderPayBank(code, payment, bankCode); | 109 | let data = yield payApi.setOrderPayBank(code, payment, bankCode); |
120 | 110 | ||
121 | return data; | 111 | return data; |
@@ -127,7 +117,7 @@ const setOrderPayBank = (code, payment, bankCode) => { | @@ -127,7 +117,7 @@ const setOrderPayBank = (code, payment, bankCode) => { | ||
127 | * @param id | 117 | * @param id |
128 | */ | 118 | */ |
129 | const getPaymentInfo = (id) => { | 119 | const getPaymentInfo = (id) => { |
130 | - return co(function*() { | 120 | + return co(function* () { |
131 | let result = yield payApi.getPaymentInfo(id); | 121 | let result = yield payApi.getPaymentInfo(id); |
132 | 122 | ||
133 | if (result && result.code === 200 && result.data) { | 123 | if (result && result.code === 200 && result.data) { |
@@ -144,7 +134,7 @@ const getPaymentInfo = (id) => { | @@ -144,7 +134,7 @@ const getPaymentInfo = (id) => { | ||
144 | * @param uid | 134 | * @param uid |
145 | */ | 135 | */ |
146 | const sendPayConfirm = (code, payment, uid) => { | 136 | const sendPayConfirm = (code, payment, uid) => { |
147 | - return co(function*() { | 137 | + return co(function* () { |
148 | let data = yield payApi.sendPayConfirm(code, payment, uid); | 138 | let data = yield payApi.sendPayConfirm(code, payment, uid); |
149 | 139 | ||
150 | return data; | 140 | return data; |
@@ -174,7 +164,7 @@ const updateOrderPayment = (code, payment, uid) => { | @@ -174,7 +164,7 @@ const updateOrderPayment = (code, payment, uid) => { | ||
174 | * @param bankCode | 164 | * @param bankCode |
175 | */ | 165 | */ |
176 | const updateOrderPayBank = (code, payment, bankCode) => { | 166 | const updateOrderPayBank = (code, payment, bankCode) => { |
177 | - return co(function*() { | 167 | + return co(function* () { |
178 | let data = yield payApi.updateOrderPayBank(code, payment, bankCode); | 168 | let data = yield payApi.updateOrderPayBank(code, payment, bankCode); |
179 | 169 | ||
180 | return data; | 170 | return data; |
@@ -188,7 +178,7 @@ const updateOrderPayBank = (code, payment, bankCode) => { | @@ -188,7 +178,7 @@ const updateOrderPayBank = (code, payment, bankCode) => { | ||
188 | * @param sessionKey | 178 | * @param sessionKey |
189 | */ | 179 | */ |
190 | const procOrderData = (payResult, uid, sessionKey) => { | 180 | const procOrderData = (payResult, uid, sessionKey) => { |
191 | - return co(function*() { | 181 | + return co(function* () { |
192 | let orderCode = payResult.orderCode; | 182 | let orderCode = payResult.orderCode; |
193 | let result = { code: 400, message: '' }; | 183 | let result = { code: 400, message: '' }; |
194 | 184 |
@@ -110,6 +110,7 @@ module.exports = { | @@ -110,6 +110,7 @@ module.exports = { | ||
110 | }, | 110 | }, |
111 | WxPayConfig: { | 111 | WxPayConfig: { |
112 | APPID: 'wx75e5a7c0c88e45c2', | 112 | APPID: 'wx75e5a7c0c88e45c2', |
113 | + MCHID: '1227694201', | ||
113 | APPSECRET: 'ce21ae4a3f93852279175a167e54509b' | 114 | APPSECRET: 'ce21ae4a3f93852279175a167e54509b' |
114 | } | 115 | } |
115 | }; | 116 | }; |
@@ -88,9 +88,10 @@ function callpay(orderCode) { | @@ -88,9 +88,10 @@ function callpay(orderCode) { | ||
88 | } else { | 88 | } else { |
89 | $.ajax({ | 89 | $.ajax({ |
90 | type: 'GET', | 90 | type: 'GET', |
91 | - url: '/shopping/pay/wechatwapapi', | 91 | + url: '/cart/index/new/pay/goalipay', |
92 | data: { | 92 | data: { |
93 | - order_code: orderCode | 93 | + order_code: orderCode, |
94 | + payment: '22_platform', | ||
94 | }, | 95 | }, |
95 | dataType: 'json', | 96 | dataType: 'json', |
96 | success: function(res) { | 97 | success: function(res) { |
-
Please register or login to post a comment