diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 805b2d34..e33c7c03 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -174,6 +174,27 @@ public function setIdempotencyKeyHeader($value) return $this->setParameter('idempotencyKey', $value); } + /** + * @return array + */ + public function getExpand() + { + return $this->getParameter('expand'); + } + + /** + * Specifies which object relations (IDs) in the response should be expanded to include the entire object. + * + * @see https://stripe.com/docs/api/expanding_objects + * + * @param array $value + * @return AbstractRequest + */ + public function setExpand($value) + { + return $this->setParameter('expand', $value); + } + abstract public function getEndpoint(); /** @@ -221,17 +242,42 @@ public function sendData($data) ); $body = $data ? http_build_query($data, '', '&') : null; - $httpResponse = $this->httpClient->request($this->getHttpMethod(), $this->getEndpoint(), $headers, $body); + $httpResponse = $this->httpClient->request( + $this->getHttpMethod(), + $this->getExpandedEndpoint(), + $headers, + $body + ); return $this->createResponse($httpResponse->getBody()->getContents(), $httpResponse->getHeaders()); } + /** + * Appends the `expand` properties to the endpoint as a querystring. + * + * @return string + */ + public function getExpandedEndpoint() + { + $endpoint = $this->getEndpoint(); + $expand = $this->getExpand(); + if (is_array($expand) && count($expand) > 0) { + $queryParams = []; + foreach ($expand as $key) { + $queryParams[] = 'expand[]=' . $key; + } + $queryString = join('&', $queryParams); + $endpoint .= '?' . $queryString; + } + + return $endpoint; + } protected function createResponse($data, $headers = []) { return $this->response = new Response($this, $data, $headers); } - + /** * @return mixed */ diff --git a/tests/Message/AbstractRequestTest.php b/tests/Message/AbstractRequestTest.php index 330aea87..9528e6cf 100644 --- a/tests/Message/AbstractRequestTest.php +++ b/tests/Message/AbstractRequestTest.php @@ -8,6 +8,9 @@ class AbstractRequestTest extends TestCase { + /** @var Mockery\Mock|AbstractRequest */ + private $request; + public function setUp() { $this->request = Mockery::mock('\Omnipay\Stripe\Message\AbstractRequest')->makePartial(); @@ -98,7 +101,6 @@ public function testStripeVersion() $this->assertTrue($httpRequest->hasHeader('Stripe-Version')); } - public function testConnectedStripeAccount() { $this->request->setConnectedStripeAccountHeader('ACCOUNT_ID'); @@ -118,4 +120,14 @@ public function testConnectedStripeAccount() $this->assertTrue($httpRequest->hasHeader('Stripe-Account')); } + + public function testExpandedEndpoint() + { + $this->request->shouldReceive('getEndpoint')->andReturn('https://api.stripe.com/v1'); + $this->request->setExpand(['foo', 'bar']); + + $actual = $this->request->getExpandedEndpoint(); + + $this->assertEquals('https://api.stripe.com/v1?expand[]=foo&expand[]=bar', $actual); + } }