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 GuzzleHttpClient();
$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
GuzzleHttpExceptionClientException Client error:
POST https://api.sandbox.paypal.com/v2/checkout/orders
resulted in a400 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"
]
]
]
]
]
);
3
Answers
Below code works for me.
The document said:
Your purchase_units right now is the purchase_unit, not an array of purchase_unit, try changing your form_params to
The PayPal REST API does not use form parameters.
You need to send a ‘json’ data object. See the GuzzleHttp client documentation.