Mui 微信支付、支付寶支付


 

利用mui 發起手機微信和支付寶支付


 

 

payStatement :調起微信支付接口的參數

參考文檔: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2

 
        

 

  /**
     * 支付
     * @param {Object} payType  支付類型
     * @param {Object} payStatement  調起支付寶或微信的statment支付訂單信息
     */
    paywx(payType, payStatement) {

        /***判斷支付通道****/
        //最終的支付通道
        var channel;
        /***
         * 用於標識支付通道: 
         * "alipay" - 表示支付寶;
         * "wxpay" - 表示微信支付; 
         */
        var payId; //支付標識
        if(payType == "ALIPAY_ANDROID") {
            payId = "alipay";
        } else {
            payId = "wxpay";
        }

        // 取出支付寶和微信的支付通道
        plus.payment.getChannels(function(channels) {
            mui.each(channels, function(index, element) {
                if(element.id == payId) {
                    channel = element;
                }
            });
            if(!channel) {
                mui.toast('獲取支付通道失敗,請重試!');
            }
            setTimeout(function() {
                mui.confirm('支付已完成', '提示', ['支付遇到問題', '支付完成'], function(e) {
                    if(e.index == 1) {
                        app.tokenAjax_Get({
                            url: API_URL_GET_ORDER_STATUS + dataSource.orderId,
                            success: function(result) {
                                if(result.status == 1) {
                                    var status = result.data;
                                    if(status == "JUST_CREATED") {
                                        mui.toast('訂單未支付');
                                    } else if(status == 'CANCEL') {
                                        mui.toast('訂單已被取消');
                                        //清除定時器
                                        clearInterval(timer);
                                        //打開
                                        plus.webview.currentWebview().close();
                                        plus.webview.getWebviewById("pay").close();
                                        plus.webview.getWebviewById("order").close();
                                    } else {
                                        //清除定時器
                                        clearInterval(timer);
                                        //打開
                                        app.openRefreshOrderListPage();
                                    }
                                }
                            },
                            error: function(xhr) {
                                app.httpError(xhr.status);
                            }
                        });
                    }
                }, 'div')
            }, 3000);
            //發起支付
            plus.payment.request(channel, payStatement, function(result) {
                mui.toast('支付完成');
                /**
                 *查詢訂單狀態是否已支付
                 * 輪詢查詢訂單狀態
                 * 
                 */
                var timer1 = setInterval(function() {
                    app.tokenAjax_Get({
                        url: API_URL_GET_ORDER_STATUS + dataSource.orderId,
                        success: function(result) {
                            if(result.status == 1) {
                                var status = result.data;
                                if(status == "JUST_CREATED") {
                                    mui.toast('訂單未支付');
                                } else if(status == 'CANCEL') {
                                    mui.toast('訂單已被取消');
                                    //清除定時器
                                    clearInterval(timer);
                                    clearInterval(timer1)
                                    //打開
                                    plus.webview.currentWebview().close();
                                    plus.webview.getWebviewById("pay").close();
                                    plus.webview.getWebviewById("order").close();
                                } else {
                                    //清除定時器
                                    clearInterval(timer);
                                    clearInterval(timer1)
                                    //打開
                                    app.openRefreshOrderListPage();
                                }
                            }
                        },
                        error: function(xhr) {
                            app.httpError(xhr.status);
                        }
                    });
                }, 1000);

            }, function(error) {
                console.log(JSON.stringify(error));
                mui.toast("支付失敗");
            });

        }, function(e) {
            mui.toast("獲取支付通道列表失敗:" + e.message);
        });

    }

 

更多錯誤信息請參考支付(Payment)規范文檔:http://www.html5plus.org/#specification#/specification/Payment.html

 

注意: (  微信支付提示{"code":-100,"message":"[payment微信:-1]General errors"}  )

  • 微信支付安卓不支持真機調試
  • 需要打包到手機測試, 打包測試需要使用自己的私有證書打包apk

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM