我有一个 Django 应用程序,我正在尝试通过贝宝结账创建和订购。我一直收到错误消息:
"Uncaught Error: Expected an order id to be passed"
我的订单和送货地址已在后端成功创建,但贝宝似乎不接受。
我已经尝试了很多方法来尝试解决这个问题。希望有人有一些想法:这是我目前的剧本;
<script>
paypal.Buttons({
// Order is created on the server and the order id is returned
createOrder: function() {
// Call the /get_cart_id/ endpoint to get the cart id
return fetch('/get_cart_id/')
.then(function(response) {
return response.json();
})
.then(function(data) {
var cartId = data.cart_id;
return fetch('/api/orders/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({
cart_id: cartId
})
})
.then(function(response) {
return response.json();
})
.then(function(order) {
// Call the Checkout function to handle the order
var shippingInfo = {
house: form.house.value,
street: form.street.value,
city: form.city.value,
postcode: form.postcode.value
};
return fetch('/get_shipping_address/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({
shipping: shippingInfo
})
})
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log('Success:', data);
alert('Transaction completed');
});
});
});
},
// Finalize the transaction on the server after payer approval
onApprove: function(data) {
return fetch(`/api/orders/${orderId}/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRFToken': csrftoken
},
body: JSON.stringify({
order_id: data.orderId
})
})
.then(function(response) {
return response.json();
})
.then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert(`Transaction ${transaction.status}: ${transaction.id}\n\nSee console for all available details`);
// When ready to go live, remove the alert and show a success message within this page. For example:
// var element = document.getElementById('paypal-button-container');
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: window.location.href = 'thank_you.html';
});
}
}).render('#paypal-button-container');