PayPal Orders v2 Create API in PHP - UNSUPPORTED_MEDIA_TYPE

Viewed 1641

I'm trying to consume this PayPal API: https://developer.paypal.com/docs/api/orders/v2/#orders_create

Here is my PHP code:

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

Error received:

Client error: POST https://api.sandbox.paypal.com/v2/checkout/orders resulted in a 415 Unsupported Media Type response: {"name":"UNSUPPORTED_MEDIA_TYPE","message":"The request payload is not supported","debug_id":"6bd372e5171ee","details":[ (truncated...)

1 Answers

'Content-Type' => 'application/x-www-form-urlencoded',

This media type is not accepted, only application/json

See the documentation for how to send json: http://docs.guzzlephp.org/en/stable/request-options.html#json

Alternatively, use the Checkout-PHP-SDK, with some guide documentation here: https://developer.paypal.com/docs/checkout/reference/server-integration/


For the best user experience, create two routes on your server, one for 'Set Up Transaction' and one for 'Capture Transaction', to be used from the following front-end code: https://developer.paypal.com/demo/checkout/#/pattern/server

Related