js网页唤起支付宝进行支付
在做uni-app项目中,打包成 ios App的时候,为了绕过苹果支付的审核,所以用的 webview 加载支付宝的网页支付,进行付款
具体实现流程:
前端通过 url 将商品单价,商品数量,商品总额等后端所需要的参数传到我们写的 html支付 页面中,
支付页面拿到参数后再像后端发起订单的请求,后端将订单返回回来,我们通过后端返回回来的订单数据,拉起支付宝发起支付!
整个过程其实不难,代码也很简单,详情请看代码......
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript">
function getRequest() {
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
}
}
return theRequest;
}
let parameter = getRequest()
$.ajax({
url: 'http://server.zhongqu.net.cn/api/user/payment/v1',
type: "post",
headers: {
'Content-Type': 'application/json;charset=utf8',
'x-access-token': parameter.token
},
dataType: "json",
data: JSON.stringify({
'goodId': parameter.goodId,
'fee': parameter.fee,
'amount': parameter.amount,
'totalFee': parameter.totalFee
}),
success: function(data) {
let temp = document.createElement("form");
temp.action = 'https://openapi.alipay.com/gateway.do?charset=utf-8'
temp.method = "post";
temp.style.display = "none";
let params = data.data;
let strs = params.split('&');
for (let v of strs) {
let arr = v.split('=');
var opt = document.createElement("input");
opt.name = arr[0];
opt.value = decodeURIComponent(arr[1]);
temp.appendChild(opt);
}
document.body.appendChild(temp);
temp.submit();
}
});
</script>
</body>
</html>