How to contact PayPal API using HttpClient from Symfony?

Viewed 34

I am trying to capture a payment through PayPal API but I keep receiving error 500. I followed the guide (translated to PHP for my needs) so I can not see where is my mistake.

My back is in PHP/Symfony so I use HTTPClient to send the request:

$response = $this->client->request('POST', sprintf('%s/%s/checkout/orders/%s/capture', $this->url, 'v2', $orderID), [
    'auth_bearer' => $this->accessToken,
    'headers' => [
        'Content-Type' => 'application/json',
        'PayPal-Request-Id' => $payment->getClientToken()
    ],
    'json' => null
]);

I added 'json' => null to force the request as application/json.

Also I noticed some little problems, I could not authenticate on v2 URLs I have to use v1 and process payment to v2.

I am using sandbox account. If I run all the requests in postman I get 201 CREATED but if I do only this one with the ID from the checkout I get 404 Not Found.

On the three ways to use the request I do not have a single one with the same return.

Do someone had this problem and solved it?

EDIT: Add curl example from PayPal API doc:

curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders/XXX/authorize \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-H "PayPal-Request-Id: XXX"
1 Answers

A PayPal-Request-Id header, if you include one, should be unique to that capture attempt. This is for API idempotency for if you need to retry the capture after receiving a 500 type error or no response.

A 404 can happen if you attempt to capture an ID from the wrong environment (e.g. the ID was created and approved in sandbox but you attempt to capture it at the live URL by mistake)

I could not authenticate on v2 URLs

Access tokens are used by all API calls, but the initial oauth request for granting an access token is a v1 API only, so that's correct. There is no other "v2" URL at which to request an access token.

Related