Home > Software design >  error trying to create order in paypal with guzzle and laravel
error trying to create order in paypal with guzzle and laravel

Time:02-15

I am trying to create an order in paypal with laravel and guzzle and it throws me this error:

GuzzleHttp\Exception\ClientException Client error: POST https://api-m.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...)

my controller code:

    $accessToken = $this->getAccessToken();   $client = new Client(['base_uri' =>  'https://api-m.sandbox.paypal.com/v2/checkout/']);
    
    
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $accessToken,        
    ];
    
    $params = [
        'intent' =>  'CAPTURE', 
        'purchase_units' => [
            'amount' => [
                'currency_code' => 'USD',
                'value' => '100.00'
            ]
        ]
    ];
    //dd($params);
    

    $response = $client->request('POST', 'orders', [
        'headers' => $headers,
        'form_params' => $params
    ]);

CodePudding user response:

'form_params' => $params

This is used to send an application/x-www-form-urlencoded POST request, which the PayPal API does not use.

You should be posting a plain string, JSON encoded. In place of form_params try passing the json key to guzzle in the request, or read its documentation on how to send json

CodePudding user response:

Your $params object is invalid. It should be a list of purchase_unit items, not an associative array.

        $params = [
            'intent' =>  'CAPTURE',
            'purchase_units' => [               
                [
                    'amount' => [
                        'currency_code' => 'USD',
                        'value' => '100.00'
                    ]
                ]
            ]
        ];

https://developer.paypal.com/api/orders/v2/#orders-create-request-body

  • Related