Skip to content

Commit

Permalink
Support for expanding Stripe objects in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
anush committed Jan 5, 2020
1 parent d9580b1 commit 819c2ca
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
50 changes: 48 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand Down Expand Up @@ -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
*/
Expand Down
14 changes: 13 additions & 1 deletion tests/Message/AbstractRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -98,7 +101,6 @@ public function testStripeVersion()
$this->assertTrue($httpRequest->hasHeader('Stripe-Version'));
}


public function testConnectedStripeAccount()
{
$this->request->setConnectedStripeAccountHeader('ACCOUNT_ID');
Expand All @@ -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);
}
}

0 comments on commit 819c2ca

Please sign in to comment.