Authored by 郭成尧

Payment-ctx

... ... @@ -7,6 +7,7 @@
'use strict';
const _ = require('lodash');
const PayModel = require('../models/pay');
const Alipay = require('./pay/alipay');
const Wechat = require('./pay/wechat');
... ... @@ -14,15 +15,23 @@ const Promise = require('bluebird');
const co = Promise.coroutine;
const logger = global.yoho.logger;
const common = require('./pay/common');
const utils = '../../../utils';
const payProcess = require(`${utils}/pay-process`);
const Payment = {
class Payment extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.ctx = _.get(ctx, 'req.ctx');
}
/**
* 统一支付入口
* reqParams: 需要从 controller 传递的参数,支付宝需要 req.protocol,微信需要 openId、ip
*/
pay(user, order, payType, reqParams) {
return co(function*() {
let self = this;
return co(function* () {
let result = {
code: 400,
message: '获取支付方式信息失败'
... ... @@ -40,11 +49,6 @@ const Payment = {
return result;
}
// if (order.pay_expire && common.getPayExpireMin(order.pay_expire) <= 0) {
// result.message = '当前订单不可支付'; // 该订单已超过2个小时
// return result;
// }
let method = common.getPaymentCode(payType);
if (!method) {
... ... @@ -52,17 +56,17 @@ const Payment = {
return result;
}
if (method === PayModel.payments.wechat) {
if (method === payProcess.PAYMENTS.WECHAT) {
result = yield Wechat.pay(user, order, reqParams.openId, reqParams.ip);
} else {
payInfo = yield PayModel.getPaymentInfo(method);
payInfo = yield self.ctx(PayModel).getPaymentInfo(method);
if (!payInfo.payParams) {
return result;
}
switch (payInfo.id) {
case PayModel.payments.alipay:
case payProcess.PAYMENTS.ALIPAY:
result = Alipay.pay(user, order, payInfo, reqParams.protocol);
break;
default:
... ... @@ -73,7 +77,7 @@ const Payment = {
logger.info(`pay to url, params = ${JSON.stringify(result)}`);
if (result.code === 200) {
let updateInfo = yield Payment.beforePay(user, order, method, bankCode);
let updateInfo = yield self.beforePay(user, order, method, bankCode);
if (updateInfo && updateInfo.code !== 200) {
return updateInfo;
... ... @@ -82,12 +86,12 @@ const Payment = {
return result;
})();
},
}
beforePay(user, order, method, bankCode) {
return Promise.all([
PayModel.updateOrderPayment(order.order_code, method, user.uid),
PayModel.getBankByOrder(order.order_code)
this.ctx(PayModel).updateOrderPayment(order.order_code, method, user.uid),
this.ctx(PayModel).getBankByOrder(order.order_code)
]).then(result => {
let paymentRecord = result[0];
let bankRecord = result[1];
... ... @@ -99,9 +103,9 @@ const Payment = {
}
if (bankRecord && bankRecord.bankCode) {
return PayModel.updateOrderPayBank(order.order_code, method, bankCode);
return this.ctx(PayModel).updateOrderPayBank(order.order_code, method, bankCode);
} else {
return PayModel.setOrderPayBank(order.order_code, method, bankCode);
return this.ctx(PayModel).setOrderPayBank(order.order_code, method, bankCode);
}
}).catch(e => {
... ... @@ -112,16 +116,18 @@ const Payment = {
message: '更新订单支付信息失败'
});
});
},
}
afterPay(query, payId, user, sessionKey) {
let self = this;
return co(function*() {
let payInfo = yield PayModel.getPaymentInfo(payId);
let payInfo = yield self.ctx(PayModel).getPaymentInfo(payId);
let payResult = {};
let payData = {};
let payName = '';
if (payId === PayModel.payments.alipay) {
if (payId === payProcess.PAYMENTS.ALIPAY) {
payResult = Alipay.notify(query, payInfo);
}
... ... @@ -139,10 +145,10 @@ const Payment = {
if (payResult && payResult.payResult === 200) {
if (payResult.orderCode) {
logger.info('pay back confirm');
yield PayModel.sendPayConfirm(payResult.orderCode, payId, user.uid);
yield self.ctx(PayModel).sendPayConfirm(payResult.orderCode, payId, user.uid);
}
payData = yield PayModel.procOrderData(payResult, user.uid, sessionKey);
payData = yield self.ctx(PayModel).procOrderData(payResult, user.uid, sessionKey);
} else {
payData = {
code: 500,
... ... @@ -155,6 +161,6 @@ const Payment = {
return payData;
})();
}
};
}
module.exports = Payment;
... ...