alipay.js 3.67 KB
/**
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 16/7/22
 */

'use strict';

const config = global.yoho.config;
const helpers = global.yoho.helpers;
const sign = require('./sign');
const md5 = require('yoho-md5');
const _ = require('lodash');

// const moment = require('moment');
const logger = global.yoho.logger;
const AlipayConfig = config.alipayConfig;


class PaySign extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }
    getPayData(order, param, protocol) {
        let options = {
            uid: _.get(this.ctx, 'req.user.uid'),
            order_code: order.order_code,
            payment_code: param.paymentCode,
            return_url: protocol + ':' + helpers.urlFormat(AlipayConfig.returnUrl),
            show_url: protocol + ':' + helpers.urlFormat('/home/orderdetail', { order_code: order.order_code })
        };

        return this.post({
            api: global.yoho.ServiceAPI,
            url: '/payment/alipay_data',
            data: options
        }).then(result => {
            if (result.code === 200) {
                return {
                    code: 200,
                    data: {
                        href: AlipayConfig.payUrl + '?' + _.get(result, 'data.payParams', '')
                    }
                };
            }

            return result;
        });
    }
}

const Alipay = {
    // // 原node生成预支付订单
    // _pay(user, order, param, protocol) {
    //     let params = {
    //         service: AlipayConfig.service,
    //         partner: AlipayConfig.partner,
    //         _input_charset: AlipayConfig.inputCharset,
    //         notify_url: AlipayConfig.notifyUrl,
    //         return_url: protocol + ':' + helpers.urlFormat(AlipayConfig.returnUrl),
    //         subject: '有货订单号:' + order.order_code,
    //         out_trade_no: order.order_code,
    //         it_b_pay: moment(order.pay_expire).format('YYYY-MM-DD HH:mm'),
    //         total_fee: parseFloat(order.payment_amount),
    //         seller_id: AlipayConfig.partner,
    //         payment_type: '1',
    //         show_url: protocol + ':' + helpers.urlFormat('/home/orderdetail', { order_code: order.order_code })
    //     };

    //     // TODO 防钓鱼配置,参考php

    //     let signStr = md5(sign.raw(params) + AlipayConfig.alipayKey);
    //     let body = sign.rawUncode(params) + '&sign=' + signStr + '&sign_type=MD5';

    //     return {
    //         code: 200,
    //         data: {
    //             href: AlipayConfig.payUrl + '?' + body
    //         }
    //     };
    // },
    pay(ctx, order, param, protocol) {
        return new PaySign(ctx).getPayData(order, param, protocol);
    },
    notify(data, param) { // eslint-disable-line
        // let payParams = JSON.parse(param.payParams);

        logger.info(`Alipay notify, params = ${JSON.stringify(data)}`);

        // if (!this.checkNotify(data, payParams)) {
        //     return { payResult: -1 };
        // }

        let orderCode = parseInt(data.out_trade_no, 10);

        return {
            bankName: '',
            orderCode: orderCode,
            payResult: data.trade_status === 'TRADE_SUCCESS' ? 200 : 400,
            payTime: data.gmt_payment || '',
            totalFee: data.total_fee,
            resultMsg: data.notify_type,
            payOrderCode: orderCode,
            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 = Alipay;