payment.js 4.38 KB
/**
 * 各种支付的入口
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 16/7/22
 */

'use strict';

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, payType) {
        return co(function*() {
            let result = {
                code: 400,
                message: '获取支付方式信息失败'
            };
            let paymentPars = payType.split('_');
            let payInfo;
            let bankCode = '';

            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);
                        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, bankCode);

                if (updateInfo && updateInfo.code !== 200) {
                    return updateInfo;
                }
            }

            return result;
        })();
    },

    beforePay(user, order, method, bankCode) {
        return Promise.all([
            OrderData.updateOrderPayment(order.orderCode, method, user.uid),
            PayData.savePrePayInfo(order.orderCode, method, user.uid),
            PayData.getBankByOrder(order.orderCode)
        ]).then(result => {
            let paymentRecord = result[0];
            let prePayResult = result[1];
            let bankRecord = result[2];

            if (!paymentRecord || paymentRecord.code !== 200 || !prePayResult || prePayResult.code !== 200) {
                let message = paymentRecord && paymentRecord.message ? paymentRecord.message : '系统繁忙,请稍后再试';

                return {code: 400, message: message};
            }

            if (bankRecord && bankRecord.bankCode) {
                return PayData.updateOrderPayBank(order.orderCode, method, bankCode);
            } else {
                return PayData.setOrderPayBank(order.orderCode, method, bankCode);
            }

        }).catch(e => {
            logger.error('update order pay info error.', e);

            return Promise.resolve({
                code: 400,
                message: '更新订单支付信息失败'
            });
        });
    },

    afterPay(query, payId, user) {
        return co(function*() {
            let payInfo = yield PayData.getPaymentInfo(payId);
            let payResult = {};

            if (payId === PayData.payments.alipay) {
                payResult = Alipay.notify(query, payInfo);
            } else if (payId === PayData.payments.alibank) {
                payResult = Alibank.notify(query, payInfo);
            }

            payResult.bankName = payResult.bankName || payInfo.payName || '';
            payResult.bankCode = payResult.bankCode || payInfo.pay_code || '';

            if (payResult && payResult.payResult === 200) {
                if (payResult.orderCode) {
                    logger.info('pay back confirm');
                    yield PayData.sendPayConfirm(payResult.orderCode, payId, user.uid);
                }

                return yield PayData.procOrderData(payResult, user.uid);
            } else {
                return {
                    code: 500,
                    message: '支付失败'
                };
            }
        })();
    }
};

module.exports = Payment;