Skip to content

Commit 1b27d76

Browse files
authored
Merge pull request #168 from anush/expand
Support for expanding Stripe objects in the response
2 parents a303eda + 819c2ca commit 1b27d76

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/Message/AbstractRequest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ public function setIdempotencyKeyHeader($value)
174174
return $this->setParameter('idempotencyKey', $value);
175175
}
176176

177+
/**
178+
* @return array
179+
*/
180+
public function getExpand()
181+
{
182+
return $this->getParameter('expand');
183+
}
184+
185+
/**
186+
* Specifies which object relations (IDs) in the response should be expanded to include the entire object.
187+
*
188+
* @see https://stripe.com/docs/api/expanding_objects
189+
*
190+
* @param array $value
191+
* @return AbstractRequest
192+
*/
193+
public function setExpand($value)
194+
{
195+
return $this->setParameter('expand', $value);
196+
}
197+
177198
abstract public function getEndpoint();
178199

179200
/**
@@ -221,17 +242,42 @@ public function sendData($data)
221242
);
222243

223244
$body = $data ? http_build_query($data, '', '&') : null;
224-
$httpResponse = $this->httpClient->request($this->getHttpMethod(), $this->getEndpoint(), $headers, $body);
245+
$httpResponse = $this->httpClient->request(
246+
$this->getHttpMethod(),
247+
$this->getExpandedEndpoint(),
248+
$headers,
249+
$body
250+
);
225251

226252
return $this->createResponse($httpResponse->getBody()->getContents(), $httpResponse->getHeaders());
227253
}
228254

255+
/**
256+
* Appends the `expand` properties to the endpoint as a querystring.
257+
*
258+
* @return string
259+
*/
260+
public function getExpandedEndpoint()
261+
{
262+
$endpoint = $this->getEndpoint();
263+
$expand = $this->getExpand();
264+
if (is_array($expand) && count($expand) > 0) {
265+
$queryParams = [];
266+
foreach ($expand as $key) {
267+
$queryParams[] = 'expand[]=' . $key;
268+
}
269+
$queryString = join('&', $queryParams);
270+
$endpoint .= '?' . $queryString;
271+
}
272+
273+
return $endpoint;
274+
}
229275

230276
protected function createResponse($data, $headers = [])
231277
{
232278
return $this->response = new Response($this, $data, $headers);
233279
}
234-
280+
235281
/**
236282
* @return mixed
237283
*/

tests/Message/AbstractRequestTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class AbstractRequestTest extends TestCase
1010
{
11+
/** @var Mockery\Mock|AbstractRequest */
12+
private $request;
13+
1114
public function setUp()
1215
{
1316
$this->request = Mockery::mock('\Omnipay\Stripe\Message\AbstractRequest')->makePartial();
@@ -98,7 +101,6 @@ public function testStripeVersion()
98101
$this->assertTrue($httpRequest->hasHeader('Stripe-Version'));
99102
}
100103

101-
102104
public function testConnectedStripeAccount()
103105
{
104106
$this->request->setConnectedStripeAccountHeader('ACCOUNT_ID');
@@ -118,4 +120,14 @@ public function testConnectedStripeAccount()
118120

119121
$this->assertTrue($httpRequest->hasHeader('Stripe-Account'));
120122
}
123+
124+
public function testExpandedEndpoint()
125+
{
126+
$this->request->shouldReceive('getEndpoint')->andReturn('https://api.stripe.com/v1');
127+
$this->request->setExpand(['foo', 'bar']);
128+
129+
$actual = $this->request->getExpandedEndpoint();
130+
131+
$this->assertEquals('https://api.stripe.com/v1?expand[]=foo&expand[]=bar', $actual);
132+
}
121133
}

0 commit comments

Comments
 (0)