PATCH https://api.paypal.com/v2/checkout/orders INVALID_REQUEST

Created on 1 April 2022, over 2 years ago
Updated 22 May 2023, over 1 year ago

Problem/Motivation

Error when paying by PayPal:

Client error: `PATCH https://api.paypal.com/v2/checkout/orders/<order-id>` resulted in a `400 Bad Request` response: {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.",...

If I modify the logger to record the full message, I see more, including:

[@"field":"/","location":"body","issue":"MALFORMED_REQUEST_JSON","description":"The request JSON is not well formed."],

Steps to reproduce

No idea.

🐛 Bug report
Status

Closed: works as designed

Version

1.0

Component

PayPal Checkout

Created by

🇬🇧United Kingdom fonant

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom fonant

    I might have found a possible problem, but can't see what is causing it.

    Emailing myself the json_encode() of the data, for one order the JSON is broken into two lines, with "! " at the end of the first line and a space at the start of the second line. The first line is 2040 characters long.

    This makes the JSON invalid, causing the problem.

    Is this a red-herring, caused by emailing the JSON? I don't think so, but could be.

    If this is a real symptom, what is inserting "! \n " into the JSON text?

  • 🇮🇱Israel jsacksick

    Unfortunately, without reproducible steps, not actually sure how to help here.

  • 🇬🇧United Kingdom fonant

    My best guess is that the problem occurs when the JSON body is more than 2,040 characters, so for an order with a LOT of line items. I'll do some more testing when I get a spare moment, using an order with a huge JSON description.

  • 🇬🇧United Kingdom fonant

    Most odd. I now catch the Guzzle exception, and log the details. This allows the PayPal orders that fail in the PATCH to continue anyway.

    GuzzleHttp\Exception\ClientException
    
    Client error: `PATCH https://api.paypal.com/v2/checkout/orders/xxxxxxxxxxxxxxxxxxxxx` resulted in a `400 Bad Request` response:
    {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id (truncated...)
    
    {"0":{"op":"replace","path":"\/purchase_units\/@reference_id=='default'","value":{"reference_id":"default","custom_id":"1234","invoice_id":"1234-1234567890","amount":{"currency_code":"GBP","value":"7.5","breakdown":{"item_total":{"currency_code":"GBP","value":"7.5"}}},"items":[{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"},{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"},{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"},{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"},{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"},{"name":"1296px \u00d7 864px JPEG download","unit_amount":{"currency_code":"GBP","value":"1.25"},"quantity":1,"sku":"DD1296x864"}]}},"purchase_units":[{"items":[{"name":"AGc19877, 1296px \u00d7 864px JPEG download"},{"name":"AG19650, 1296px \u00d7 864px JPEG download"},{"name":"AB00516, 1296px \u00d7 864px JPEG download"},{"name":"AG06613, 1296px \u00d7 864px JPEG download"},{"name":"AG06615, 1296px \u00d7 864px JPEG download"},{"name":"AG01394, 1296px \u00d7 864px JPEG download"}]}]}

    (some possibly-sensitive data replaced with equivalent random text)

    This was a shorter order, so nothing to do with the length of the JSON.

    According to PhpStorm the JSON is valid.

  • 🇬🇧United Kingdom fonant

    Found the problem!

    It was my own custom code, that modified the order line item titles sent to PayPal using PayPalEvents::CHECKOUT_CREATE_ORDER_REQUEST and PayPalEvents::CHECKOUT_UPDATE_ORDER_REQUEST.

    For the first event, CREATE, the line item titles are stored in $params['purchase_units'][0]['items'].

    For the second event, UPDATE, the line item titles are stored in $params[0]['value']['items'].

  • Status changed to Closed: works as designed over 1 year ago
  • 🇬🇧United Kingdom fonant

    The code is working as designed.

    Be careful if you want to modify order data using PayPalEvents::CHECKOUT_CREATE_ORDER_REQUEST => 'addCatnums' and PayPalEvents::CHECKOUT_UPDATE_ORDER_REQUEST, the data is in different places in each.

Production build 0.71.5 2024