From e82ab3f41457ea22395e8ee0540bfe86d93eb21c Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Fri, 16 Nov 2018 10:47:58 +0100 Subject: [PATCH] Update constructors of Stripe exception classes --- lib/ApiRequestor.php | 17 ++++++------- lib/Error/Base.php | 37 ++++++++++++++++------------- lib/Error/Card.php | 20 ++++++---------- lib/Error/InvalidRequest.php | 7 ++++-- lib/Error/OAuth/OAuthBase.php | 4 +++- lib/Error/SignatureVerification.php | 4 +++- 6 files changed, 48 insertions(+), 41 deletions(-) diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 967389490..76400591d 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -193,31 +193,32 @@ private static function _specificAPIError($rbody, $rcode, $rheaders, $resp, $err $param = isset($errorData['param']) ? $errorData['param'] : null; $code = isset($errorData['code']) ? $errorData['code'] : null; $type = isset($errorData['type']) ? $errorData['type'] : null; + $declineCode = isset($errorData['decline_code']) ? $errorData['decline_code'] : null; switch ($rcode) { case 400: // '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); + return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); } if ($type == 'idempotency_error') { - return new Error\Idempotency($msg, $rcode, $rbody, $resp, $rheaders); + return new Error\Idempotency($msg, $rcode, $rbody, $resp, $rheaders, $code); } // intentional fall-through case 404: - return new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders); + return new Error\InvalidRequest($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); case 401: - return new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders); + return new Error\Authentication($msg, $rcode, $rbody, $resp, $rheaders, $code); case 402: - return new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders); + return new Error\Card($msg, $param, $code, $rcode, $rbody, $resp, $rheaders, $declineCode); case 403: - return new Error\Permission($msg, $rcode, $rbody, $resp, $rheaders); + return new Error\Permission($msg, $rcode, $rbody, $resp, $rheaders, $code); case 429: - return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders); + return new Error\RateLimit($msg, $param, $rcode, $rbody, $resp, $rheaders, $code); default: - return new Error\Api($msg, $rcode, $rbody, $resp, $rheaders); + return new Error\Api($msg, $rcode, $rbody, $resp, $rheaders, $code); } } diff --git a/lib/Error/Base.php b/lib/Error/Base.php index c0051e6a4..fa483084f 100644 --- a/lib/Error/Base.php +++ b/lib/Error/Base.php @@ -6,42 +6,47 @@ abstract class Base extends Exception { + protected $httpBody; + protected $httpHeaders; + protected $httpStatus; + protected $jsonBody; + protected $requestId; + protected $stripeCode; + public function __construct( $message, $httpStatus = null, $httpBody = null, $jsonBody = null, - $httpHeaders = null + $httpHeaders = null, + $stripeCode = null ) { parent::__construct($message); $this->httpStatus = $httpStatus; $this->httpBody = $httpBody; $this->jsonBody = $jsonBody; $this->httpHeaders = $httpHeaders; - $this->requestId = null; - - // TODO: make this a proper constructor argument in the next major - // release. - $this->stripeCode = isset($jsonBody["error"]["code"]) ? $jsonBody["error"]["code"] : null; + $this->stripeCode = $stripeCode; + $this->requestId = null; if ($httpHeaders && isset($httpHeaders['Request-Id'])) { $this->requestId = $httpHeaders['Request-Id']; } } - public function getStripeCode() + public function getHttpBody() { - return $this->stripeCode; + return $this->httpBody; } - public function getHttpStatus() + public function getHttpHeaders() { - return $this->httpStatus; + return $this->httpHeaders; } - public function getHttpBody() + public function getHttpStatus() { - return $this->httpBody; + return $this->httpStatus; } public function getJsonBody() @@ -49,14 +54,14 @@ public function getJsonBody() return $this->jsonBody; } - public function getHttpHeaders() + public function getRequestId() { - return $this->httpHeaders; + return $this->requestId; } - public function getRequestId() + public function getStripeCode() { - return $this->requestId; + return $this->stripeCode; } public function __toString() diff --git a/lib/Error/Card.php b/lib/Error/Card.php index f3ff343d8..88e273db3 100644 --- a/lib/Error/Card.php +++ b/lib/Error/Card.php @@ -4,6 +4,9 @@ class Card extends Base { + protected $declineCode; + protected $stripeParam; + public function __construct( $message, $stripeParam, @@ -11,21 +14,12 @@ public function __construct( $httpStatus, $httpBody, $jsonBody, - $httpHeaders = null + $httpHeaders = null, + $declineCode = null ) { - parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders); + parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders, $stripeCode); + $this->declineCode = $declineCode; $this->stripeParam = $stripeParam; - - // TODO: once Error\Base accepts the error code as an argument, pass it - // in the call to parent::__construct() and stop setting it here. - $this->stripeCode = $stripeCode; - - // This one is not like the others because it was added later and we're - // trying to do our best not to change the public interface of this class' - // constructor. - // TODO: make this a proper constructor argument in the next major - // release. - $this->declineCode = isset($jsonBody["error"]["decline_code"]) ? $jsonBody["error"]["decline_code"] : null; } public function getDeclineCode() diff --git a/lib/Error/InvalidRequest.php b/lib/Error/InvalidRequest.php index 493bc184c..22e5606cd 100644 --- a/lib/Error/InvalidRequest.php +++ b/lib/Error/InvalidRequest.php @@ -4,15 +4,18 @@ class InvalidRequest extends Base { + protected $stripeParam; + public function __construct( $message, $stripeParam, $httpStatus = null, $httpBody = null, $jsonBody = null, - $httpHeaders = null + $httpHeaders = null, + $stripeCode = null ) { - parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders); + parent::__construct($message, $httpStatus, $httpBody, $jsonBody, $httpHeaders, $stripeCode); $this->stripeParam = $stripeParam; } diff --git a/lib/Error/OAuth/OAuthBase.php b/lib/Error/OAuth/OAuthBase.php index 03ada1c7b..bece8e8d4 100644 --- a/lib/Error/OAuth/OAuthBase.php +++ b/lib/Error/OAuth/OAuthBase.php @@ -4,6 +4,8 @@ class OAuthBase extends \Stripe\Error\Base { + protected $errorCode; + public function __construct( $code, $description, @@ -12,7 +14,7 @@ public function __construct( $jsonBody = null, $httpHeaders = null ) { - parent::__construct($description, $httpStatus, $httpBody, $jsonBody, $httpHeaders); + parent::__construct($description, $httpStatus, $httpBody, $jsonBody, $httpHeaders, null); $this->errorCode = $code; } diff --git a/lib/Error/SignatureVerification.php b/lib/Error/SignatureVerification.php index 98ddd3b02..4b5cd025b 100644 --- a/lib/Error/SignatureVerification.php +++ b/lib/Error/SignatureVerification.php @@ -4,12 +4,14 @@ class SignatureVerification extends Base { + protected $sigHeader; + public function __construct( $message, $sigHeader, $httpBody = null ) { - parent::__construct($message, null, $httpBody, null, null); + parent::__construct($message, null, $httpBody, null, null, null); $this->sigHeader = $sigHeader; }