【问题标题】:Django Paypal Client/Server Data doesn't seem to come to serverDjango Paypal 客户端/服务器数据似乎没有到达服务器
【发布时间】:2021-11-28 01:09:00
【问题描述】:

我将此实现用于前端: https://developer.paypal.com/demo/checkout/#/pattern/server

尤其是我的前端:

............
// 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'}]}

我在前端的错误代码

Uncaught Error: Expected an order id to be passed

对我来说,响应似乎没有到达我的前端。我错过了什么吗?

【问题讨论】:

  • 检查源,切换到网络选项卡并单击匹配的网址...并查看预览它会显示返回的内容...
  • 谢谢。我认为问题是,我在传递一些其他文件后调用该函数。所以响应是对最后一个文件而不是第一个文件。必须检查一下。

标签: python django paypal


【解决方案1】:

将中间部分更改为

.then(function (res) {
            console.log("res");
            console.log(res);
            return res.json();
          })

您可能还需要将其包装在响应中

可能是JSONResponse,但我认为在这种情况下,普通的HTTPResponse 就足够了

在python中

return HTTPResponse(content=json.dumps(r))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 2020-07-16
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2017-07-23
    相关资源
    最近更新 更多