paypal贝宝按钮创建
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="paypal-button-container"></div>
<script>
function loadJs(path, data, callback) {
var header = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.setAttribute('src', path);
script.setAttribute('data-sdk-integration-source', data);
header.appendChild(script);
if (! false) {
script.onload = function () {
console.log("非ie");
callback();
}
} else {
script.onreadystatechange = function () {
if (script.readystate == "loaded" || script.readState == 'complate') {
console.log("ie");
callback();
}
}
}
}
console.log("first test");
loadJs("https://www.paypal.com/sdk/js?client-id=AUP0N4SX2Vj3vRfijKVbu848kT6GTlejpwjz3C1ulZtnONmkCJweeYV0bIsu3GTy-F4pnpXOQwawgMkx",
"button-factory",
function () {
console.log("已经加载完成")
paypal.Buttons({
style: {
shape: 'pill',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function () {
var orderID = 101;
return fetch('https://localhost:8080/paypal/create?id=' + orderID, {
method: 'get'
}).then(function (res) {
return res.json();
}).then(function (data) {
return data.result.payId;
});
},
onApprove: function (data) {
return fetch('https://localhost:8080/paypal/capture?payId=' + data.orderID, {
method: 'get'
}).then(function (res) {
return res.json();
}).then(function (details) {
alert('Transaction funds captured from ' + details.result.tranId);
})
}
}).render('#paypal-button-container');
});
console.log("second test");
</script>
</body>
</html>