Call micro-channel front-end payment functions

// WeixinJSBridge is a micro-channel built-in objects, payment function can only be used in micro-channel terminal to 
function wxPay () {
     // need to determine whether it is micro-channel browser, and micro-channel built-in package is loaded 
    // sometimes occur when a triggering event time built-in micro-channel packet has not been loaded, the following must be added is determined 
    IF ( typeof WeixinJSBridge == "undefined" ) {
         IF (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if(document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    } The else {
         // perform the following method 
        // pass the following parameters, which need to obtain from the background 
        onBridgeReady (appId, timeStamp, nonceStr, package, paySign);
    }
}

//api方法
function onBridgeReady(appId,timeStamp,nonceStr,package,paySign) {
    WeixinJSBridge.invoke (
        'getBrandWCPayRequest' , {
             "appId": appId, // need to bind business number in the micro letter, there will be generated after the successful appid    
            "timeStamp": timeStamp, // timestamp, since 1970 the number of seconds, the front needs from the background the Get data      
            "nonceStr": nonceStr, // random string, the front end of the need to get data from the background      
            "Package": Package, // distal obtain the required data from the background      
            "signType": "the MD5", // micro-channel signature scheme, the default is "MD5", can also be obtained from the background 
            "paySign": paySign // micro-channel signature, the need to obtain data from the front end of the background 
        },
        function (RES) {
             // This step is very important, during the commissioning will be reported on the appropriate error message, because micro-channel debugging tool is not actually paid, and the micro-channel can not be debugged, only here alert
            Alert (res.err_msg);
             IF (res.err_msg == "get_brand_wcpay_request: the ok" ) {
                 // After successful purchase needs to be done ... 
                Alert ( 'buy success' );
            } // use the above way to determine return to the front, micro-letter team solemnly Tip: res.err_msg returns ok after a user successful payment, but does not guarantee it is absolutely reliable. 
        }
    );
}

Guess you like

Origin www.cnblogs.com/hs610/p/12054615.html
Recommended