【问题标题】:Django Paypal Integration createOrder curlDjango Paypal 集成 createOrder curl
【发布时间】:2021-11-27 04:14:39
【问题描述】:

我正在尝试在没有任何 SDK 或包的情况下在 Django 中实现 PayPal。

https://developer.paypal.com/docs/business/checkout/server-side-api-calls/create-order/

想将此 cURL 重写为 Python

 curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders \ 
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
  "intent": "CAPTURE",
  "purchase_units": [
    {
      "amount": {
        "currency_code": "USD",
        "value": "100.00"
      }
    }
  ]
}'

我当前的代码:

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, data=d).json()

我的错误:

Internal Server Error: /createOrder
.....
AttributeError: 'set' object has no attribute 'items'

不记名令牌没问题。

有什么想法吗?我错过了什么?

【问题讨论】:

  • 正确的标题是{"Content-Type": "application/json", "Authorization": "Bearer "+t}。您当前拥有的实际上是 set 而不是 dict 因此错误
  • 谢谢。现在我得到 '{'name': 'INVALID_REQUEST', 'message': '请求格式不正确、语法不正确或违反架构。', 'debug_id': '252e2f9a9587d', 'details': [{ 'location': 'body', 'issue': 'MALFORMED_REQUEST_JSON', 'description': '请求 JSON 格式不正确'}], 'links': [{'href': 'developer.paypal.com/docs/api/orders/v2/…', ' rel': 'information_link', 'encType': 'application/json'}]} 那么我的 d 部分有什么错误吗?
  • 看起来不错。它适用于 curl 吗?

标签: django curl paypal


【解决方案1】:
 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()

工作。

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2011-02-12
    • 2011-02-01
    • 2018-04-12
    • 2021-10-01
    • 2023-02-20
    • 2012-10-22
    • 2016-10-19
    相关资源
    最近更新 更多