skip to Main Content

I keep getting malformed request when trying to create an order. My json looks like this.

{
   "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"
   }
}

Here is the documentation of the api. https://developer.paypal.com/docs/api/orders/v2/#orders_create
I look forward to everyones answers. Thanks in advance!

EDIT

added breakdown to items array and changed application context to payment source as application context is DEPRECATED (mostly)

{
   "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"
         }
      }
   }
}

this is my error:

"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"
      }
    ]
  }
}

this error didn’t change when adding the ‘breakdown’ I had this also before but saw in the documentation that it was not required

EDIT 2

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

Where the type of my type(data) = <class.'dict'>

2

Answers


  1. Chosen as BEST ANSWER
    new_data = json.loads(data)
    

    Enter new_data var in the api. The problem was not with the json it just accepts strings and not dicts.


  2. The error is not a malformed request. The error is UNPROCESSABLE_ENTITY, ITEM_TOTAL_REQUIRED.

    When a purchase_unit includes an items array, it must also include an amount with breakdown (amount.breakdown.item_total.value and amount.breakdown.item_total.currency_code).

    The amount total must match the breakdown item total plus any other totals, and the item_total must correspond to the total of the items array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search