alibank.js 3.13 KB
/**
 * TAR NOTE: 从 YOHO-BLK 项目复制,未针对本项目做适配
 * @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 payHelpersBank = require('./bank');
const md5 = require('md5');
const logger = global.yoho.logger;

const ALIPAY_URL = 'https://mapi.alipay.com/gateway.do';

const Alibank = {

    pay(user, order, param, protocol) {
        let payParams = JSON.parse(param.payParams);
        let extraParam = JSON.stringify({
            sign_id_ext: user.uid,
            defaultbank: param.bankCode || ''
        });

        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: protocol + ':' + helpers.urlFormat('/shopping/pay/callback/alibank'),
            subject: 'BLK订单号:' + order.order_code,
            out_trade_no: order.order_code,
            it_b_pay: common.getPayExpireMin(order.pay_expire) + 'm',
            total_fee: order.payment_amount,
            payment_type: '1',
            defaultbank: param.bankCode,
            seller_email: payParams.merchant_other_code,
            extra_common_param: extraParam
        };

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

        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 = param.payParams && JSON.parse(param.payParams) || {};
        let orderCode = parseInt(data.out_trade_no, 10);
        let extraParam = data.extra_common_param && JSON.parse(data.extra_common_param) || {};
        let bankName = payHelpersBank.getList()[extraParam.defaultbank] &&
            payHelpersBank.getList()[extraParam.defaultbank].name || '';

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

        if (!this.checkNotify(data, payParams)) {
            return {payResult: -1, bankName: bankName};
        } else {
            return {
                bankName: 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: data.bank_seq_no || ''
            };
        }
    },

    checkNotify(data, payParams) {
        let signValue = data.sign;

        if (!payParams || !data) {
            return false;
        }

        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;