diff --git a/lib/ApiOperations/All.php b/lib/ApiOperations/All.php index ff2955d18..f421f8856 100644 --- a/lib/ApiOperations/All.php +++ b/lib/ApiOperations/All.php @@ -19,19 +19,8 @@ trait All */ public static function all($params = null, $opts = null) { - self::_validateParams($params); $url = static::classUrl(); - 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\Exception\UnexpectedValueException( - 'Expected type ' . \Stripe\Collection::class . ', got "' . \get_class($obj) . '" instead.' - ); - } - $obj->setLastResponse($response); - $obj->setFilters($params); - - return $obj; + return static::_requestPage($url, \Stripe\Collection::class, $params, $opts); } } diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index daf845842..8165a08d2 100644 --- a/lib/ApiOperations/Request.php +++ b/lib/ApiOperations/Request.php @@ -18,9 +18,9 @@ protected static function _validateParams($params = null) { if ($params && !\is_array($params)) { $message = 'You must pass an array as the first argument to Stripe API ' - . 'method calls. (HINT: an example call to create a charge ' - . "would be: \"Stripe\\Charge::create(['amount' => 100, " - . "'currency' => 'usd', 'source' => 'tok_1234'])\")"; + . '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\Exception\InvalidArgumentException($message); } @@ -46,6 +46,34 @@ protected function _request($method, $url, $params = [], $options = null, $usage return [$resp->json, $options]; } + /** + * @param string $url URL for the request + * @param class-string< \Stripe\SearchResult|\Stripe\Collection > $resultClass indicating what type of paginated result is returned + * @param null|array $params list of parameters for the request + * @param null|array|string $options + * @param string[] $usage names of tracked behaviors associated with this request + * + * @throws \Stripe\Exception\ApiErrorException if the request fails + * + * @return \Stripe\Collection|\Stripe\SearchResult + */ + protected static function _requestPage($url, $resultClass, $params = null, $options = null, $usage = []) + { + self::_validateParams($params); + + list($response, $opts) = static::_staticRequest('get', $url, $params, $options, $usage); + $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts); + if (!($obj instanceof $resultClass)) { + throw new \Stripe\Exception\UnexpectedValueException( + 'Expected type ' . $resultClass . ', got "' . \get_class($obj) . '" instead.' + ); + } + $obj->setLastResponse($response); + $obj->setFilters($params); + + return $obj; + } + /** * @param 'delete'|'get'|'post' $method HTTP method ('get', 'post', etc.) * @param string $url URL for the request diff --git a/lib/ApiOperations/Search.php b/lib/ApiOperations/Search.php index 09472ebaa..4ecd0aa18 100644 --- a/lib/ApiOperations/Search.php +++ b/lib/ApiOperations/Search.php @@ -20,18 +20,6 @@ trait Search */ protected static function _searchResource($searchUrl, $params = null, $opts = null) { - self::_validateParams($params); - - list($response, $opts) = static::_staticRequest('get', $searchUrl, $params, $opts); - $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts); - if (!($obj instanceof \Stripe\SearchResult)) { - throw new \Stripe\Exception\UnexpectedValueException( - 'Expected type ' . \Stripe\SearchResult::class . ', got "' . \get_class($obj) . '" instead.' - ); - } - $obj->setLastResponse($response); - $obj->setFilters($params); - - return $obj; + return static::_requestPage($searchUrl, \Stripe\SearchResult::class, $params, $opts); } } diff --git a/tests/Stripe/WebhookTest.php b/tests/Stripe/WebhookTest.php index 6927ea7ea..eeebd0324 100644 --- a/tests/Stripe/WebhookTest.php +++ b/tests/Stripe/WebhookTest.php @@ -13,7 +13,8 @@ final class WebhookTest extends \Stripe\TestCase const EVENT_PAYLOAD = '{ "id": "evt_test_webhook", - "object": "event" + "object": "event", + "data": { "object": { "id": "rdr_123", "object": "terminal.reader" } } }'; const SECRET = 'whsec_test_secret'; @@ -37,6 +38,7 @@ public function testValidJsonAndHeader() $sigHeader = $this->generateHeader(); $event = Webhook::constructEvent(self::EVENT_PAYLOAD, $sigHeader, self::SECRET); static::assertSame('evt_test_webhook', $event->id); + static::assertInstanceOf(\Stripe\Terminal\Reader::class, $event->data->__get('object')); } public function testInvalidJson()