From 862f1bc169070976a60649ccd6a17d02371d91cb Mon Sep 17 00:00:00 2001 From: Annie Li Date: Wed, 24 May 2023 21:32:16 -0700 Subject: [PATCH] Handle developer_message in preview responses --- lib/ApiRequestor.php | 2 +- tests/Stripe/ApiRequestorTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 8dc554a8e..c3a325f92 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -194,7 +194,7 @@ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp) */ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $errorData) { - $msg = isset($errorData['message']) ? $errorData['message'] : null; + $msg = isset($errorData['message']) ? $errorData['message'] : (isset($errorData['developer_message']) ? $errorData['developer_message'] : null); $param = isset($errorData['param']) ? $errorData['param'] : null; $code = isset($errorData['code']) ? $errorData['code'] : null; $type = isset($errorData['type']) ? $errorData['type'] : null; diff --git a/tests/Stripe/ApiRequestorTest.php b/tests/Stripe/ApiRequestorTest.php index 6615b12a6..6b4ef0018 100644 --- a/tests/Stripe/ApiRequestorTest.php +++ b/tests/Stripe/ApiRequestorTest.php @@ -503,6 +503,33 @@ public function testRaisesOAuthUnsupportedResponseTypeError() } } + public function testHandlesErrorWithDeveloperMessage() + { + $this->stubRequest( + 'POST', + '/v1/charges', + [], + null, + false, + [ + 'error' => [ + 'developer_message' => 'Unacceptable', + ], + ], + 400 + ); + + try { + Charge::create(); + static::fail('Did not raise error'); + } catch (Exception\InvalidRequestException $e) { + static::assertSame(400, $e->getHttpStatus()); + static::assertSame('Unacceptable', $e->getMessage()); + } catch (\Exception $e) { + static::fail('Unexpected exception: ' . \get_class($e)); + } + } + public function testHeaderStripeVersionGlobal() { Stripe::setApiVersion('2222-22-22');