Authored by 郝肖肖

网银支付

@@ -86,7 +86,7 @@ const online = (req, res, next) => { @@ -86,7 +86,7 @@ const online = (req, res, next) => {
86 */ 86 */
87 const toPay = (req, res, next) => { 87 const toPay = (req, res, next) => {
88 let orderCode = req.body.code; 88 let orderCode = req.body.code;
89 - let method = req.body.method; 89 + let payType = req.body.payType;
90 let uid = req.user.uid; 90 let uid = req.user.uid;
91 let user = req.user; 91 let user = req.user;
92 92
@@ -95,13 +95,11 @@ const toPay = (req, res, next) => { @@ -95,13 +95,11 @@ const toPay = (req, res, next) => {
95 return next(); 95 return next();
96 } 96 }
97 97
98 - method = parseInt(method, 10);  
99 -  
100 OrderData.orderDetail(uid, orderCode).then(result => { 98 OrderData.orderDetail(uid, orderCode).then(result => {
101 if (result && result.data) { 99 if (result && result.data) {
102 let order = camelCase(result.data); 100 let order = camelCase(result.data);
103 101
104 - return PayHelpers.pay(user, order, method); 102 + return PayHelpers.pay(user, order, payType);
105 } else { 103 } else {
106 return { 104 return {
107 code: 400, 105 code: 400,
@@ -173,11 +171,15 @@ const weixinPayState = (req, res) => { @@ -173,11 +171,15 @@ const weixinPayState = (req, res) => {
173 * @param req 171 * @param req
174 * @param res 172 * @param res
175 */ 173 */
176 -const callback = (req, res) => { 174 +const callback = (req, res, next) => {
177 let type = req.params.type; 175 let type = req.params.type;
178 let payId = PayData.payments[type]; 176 let payId = PayData.payments[type];
179 let query = req.query; 177 let query = req.query;
180 178
  179 + if (!payId) {
  180 + return next();
  181 + }
  182 +
181 PayHelpers.afterPay(query, payId, req.user).then(result => { 183 PayHelpers.afterPay(query, payId, req.user).then(result => {
182 if (result.code === 200 && result.data && result.data.order) { 184 if (result.code === 200 && result.data && result.data.order) {
183 let order = result.data.order; 185 let order = result.data.order;
@@ -204,7 +206,7 @@ const callback = (req, res) => { @@ -204,7 +206,7 @@ const callback = (req, res) => {
204 } 206 }
205 }); 207 });
206 } 208 }
207 - }); 209 + }).catch(next);
208 210
209 }; 211 };
210 212
  1 +/**
  2 + *
  3 + * @author: jiangfeng<jeff.jiang@yoho.cn>
  4 + * @date: 16/7/22
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const config = global.yoho.config;
  10 +const helpers = global.yoho.helpers;
  11 +const common = require('./common');
  12 +const sign = require('./sign');
  13 +const md5 = require('md5');
  14 +const logger = global.yoho.logger;
  15 +
  16 +const ALIPAY_URL = 'https://mapi.alipay.com/gateway.do';
  17 +
  18 +const Alibank = {
  19 +
  20 + pay(user, order, param) {
  21 + let payParams = JSON.parse(param.payParams);
  22 +
  23 + let params = {
  24 + service: 'create_direct_pay_by_user',
  25 + partner: payParams.merchant_id,
  26 + _input_charset: 'utf-8',
  27 + notify_url: config.pay.serviceNotify + 'payment/alipay_notify',
  28 +
  29 + // return_url: 'http://www.yohobuy.com/shopping/pay/callback/alipay',
  30 + return_url: 'http:' + helpers.urlFormat('/shopping/pay/callback/alibank'),
  31 + subject: 'BLK订单号:' + order.orderCode,
  32 + out_trade_no: order.orderCode,
  33 + it_b_pay: common.getPayExpireMin(order.payExpire) + 'm',
  34 + total_fee: order.paymentAmount,
  35 + payment_type: '1',
  36 + defaultbank: param.bankCode,
  37 + seller_email: payParams.merchant_other_code,
  38 + sign_id_ext: user.uid,
  39 + sign_name_ext: user.username
  40 + };
  41 +
  42 + // TODO 防钓鱼配置,参考php
  43 + console.log(params, '222');
  44 + let signStr = md5(sign.raw(params) + payParams.merchant_key);
  45 + let body = sign.rawUncode(params) + '&sign=' + signStr + '&sign_type=MD5';
  46 +
  47 + return {
  48 + code: 200,
  49 + data: {
  50 + href: ALIPAY_URL + '?' + body
  51 + }
  52 + };
  53 + },
  54 + notify(data, param) {
  55 + let payParams = JSON.parse(param.payParams);
  56 +
  57 + logger.info(`Alipay notify, params = ${JSON.stringify(data)}`);
  58 +
  59 + if (!this.checkNotify(data, payParams)) {
  60 + return {payResult: -1};
  61 + } else {
  62 + return {
  63 + bankName: '',
  64 + orderCode: data.out_trade_no,
  65 + payResult: data.trade_status === 'TRADE_SUCCESS' ? 200 : 400,
  66 + payTime: data.gmt_payment || '',
  67 + totalFee: data.total_fee,
  68 + resultMsg: data.notify_type,
  69 + payOrderCode: data.out_trade_no,
  70 + tradeNo: data.trade_no,
  71 + bankBillNo: ''
  72 + };
  73 + }
  74 + },
  75 +
  76 + checkNotify(data, payParams) {
  77 + let signValue = data.sign;
  78 +
  79 + delete data.sign;
  80 + delete data.sign_type;
  81 + delete data.code;
  82 +
  83 + let signStr = md5(sign.raw(data) + payParams.merchant_key);
  84 +
  85 + return signValue === signStr;
  86 + }
  87 +};
  88 +
  89 +module.exports = Alibank;
@@ -10,30 +10,55 @@ @@ -10,30 +10,55 @@
10 const PayData = require('../models/pay'); 10 const PayData = require('../models/pay');
11 const OrderData = require('../models/order'); 11 const OrderData = require('../models/order');
12 const Alipay = require('./pay/alipay'); 12 const Alipay = require('./pay/alipay');
  13 +const Alibank = require('./pay/alibank');
13 const Wechat = require('./pay/wechat'); 14 const Wechat = require('./pay/wechat');
14 const Promise = require('bluebird'); 15 const Promise = require('bluebird');
15 const co = Promise.coroutine; 16 const co = Promise.coroutine;
16 const logger = global.yoho.logger; 17 const logger = global.yoho.logger;
17 18
18 const Payment = { 19 const Payment = {
19 - pay(user, order, method) { 20 + pay(user, order, payType) {
20 return co(function*() { 21 return co(function*() {
21 - let payInfo = yield PayData.getPaymentInfo(method);  
22 let result = { 22 let result = {
23 code: 400, 23 code: 400,
24 message: '获取支付方式信息失败' 24 message: '获取支付方式信息失败'
25 }; 25 };
  26 + let paymentPars = payType.split('_');
  27 + let payInfo;
  28 + let bankCode = '';
26 29
27 - if (payInfo && payInfo.id === method) {  
28 - if (method === PayData.payments.alipay) { 30 + if (paymentPars.length !== 2) {
  31 + return result;
  32 + }
  33 +
  34 + let method = paymentPars[0] * 1;
  35 +
  36 + if (method === PayData.payments.wechat) {
  37 + // 如果是微信支付,不需要调用获取支付方式详情接口
  38 + result = yield Wechat.pay(user, order, {id: PayData.payments.wechat});
  39 + } else {
  40 + payInfo = yield PayData.getPaymentInfo(method);
  41 +
  42 + if (!payInfo.payParams) {
  43 + return result;
  44 + }
  45 +
  46 + switch (payInfo.id) {
  47 + case PayData.payments.alipay:
29 result = Alipay.pay(user, order, payInfo); 48 result = Alipay.pay(user, order, payInfo);
30 - } else if (method === PayData.payments.wechat) {  
31 - result = yield Wechat.pay(user, order, payInfo); 49 + break;
  50 + case PayData.payments.alibank:
  51 + bankCode = paymentPars[1];
  52 + payInfo.bankCode = bankCode;
  53 + result = Alibank.pay(user, order, payInfo);
  54 + break;
  55 + default:
  56 + break;
32 } 57 }
33 } 58 }
34 59
35 if (result.code === 200) { 60 if (result.code === 200) {
36 - let updateInfo = yield Payment.beforePay(user, order, method); 61 + let updateInfo = yield Payment.beforePay(user, order, method, bankCode);
37 62
38 if (updateInfo && updateInfo.code !== 200) { 63 if (updateInfo && updateInfo.code !== 200) {
39 return updateInfo; 64 return updateInfo;
@@ -44,7 +69,7 @@ const Payment = { @@ -44,7 +69,7 @@ const Payment = {
44 })(); 69 })();
45 }, 70 },
46 71
47 - beforePay(user, order, method) { 72 + beforePay(user, order, method, bankCode) {
48 return Promise.all([ 73 return Promise.all([
49 OrderData.updateOrderPayment(order.orderCode, method, user.uid), 74 OrderData.updateOrderPayment(order.orderCode, method, user.uid),
50 PayData.savePrePayInfo(order.orderCode, method, user.uid), 75 PayData.savePrePayInfo(order.orderCode, method, user.uid),
@@ -53,8 +78,6 @@ const Payment = { @@ -53,8 +78,6 @@ const Payment = {
53 let paymentRecord = result[0]; 78 let paymentRecord = result[0];
54 let prePayResult = result[1]; 79 let prePayResult = result[1];
55 let bankRecord = result[2]; 80 let bankRecord = result[2];
56 - let bankCode = ''; // 暂时写成'', 参考php代码 Payment.php:564  
57 -  
58 81
59 if (!paymentRecord || paymentRecord.code !== 200 || !prePayResult || prePayResult.code !== 200) { 82 if (!paymentRecord || paymentRecord.code !== 200 || !prePayResult || prePayResult.code !== 200) {
60 let message = paymentRecord && paymentRecord.message ? paymentRecord.message : '系统繁忙,请稍后再试'; 83 let message = paymentRecord && paymentRecord.message ? paymentRecord.message : '系统繁忙,请稍后再试';
@@ -62,7 +85,6 @@ const Payment = { @@ -62,7 +85,6 @@ const Payment = {
62 return {code: 400, message: message}; 85 return {code: 400, message: message};
63 } 86 }
64 87
65 -  
66 if (bankRecord && bankRecord.bankCode) { 88 if (bankRecord && bankRecord.bankCode) {
67 return PayData.updateOrderPayBank(order.orderCode, method, bankCode); 89 return PayData.updateOrderPayBank(order.orderCode, method, bankCode);
68 } else { 90 } else {
@@ -86,8 +108,8 @@ const Payment = { @@ -86,8 +108,8 @@ const Payment = {
86 108
87 if (payId === PayData.payments.alipay) { 109 if (payId === PayData.payments.alipay) {
88 payResult = Alipay.notify(query, payInfo); 110 payResult = Alipay.notify(query, payInfo);
89 - } else if (payId === PayData.payments.wechat) {  
90 - payResult = Wechat.notify(query, payInfo); 111 + } else if (payId === PayData.payments.alibank) {
  112 + payResult = Alibank.notify(query, payInfo);
91 } 113 }
92 114
93 payResult.bankName = payResult.bankName || payInfo.payName || ''; 115 payResult.bankName = payResult.bankName || payInfo.payName || '';
@@ -20,7 +20,8 @@ const _ = require('lodash'); @@ -20,7 +20,8 @@ const _ = require('lodash');
20 20
21 const payments = { 21 const payments = {
22 alipay: 33, 22 alipay: 33,
23 - wechat: 36 23 + wechat: 36,
  24 + alibank: 12
24 }; 25 };
25 26
26 /** 27 /**
@@ -121,7 +122,7 @@ const getPayInfo = (uid, code) => { @@ -121,7 +122,7 @@ const getPayInfo = (uid, code) => {
121 payment[0][1].selected = true; 122 payment[0][1].selected = true;
122 payment[0][1].children[findIndex].selected = true; 123 payment[0][1].children[findIndex].selected = true;
123 } else if (payment[0] && payment[0][0]) { // 支付宝等平台 124 } else if (payment[0] && payment[0][0]) { // 支付宝等平台
124 - findIndex = _.findIndex(payment[0][0].children, {'id': paymentId}); 125 + findIndex = _.findIndex(payment[0][0].children, {id: paymentId});
125 findIndex = findIndex > 0 ? findIndex : 0; 126 findIndex = findIndex > 0 ? findIndex : 0;
126 payment[0][0].selected = true; 127 payment[0][0].selected = true;
127 payment[0][0].children[findIndex].selected = true; 128 payment[0][0].children[findIndex].selected = true;
@@ -84,7 +84,7 @@ function showDialog() { @@ -84,7 +84,7 @@ function showDialog() {
84 84
85 // 去支付 85 // 去支付
86 $goPayBtn.click(function() { 86 $goPayBtn.click(function() {
87 - var payType = $('.pay-type-icon.active').data('id'); 87 + var payType = $('.pay-type-icon.active').data('value');
88 var order = $(this).data('order'); 88 var order = $(this).data('order');
89 89
90 $.ajax({ 90 $.ajax({
@@ -93,7 +93,7 @@ $goPayBtn.click(function() { @@ -93,7 +93,7 @@ $goPayBtn.click(function() {
93 async: false, 93 async: false,
94 data: { 94 data: {
95 code: order, 95 code: order,
96 - method: payType 96 + payType: payType
97 } 97 }
98 }).then(function(data) { 98 }).then(function(data) {
99 if (data.code === 200) { 99 if (data.code === 200) {