如何解决函数调用后自动刷新
我正在将 Paypal 集成到我的 Web 应用程序中。该代码有效,但我需要有一个功能,可以在付款后自动刷新页面。我在哪里可以放置自动刷新页面的功能,代码是什么?抱歉,我是 JavaScript 新手。
附加信息:我使用的主要语言是 Python/Django 框架,但要集成 Paypal 支付,我需要使用 Sandbox 中的此代码。
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=PHP"></script>
<script>
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0,name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
var total = '{{object.get_total}}'
var orderID = '{{object.id}}'
function completeOrder(){
var url = "{% url 'complete' %}"
fetch(url,{
method: 'POST',headers:{
'Content-type' : "application/json",'X-CSRFToken': csrftoken,},body: JSON.stringify({'orderID':orderID,'total':total})
})
}
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
style: {
color: 'blue',shape: 'pill',label: 'pay',height: 40
},// Set up the transaction
createOrder: function(data,actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: total
}
}]
});
},// Finalize the transaction
onApprove: function(data,actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
completeOrder()
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
</script>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。