mui HTML5+ 微信支付宝支付

利用html5+ 来发起手机中的微信和支付宝支付
官网的api链接

/**
             * 支付
             * @param {Object} payType  支付类型
             * @param {Object} payStatement  调起支付宝或微信的statment支付订单信息
             */
            function pay(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);
                });
            }
  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值