我在前端使用这个实现:https://developer.paypal.com/demo/checkout//模式/服务器
我的前端:
............
// Call your server to set up the transaction
createOrder: function (data, actions) {
return fetch("/createOrder", {
method: "post",
credentials: "same-origin",
headers: {
"X-CSRFToken": csrftoken,
},
})
.then(function (res) {
console.log("res");
console.log(res);
return res;
})
.then(function (orderData) {
console.log("orderData");
console.log(orderData);
return orderData.id;
});
},
......................
我的后端:
def sth(request):
logger.error('called')
t = gettoken()
d = {"intent": "CAPTURE","purchase_units": [{"amount": {"currency_code": "USD","value": "100.00"}}]}
h = {"Content-Type": "application/json", "Authorization": "Bearer "+t}
r = requests.post('https://api-m.sandbox.paypal.com/v2/checkout/orders', headers=h, json=d).json()
logger.error(r)
return r
Python控制台(logger.error(r)):
{'id': '597275692P0354804', 'status': 'CREATED', 'links': [{'href': 'https://api.sandbox.paypal.com/v2/checkout/orders/597275692P0354804', 'rel': 'self', 'method': 'GET'}, {'href': 'https://www.sandbox.paypal.com/checkoutnow?token=597275692P0354804', 'rel': 'approve', 'method': 'GET'}, {'href': 'https://api.sandbox.paypal.com/v2/checkout/orders/597275692P0354804', 'rel': 'update', 'method': 'PATCH'}, {'href': 'https://api.sandbox.paypal.com/v2/checkout/orders/597275692P0354804/capture', 'rel': 'capture', 'method': 'POST'}]}
My errorcode in Frontend
Uncaught Error: Expected an order id to be passed
对我来说,看起来响应没有到达我的前端。我错过什么了吗?
将中间部分改为
.then(function (res) {
console.log("res");
console.log(res);
return res.json();
})
您可能还需要将其封装在响应
中。可能是JSONResponse
,但我认为在这种情况下,正常的HTTPResponse
就足够了
在python中
return HTTPResponse(content=json.dumps(r))