//plus.storage.getItem.setItem("payUrl", "http://118.26.132.3:10000/pay");

var channel = null; //支付通道 
var payUrl = null; //地址

mui.plusReady(function() {
	payUrl = plus.storage.getItem("payUrl");
})

// 2. 发起支付请求  
function pay(payChannel, payData, successFn) {
	payH5(payChannel, payData, successFn);
}

function payH5(payChannel, payData, successFn) {
	// 从服务器请求支付订单  
	var PAYSERVER = '';
	if (payChannel == '1') {
		PAYSERVER = payUrl + '/ali/payH5';
	} else if (payChannel == '2') {
		PAYSERVER = payUrl + '/wx/payH5';
	} else if (payChannel == '5') {
		PAYSERVER = payUrl + '/trial/pay/app';
	} else if (payChannel == '10') {
		PAYSERVER = payUrl + '/zero/pay/app';
	} else {
		plus.nativeUI.alert("不支持此支付通道！", null, "提示");
		return;
	}
	mui.ajax(PAYSERVER, {
		contentType: "application/json; charset=utf-8",
		data: JSON.stringify(payData),
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(rs) {
			//如果是试用
			if (payChannel === "5") {
				successFn(rs);
				return;
			}
			//如果是0元购
			if (payChannel === "10") {
				successFn(rs);
				return;
			}

			if (rs.code == 500) {
				alert(rs.msg);
				return;
			}
			//支付宝支付
			if (payChannel === "1") {
				var data = rs.data //返回的form表单
				console.log("即将跳转支付宝支付,返回的数据为：" + data)
				var div = $("div");
				div.html(rs.data);
				$("script", div).remove();
				$("body").append(div);
				$("form", div).submit();
				div.remove()
			}
			//微信支付
			if (payChannel === "2") {
				var h5PayUrl = rs.data;
				var url = h5PayUrl + "&redirect_url=" + encodeURIComponent(payUrl + '/wx/wxPaySuccess');
				plus.webview.create(url, 'h5Pay', {
					additionalHttpHeaders: {
						Referer: payUrl
					}
				}).show();
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			alert("失败：" + xhr.responseText);
		}
	});

}
//打印属性和属性值
function printPro(obj) {
	console.log(toString(obj));
}
//打印属性和属性值
function alertPro(obj) {
	alert(toString(obj));
}
//打印属性和属性值
function toString(obj) {
	var des = "";
	for (var name in obj) {
		des += name + ":" + obj[name] + ";";
	}
	return des;
}