chinabank.js 2.05 KB
/**
 * @author: xiaoxiao.hao<xiaoxiao.hao@yoho.cn>
 * @date: 2017/3/23
 */
'use strict';

const helpers = global.yoho.helpers;
const md5 = require('md5');
const PAY_URL = 'https://pay3.chinabank.com.cn/PayGate';

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    pay(user, order, param, protocol) {
        let payParams = JSON.parse(param.payParams);
        let extraParam = JSON.stringify({
            sign_id_ext: user.uid
        });
        let params = {
            v_mid: payParams.merchant_id, // 商户号
            v_oid: order.order_code, // 商户订单号
            v_amount: order.payment_amount, // 总金额 以元为单位
            v_moneytype: 'CNY', // 以人民币为单位
            v_url: protocol + ':' + helpers.urlFormat('/shopping/newpay/callback/chinabank'), // 同步地址
            v_md5info: '', // 签名
            remark1: extraParam
        };

        params.v_md5info = md5(
            `${params.v_amount}${params.v_moneytype}${params.v_oid}` +
                `${params.v_mid}${params.v_url}${payParams.merchant_key}`
        ).toUpperCase();
        return {
            code: 200,
            data: Object.assign({reqType: PAY_URL}, params)
        };
    }

    notify(data, param) {
        let payParams = JSON.parse(param.payParams || '{}');
        let orderCode = parseInt(data.v_oid, 10);

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

        return {
            bankName: '',
            orderCode: orderCode,
            payResult: 200,
            totalFee: data.v_amount,
            tradeNo: '',
            bankBillNo: ''
        };
    }

    // 验证验证
    checkResponse(data, payParams) {
        let signText;

        if (parseInt(data.v_pstatus, 10) !== 20) {
            return false;
        }

        signText = md5(
            `${data.v_oid}${data.v_pstatus}${data.v_amount}${data.v_moneytype}${payParams.merchant_key}`
        ).toUpperCase();

        return data.v_md5str === signText;
    }
};