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

const helpers = global.yoho.helpers;
const PAY_URL = 'https://gateway.95516.com/gateway/api/frontTransReq.do';

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

    pay(user, order, info) {
        let options = {
            order_code: order.order_code,
            payment_code: info.id,
            uid: user.uid,
            front_url: 'https:' + helpers.urlFormat('/shopping/newpay/callback/unionpay')
        };

        return this.get({
            api: global.yoho.ServiceAPI,
            url: 'payment/unionpay_data',
            data: options
        }).then(result => {
            if (result && result.code === 200 && result.data) {
                return {
                    code: 200, // reqType是post方式,默认
                    data: Object.assign({reqType: PAY_URL}, result.data)
                };
            }

            return {
                code: 400,
                message: result.message
            };
        });
    }

    notify(data) {
        let orderCode = parseInt(data.orderId, 10);

        // https://open.unionpay.com/ajweb/help/faq/list?id=234&level=0&from=0
        if (!data.respCode || (data.respCode.indexOf('00') < 0 && data.respCode.indexOf('A6') < 0)) {
            return {payResult: -1};
        }

        return {
            bankName: '',
            orderCode: orderCode,
            payResult: 200,
            totalFee: data.txnAmt * 0.01,
            tradeNo: data.queryId,
            bankBillNo: ''
        };
    }
};