Search code examples
phplaravelpaypalpaypal-sandboxguzzle

PayPal Orders v2: Request is not well-formed, syntactically incorrect, or violates schema


I am trying to use PayPal API. Here is the reference of Curl usage: https://developer.paypal.com/docs/api/orders/v2/

After searching in google, I found a link that shows a similar error: but, no hints given to fix it. https://www.paypal-community.com/t5/REST-APIs/Request-is-not-well-formed-syntactically-incorrect-or-violates/td-p/2090480

I have following code in PHP and Laravel

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
        'headers' => [
            'Accept' => 'application/json',
            'Accept-Language' => 'en_US',
            'Content-Type' => 'application/json',
            'Authorization' => "Bearer " . $this->token
        ],
        'form_params' => [
            "intent" => "CAPTURE",
            "purchase_units" => [
                "amount" => [
                    "currency_code" => "USD",
                    "value" => "100.00"
                ]
            ]
        ]
    ]
);

When I run the code, I get the following error

GuzzleHttp\Exception\ClientException Client error: POST https://api.sandbox.paypal.com/v2/checkout/orders resulted in a 400 Bad Request response: {"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id (truncated...)

Update 1 - Modified the code as told in answer, But still same error message

$response = $client->request('POST', $uri, [
        'headers' => [
            'Accept' => 'application/json',
            'Accept-Language' => 'en_US',
            'Content-Type' => 'application/json',
            'Authorization' => "Bearer " . $this->token
        ],
        'form_params' => [
            "intent" => "CAPTURE",
            "purchase_units" => [
                [
                    "amount" => [
                        "currency_code" => "USD",
                        "value" => "100.00"
                    ]
                ]
            ]
        ]
    ]
);

Solution

  • Below code works for me.

    $client = new \GuzzleHttp\Client();
    $response = $client->request('POST', $uri, [
            'json' => [
                "intent" => "CAPTURE",
                "purchase_units" => [
                    [
                        "amount" => [
                            "currency_code" => "USD",
                            "value" => "100.00"
                        ]
                    ]
                ]
            ],
            'headers' => [
                'Accept' => 'application/json',
                'Accept-Language' => 'en_US',
                'Content-Type' => 'application/json',
                'Authorization' => "Bearer " . $this->token
            ]
        ]            
    );