Last active
March 10, 2026 09:08
-
-
Save stephenreay/25bd5c2189f084b66ab0d36bdb404604 to your computer and use it in GitHub Desktop.
PayPal PHP SDK Error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use PaypalServerSdkLib\Http; | |
| use PaypalServerSdkLib\Exceptions; | |
| use PaypalServerSdkLib\PaypalServerSdkClient; | |
| class PaypalErrorWrapper { | |
| public function doSomePaypalAction() { | |
| /** @var PaypalServerSdkClient $client */ | |
| try { | |
| $response = $client->getOrdersController()->createOrder($options); | |
| if ($response->isError()) { | |
| $this->simulateHTTPException($response); | |
| } | |
| } | |
| catch(Exceptions\ErrorException $exception) { | |
| // Do something with the Exception | |
| } | |
| } | |
| /** | |
| * @throws Exceptions\ErrorException | |
| */ | |
| protected function simulateHTTPException(Http\ApiResponse $response): void { | |
| $result = $response->getResult(); | |
| switch ($response->getStatusCode()) { | |
| case 400: | |
| $message = 'Request is not well-formed, syntactically incorrect, or violates schema.'; | |
| break; | |
| case 401: | |
| $message = 'Authentication failed due to missing authorization header, or invalid authentication credentials.'; | |
| break; | |
| case 403: | |
| $message = 'The authorized payment failed due to insufficient permissions.'; | |
| break; | |
| case 404: | |
| $message = 'The specified resource does not exist.'; | |
| break; | |
| case 409: | |
| $message = 'The request failed because a previous call for the given resource is in progress.'; | |
| break; | |
| case 422: | |
| $message = 'The requested action could not be performed, semantically incorrect, or failed business validation.'; | |
| break; | |
| case 500: | |
| $message = 'An internal server error has occurred.'; | |
| break; | |
| default: | |
| $message = null; | |
| } | |
| if ($message && \is_array($result)) { | |
| $exception = new Exceptions\ErrorException($message, $response->getRequest(), new Http\HttpResponse($response->getStatusCode(), $response->getHeaders(), $response->getBody()), $result['name'], $result['message'], $result['debug_id']); | |
| $exception->setDetails($result['details']); | |
| throw $exception; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment