payment.js 7.58 KB
/**
 * 各种支付的入口
 *
 * @author: xiaoxiao.hao<xiaoxiao.hao@yoho.cn>
 * @date: 16/7/22
 */

'use strict';

const PayData = require('../models/pay');
const Alipay = require('./pay/alipay');
const Alibank = require('./pay/alibank');
const Wechat = require('./pay/wechat');
const Unionpay = require('./pay/unionpay');
const Promise = require('bluebird');
const common = require('./pay/common');
const co = Promise.coroutine;
const logger = global.yoho.logger;
const md5 = require('md5');
const paySign = require('./pay/sign');
const helpers = global.yoho.helpers;
const _ = require('lodash');

const Payment = {
    pay(user, order, payType, protocol) {
        return co(function*() {
            let result = {
                code: 400,
                message: '获取支付方式信息失败'
            };
            let paymentPars = payType.split('_');
            let payInfo;
            let bankCode = '';

            if (paymentPars.length !== 2) {
                return result;
            }

            if (!order.order_code) {
                result.message = '没有找到该订单';
                return result;
            }

            if (order.is_cancel && order.is_cancel === 'Y') {
                result.message = '该订单已经取消';
                return result;
            }

            if (order.pay_expire && common.getPayExpireMin(order.pay_expire) <= 0) {
                result.message = '当前订单不可支付';// 该订单已超过2个小时
                return result;
            }

            let method = parseInt(paymentPars[0], 10);

            if (method === PayData.payments.wechat) {
                // 如果是微信支付,不需要调用获取支付方式详情接口
                result = yield Wechat.pay(user, order, {id: method});
            } else if (method === PayData.payments.unionpay) {
                // 不需要调用获取支付方式详情接口
                result = yield Unionpay.pay(user, order, {id: method, protocol: protocol});
            } else {
                payInfo = yield PayData.getPaymentInfo(method);

                if (!payInfo.payParams) {
                    return result;
                }

                switch (payInfo.id) {
                    case PayData.payments.alipay:
                        result = Alipay.pay(user, order, payInfo, protocol);
                        break;
                    case PayData.payments.alibank:
                        bankCode = paymentPars[1];
                        payInfo.bankCode = bankCode;// 设置默认银行
                        result = Alibank.pay(user, order, payInfo, protocol);
                        break;
                    default:
                        break;
                }
            }

            logger.info(`pay to url, params = ${JSON.stringify(result)}`);

            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([
            PayData.updateOrderPayment(order.order_code, method, user.uid),
            PayData.getBankByOrder(order.order_code)
        ]).then(result => {
            let paymentRecord = result[0];
            let bankRecord = result[1];

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

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

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

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

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

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

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

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

            // 记录日志
            logger.info(`\r\n\r\n pay back confirmreq = ${JSON.stringify({
                query: query,
                payId: payId,
                user: user,
                payResult: payResult
            })}`);

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

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

            payData.payName = payName;

            return payData;
        })();
    },

    afterPay(query, payId, user) {
        let _this = this;

        return co(function*() {
            let payData = {code: 500, message: '支付失败'};
            let sign = query.sign || '';

            switch (payId) {
                case PayData.payments.wechat: // 微信支付不须要验证,但前端必须校验sign
                    delete query.sign;
                    if (md5(paySign.raw(Object.assign({tradeStatus: 'Y'}, query))) === sign) {
                        payData = yield PayData.procOrderData(query, user.uid, payId);
                    }
                    payData.payName = '微信';
                    break;
                default:// 须要验证的支付方式
                    payData = _this._validate(query, payId, user);
                    break;
            }

            return payData;
        })();
    },

    getWxSign(uid, code) {
        return PayData.orderDetail(uid, code).then(result => {
            let data = {};

            result = (result.code === 200 ? result.data : {});

            if (result.is_cancel === 'Y') {
                return {
                    code: 401,
                    message: '订单已取消'
                };
            }

            if (result.payment_status === 'Y') {
                let payParams = {
                    orderCode: code,
                    tradeStatus: 'Y',
                    totalFee: _.round(parseFloat(result.payment_amount), 2)
                };

                data = {
                    code: 200,
                    data: {
                        href: helpers.urlFormat('/shopping/pay/callback/wechat', Object.assign(payParams, {
                            sign: md5(paySign.raw(payParams))
                        }))
                    }
                };
            }

            return data;
        });
    }
};

module.exports = Payment;