json Paypal API V2创建订单格式错误的请求

xwbd5t1u  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(139)

当我试图创建一个订单时,我总是收到格式错误的请求。我的json看起来像这样。

{
   "intent":"CAPTURE",
   "purchase_units":[
      {
         "items":[
            {
               "name":"Heartless",
               "description":"Short sleeve tshirt 230gram of 100% cotton, Heartless print on front empty back.",
               "quantity":2,
               "unit_amount":{
                  "currency_code":"EUR",
                  "value":25.0
               }
            },
            {
               "name":"NASCAR",
               "description":"Short sleeve tshirt 230gram of 100% cotton, NASCAR car print on front empty back.",
               "quantity":1,
               "unit_amount":{
                  "currency_code":"EUR",
                  "value":25.0
               }
            }
         ],
         "amount":{
            "currency_code":"EUR",
            "value":"75"
         },
         "custom_id":65
      }
   ],
   "application_context":{
      "return_url":"https://localhost:3000/order/success",
      "cancel_url":"https://localhost:3000/cancel"
   }
}

字符串
这里是API的文档。https://developer.paypal.com/docs/api/orders/v2/#orders_create我期待着大家的回答。提前感谢!

编辑

添加了项目数组的细分,并将应用程序上下文更改为付款来源,因为应用程序上下文已弃用(大部分)

{
   "purchase_units":[
      {
         "items":[
            {
               "name":"Heartless",
               "description":"Short sleeve tshirt 230gram of 100% cotton, Heartless print on front empty back.",
               "quantity":2,
               "unit_amount":{
                  "currency_code":"EUR",
                  "value":25.0
               }
            },
            {
               "name":"NASCAR",
               "description":"Short sleeve tshirt 230gram of 100% cotton, NASCAR car print on front empty back.",
               "quantity":1,
               "unit_amount":{
                  "currency_code":"EUR",
                  "value":25.0
               }
            }
         ],
         "amount":{
            "currency_code":"EUR",
            "value":"75",
            "breakdown":{
               "item_total":{
                  "currency_code":"EUR",
                  "value":"75"
               }
            }
         },
         "custom_id":65
      }
   ],
   "intent":"CAPTURE",
   "payment_source":{
      "paypal":{
         "experience_context":{
            "brand_name":"Fimetsu",
            "user_action":"PAY_NOW",
            "return_url":"https://localhost:3000/order/success",
            "cancel_url":"https://localhost:3000/cancel"
         }
      }
   }
}


这是我的错误:

"status": "success",
  "url": {
    "name": "INVALID_REQUEST",
    "message": "Request is not well-formed, syntactically incorrect, or violates schema.",
    "debug_id": "94d5cca2ed833",
    "details": [
      {
        "location": "body",
        "issue": "MALFORMED_REQUEST_JSON",
        "description": "The request JSON is not well formed."
      }
    ],
    "links": [
      {
        "href": "https://developer.paypal.com/docs/api/orders/v2/#error-MALFORMED_REQUEST_JSON",
        "rel": "information_link",
        "encType": "application/json"
      }
    ]
  }
}


这个错误没有改变时,添加'故障'我也有这个之前,但在文档中看到,它是不需要的

编辑2

response = requests.post('https://apim.sandbox.paypal.com/v2/checkout/orders', headers=headers, data=data)


我的type(data) = <class.'dict'>的类型

lawou6xi

lawou6xi1#

该错误不是格式错误的请求。错误是UNPLESSABLE_ENTITY,Item_TOTAL_REQUIRED。
当purchase_unit包含items数组时,它还必须包含带有细分的金额(amount.breakdown.item_total.valueamount.breakdown.item_total.currency_code)。
总金额必须与细分项合计加上任何其他合计相匹配,并且item_total必须与项数组的合计相对应。

kx7yvsdv

kx7yvsdv2#

new_data = json.loads(data)

字符串
在API中输入new_data var。问题不在于JSON,它只接受字符串而不接受字典。

相关问题