From 67264eb6a7204641e9c479bd263bf147df772e00 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Fri, 23 Aug 2019 10:22:18 -0700 Subject: [PATCH] Modernize exceptions --- examples/oauth.php | 4 +- init.php | 43 ++-- lib/Account.php | 2 +- lib/AlipayAccount.php | 10 +- lib/ApiOperations/All.php | 2 +- lib/ApiOperations/Request.php | 4 +- lib/ApiRequestor.php | 134 +++++------ lib/ApiResource.php | 2 +- lib/ApplicationFeeRefund.php | 2 +- lib/BankAccount.php | 10 +- lib/Capability.php | 10 +- lib/Card.php | 10 +- lib/Charge.php | 2 +- lib/Collection.php | 2 +- lib/CustomerBalanceTransaction.php | 10 +- lib/EphemeralKey.php | 2 +- lib/Error/Api.php | 14 -- lib/Error/ApiConnection.php | 13 -- lib/Error/Authentication.php | 12 - lib/Error/Base.php | 95 -------- lib/Error/Card.php | 39 ---- lib/Error/Idempotency.php | 12 - lib/Error/InvalidRequest.php | 31 --- lib/Error/OAuth/InvalidClient.php | 14 -- lib/Error/OAuth/InvalidGrant.php | 14 -- lib/Error/OAuth/InvalidRequest.php | 13 -- lib/Error/OAuth/InvalidScope.php | 12 - lib/Error/OAuth/OAuthBase.php | 39 ---- lib/Error/OAuth/UnsupportedGrantType.php | 12 - lib/Error/OAuth/UnsupportedResponseType.php | 12 - lib/Error/Permission.php | 12 - lib/Error/RateLimit.php | 13 -- lib/Error/SignatureVerification.php | 27 --- lib/Exception/ApiConnectionException.php | 15 ++ lib/Exception/ApiException.php | 16 ++ lib/Exception/AuthenticationException.php | 14 ++ lib/Exception/BadMethodCallException.php | 7 + lib/Exception/CardException.php | 89 +++++++ lib/Exception/ExceptionInterface.php | 14 ++ lib/Exception/ExceptionTrait.php | 218 ++++++++++++++++++ lib/Exception/IdempotencyException.php | 14 ++ lib/Exception/InvalidArgumentException.php | 7 + lib/Exception/InvalidRequestException.php | 65 ++++++ lib/Exception/OAuth/ExceptionInterface.php | 10 + lib/Exception/OAuth/ExceptionTrait.php | 17 ++ .../OAuth/InvalidClientException.php | 15 ++ lib/Exception/OAuth/InvalidGrantException.php | 16 ++ .../OAuth/InvalidRequestException.php | 14 ++ lib/Exception/OAuth/InvalidScopeException.php | 13 ++ .../OAuth/UnsupportedGrantTypeException.php | 14 ++ .../UnsupportedResponseTypeException.php | 14 ++ lib/Exception/PermissionException.php | 14 ++ lib/Exception/RateLimitException.php | 14 ++ .../SignatureVerificationException.php | 57 +++++ lib/Exception/UnexpectedValueException.php | 7 + lib/HttpClient/ClientInterface.php | 4 +- lib/HttpClient/CurlClient.php | 12 +- lib/OAuth.php | 2 +- lib/Person.php | 10 +- lib/Source.php | 4 +- lib/StripeObject.php | 8 +- lib/TaxId.php | 9 +- lib/TransferReversal.php | 2 +- lib/Util/DefaultLogger.php | 2 +- lib/Util/RequestOptions.php | 4 +- lib/Webhook.php | 16 +- lib/WebhookSignature.php | 16 +- tests/Stripe/AlipayAccountTest.php | 4 +- tests/Stripe/ApiRequestorTest.php | 43 ++-- tests/Stripe/BankAccountTest.php | 4 +- tests/Stripe/CapabilityTest.php | 4 +- tests/Stripe/CardTest.php | 4 +- .../Error/SignatureVerificationTest.php | 12 - .../ExceptionTraitTest.php} | 15 +- .../OAuth/ExceptionTraitTest.php} | 16 +- .../SignatureVerificationExceptionTest.php | 12 + tests/Stripe/HttpClient/CurlClientTest.php | 2 +- tests/Stripe/OAuthTest.php | 2 +- tests/Stripe/PersonTest.php | 4 +- tests/Stripe/SourceTest.php | 2 +- tests/Stripe/Util/RequestOptionsTest.php | 2 +- tests/Stripe/WebhookTest.php | 14 +- 82 files changed, 912 insertions(+), 614 deletions(-) delete mode 100644 lib/Error/Api.php delete mode 100644 lib/Error/ApiConnection.php delete mode 100644 lib/Error/Authentication.php delete mode 100644 lib/Error/Base.php delete mode 100644 lib/Error/Card.php delete mode 100644 lib/Error/Idempotency.php delete mode 100644 lib/Error/InvalidRequest.php delete mode 100644 lib/Error/OAuth/InvalidClient.php delete mode 100644 lib/Error/OAuth/InvalidGrant.php delete mode 100644 lib/Error/OAuth/InvalidRequest.php delete mode 100644 lib/Error/OAuth/InvalidScope.php delete mode 100644 lib/Error/OAuth/OAuthBase.php delete mode 100644 lib/Error/OAuth/UnsupportedGrantType.php delete mode 100644 lib/Error/OAuth/UnsupportedResponseType.php delete mode 100644 lib/Error/Permission.php delete mode 100644 lib/Error/RateLimit.php delete mode 100644 lib/Error/SignatureVerification.php create mode 100644 lib/Exception/ApiConnectionException.php create mode 100644 lib/Exception/ApiException.php create mode 100644 lib/Exception/AuthenticationException.php create mode 100644 lib/Exception/BadMethodCallException.php create mode 100644 lib/Exception/CardException.php create mode 100644 lib/Exception/ExceptionInterface.php create mode 100644 lib/Exception/ExceptionTrait.php create mode 100644 lib/Exception/IdempotencyException.php create mode 100644 lib/Exception/InvalidArgumentException.php create mode 100644 lib/Exception/InvalidRequestException.php create mode 100644 lib/Exception/OAuth/ExceptionInterface.php create mode 100644 lib/Exception/OAuth/ExceptionTrait.php create mode 100644 lib/Exception/OAuth/InvalidClientException.php create mode 100644 lib/Exception/OAuth/InvalidGrantException.php create mode 100644 lib/Exception/OAuth/InvalidRequestException.php create mode 100644 lib/Exception/OAuth/InvalidScopeException.php create mode 100644 lib/Exception/OAuth/UnsupportedGrantTypeException.php create mode 100644 lib/Exception/OAuth/UnsupportedResponseTypeException.php create mode 100644 lib/Exception/PermissionException.php create mode 100644 lib/Exception/RateLimitException.php create mode 100644 lib/Exception/SignatureVerificationException.php create mode 100644 lib/Exception/UnexpectedValueException.php delete mode 100644 tests/Stripe/Error/SignatureVerificationTest.php rename tests/Stripe/{Error/BaseTest.php => Exception/ExceptionTraitTest.php} (69%) rename tests/Stripe/{Error/OAuth/OAuthBaseTest.php => Exception/OAuth/ExceptionTraitTest.php} (72%) create mode 100644 tests/Stripe/Exception/SignatureVerificationExceptionTest.php diff --git a/examples/oauth.php b/examples/oauth.php index 3a6291129..0626cb853 100644 --- a/examples/oauth.php +++ b/examples/oauth.php @@ -15,7 +15,7 @@ 'grant_type' => 'authorization_code', 'code' => $code, ]); - } catch (\Stripe\Error\OAuth\OAuthBase $e) { + } catch (\Stripe\Exception\OAuth\ExceptionInterface $e) { exit("Error: " . $e->getMessage()); } @@ -38,7 +38,7 @@ \Stripe\OAuth::deauthorize([ 'stripe_user_id' => $accountId, ]); - } catch (\Stripe\Error\OAuth\OAuthBase $e) { + } catch (\Stripe\Exception\OAuth\ExceptionInterface $e) { exit("Error: " . $e->getMessage()); } diff --git a/init.php b/init.php index 2822d0c8d..a3457eaaa 100644 --- a/init.php +++ b/init.php @@ -16,26 +16,31 @@ require(dirname(__FILE__) . '/lib/HttpClient/ClientInterface.php'); require(dirname(__FILE__) . '/lib/HttpClient/CurlClient.php'); -// Errors -require(dirname(__FILE__) . '/lib/Error/Base.php'); -require(dirname(__FILE__) . '/lib/Error/Api.php'); -require(dirname(__FILE__) . '/lib/Error/ApiConnection.php'); -require(dirname(__FILE__) . '/lib/Error/Authentication.php'); -require(dirname(__FILE__) . '/lib/Error/Card.php'); -require(dirname(__FILE__) . '/lib/Error/Idempotency.php'); -require(dirname(__FILE__) . '/lib/Error/InvalidRequest.php'); -require(dirname(__FILE__) . '/lib/Error/Permission.php'); -require(dirname(__FILE__) . '/lib/Error/RateLimit.php'); -require(dirname(__FILE__) . '/lib/Error/SignatureVerification.php'); +// Exceptions +require(dirname(__FILE__) . '/lib/Exception/ExceptionInterface.php'); +require(dirname(__FILE__) . '/lib/Exception/ExceptionTrait.php'); +require(dirname(__FILE__) . '/lib/Exception/ApiConnectionException.php'); +require(dirname(__FILE__) . '/lib/Exception/ApiException.php'); +require(dirname(__FILE__) . '/lib/Exception/AuthenticationException.php'); +require(dirname(__FILE__) . '/lib/Exception/BadMethodCallException.php'); +require(dirname(__FILE__) . '/lib/Exception/CardException.php'); +require(dirname(__FILE__) . '/lib/Exception/IdempotencyException.php'); +require(dirname(__FILE__) . '/lib/Exception/InvalidArgumentException.php'); +require(dirname(__FILE__) . '/lib/Exception/InvalidRequestException.php'); +require(dirname(__FILE__) . '/lib/Exception/PermissionException.php'); +require(dirname(__FILE__) . '/lib/Exception/RateLimitException.php'); +require(dirname(__FILE__) . '/lib/Exception/SignatureVerificationException.php'); +require(dirname(__FILE__) . '/lib/Exception/UnexpectedValueException.php'); -// OAuth errors -require(dirname(__FILE__) . '/lib/Error/OAuth/OAuthBase.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidClient.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidGrant.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidRequest.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/InvalidScope.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedGrantType.php'); -require(dirname(__FILE__) . '/lib/Error/OAuth/UnsupportedResponseType.php'); +// OAuth exceptions +require(dirname(__FILE__) . '/lib/Exception/OAuth/ExceptionInterface.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/ExceptionTrait.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidClientException.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidGrantException.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidRequestException.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/InvalidScopeException.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/UnsupportedGrantTypeException.php'); +require(dirname(__FILE__) . '/lib/Exception/OAuth/UnsupportedResponseTypeException.php'); // API operations require(dirname(__FILE__) . '/lib/ApiOperations/All.php'); diff --git a/lib/Account.php b/lib/Account.php index 4993ca354..ea8075eec 100644 --- a/lib/Account.php +++ b/lib/Account.php @@ -370,7 +370,7 @@ private function serializeAdditionalOwners($legalEntity, $additionalOwners) $originalValue = []; } if (($originalValue) && (count($originalValue) > count($additionalOwners))) { - throw new \InvalidArgumentException( + throw new Exception\InvalidArgumentException( "You cannot delete an item from an array, you must instead set a new array" ); } diff --git a/lib/AlipayAccount.php b/lib/AlipayAccount.php index fbb13103b..1dcc09517 100644 --- a/lib/AlipayAccount.php +++ b/lib/AlipayAccount.php @@ -29,7 +29,7 @@ public function instanceUrl() $path = 'sources'; } else { $msg = "Alipay accounts cannot be accessed without a customer ID."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\UnexpectedValueException($msg); } $parentExtn = urlencode(Util\Util::utf8($parent)); $extn = urlencode(Util\Util::utf8($this['id'])); @@ -40,7 +40,7 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException * * @deprecated Alipay accounts are deprecated. Please use the sources API instead. * @link https://stripe.com/docs/sources/alipay @@ -50,7 +50,7 @@ public static function retrieve($_id, $_opts = null) $msg = "Alipay accounts cannot be retrieved without a customer ID. " . "Retrieve an Alipay account using `Customer::retrieveSource(" . "'customer_id', 'alipay_account_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg); } /** @@ -58,7 +58,7 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException * * @deprecated Alipay accounts are deprecated. Please use the sources API instead. * @link https://stripe.com/docs/sources/alipay @@ -68,6 +68,6 @@ public static function update($_id, $_params = null, $_options = null) $msg = "Alipay accounts cannot be updated without a customer ID. " . "Update an Alipay account using `Customer::updateSource(" . "'customer_id', 'alipay_account_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg); } } diff --git a/lib/ApiOperations/All.php b/lib/ApiOperations/All.php index 6ae462713..f674cd408 100644 --- a/lib/ApiOperations/All.php +++ b/lib/ApiOperations/All.php @@ -23,7 +23,7 @@ public static function all($params = null, $opts = null) list($response, $opts) = static::_staticRequest('get', $url, $params, $opts); $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts); if (!($obj instanceof \Stripe\Collection)) { - throw new \Stripe\Error\Api( + throw new \Stripe\Exception\UnexpectedValueException( 'Expected type ' . \Stripe\Collection::class . ', got "' . get_class($obj) . '" instead.' ); } diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index dd048dc5f..6b80c6a27 100644 --- a/lib/ApiOperations/Request.php +++ b/lib/ApiOperations/Request.php @@ -12,7 +12,7 @@ trait Request /** * @param array|null|mixed $params The list of parameters to validate * - * @throws \Stripe\Error\Api if $params exists and is not an array + * @throws \Stripe\Exception\InvalidArgumentException if $params exists and is not an array */ protected static function _validateParams($params = null) { @@ -21,7 +21,7 @@ protected static function _validateParams($params = null) . "method calls. (HINT: an example call to create a charge " . "would be: \"Stripe\\Charge::create(['amount' => 100, " . "'currency' => 'usd', 'source' => 'tok_1234'])\")"; - throw new \Stripe\Error\Api($message); + throw new \Stripe\Exception\InvalidArgumentException($message); } } diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 356e3500d..ee10591d3 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -102,20 +102,20 @@ private static function _encodeObjects($d) * * @return array An array whose first element is an API response and second * element is the API key used to make the request. - * @throws Error\Api - * @throws Error\Authentication - * @throws Error\Card - * @throws Error\InvalidRequest - * @throws Error\OAuth\InvalidClient - * @throws Error\OAuth\InvalidGrant - * @throws Error\OAuth\InvalidRequest - * @throws Error\OAuth\InvalidScope - * @throws Error\OAuth\UnsupportedGrantType - * @throws Error\OAuth\UnsupportedResponseType - * @throws Error\Permission - * @throws Error\RateLimit - * @throws Error\Idempotency - * @throws Error\ApiConnection + * @throws Exception\ApiException + * @throws Exception\AuthenticationException + * @throws Exception\CardException + * @throws Exception\InvalidRequestException + * @throws Exception\OAuth\InvalidClientException + * @throws Exception\OAuth\InvalidGrantException + * @throws Exception\OAuth\InvalidRequestException + * @throws Exception\OAuth\InvalidScopeException + * @throws Exception\OAuth\UnsupportedGrantTypeException + * @throws Exception\OAuth\UnsupportedResponseTypeException + * @throws Exception\PermissionException + * @throws Exception\RateLimitException + * @throws Exception\IdempotencyException + * @throws Exception\ApiConnectionException */ public function request($method, $url, $params = null, $headers = null) { @@ -134,33 +134,33 @@ public function request($method, $url, $params = null, $headers = null) * @param array $rheaders * @param array $resp * - * @throws Error\InvalidRequest if the error is caused by the user. - * @throws Error\Authentication if the error is caused by a lack of + * @throws Exception\InvalidRequestException if the error is caused by the user. + * @throws Exception\AuthenticationException if the error is caused by a lack of * permissions. - * @throws Error\Permission if the error is caused by insufficient + * @throws Exception\PermissionException if the error is caused by insufficient * permissions. - * @throws Error\Card if the error is the error code is 402 (payment + * @throws Exception\CardException if the error is the error code is 402 (payment * required) - * @throws Error\InvalidRequest if the error is caused by the user. - * @throws Error\Idempotency if the error is caused by an idempotency key. - * @throws Error\OAuth\InvalidClient - * @throws Error\OAuth\InvalidGrant - * @throws Error\OAuth\InvalidRequest - * @throws Error\OAuth\InvalidScope - * @throws Error\OAuth\UnsupportedGrantType - * @throws Error\OAuth\UnsupportedResponseType - * @throws Error\Permission if the error is caused by insufficient + * @throws Exception\InvalidRequestException if the error is caused by the user. + * @throws Exception\IdempotencyException if the error is caused by an idempotency key. + * @throws Exception\OAuth\InvalidClientException + * @throws Exception\OAuth\InvalidGrantException + * @throws Exception\OAuth\InvalidRequestException + * @throws Exception\OAuth\InvalidScopeException + * @throws Exception\OAuth\UnsupportedGrantTypeException + * @throws Exception\OAuth\UnsupportedResponseTypeException + * @throws Exception\PermissionException if the error is caused by insufficient * permissions. - * @throws Error\RateLimit if the error is caused by too many requests + * @throws Exception\RateLimitException if the error is caused by too many requests * hitting the API. - * @throws Error\Api otherwise. + * @throws Exception\ApiException otherwise. */ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp) { if (!is_array($resp) || !isset($resp['error'])) { $msg = "Invalid response object from API: $rbody " . "(HTTP response code was $rcode)"; - throw new Error\Api($msg, $rcode, $rbody, $resp, $rheaders); + throw new Exception\UnexpectedValueException($msg, $rcode, $rbody, $resp, $rheaders); } $errorData = $resp['error']; @@ -185,7 +185,7 @@ public function handleErrorResponse($rbody, $rcode, $rheaders, $resp) * @param array $resp * @param array $errorData * - * @return Error\RateLimit|Error\Idempotency|Error\InvalidRequest|Error\Authentication|Error\Card|Error\Permission|Error\Api + * @return Exception\ExceptionInterface */ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $errorData) { @@ -200,25 +200,25 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err // 'rate_limit' code is deprecated, but left here for backwards compatibility // for API versions earlier than 2015-09-08 if ($code == 'rate_limit') { - return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\RateLimitException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code, $param); } if ($type == 'idempotency_error') { - return new Error\Idempotency($msg, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\IdempotencyException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code); } // no break case 404: - return new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\InvalidRequestException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code, $param); case 401: - return new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\AuthenticationException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code); case 402: - return new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders, $declineCode); + return Exception\CardException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code, $declineCode, $param); case 403: - return new Error\Permission($msg, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\PermissionException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code); case 429: - return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\RateLimitException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code, $param); default: - return new Error\Api($msg, $rcode, $rbody, $resp, $rheaders, $code); + return Exception\ApiException::factory($msg, $rcode, $rbody, $resp, $rheaders, $code); } } @@ -231,7 +231,7 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err * @param array $resp * @param string $errorCode * - * @return null|Error\OAuth\InvalidClient|Error\OAuth\InvalidGrant|Error\OAuth\InvalidRequest|Error\OAuth\InvalidScope|Error\OAuth\UnsupportedGrantType|Error\OAuth\UnsupportedResponseType + * @return null|Exception\OAuth\ExceptionInterface */ private static function _specificOAuthError($rbody, $rcode, $rheaders, $resp, $errorCode) { @@ -239,17 +239,17 @@ private static function _specificOAuthError($rbody, $rcode, $rheaders, $resp, $e switch ($errorCode) { case 'invalid_client': - return new Error\OAuth\InvalidClient($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\InvalidClientException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); case 'invalid_grant': - return new Error\OAuth\InvalidGrant($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\InvalidGrantException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); case 'invalid_request': - return new Error\OAuth\InvalidRequest($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\InvalidRequestException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); case 'invalid_scope': - return new Error\OAuth\InvalidScope($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\InvalidScopeException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); case 'unsupported_grant_type': - return new Error\OAuth\UnsupportedGrantType($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\UnsupportedGrantTypeException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); case 'unsupported_response_type': - return new Error\OAuth\UnsupportedResponseType($errorCode, $description, $rcode, $rbody, $resp, $rheaders); + return Exception\OAuth\UnsupportedResponseTypeException::factory($description, $rcode, $rbody, $resp, $rheaders, $errorCode); } return null; @@ -324,9 +324,9 @@ private static function _defaultHeaders($apiKey, $clientInfo = null) * @param array $headers * * @return array - * @throws Error\Api - * @throws Error\ApiConnection - * @throws Error\Authentication + * @throws Exception\ApiException + * @throws Exception\ApiConnectionException + * @throws Exception\AuthenticationException */ private function _requestRaw($method, $url, $params, $headers) { @@ -340,7 +340,7 @@ private function _requestRaw($method, $url, $params, $headers) . '"Stripe::setApiKey()". You can generate API keys from ' . 'the Stripe web interface. See https://stripe.com/api for ' . 'details, or email support@stripe.com if you have any questions.'; - throw new Error\Authentication($msg); + throw new Exception\AuthenticationException($msg); } // Clients can supply arbitrary additional keys to be included in the @@ -413,19 +413,19 @@ private function _requestRaw($method, $url, $params, $headers) * @param resource $resource * * @return \CURLFile|string - * @throws Error\Api + * @throws Exception\InvalidArgumentException */ private function _processResourceParam($resource) { if (get_resource_type($resource) !== 'stream') { - throw new Error\Api( + throw new Exception\InvalidArgumentException( 'Attempted to upload a resource that is not a stream' ); } $metaData = stream_get_meta_data($resource); if ($metaData['wrapper_type'] !== 'plainfile') { - throw new Error\Api( + throw new Exception\InvalidArgumentException( 'Only plainfile resource streams are supported' ); } @@ -440,19 +440,19 @@ private function _processResourceParam($resource) * @param array $rheaders * * @return mixed - * @throws Error\Api - * @throws Error\Authentication - * @throws Error\Card - * @throws Error\InvalidRequest - * @throws Error\OAuth\InvalidClient - * @throws Error\OAuth\InvalidGrant - * @throws Error\OAuth\InvalidRequest - * @throws Error\OAuth\InvalidScope - * @throws Error\OAuth\UnsupportedGrantType - * @throws Error\OAuth\UnsupportedResponseType - * @throws Error\Permission - * @throws Error\RateLimit - * @throws Error\Idempotency + * @throws Exception\ApiException + * @throws Exception\AuthenticationException + * @throws Exception\CardException + * @throws Exception\InvalidRequestException + * @throws Exception\OAuth\InvalidClientException + * @throws Exception\OAuth\InvalidGrantException + * @throws Exception\OAuth\InvalidRequestException + * @throws Exception\OAuth\InvalidScopeException + * @throws Exception\OAuth\UnsupportedGrantTypeException + * @throws Exception\OAuth\UnsupportedResponseTypeException + * @throws Exception\PermissionException + * @throws Exception\RateLimitException + * @throws Exception\IdempotencyException */ private function _interpretResponse($rbody, $rcode, $rheaders) { @@ -461,7 +461,7 @@ private function _interpretResponse($rbody, $rcode, $rheaders) if ($resp === null && $jsonError !== JSON_ERROR_NONE) { $msg = "Invalid response body from API: $rbody " . "(HTTP response code was $rcode, json_last_error() was $jsonError)"; - throw new Error\Api($msg, $rcode, $rbody); + throw new Exception\UnexpectedValueException($msg, $rcode, $rbody); } if ($rcode < 200 || $rcode >= 300) { diff --git a/lib/ApiResource.php b/lib/ApiResource.php index fe5943216..725615aaa 100644 --- a/lib/ApiResource.php +++ b/lib/ApiResource.php @@ -94,7 +94,7 @@ public static function resourceUrl($id) $class = get_called_class(); $message = "Could not determine which URL to request: " . "$class instance has invalid ID: $id"; - throw new Error\InvalidRequest($message, null); + throw new Exception\UnexpectedValueException($message); } $id = Util\Util::utf8($id); $base = static::classUrl(); diff --git a/lib/ApplicationFeeRefund.php b/lib/ApplicationFeeRefund.php index b242f2024..77ba0ccd7 100644 --- a/lib/ApplicationFeeRefund.php +++ b/lib/ApplicationFeeRefund.php @@ -32,7 +32,7 @@ public function instanceUrl() $id = $this['id']; $fee = $this['fee']; if (!$id) { - throw new Error\InvalidRequest( + throw new Exception\UnexpectedValueException( "Could not determine which URL to request: " . "class instance has invalid ID: $id", null diff --git a/lib/BankAccount.php b/lib/BankAccount.php index ca019c2c7..8108714ea 100644 --- a/lib/BankAccount.php +++ b/lib/BankAccount.php @@ -56,7 +56,7 @@ public function instanceUrl() $path = 'external_accounts'; } else { $msg = "Bank accounts cannot be accessed without a customer ID or account ID."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\UnexpectedValueException($msg, null); } $parentExtn = urlencode(Util\Util::utf8($parent)); $extn = urlencode(Util\Util::utf8($this['id'])); @@ -67,7 +67,7 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { @@ -76,7 +76,7 @@ public static function retrieve($_id, $_opts = null) "`Customer::retrieveSource('customer_id', " . "'bank_account_id')` or `Account::retrieveExternalAccount(" . "'account_id', 'bank_account_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } /** @@ -84,7 +84,7 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function update($_id, $_params = null, $_options = null) { @@ -93,7 +93,7 @@ public static function update($_id, $_params = null, $_options = null) "`Customer::updateSource('customer_id', 'bank_account_id', " . "\$updateParams)` or `Account::updateExternalAccount(" . "'account_id', 'bank_account_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } /** diff --git a/lib/Capability.php b/lib/Capability.php index 5cb224f9e..86324ad58 100644 --- a/lib/Capability.php +++ b/lib/Capability.php @@ -38,7 +38,7 @@ public function instanceUrl() $id = $this['id']; $account = $this['account']; if (!$id) { - throw new Error\InvalidRequest( + throw new Exception\UnexpectedValueException( "Could not determine which URL to request: " . "class instance has invalid ID: $id", null @@ -57,14 +57,14 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { $msg = "Capabilities cannot be retrieved without an account ID. " . "Retrieve a capability using `Account::retrieveCapability(" . "'account_id', 'capability_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } /** @@ -72,13 +72,13 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function update($_id, $_params = null, $_options = null) { $msg = "Capabilities cannot be updated without an account ID. " . "Update a capability using `Account::updateCapability(" . "'account_id', 'capability_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } } diff --git a/lib/Card.php b/lib/Card.php index 98dc70596..a306f4864 100644 --- a/lib/Card.php +++ b/lib/Card.php @@ -89,7 +89,7 @@ public function instanceUrl() $path = 'cards'; } else { $msg = "Cards cannot be accessed without a customer ID, account ID or recipient ID."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\UnexpectedValueException($msg); } $parentExtn = urlencode(Util\Util::utf8($parent)); $extn = urlencode(Util\Util::utf8($this['id'])); @@ -100,7 +100,7 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { @@ -108,7 +108,7 @@ public static function retrieve($_id, $_opts = null) "account ID. Retrieve a card using " . "`Customer::retrieveSource('customer_id', 'card_id')` or " . "`Account::retrieveExternalAccount('account_id', 'card_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg); } /** @@ -116,7 +116,7 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function update($_id, $_params = null, $_options = null) { @@ -125,6 +125,6 @@ public static function update($_id, $_params = null, $_options = null) "`Customer::updateSource('customer_id', 'card_id', " . "\$updateParams)` or `Account::updateExternalAccount(" . "'account_id', 'card_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg); } } diff --git a/lib/Charge.php b/lib/Charge.php index 3ca70b1fc..9b2369331 100644 --- a/lib/Charge.php +++ b/lib/Charge.php @@ -62,7 +62,7 @@ class Charge extends ApiResource /** * Possible string representations of decline codes. - * These strings are applicable to the decline_code property of the \Stripe\Error\Card exception. + * These strings are applicable to the decline_code property of the \Stripe\Exception\CardException exception. * @link https://stripe.com/docs/declines/codes */ const DECLINED_APPROVE_WITH_ID = 'approve_with_id'; diff --git a/lib/Collection.php b/lib/Collection.php index f51a3ce41..161ebc925 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -107,7 +107,7 @@ private function extractPathAndUpdateParams($params) { $url = parse_url($this->url); if (!isset($url['path'])) { - throw new Error\Api("Could not parse list url into parts: $url"); + throw new Exception\UnexpectedValueException("Could not parse list url into parts: $url"); } if (isset($url['query'])) { diff --git a/lib/CustomerBalanceTransaction.php b/lib/CustomerBalanceTransaction.php index df1f87902..357b0b83c 100644 --- a/lib/CustomerBalanceTransaction.php +++ b/lib/CustomerBalanceTransaction.php @@ -45,7 +45,7 @@ public function instanceUrl() $id = $this['id']; $customer = $this['customer']; if (!$id) { - throw new Error\InvalidRequest( + throw new Exception\UnexpectedValueException( "Could not determine which URL to request: class instance has invalid ID: $id", null ); @@ -63,7 +63,7 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { @@ -71,7 +71,7 @@ public static function retrieve($_id, $_opts = null) "customer ID. Retrieve a Customer Balance Transaction using " . "`Customer::retrieveBalanceTransaction('customer_id', " . "'balance_transaction_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } /** @@ -79,7 +79,7 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function update($_id, $_params = null, $_options = null) { @@ -87,6 +87,6 @@ public static function update($_id, $_params = null, $_options = null) "customer ID. Update a Customer Balance Transaction using " . "`Customer::updateBalanceTransaction('customer_id', " . "'balance_transaction_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } } diff --git a/lib/EphemeralKey.php b/lib/EphemeralKey.php index ea4cc756c..389b85a4a 100644 --- a/lib/EphemeralKey.php +++ b/lib/EphemeralKey.php @@ -33,7 +33,7 @@ class EphemeralKey extends ApiResource public static function create($params = null, $opts = null) { if (!$opts['stripe_version']) { - throw new \InvalidArgumentException('stripe_version must be specified to create an ephemeral key'); + throw new Exception\InvalidArgumentException('stripe_version must be specified to create an ephemeral key'); } return self::_create($params, $opts); } diff --git a/lib/Error/Api.php b/lib/Error/Api.php deleted file mode 100644 index df97730af..000000000 --- a/lib/Error/Api.php +++ /dev/null @@ -1,14 +0,0 @@ -httpStatus = $httpStatus; - $this->httpBody = $httpBody; - $this->jsonBody = $jsonBody; - $this->httpHeaders = $httpHeaders; - $this->stripeCode = $stripeCode; - - $this->requestId = null; - if ($httpHeaders && isset($httpHeaders['Request-Id'])) { - $this->requestId = $httpHeaders['Request-Id']; - } - - $this->error = $this->constructErrorObject(); - } - - public function getError() - { - return $this->error; - } - - public function getHttpBody() - { - return $this->httpBody; - } - - public function getHttpHeaders() - { - return $this->httpHeaders; - } - - public function getHttpStatus() - { - return $this->httpStatus; - } - - public function getJsonBody() - { - return $this->jsonBody; - } - - public function getRequestId() - { - return $this->requestId; - } - - public function getStripeCode() - { - return $this->stripeCode; - } - - public function __toString() - { - $statusStr = ($this->getHttpStatus() == null) ? "" : "(Status {$this->getHttpStatus()}) "; - $idStr = ($this->getRequestId() == null) ? "" : "(Request {$this->getRequestId()}) "; - return "{$statusStr}{$idStr}{$this->getMessage()}"; - } - - protected function constructErrorObject() - { - if (is_null($this->jsonBody) || !array_key_exists('error', $this->jsonBody)) { - return null; - } - - return \Stripe\ErrorObject::constructFrom($this->jsonBody['error']); - } -} diff --git a/lib/Error/Card.php b/lib/Error/Card.php deleted file mode 100644 index 205f238aa..000000000 --- a/lib/Error/Card.php +++ /dev/null @@ -1,39 +0,0 @@ -declineCode = $declineCode; - $this->stripeParam = $stripeParam; - } - - public function getDeclineCode() - { - return $this->declineCode; - } - - public function getStripeParam() - { - return $this->stripeParam; - } -} diff --git a/lib/Error/Idempotency.php b/lib/Error/Idempotency.php deleted file mode 100644 index 2d7eca3d8..000000000 --- a/lib/Error/Idempotency.php +++ /dev/null @@ -1,12 +0,0 @@ -stripeParam = $stripeParam; - } - - public function getStripeParam() - { - return $this->stripeParam; - } -} diff --git a/lib/Error/OAuth/InvalidClient.php b/lib/Error/OAuth/InvalidClient.php deleted file mode 100644 index 20fc4fce0..000000000 --- a/lib/Error/OAuth/InvalidClient.php +++ /dev/null @@ -1,14 +0,0 @@ -errorCode = $code; - } - - public function getErrorCode() - { - return $this->errorCode; - } - - protected function constructErrorObject() - { - if (is_null($this->jsonBody)) { - return null; - } - - return \Stripe\ErrorObject::constructFrom($this->jsonBody); - } -} diff --git a/lib/Error/OAuth/UnsupportedGrantType.php b/lib/Error/OAuth/UnsupportedGrantType.php deleted file mode 100644 index 4a720b3f9..000000000 --- a/lib/Error/OAuth/UnsupportedGrantType.php +++ /dev/null @@ -1,12 +0,0 @@ -sigHeader = $sigHeader; - } - - public function getSigHeader() - { - return $this->sigHeader; - } -} diff --git a/lib/Exception/ApiConnectionException.php b/lib/Exception/ApiConnectionException.php new file mode 100644 index 000000000..3a77e5fe5 --- /dev/null +++ b/lib/Exception/ApiConnectionException.php @@ -0,0 +1,15 @@ +setDeclineCode($declineCode); + $instance->setStripeParam($stripeParam); + + return $instance; + } + + /** + * Gets the decline code. + * + * @return string|null + */ + public function getDeclineCode() + { + return $this->declineCode; + } + + /** + * Sets the decline code. + * + * @param string|null $declineCode + */ + public function setDeclineCode($declineCode) + { + $this->declineCode = $declineCode; + } + + /** + * Gets the parameter related to the error. + * + * @return string|null + */ + public function getStripeParam() + { + return $this->stripeParam; + } + + /** + * Sets the parameter related to the error. + * + * @param string|null $stripeParam + */ + public function setStripeParam($stripeParam) + { + $this->stripeParam = $stripeParam; + } +} diff --git a/lib/Exception/ExceptionInterface.php b/lib/Exception/ExceptionInterface.php new file mode 100644 index 000000000..5558c3826 --- /dev/null +++ b/lib/Exception/ExceptionInterface.php @@ -0,0 +1,14 @@ +setHttpStatus($httpStatus); + $instance->setHttpBody($httpBody); + $instance->setJsonBody($jsonBody); + $instance->setHttpHeaders($httpHeaders); + $instance->setStripeCode($stripeCode); + + $instance->setRequestId(null); + if ($httpHeaders && isset($httpHeaders['Request-Id'])) { + $instance->setRequestId($httpHeaders['Request-Id']); + } + + $instance->setError($instance->constructErrorObject()); + + return $instance; + } + + /** + * Gets the Stripe error object. + * + * @return \Stripe\ErrorObject|null + */ + public function getError() + { + return $this->error; + } + + /** + * Sets the Stripe error object. + * + * @param \Stripe\ErrorObject|null $error + */ + public function setError($error) + { + $this->error = $error; + } + + /** + * Gets the HTTP body as a string. + * + * @return string|null + */ + public function getHttpBody() + { + return $this->httpBody; + } + + /** + * Sets the HTTP body as a string. + * + * @param string|null $httpBody + */ + public function setHttpBody($httpBody) + { + $this->httpBody = $httpBody; + } + + /** + * Gets the HTTP headers array. + * + * @return array|\Stripe\Util\CaseInsensitiveArray|null + */ + public function getHttpHeaders() + { + return $this->httpHeaders; + } + + /** + * Sets the HTTP headers array. + * + * @param array|\Stripe\Util\CaseInsensitiveArray|null $httpHeaders + */ + public function setHttpHeaders($httpHeaders) + { + $this->httpHeaders = $httpHeaders; + } + + /** + * Gets the HTTP status code. + * + * @return int|null + */ + public function getHttpStatus() + { + return $this->httpStatus; + } + + /** + * Sets the HTTP status code. + * + * @param int|null $httpStatus + */ + public function setHttpStatus($httpStatus) + { + $this->httpStatus = $httpStatus; + } + + /** + * Gets the JSON deserialized body. + * + * @return array|null + */ + public function getJsonBody() + { + return $this->jsonBody; + } + + /** + * Sets the JSON deserialized body. + * + * @param array|null $jsonBody + */ + public function setJsonBody($jsonBody) + { + $this->jsonBody = $jsonBody; + } + + /** + * Gets the Stripe request ID. + * + * @return string|null + */ + public function getRequestId() + { + return $this->requestId; + } + + /** + * Sets the Stripe request ID. + * + * @param string|null $requestId + */ + public function setRequestId($requestId) + { + $this->requestId = $requestId; + } + + /** + * Gets the Stripe error code. + * + * @return string|null + */ + public function getStripeCode() + { + return $this->stripeCode; + } + + /** + * Sets the Stripe error code. + * + * @param string|null $stripeCode + */ + public function setStripeCode($stripeCode) + { + $this->stripeCode = $stripeCode; + } + + /** + * Returns the string representation of the exception. + * + * @return string + */ + public function __toString() + { + $statusStr = ($this->getHttpStatus() == null) ? "" : "(Status {$this->getHttpStatus()}) "; + $idStr = ($this->getRequestId() == null) ? "" : "(Request {$this->getRequestId()}) "; + return "{$statusStr}{$idStr}{$this->getMessage()}"; + } + + protected function constructErrorObject() + { + if (is_null($this->jsonBody) || !array_key_exists('error', $this->jsonBody)) { + return null; + } + + return \Stripe\ErrorObject::constructFrom($this->jsonBody['error']); + } +} diff --git a/lib/Exception/IdempotencyException.php b/lib/Exception/IdempotencyException.php new file mode 100644 index 000000000..b89b3f6e1 --- /dev/null +++ b/lib/Exception/IdempotencyException.php @@ -0,0 +1,14 @@ +setStripeParam($stripeParam); + + return $instance; + } + + /** + * Gets the parameter related to the error. + * + * @return string|null + */ + public function getStripeParam() + { + return $this->stripeParam; + } + + /** + * Sets the parameter related to the error. + * + * @param string|null $stripeParam + */ + public function setStripeParam($stripeParam) + { + $this->stripeParam = $stripeParam; + } +} diff --git a/lib/Exception/OAuth/ExceptionInterface.php b/lib/Exception/OAuth/ExceptionInterface.php new file mode 100644 index 000000000..dd4266201 --- /dev/null +++ b/lib/Exception/OAuth/ExceptionInterface.php @@ -0,0 +1,10 @@ +jsonBody)) { + return null; + } + + return \Stripe\OAuthErrorObject::constructFrom($this->jsonBody); + } +} diff --git a/lib/Exception/OAuth/InvalidClientException.php b/lib/Exception/OAuth/InvalidClientException.php new file mode 100644 index 000000000..48dce3b58 --- /dev/null +++ b/lib/Exception/OAuth/InvalidClientException.php @@ -0,0 +1,15 @@ +setSigHeader($sigHeader); + + return $instance; + } + + /** + * Gets the `Stripe-Signature` HTTP header. + * + * @return string|null + */ + public function getSigHeader() + { + return $this->sigHeader; + } + + /** + * Sets the `Stripe-Signature` HTTP header. + * + * @param string|null $sigHeader + */ + public function setSigHeader($sigHeader) + { + $this->sigHeader = $sigHeader; + } +} diff --git a/lib/Exception/UnexpectedValueException.php b/lib/Exception/UnexpectedValueException.php new file mode 100644 index 000000000..0a629edf7 --- /dev/null +++ b/lib/Exception/UnexpectedValueException.php @@ -0,0 +1,7 @@ +defaultOptions)) { // call defaultOptions callback, set options to return value $opts = call_user_func_array($this->defaultOptions, func_get_args()); if (!is_array($opts)) { - throw new Error\Api("Non-array value returned by defaultOptions CurlClient callback"); + throw new Exception\UnexpectedValueException("Non-array value returned by defaultOptions CurlClient callback"); } } elseif (is_array($this->defaultOptions)) { // set default curlopts from array $opts = $this->defaultOptions; @@ -174,7 +174,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile) if ($method == 'get') { if ($hasFile) { - throw new Error\Api( + throw new Exception\UnexpectedValueException( "Issuing a GET request with a file parameter" ); } @@ -193,7 +193,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile) $absUrl = "$absUrl?$encoded"; } } else { - throw new Error\Api("Unrecognized method $method"); + throw new Exception\UnexpectedValueException("Unrecognized method $method"); } // It is only safe to retry network failures on POST requests if we @@ -298,7 +298,7 @@ private function executeRequestWithRetries($opts, $absUrl) * @param int $errno * @param string $message * @param int $numRetries - * @throws Error\ApiConnection + * @throws Exception\ApiConnectionException */ private function handleCurlError($url, $errno, $message, $numRetries) { @@ -330,7 +330,7 @@ private function handleCurlError($url, $errno, $message, $numRetries) $msg .= "\n\nRequest was retried $numRetries times."; } - throw new Error\ApiConnection($msg); + throw new Exception\ApiConnectionException($msg); } /** diff --git a/lib/OAuth.php b/lib/OAuth.php index e9a82e62a..2e77f7e89 100644 --- a/lib/OAuth.php +++ b/lib/OAuth.php @@ -86,7 +86,7 @@ private static function _getClientId($params = null) . 'after registering your account as a platform. See ' . 'https://stripe.com/docs/connect/standard-accounts for details, ' . 'or email support@stripe.com if you have any questions.'; - throw new Error\Authentication($msg); + throw new Exception\AuthenticationException($msg); } return $clientId; } diff --git a/lib/Person.php b/lib/Person.php index 52f920a8e..c100218b2 100644 --- a/lib/Person.php +++ b/lib/Person.php @@ -63,7 +63,7 @@ public function instanceUrl() $id = $this['id']; $account = $this['account']; if (!$id) { - throw new Error\InvalidRequest( + throw new Exception\UnexpectedValueException( "Could not determine which URL to request: " . "class instance has invalid ID: $id", null @@ -82,14 +82,14 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { $msg = "Persons cannot be retrieved without an account ID. Retrieve " . "a person using `Account::retrievePerson('account_id', " . "'person_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } /** @@ -97,13 +97,13 @@ public static function retrieve($_id, $_opts = null) * @param array|null $_params * @param array|string|null $_options * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function update($_id, $_params = null, $_options = null) { $msg = "Persons cannot be updated without an account ID. Update " . "a person using `Account::updatePerson('account_id', " . "'person_id', \$updateParams)`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg, null); } } diff --git a/lib/Source.php b/lib/Source.php index f5af360d7..2356aed39 100644 --- a/lib/Source.php +++ b/lib/Source.php @@ -90,7 +90,7 @@ public function detach($params = null, $options = null) $class = get_class($this); $msg = "Could not determine which URL to request: $class instance " . "has invalid ID: $id"; - throw new Error\InvalidRequest($msg, null); + throw new Exception\UnexpectedValueException($msg, null); } if ($this['customer']) { @@ -105,7 +105,7 @@ public function detach($params = null, $options = null) } else { $message = "This source object does not appear to be currently attached " . "to a customer object."; - throw new Error\Api($message); + throw new Exception\UnexpectedValueException($message); } } diff --git a/lib/StripeObject.php b/lib/StripeObject.php index ea3f60fbd..635ce546f 100644 --- a/lib/StripeObject.php +++ b/lib/StripeObject.php @@ -116,14 +116,14 @@ public function __construct($id = null, $opts = null) public function __set($k, $v) { if (static::getPermanentAttributes()->includes($k)) { - throw new \InvalidArgumentException( + throw new Exception\InvalidArgumentException( "Cannot set $k on this object. HINT: you can't set: " . join(', ', static::getPermanentAttributes()->toArray()) ); } if ($v === "") { - throw new \InvalidArgumentException( + throw new Exception\InvalidArgumentException( 'You cannot set \''.$k.'\'to an empty string. ' .'We interpret empty strings as NULL in requests. ' .'You may set obj->'.$k.' = NULL to delete the property' @@ -367,7 +367,7 @@ public function serializeParamsValue($value, $original, $unsaved, $force, $key = } elseif (isset($value->id)) { return $value; } else { - throw new \InvalidArgumentException( + throw new Exception\InvalidArgumentException( "Cannot save property `$key` containing an API resource of type " . get_class($value) . ". It doesn't appear to be persisted and is " . "not marked as `saveWithParent`." @@ -508,7 +508,7 @@ public static function emptyValues($obj) } elseif ($obj instanceof StripeObject) { $values = $obj->_values; } else { - throw new \InvalidArgumentException( + throw new Exception\InvalidArgumentException( "empty_values got got unexpected object type: " . get_class($obj) ); } diff --git a/lib/TaxId.php b/lib/TaxId.php index 892954e55..163274e36 100644 --- a/lib/TaxId.php +++ b/lib/TaxId.php @@ -51,9 +51,8 @@ public function instanceUrl() $id = $this['id']; $customer = $this['customer']; if (!$id) { - throw new Error\InvalidRequest( - "Could not determine which URL to request: class instance has invalid ID: $id", - null + throw new Exception\UnexpectedValueException( + "Could not determine which URL to request: class instance has invalid ID: $id" ); } $id = Util\Util::utf8($id); @@ -69,13 +68,13 @@ public function instanceUrl() * @param array|string $_id * @param array|string|null $_opts * - * @throws \Stripe\Error\InvalidRequest + * @throws \Stripe\Exception\BadMethodCallException */ public static function retrieve($_id, $_opts = null) { $msg = "Tax IDs cannot be retrieved without a customer ID. Retrieve " . "a tax ID using `Customer::retrieveTaxId('customer_id', " . "'tax_id_id')`."; - throw new Error\InvalidRequest($msg, null); + throw new Exception\BadMethodCallException($msg); } } diff --git a/lib/TransferReversal.php b/lib/TransferReversal.php index a4f94f2fb..da8125353 100644 --- a/lib/TransferReversal.php +++ b/lib/TransferReversal.php @@ -34,7 +34,7 @@ public function instanceUrl() $id = $this['id']; $transfer = $this['transfer']; if (!$id) { - throw new Error\InvalidRequest( + throw new Exception\UnexpectedValueException( "Could not determine which URL to request: " . "class instance has invalid ID: $id", null diff --git a/lib/Util/DefaultLogger.php b/lib/Util/DefaultLogger.php index 1a8663699..ce3b0b698 100644 --- a/lib/Util/DefaultLogger.php +++ b/lib/Util/DefaultLogger.php @@ -11,7 +11,7 @@ class DefaultLogger implements LoggerInterface public function error($message, array $context = []) { if (count($context) > 0) { - throw new \Exception('DefaultLogger does not currently implement context. Please implement if you need it.'); + throw new \Stripe\Exception\BadMethodCallException('DefaultLogger does not currently implement context. Please implement if you need it.'); } error_log($message); } diff --git a/lib/Util/RequestOptions.php b/lib/Util/RequestOptions.php index 495236224..711632909 100644 --- a/lib/Util/RequestOptions.php +++ b/lib/Util/RequestOptions.php @@ -2,7 +2,7 @@ namespace Stripe\Util; -use Stripe\Error; +use Stripe\Exception; class RequestOptions { @@ -103,6 +103,6 @@ public static function parse($options) . 'optional per-request apiKey, which must be a string, or ' . 'per-request options, which must be an array. (HINT: you can set ' . 'a global apiKey by "Stripe::setApiKey()")'; - throw new Error\Api($message); + throw new Exception\InvalidArgumentException($message); } } diff --git a/lib/Webhook.php b/lib/Webhook.php index 45c7dc0f3..7f0fd510e 100644 --- a/lib/Webhook.php +++ b/lib/Webhook.php @@ -7,10 +7,10 @@ abstract class Webhook const DEFAULT_TOLERANCE = 300; /** - * Returns an Event instance using the provided JSON payload. Throws a - * \UnexpectedValueException if the payload is not valid JSON, and a - * \Stripe\SignatureVerificationException if the signature verification - * fails for any reason. + * Returns an Event instance using the provided JSON payload. Throws an + * Exception\UnexpectedValueException if the payload is not valid JSON, and + * an Exception\SignatureVerificationException if the signature + * verification fails for any reason. * * @param string $payload the payload sent by Stripe. * @param string $sigHeader the contents of the signature header sent by @@ -18,9 +18,9 @@ abstract class Webhook * @param string $secret secret used to generate the signature. * @param int $tolerance maximum difference allowed between the header's * timestamp and the current time - * @return \Stripe\Event the Event instance - * @throws \UnexpectedValueException if the payload is not valid JSON, - * @throws \Stripe\Error\SignatureVerification if the verification fails. + * @return Event the Event instance + * @throws Exception\UnexpectedValueException if the payload is not valid JSON, + * @throws Exception\SignatureVerification if the verification fails. */ public static function constructEvent($payload, $sigHeader, $secret, $tolerance = self::DEFAULT_TOLERANCE) { @@ -31,7 +31,7 @@ public static function constructEvent($payload, $sigHeader, $secret, $tolerance if ($data === null && $jsonError !== JSON_ERROR_NONE) { $msg = "Invalid payload: $payload " . "(json_last_error() was $jsonError)"; - throw new \UnexpectedValueException($msg); + throw new Exception\UnexpectedValueException($msg); } $event = Event::constructFrom($data); diff --git a/lib/WebhookSignature.php b/lib/WebhookSignature.php index 9f8be8777..1a9a95ca8 100644 --- a/lib/WebhookSignature.php +++ b/lib/WebhookSignature.php @@ -7,9 +7,9 @@ abstract class WebhookSignature const EXPECTED_SCHEME = "v1"; /** - * Verifies the signature header sent by Stripe. Throws a - * SignatureVerification exception if the verification fails for any - * reason. + * Verifies the signature header sent by Stripe. Throws an + * Exception\SignatureVerification exception if the verification fails for + * any reason. * * @param string $payload the payload sent by Stripe. * @param string $header the contents of the signature header sent by @@ -17,7 +17,7 @@ abstract class WebhookSignature * @param string $secret secret used to generate the signature. * @param int $tolerance maximum difference allowed between the header's * timestamp and the current time - * @throws \Stripe\Error\SignatureVerification if the verification fails. + * @throws Exception\SignatureVerification if the verification fails. * @return bool */ public static function verifyHeader($payload, $header, $secret, $tolerance = null) @@ -26,14 +26,14 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul $timestamp = self::getTimestamp($header); $signatures = self::getSignatures($header, self::EXPECTED_SCHEME); if ($timestamp == -1) { - throw new Error\SignatureVerification( + throw Exception\SignatureVerificationException::factory( "Unable to extract timestamp and signatures from header", $header, $payload ); } if (empty($signatures)) { - throw new Error\SignatureVerification( + throw Exception\SignatureVerificationException::factory( "No signatures found with expected scheme", $header, $payload @@ -52,7 +52,7 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul } } if (!$signatureFound) { - throw new Error\SignatureVerification( + throw Exception\SignatureVerificationException::factory( "No signatures found matching the expected signature for payload", $header, $payload @@ -61,7 +61,7 @@ public static function verifyHeader($payload, $header, $secret, $tolerance = nul // Check if timestamp is within tolerance if (($tolerance > 0) && (abs(time() - $timestamp) > $tolerance)) { - throw new Error\SignatureVerification( + throw Exception\SignatureVerificationException::factory( "Timestamp outside the tolerance zone", $header, $payload diff --git a/tests/Stripe/AlipayAccountTest.php b/tests/Stripe/AlipayAccountTest.php index cb48a1007..6f1716e44 100644 --- a/tests/Stripe/AlipayAccountTest.php +++ b/tests/Stripe/AlipayAccountTest.php @@ -34,7 +34,7 @@ public function testHasCorrectUrlForCustomer() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyRetrievable() { @@ -54,7 +54,7 @@ public function testIsSaveable() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyUpdatable() { diff --git a/tests/Stripe/ApiRequestorTest.php b/tests/Stripe/ApiRequestorTest.php index 8105af7ff..60ab94a69 100644 --- a/tests/Stripe/ApiRequestorTest.php +++ b/tests/Stripe/ApiRequestorTest.php @@ -80,7 +80,7 @@ public function testDefaultHeaders() } /** - * @expectedException \Stripe\Error\Authentication + * @expectedException \Stripe\Exception\AuthenticationException * @expectedExceptionMessageRegExp #No API key provided# */ public function testRaisesAuthenticationErrorWhenNoApiKey() @@ -110,7 +110,7 @@ public function testRaisesInvalidRequestErrorOn400() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\InvalidRequest $e) { + } catch (Exception\InvalidRequestException $e) { $this->assertSame(400, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('Missing id', $e->getMessage()); @@ -140,7 +140,7 @@ public function testRaisesIdempotencyErrorOn400AndTypeIdempotencyError() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\Idempotency $e) { + } catch (Exception\IdempotencyException $e) { $this->assertSame(400, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame("Keys for idempotent requests can only be used with the same parameters they were first used with. Try using a key other than 'abc' if you meant to execute a different request.", $e->getMessage()); @@ -169,7 +169,7 @@ public function testRaisesAuthenticationErrorOn401() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\Authentication $e) { + } catch (Exception\AuthenticationException $e) { $this->assertSame(401, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('You did not provide an API key.', $e->getMessage()); @@ -202,7 +202,7 @@ public function testRaisesCardErrorOn402() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\Card $e) { + } catch (Exception\CardException $e) { $this->assertSame(402, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('Your card was declined.', $e->getMessage()); @@ -234,7 +234,7 @@ public function testRaisesPermissionErrorOn403() try { Account::retrieve('foo'); $this->fail("Did not raise error"); - } catch (Error\Permission $e) { + } catch (Exception\PermissionException $e) { $this->assertSame(403, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame("The provided key 'sk_test_********************1234' does not have access to account 'foo' (or that account does not exist). Application access may have been revoked.", $e->getMessage()); @@ -264,7 +264,7 @@ public function testRaisesInvalidRequestErrorOn404() try { Charge::retrieve('foo'); $this->fail("Did not raise error"); - } catch (Error\InvalidRequest $e) { + } catch (Exception\InvalidRequestException $e) { $this->assertSame(404, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('No such charge: foo', $e->getMessage()); @@ -293,11 +293,12 @@ public function testRaisesRateLimitErrorOn429() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\RateLimit $e) { + } catch (Exception\RateLimitException $e) { $this->assertSame(429, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('Too many requests', $e->getMessage()); } catch (\Exception $e) { + var_dump($e); $this->fail("Unexpected exception: " . get_class($e)); } } @@ -322,7 +323,7 @@ public function testRaisesRateLimitErrorOn400AndCodeRateLimit() try { Charge::create(); $this->fail("Did not raise error"); - } catch (Error\RateLimit $e) { + } catch (Exception\RateLimitException $e) { $this->assertSame(400, $e->getHttpStatus()); $this->assertTrue(is_array($e->getJsonBody())); $this->assertSame('Too many requests', $e->getMessage()); @@ -350,9 +351,9 @@ public function testRaisesOAuthInvalidRequestError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\InvalidRequest $e) { + } catch (Exception\OAuth\InvalidRequestException $e) { $this->assertSame(400, $e->getHttpStatus()); - $this->assertSame('invalid_request', $e->getErrorCode()); + $this->assertSame('invalid_request', $e->getStripeCode()); $this->assertSame('No grant type specified', $e->getMessage()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); @@ -378,9 +379,9 @@ public function testRaisesOAuthInvalidClientError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\InvalidClient $e) { + } catch (Exception\OAuth\InvalidClientException $e) { $this->assertSame(401, $e->getHttpStatus()); - $this->assertSame('invalid_client', $e->getErrorCode()); + $this->assertSame('invalid_client', $e->getStripeCode()); $this->assertSame('No authentication was provided. Send your secret API key using the Authorization header, or as a client_secret POST parameter.', $e->getMessage()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); @@ -406,9 +407,9 @@ public function testRaisesOAuthInvalidGrantError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\InvalidGrant $e) { + } catch (Exception\OAuth\InvalidGrantException $e) { $this->assertSame(400, $e->getHttpStatus()); - $this->assertSame('invalid_grant', $e->getErrorCode()); + $this->assertSame('invalid_grant', $e->getStripeCode()); $this->assertSame('This authorization code has already been used. All tokens issued with this code have been revoked.', $e->getMessage()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); @@ -434,9 +435,9 @@ public function testRaisesOAuthInvalidScopeError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\InvalidScope $e) { + } catch (Exception\OAuth\InvalidScopeException $e) { $this->assertSame(400, $e->getHttpStatus()); - $this->assertSame('invalid_scope', $e->getErrorCode()); + $this->assertSame('invalid_scope', $e->getStripeCode()); $this->assertSame('Invalid scope provided: invalid_scope.', $e->getMessage()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); @@ -461,9 +462,9 @@ public function testRaisesOAuthUnsupportedGrantTypeError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\UnsupportedGrantType $e) { + } catch (Exception\OAuth\UnsupportedGrantTypeException $e) { $this->assertSame(400, $e->getHttpStatus()); - $this->assertSame('unsupported_grant_type', $e->getErrorCode()); + $this->assertSame('unsupported_grant_type', $e->getStripeCode()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); } @@ -488,9 +489,9 @@ public function testRaisesOAuthUnsupportedResponseTypeError() try { OAuth::token(); $this->fail("Did not raise error"); - } catch (Error\OAuth\UnsupportedResponseType $e) { + } catch (Exception\OAuth\UnsupportedResponseTypeException $e) { $this->assertSame(400, $e->getHttpStatus()); - $this->assertSame('unsupported_response_type', $e->getErrorCode()); + $this->assertSame('unsupported_response_type', $e->getStripeCode()); $this->assertSame("Only 'code' response_type is supported, but 'unsupported_response_type' was provided", $e->getMessage()); } catch (\Exception $e) { $this->fail("Unexpected exception: " . get_class($e)); diff --git a/tests/Stripe/BankAccountTest.php b/tests/Stripe/BankAccountTest.php index 33ab4f792..70d200dad 100644 --- a/tests/Stripe/BankAccountTest.php +++ b/tests/Stripe/BankAccountTest.php @@ -43,7 +43,7 @@ public function testHasCorrectUrlForAccount() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyRetrievable() { @@ -63,7 +63,7 @@ public function testIsSaveable() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyUpdatable() { diff --git a/tests/Stripe/CapabilityTest.php b/tests/Stripe/CapabilityTest.php index 1d0b80717..1e20a9196 100644 --- a/tests/Stripe/CapabilityTest.php +++ b/tests/Stripe/CapabilityTest.php @@ -17,7 +17,7 @@ public function testHasCorrectUrl() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyRetrievable() { @@ -37,7 +37,7 @@ public function testIsSaveable() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyUpdatable() { diff --git a/tests/Stripe/CardTest.php b/tests/Stripe/CardTest.php index ec19558f1..59aa4334b 100644 --- a/tests/Stripe/CardTest.php +++ b/tests/Stripe/CardTest.php @@ -52,7 +52,7 @@ public function testHasCorrectUrlForRecipient() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyRetrievable() { @@ -72,7 +72,7 @@ public function testIsSaveable() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyUpdatable() { diff --git a/tests/Stripe/Error/SignatureVerificationTest.php b/tests/Stripe/Error/SignatureVerificationTest.php deleted file mode 100644 index 020a41f8f..000000000 --- a/tests/Stripe/Error/SignatureVerificationTest.php +++ /dev/null @@ -1,12 +0,0 @@ -assertSame('sig_header', $e->getSigHeader()); - } -} diff --git a/tests/Stripe/Error/BaseTest.php b/tests/Stripe/Exception/ExceptionTraitTest.php similarity index 69% rename from tests/Stripe/Error/BaseTest.php rename to tests/Stripe/Exception/ExceptionTraitTest.php index e1c1ba183..c2013a439 100644 --- a/tests/Stripe/Error/BaseTest.php +++ b/tests/Stripe/Exception/ExceptionTraitTest.php @@ -1,12 +1,13 @@ getMockForAbstractClass(\Stripe\Error\Base::class, [ + $mock = $this->getMockForTrait(ExceptionTrait::class); + $instance = $mock::factory( 'message', 200, '{"error": {"code": "some_code"}}', @@ -15,7 +16,12 @@ public function createFixture() 'Some-Header' => 'Some Value', 'Request-Id' => 'req_test', ], - ]); + 'some_code' + ); + $instance->expects($this->any()) + ->method('getMessage') + ->will($this->returnValue('message')); + return $instance; } public function testGetters() @@ -26,6 +32,7 @@ public function testGetters() $this->assertSame(['error' => ['code' => 'some_code']], $e->getJsonBody()); $this->assertSame('Some Value', $e->getHttpHeaders()['Some-Header']); $this->assertSame('req_test', $e->getRequestId()); + $this->assertSame('some_code', $e->getStripeCode()); $this->assertNotNull($e->getError()); $this->assertSame('some_code', $e->getError()->code); } diff --git a/tests/Stripe/Error/OAuth/OAuthBaseTest.php b/tests/Stripe/Exception/OAuth/ExceptionTraitTest.php similarity index 72% rename from tests/Stripe/Error/OAuth/OAuthBaseTest.php rename to tests/Stripe/Exception/OAuth/ExceptionTraitTest.php index 6489e8dc1..0daf69cd2 100644 --- a/tests/Stripe/Error/OAuth/OAuthBaseTest.php +++ b/tests/Stripe/Exception/OAuth/ExceptionTraitTest.php @@ -1,13 +1,13 @@ getMockForAbstractClass(\Stripe\Error\OAuth\OAuthBase::class, [ - 'code', + $mock = $this->getMockForTrait(ExceptionTrait::class); + $instance = $mock::factory( 'description', 200, '{"error": "code", "error_description": "description"}', @@ -16,7 +16,12 @@ public function createFixture() 'Some-Header' => 'Some Value', 'Request-Id' => 'req_test', ], - ]); + 'code' + ); + $instance->expects($this->any()) + ->method('getMessage') + ->will($this->returnValue('description')); + return $instance; } public function testGetters() @@ -27,6 +32,7 @@ public function testGetters() $this->assertSame(['error' => 'code', 'error_description' => 'description'], $e->getJsonBody()); $this->assertSame('Some Value', $e->getHttpHeaders()['Some-Header']); $this->assertSame('req_test', $e->getRequestId()); + $this->assertSame('code', $e->getStripeCode()); $this->assertNotNull($e->getError()); $this->assertSame('code', $e->getError()->error); $this->assertSame('description', $e->getError()->error_description); diff --git a/tests/Stripe/Exception/SignatureVerificationExceptionTest.php b/tests/Stripe/Exception/SignatureVerificationExceptionTest.php new file mode 100644 index 000000000..40f73fad8 --- /dev/null +++ b/tests/Stripe/Exception/SignatureVerificationExceptionTest.php @@ -0,0 +1,12 @@ +assertSame('sig_header', $e->getSigHeader()); + } +} diff --git a/tests/Stripe/HttpClient/CurlClientTest.php b/tests/Stripe/HttpClient/CurlClientTest.php index 25c24cede..93054277a 100644 --- a/tests/Stripe/HttpClient/CurlClientTest.php +++ b/tests/Stripe/HttpClient/CurlClientTest.php @@ -112,7 +112,7 @@ public function testDefaultOptions() $withBadClosure = new CurlClient(function () { return 'thisShouldNotWork'; }); - $this->setExpectedException('Stripe\Error\Api', "Non-array value returned by defaultOptions CurlClient callback"); + $this->setExpectedException('Stripe\Exception\UnexpectedValueException', "Non-array value returned by defaultOptions CurlClient callback"); $withBadClosure->request('get', 'https://httpbin.org/status/200', [], [], false); } diff --git a/tests/Stripe/OAuthTest.php b/tests/Stripe/OAuthTest.php index b4e43a881..aa9e5b045 100644 --- a/tests/Stripe/OAuthTest.php +++ b/tests/Stripe/OAuthTest.php @@ -31,7 +31,7 @@ public function testAuthorizeUrl() } /** - * @expectedException \Stripe\Error\Authentication + * @expectedException \Stripe\Exception\AuthenticationException * @expectedExceptionMessageRegExp #No client_id provided# */ public function testRaisesAuthenticationErrorWhenNoClientId() diff --git a/tests/Stripe/PersonTest.php b/tests/Stripe/PersonTest.php index fa5a10356..d91f2bfad 100644 --- a/tests/Stripe/PersonTest.php +++ b/tests/Stripe/PersonTest.php @@ -17,7 +17,7 @@ public function testHasCorrectUrl() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyRetrievable() { @@ -37,7 +37,7 @@ public function testIsSaveable() } /** - * @expectedException \Stripe\Error\InvalidRequest + * @expectedException \Stripe\Exception\BadMethodCallException */ public function testIsNotDirectlyUpdatable() { diff --git a/tests/Stripe/SourceTest.php b/tests/Stripe/SourceTest.php index 49ec9ea47..e8ce769e0 100644 --- a/tests/Stripe/SourceTest.php +++ b/tests/Stripe/SourceTest.php @@ -101,7 +101,7 @@ public function testIsDetachableWhenAttached() } /** - * @expectedException \Stripe\Error\Api + * @expectedException \Stripe\Exception\UnexpectedValueException */ public function testIsNotDetachableWhenUnattached() { diff --git a/tests/Stripe/Util/RequestOptionsTest.php b/tests/Stripe/Util/RequestOptionsTest.php index 558f73982..4175b0545 100644 --- a/tests/Stripe/Util/RequestOptionsTest.php +++ b/tests/Stripe/Util/RequestOptionsTest.php @@ -60,7 +60,7 @@ public function testKeyArray() } /** - * @expectedException Stripe\Error\Api + * @expectedException Stripe\Exception\InvalidArgumentException */ public function testWrongType() { diff --git a/tests/Stripe/WebhookTest.php b/tests/Stripe/WebhookTest.php index 395d7e8f2..e08d0579a 100644 --- a/tests/Stripe/WebhookTest.php +++ b/tests/Stripe/WebhookTest.php @@ -32,7 +32,7 @@ public function testValidJsonAndHeader() } /** - * @expectedException \UnexpectedValueException + * @expectedException \Stripe\Exception\UnexpectedValueException */ public function testInvalidJson() { @@ -42,7 +42,7 @@ public function testInvalidJson() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException */ public function testValidJsonAndInvalidHeader() { @@ -51,7 +51,7 @@ public function testValidJsonAndInvalidHeader() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException * @expectedExceptionMessage Unable to extract timestamp and signatures from header */ public function testMalformedHeader() @@ -61,7 +61,7 @@ public function testMalformedHeader() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException * @expectedExceptionMessage No signatures found with expected scheme */ public function testNoSignaturesWithExpectedScheme() @@ -71,7 +71,7 @@ public function testNoSignaturesWithExpectedScheme() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException * @expectedExceptionMessage No signatures found matching the expected signature for payload */ public function testNoValidSignatureForPayload() @@ -81,7 +81,7 @@ public function testNoValidSignatureForPayload() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException * @expectedExceptionMessage Timestamp outside the tolerance zone */ public function testTimestampTooOld() @@ -91,7 +91,7 @@ public function testTimestampTooOld() } /** - * @expectedException \Stripe\Error\SignatureVerification + * @expectedException \Stripe\Exception\SignatureVerificationException * @expectedExceptionMessage Timestamp outside the tolerance zone */ public function testTimestampTooRecent()