skip to Main Content

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 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"
                    ]
                ]
            ]
        ]
    ]
);

3

Answers


  1. Chosen as BEST ANSWER

    Below code works for me.

    $client = new GuzzleHttpClient();
    $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
            ]
        ]            
    );
    

  2. The document said:

    purchase_units: array (contains the purchase_unit object)
    An array of purchase units. Each purchase unit establishes a contract between a customer and merchant. Each purchase unit represents either a full or partial order that the customer intends to purchase from the merchant.

    Your purchase_units right now is the purchase_unit, not an array of purchase_unit, try changing your form_params to

    [
        "intent" => "CAPTURE",
        "purchase_units" => [
           [ 
              "amount" => [
                "currency_code" => "USD",
                "value" => "100.00"
              ]
           ]
        ]
    ]
    
    Login or Signup to reply.
  3.     'form_params' => [
    

    The PayPal REST API does not use form parameters.

    You need to send a ‘json’ data object. See the GuzzleHttp client documentation.

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