Authored by 郝肖肖

网银支付

... ... @@ -86,7 +86,7 @@ const online = (req, res, next) => {
*/
const toPay = (req, res, next) => {
let orderCode = req.body.code;
let method = req.body.method;
let payType = req.body.payType;
let uid = req.user.uid;
let user = req.user;
... ... @@ -95,13 +95,11 @@ const toPay = (req, res, next) => {
return next();
}
method = parseInt(method, 10);
OrderData.orderDetail(uid, orderCode).then(result => {
if (result && result.data) {
let order = camelCase(result.data);
return PayHelpers.pay(user, order, method);
return PayHelpers.pay(user, order, payType);
} else {
return {
code: 400,
... ... @@ -173,11 +171,15 @@ const weixinPayState = (req, res) => {
* @param req
* @param res
*/
const callback = (req, res) => {
const callback = (req, res, next) => {
let type = req.params.type;
let payId = PayData.payments[type];
let query = req.query;
if (!payId) {
return next();
}
PayHelpers.afterPay(query, payId, req.user).then(result => {
if (result.code === 200 && result.data && result.data.order) {
let order = result.data.order;
... ... @@ -204,7 +206,7 @@ const callback = (req, res) => {
}
});
}
});
}).catch(next);
};
... ...
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/7/22
*/
'use strict';
const config = global.yoho.config;
const helpers = global.yoho.helpers;
const common = require('./common');
const sign = require('./sign');
const md5 = require('md5');
const logger = global.yoho.logger;
const ALIPAY_URL = 'https://mapi.alipay.com/gateway.do';
const Alibank = {
pay(user, order, param) {
let payParams = JSON.parse(param.payParams);
let params = {
service: 'create_direct_pay_by_user',
partner: payParams.merchant_id,
_input_charset: 'utf-8',
notify_url: config.pay.serviceNotify + 'payment/alipay_notify',
// return_url: 'http://www.yohobuy.com/shopping/pay/callback/alipay',
return_url: 'http:' + helpers.urlFormat('/shopping/pay/callback/alibank'),
subject: 'BLK订单号:' + order.orderCode,
out_trade_no: order.orderCode,
it_b_pay: common.getPayExpireMin(order.payExpire) + 'm',
total_fee: order.paymentAmount,
payment_type: '1',
defaultbank: param.bankCode,
seller_email: payParams.merchant_other_code,
sign_id_ext: user.uid,
sign_name_ext: user.username
};
// TODO 防钓鱼配置,参考php
console.log(params, '222');
let signStr = md5(sign.raw(params) + payParams.merchant_key);
let body = sign.rawUncode(params) + '&sign=' + signStr + '&sign_type=MD5';
return {
code: 200,
data: {
href: ALIPAY_URL + '?' + body
}
};
},
notify(data, param) {
let payParams = JSON.parse(param.payParams);
logger.info(`Alipay notify, params = ${JSON.stringify(data)}`);
if (!this.checkNotify(data, payParams)) {
return {payResult: -1};
} else {
return {
bankName: '',
orderCode: data.out_trade_no,
payResult: data.trade_status === 'TRADE_SUCCESS' ? 200 : 400,
payTime: data.gmt_payment || '',
totalFee: data.total_fee,
resultMsg: data.notify_type,
payOrderCode: data.out_trade_no,
tradeNo: data.trade_no,
bankBillNo: ''
};
}
},
checkNotify(data, payParams) {
let signValue = data.sign;
delete data.sign;
delete data.sign_type;
delete data.code;
let signStr = md5(sign.raw(data) + payParams.merchant_key);
return signValue === signStr;
}
};
module.exports = Alibank;
... ...
... ... @@ -10,30 +10,55 @@
const PayData = require('../models/pay');
const OrderData = require('../models/order');
const Alipay = require('./pay/alipay');
const Alibank = require('./pay/alibank');
const Wechat = require('./pay/wechat');
const Promise = require('bluebird');
const co = Promise.coroutine;
const logger = global.yoho.logger;
const Payment = {
pay(user, order, method) {
pay(user, order, payType) {
return co(function*() {
let payInfo = yield PayData.getPaymentInfo(method);
let result = {
code: 400,
message: '获取支付方式信息失败'
};
let paymentPars = payType.split('_');
let payInfo;
let bankCode = '';
if (payInfo && payInfo.id === method) {
if (method === PayData.payments.alipay) {
if (paymentPars.length !== 2) {
return result;
}
let method = paymentPars[0] * 1;
if (method === PayData.payments.wechat) {
// 如果是微信支付,不需要调用获取支付方式详情接口
result = yield Wechat.pay(user, order, {id: PayData.payments.wechat});
} else {
payInfo = yield PayData.getPaymentInfo(method);
if (!payInfo.payParams) {
return result;
}
switch (payInfo.id) {
case PayData.payments.alipay:
result = Alipay.pay(user, order, payInfo);
} else if (method === PayData.payments.wechat) {
result = yield Wechat.pay(user, order, payInfo);
break;
case PayData.payments.alibank:
bankCode = paymentPars[1];
payInfo.bankCode = bankCode;
result = Alibank.pay(user, order, payInfo);
break;
default:
break;
}
}
if (result.code === 200) {
let updateInfo = yield Payment.beforePay(user, order, method);
let updateInfo = yield Payment.beforePay(user, order, method, bankCode);
if (updateInfo && updateInfo.code !== 200) {
return updateInfo;
... ... @@ -44,7 +69,7 @@ const Payment = {
})();
},
beforePay(user, order, method) {
beforePay(user, order, method, bankCode) {
return Promise.all([
OrderData.updateOrderPayment(order.orderCode, method, user.uid),
PayData.savePrePayInfo(order.orderCode, method, user.uid),
... ... @@ -53,8 +78,6 @@ const Payment = {
let paymentRecord = result[0];
let prePayResult = result[1];
let bankRecord = result[2];
let bankCode = ''; // 暂时写成'', 参考php代码 Payment.php:564
if (!paymentRecord || paymentRecord.code !== 200 || !prePayResult || prePayResult.code !== 200) {
let message = paymentRecord && paymentRecord.message ? paymentRecord.message : '系统繁忙,请稍后再试';
... ... @@ -62,7 +85,6 @@ const Payment = {
return {code: 400, message: message};
}
if (bankRecord && bankRecord.bankCode) {
return PayData.updateOrderPayBank(order.orderCode, method, bankCode);
} else {
... ... @@ -86,8 +108,8 @@ const Payment = {
if (payId === PayData.payments.alipay) {
payResult = Alipay.notify(query, payInfo);
} else if (payId === PayData.payments.wechat) {
payResult = Wechat.notify(query, payInfo);
} else if (payId === PayData.payments.alibank) {
payResult = Alibank.notify(query, payInfo);
}
payResult.bankName = payResult.bankName || payInfo.payName || '';
... ...
... ... @@ -20,7 +20,8 @@ const _ = require('lodash');
const payments = {
alipay: 33,
wechat: 36
wechat: 36,
alibank: 12
};
/**
... ... @@ -121,7 +122,7 @@ const getPayInfo = (uid, code) => {
payment[0][1].selected = true;
payment[0][1].children[findIndex].selected = true;
} else if (payment[0] && payment[0][0]) { // 支付宝等平台
findIndex = _.findIndex(payment[0][0].children, {'id': paymentId});
findIndex = _.findIndex(payment[0][0].children, {id: paymentId});
findIndex = findIndex > 0 ? findIndex : 0;
payment[0][0].selected = true;
payment[0][0].children[findIndex].selected = true;
... ...
... ... @@ -84,7 +84,7 @@ function showDialog() {
// 去支付
$goPayBtn.click(function() {
var payType = $('.pay-type-icon.active').data('id');
var payType = $('.pay-type-icon.active').data('value');
var order = $(this).data('order');
$.ajax({
... ... @@ -93,7 +93,7 @@ $goPayBtn.click(function() {
async: false,
data: {
code: order,
method: payType
payType: payType
}
}).then(function(data) {
if (data.code === 200) {
... ...