jquery-weui微信支付報錯問題解決

佀無極發表於2018-09-11

微信支付報錯問題解決
支付回撥各種報錯,openID、APPID等等原因,列印 console.log(typeof(res.jsApiParameters));
重點把string轉化為Object就ok了。
每次支付請求會生成訂單,根據對應訂單號,修改資料庫訂單狀態才可以完成。

{
  "appId":"wx1b35c*********3",
  "nonceStr":"nam97dundxjiisasumrevju*******",
  "package":"prepay_id=wx***",
  "signType":"MD5",
  "timeStamp":"1**",
  "paySign":"7CD01*****9"
}
//獲取充值套餐
$.ajax({
    type: `post`,
    url: api.domain + api.recharge_package,
    data: {
        "access_token": access
    },
    success : function(res) {
        if (res.status == 1) {
            var n = res.data.length;
            var menu = [];
            if(n < 1){
                return false;
            }
            for(var i = 0;i < n;i++){
                menu.push(res.data[i].id);
                var selected;
                //設定預設套餐與價格
                if(i == 0){
                    selected = ` btn-active`;
                    $(`#money`).html(res.data[i].price);
                } else {
                    selected = ``;
                }
                var $list = $(`<span class="weui-btn btn-check js-pay ` + selected + `" title="` +
                res.data[i].id + `" price="` + res.data[i].price + `">` + res.data[i].name + `</span>`);
                $list.appendTo($(`#parent`));
            }
            $(`#count`).val(menu);
        } else {
            $.toptip(res.msg,`cancel`);
        }
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log(`XMLHttpRequest.status = ` + XMLHttpRequest.status);
        console.log(`XMLHttpRequest.readyState = ` + XMLHttpRequest.readyState);
        console.log(`textStatus = ` + textStatus);
    }
});
//點選套餐
$(`body`).on(`click`, `.js-pay`, function(){
    var param_b = $(this).attr(`title`);
    var menu = $(`#count`).val().split(`,`);
    for(var p = 0; p < menu.length;p++) {
        if(menu[p] == param_b) {
            $(`span[title="` + menu[p] + `"]`).addClass(`btn-active`);
        } else {
            $(`span[title="` + menu[p] + `"]`).removeClass(`btn-active`);
        }
    }
    $(`#money`).html($(this).attr(`price`));
});
//充值付費
$("#pay").click(function() {
    var package = $(`.btn-active`).attr(`title`);
    $.ajax({
        type: `post`,
        url: `/recharge/pay`,
        data: {
            "access_token": access,
            "package_id": package
        },
        success : function(res) {
            console.log(typeof(res));
            console.log(res.jsApiParameters);
            console.log(typeof(res.jsApiParameters));
            if (res.status == 1) {
                if (typeof WeixinJSBridge == "undefined"){
                    if( document.addEventListener ){
                        document.addEventListener(`WeixinJSBridgeReady`, jsApiCall, false);
                    }else if (document.attachEvent){
                        document.attachEvent(`WeixinJSBridgeReady`, jsApiCall);
                        document.attachEvent(`onWeixinJSBridgeReady`, jsApiCall);
                    }
                }else{
                    jsApiCall($.parseJSON(res.jsApiParameters),res.order_no);
                }
            } else {
                $.toptip(res.msg,`cancel`);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(`XMLHttpRequest.status = ` + XMLHttpRequest.status);
            console.log(`XMLHttpRequest.readyState = ` + XMLHttpRequest.readyState);
            console.log(`textStatus = ` + textStatus);
        }
    });

});

//呼叫微信JS api 支付
function jsApiCall(n,order_no){
    console.log(n);
    WeixinJSBridge.invoke(
        `getBrandWCPayRequest`,
        n,
        function(res){
            WeixinJSBridge.log(res.err_msg);
            //alert(res.err_code+res.err_desc+res.err_msg);
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    paySuccess(order_no);
              }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
                $.toast("支付取消", "cancel");
              }else{
                $.toast("支付失敗", "forbidden");
              }     // 使用以上方式判斷前端返回,微信團隊鄭重提示:res.err_msg將在使用者支付成功後返回    ok,但並不保證它絕對可靠。 
        }
    );
}
//微信回撥成功,跳轉成功頁面
function paySuccess(n){
    $.ajax({
        type: `post`,
        url: api.domain + api,
        data: {
            "access_token": access,
            "order_no": n
        },
        success : function(res) {
            if (res.status == 1) {
                location.href = `/recharge/success`;
            } else {
                $.toptip(res.msg,`cancel`);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log(`XMLHttpRequest.status = ` + XMLHttpRequest.status);
            console.log(`XMLHttpRequest.readyState = ` + XMLHttpRequest.readyState);
            console.log(`textStatus = ` + textStatus);
        }
    });
}


相關文章