From 48191cb294cb4036cda08ca589a0f9e1e605178c Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Mon, 19 Oct 2015 18:27:10 -0700 Subject: [PATCH] Save the last API response to StripeObjects. Calling `getLastResponse` will return the last API response made by that object. The API response contains the headers, status code, response body and JSON. --- init.php | 1 + lib/ApiRequestor.php | 5 +++-- lib/ApiResource.php | 15 +++++++++++---- lib/ApiResponse.php | 27 +++++++++++++++++++++++++++ lib/Invoice.php | 4 +++- lib/StripeObject.php | 19 +++++++++++++++++++ tests/ChargeTest.php | 4 +++- 7 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 lib/ApiResponse.php diff --git a/init.php b/init.php index 5c1108e4b..30e88d607 100644 --- a/init.php +++ b/init.php @@ -22,6 +22,7 @@ require(dirname(__FILE__) . '/lib/Error/RateLimit.php'); // Plumbing +require(dirname(__FILE__) . '/lib/ApiResponse.php'); require(dirname(__FILE__) . '/lib/JsonSerializable.php'); require(dirname(__FILE__) . '/lib/StripeObject.php'); require(dirname(__FILE__) . '/lib/ApiRequestor.php'); diff --git a/lib/ApiRequestor.php b/lib/ApiRequestor.php index 5871d3561..01ef28bab 100644 --- a/lib/ApiRequestor.php +++ b/lib/ApiRequestor.php @@ -44,7 +44,7 @@ private static function _encodeObjects($d) * @param array|null $params * @param array|null $headers * - * @return array An array whose first element is the response and second + * @return array An array whose first element is an API response and second * element is the API key used to make the request. */ public function request($method, $url, $params = null, $headers = null) @@ -57,7 +57,8 @@ public function request($method, $url, $params = null, $headers = null) } list($rbody, $rcode, $rheaders, $myApiKey) = $this->_requestRaw($method, $url, $params, $headers); - $resp = $this->_interpretResponse($rbody, $rcode, $rheaders); + $json = $this->_interpretResponse($rbody, $rcode, $rheaders); + $resp = new ApiResponse($rbody, $rcode, $rheaders, $json); return array($resp, $myApiKey); } diff --git a/lib/ApiResource.php b/lib/ApiResource.php index 7dfbd8f3f..ecca74d4d 100644 --- a/lib/ApiResource.php +++ b/lib/ApiResource.php @@ -25,7 +25,8 @@ public function refresh() $this->_retrieveOptions, $this->_opts->headers ); - $this->refreshFrom($response, $this->_opts); + $this->setLastResponse($response); + $this->refreshFrom($response->json, $this->_opts); return $this; } @@ -95,7 +96,9 @@ private static function _validateParams($params = null) protected function _request($method, $url, $params = array(), $options = null) { $opts = $this->_opts->merge($options); - return static::_staticRequest($method, $url, $params, $opts); + list($resp, $options) = static::_staticRequest($method, $url, $params, $opts); + $this->setLastResponse($resp); + return array($resp->json, $options); } protected static function _staticRequest($method, $url, $params, $options) @@ -125,7 +128,9 @@ protected static function _all($params = null, $options = null) $url = static::classUrl(); list($response, $opts) = static::_staticRequest('get', $url, $params, $options); - return Util\Util::convertToStripeObject($response, $opts); + $obj = Util\Util::convertToStripeObject($response->json, $opts); + $obj->setLastResponse($response); + return $obj; } protected static function _create($params = null, $options = null) @@ -135,7 +140,9 @@ protected static function _create($params = null, $options = null) $url = static::classUrl(); list($response, $opts) = static::_staticRequest('post', $url, $params, $options); - return Util\Util::convertToStripeObject($response, $opts); + $obj = Util\Util::convertToStripeObject($response->json, $opts); + $obj->setLastResponse($response); + return $obj; } protected function _save($options = null) diff --git a/lib/ApiResponse.php b/lib/ApiResponse.php new file mode 100644 index 000000000..467ee7e87 --- /dev/null +++ b/lib/ApiResponse.php @@ -0,0 +1,27 @@ +body = $body; + $this->code = $code; + $this->headers = $headers; + $this->json = $json; + } +} diff --git a/lib/Invoice.php b/lib/Invoice.php index 9db6ab105..1cfe62934 100644 --- a/lib/Invoice.php +++ b/lib/Invoice.php @@ -47,7 +47,9 @@ public static function upcoming($params = null, $opts = null) { $url = static::classUrl() . '/upcoming'; list($response, $opts) = static::_staticRequest('get', $url, $params, $opts); - return Util\Util::convertToStripeObject($response, $opts); + $obj = Util\Util::convertToStripeObject($response->json, $opts); + $obj->setLastResponse($response); + return $obj; } /** diff --git a/lib/StripeObject.php b/lib/StripeObject.php index 912a85a15..1a5ca8ecf 100644 --- a/lib/StripeObject.php +++ b/lib/StripeObject.php @@ -29,11 +29,30 @@ public static function init() )); } + /** + * @return object The last response from the Stripe API + */ + public function getLastResponse() + { + return $this->_lastResponse; + } + + /** + * @param ApiResponse + * + * @return void Set the last response from the Stripe API + */ + public function setLastResponse($resp) + { + $this->_lastResponse = $resp; + } + protected $_opts; protected $_values; protected $_unsavedValues; protected $_transientValues; protected $_retrieveOptions; + protected $_lastResponse; public function __construct($id = null, $opts = null) { diff --git a/tests/ChargeTest.php b/tests/ChargeTest.php index 2d77fd4f3..6ec0fa1f1 100644 --- a/tests/ChargeTest.php +++ b/tests/ChargeTest.php @@ -54,7 +54,7 @@ public function testIdempotentCreate() ); $this->assertTrue($c->paid); - $this->assertFalse($c->refunded); + $this->assertSame(200, $c->getLastResponse()->code); } public function testRetrieve() @@ -75,6 +75,7 @@ public function testRetrieve() ) ); $d = Charge::retrieve($c->id); + $this->assertSame(200, $d->getLastResponse()->code); $this->assertSame($d->id, $c->id); } @@ -123,6 +124,7 @@ public function testUpdateMetadataAll() $charge->metadata = array('test' => 'foo bar'); $charge->save(); + $this->assertSame(200, $charge->getLastResponse()->code); $updatedCharge = Charge::retrieve($charge->id); $this->assertSame('foo bar', $updatedCharge->metadata['test']);