wechat-pay.js 3.66 KB
import payModel from '../models/pay/pay';
import Yas from '../common/yas';

const PAYMENT_CODE = '45';

let app = getApp();
let router = global.router;

const yas = new Yas(app);

function showErrorTip(text) {
    wx.showModal({
        content: text,
        showCancel: false,
        confirmText: "确定"
    });
}

function redirectOrdersPage(confirm, jump) {
    if (confirm) {
        wx.showModal({
            content: confirm,
            showCancel: false,
            confirmText: '确定',
            confirmColor: '#000',
            complete: function(res) {
                jump && router.go('orderList', {type: 2}, 'redirectTo');
            }
        });
    } else {
        jump && router.go('orderList', {type: 2}, 'redirectTo');
    }
}

function reportPayStatus(success) {
    yas.report('YB_SC_PAY_RES', {
        ORD_NUM: orderCode,
        ORDER_AMOUNT: orderAmount,
        PAY_TYPE: 1,
        PAY_RES: success ? 1 : 0
    });
}

function wechatPay(order, callback) {
    order = order || {};

    let orderCode = order.order_code,
        orderAmount = order.order_amount;

    if (parseInt(orderAmount) <= 0) {
        router.go('paySuccess', {
            orderCode: orderCode,
            price: orderAmount
        });
    }

    if (!orderCode || !orderAmount) {
        reportPayStatus();

        return showErrorTip('支付失败,请求参数不完整.');
    }

    let wechatSession = app.getWechatThirdSession();

    if (!wechatSession) {
        reportPayStatus();

        return showErrorTip('支付失败,用户session未获取到.');
    }

    const needJump = order.needJump;

    wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: 10000
    });

    payModel.wechatPay({
       order_code: orderCode,
       payment_code: PAYMENT_CODE,
       '3rd_session': wechatSession
    }).then(res => {
        wx.hideToast();

        if (res.code !== 200) {
            return redirectOrdersPage(`${data.code}:${data.message}`, needJump);
        }

        let data = res.data;

        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success: function(res) {
                let prepareID = data.package;

                if (prepareID && prepareID.length > 10) {
                    prepareID = prepareID.slice(10);

                    //上报formID
                    payModel.paySuccessConfirm({
                        order_code: orderCode,
                        openId: app.getOpenID(),
                        miniapp_type: app.getMiniappType(),
                        formId: prepareID,
                        method: 'wechat.formId.add',
                        formType: 'pay',
                    });
                }

                reportPayStatus(true);

                // 支付成功二次确认
                payModel.paySuccessConfirm({
                    order_code: orderCode,
                    payment_id: PAYMENT_CODE
                });

                router.go('paySuccess', {
                    orderCode: orderCode,
                    price: orderAmount
                });
            },
            fail: function(res) {
                let message = '';

                reportPayStatus();

                if (res.errMsg !== 'requestPayment:fail cancel') {
                    message = res.errMsg;
                }
                redirectOrdersPage(message, needJump);
            }
        })
    }).catch(e => {
        return redirectOrdersPage('获取订单信息失败', needJump);
    });
}

module.exports = {
    wechatPay: wechatPay
};