当前位置:Gxl网 > 前端开发 > 微信h5支付

微信h5支付

时间:2021-07-01 10:21:17 帮助过:20人阅读

export function chooseWXPay(params: wx.ChooseWXPayConfig) {
    return new Promise<{
        status: ‘success‘ | ‘error‘ | ‘cancel‘;
        result?: string;
        message?: string;
        [key: string]: any;
    }>((resolve, reject) => {
        if (!store.state.jssdk || !store.state.jssdk.jssdkReady) {
            Toast(`微信支付加载失败`);
            return;
        }

        wx.chooseWXPay({
            timestamp: params.timestamp,
            nonceStr: params.nonceStr,
            package: params.package,
            signType: params.signType,
            paySign: params.paySign,
            // 支付成功后的回调函数
            success: (res: WeixinJSSDKCallbackParams) => {
                if (res.errMsg === ‘chooseWXPay:ok‘) {
                    resolve({ // 支付成功
                        status: ‘success‘,
                    });
                } else if (res.errMsg === ‘chooseWXPay:cancel‘) {
                    resolve({ // 支付成功
                        status: ‘cancel‘,
                    });
                } else {
                    resolve({
                        status: ‘error‘,
                        result: res.errMsg,
                        message: res.errMsg,
                    });
                }
            },
            cancel: () => {
                resolve({ // 支付成功
                    status: ‘cancel‘,
                });
            },
            fail: (res: WeixinJSSDKCallbackParams) => {
                resolve({
                    status: ‘error‘,
                    result: res.errMsg,
                    message: res.errMsg,
                });
            },
        });
    });
}