Skip to content

Commit

Permalink
Add support for default API version and Stripe account in client
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Mar 25, 2020
1 parent f365a3d commit 9cc7a8b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
15 changes: 14 additions & 1 deletion lib/BaseStripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class BaseStripeClient implements StripeClientInterface
/** @var null|string */
private $clientId;

/** @var \Stripe\Util\RequestOptions */
private $defaultOpts;

/** @var string */
private $apiBase;

Expand All @@ -38,6 +41,10 @@ class BaseStripeClient implements StripeClientInterface
*
* - api_key (string): the Stripe API key, to be used in regular API requests.
* - client_id (string): the Stripe client ID, to be used in OAuth requests.
* - stripe_account (string): a Stripe account ID. If set, all requests sent by the client
* will automatically use the {@code Stripe-Account} header with that account ID.
* - stripe_version (string): a Stripe API verion. If set, all requests sent by the client
* will include the {@code Stripe-Version} header with that API version.
* - api_base (string): the base URL for regular API requests. Defaults to
* {@link DEFAULT_API_BASE}. Changing this should rarely be necessary.
* - connect_base (string): the base URL for OAuth requests. Defaults to
Expand All @@ -57,6 +64,8 @@ public function __construct($config = [])
$defaults = [
'api_key' => null,
'client_id' => null,
'stripe_account' => null,
'stripe_version' => null,
'api_base' => self::DEFAULT_API_BASE,
'connect_base' => self::DEFAULT_CONNECT_BASE,
'files_base' => self::DEFAULT_FILES_BASE,
Expand All @@ -79,6 +88,10 @@ public function __construct($config = [])

$this->apiKey = $apiKey;
$this->clientId = $config['client_id'];
$this->defaultOpts = \Stripe\Util\RequestOptions::parse([
'stripe_account' => $config['stripe_account'],
'stripe_version' => $config['stripe_version'],
]);
$this->apiBase = $config['api_base'];
$this->connectBase = $config['connect_base'];
$this->filesBase = $config['files_base'];
Expand Down Expand Up @@ -146,7 +159,7 @@ public function getFilesBase()
*/
public function request($method, $path, $params, $opts)
{
$opts = \Stripe\Util\RequestOptions::parse($opts, true);
$opts = $this->defaultOpts->merge($opts, true);
$baseUrl = $opts->apiBase ?: $this->getApiBase();
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
Expand Down
66 changes: 60 additions & 6 deletions tests/Stripe/BaseStripeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
*/
final class BaseStripeClientTest extends \PHPUnit\Framework\TestCase
{
/** @var \ReflectionProperty */
private $optsReflector;

/** @before */
protected function setUpOptsReflector()
{
$this->optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$this->optsReflector->setAccessible(true);
}

public function testCtorDoesNotThrowWhenNoParams()
{
$client = new BaseStripeClient();
Expand Down Expand Up @@ -36,19 +46,15 @@ public function testRequestWithClientApiKey()
$client = new BaseStripeClient(['api_key' => 'sk_test_client', 'api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
$optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$optsReflector->setAccessible(true);
static::assertSame('sk_test_client', $optsReflector->getValue($charge)->apiKey);
static::assertSame('sk_test_client', $this->optsReflector->getValue($charge)->apiKey);
}

public function testRequestWithOptsApiKey()
{
$client = new BaseStripeClient(['api_base' => MOCK_URL]);
$charge = $client->request('get', '/v1/charges/ch_123', [], ['api_key' => 'sk_test_opts']);
static::assertNotNull($charge);
$optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts');
$optsReflector->setAccessible(true);
static::assertSame('sk_test_opts', $optsReflector->getValue($charge)->apiKey);
static::assertSame('sk_test_opts', $this->optsReflector->getValue($charge)->apiKey);
}

public function testRequestThrowsIfNoApiKeyInClientAndOpts()
Expand Down Expand Up @@ -83,4 +89,52 @@ public function testRequestThrowsIfOptsIsArrayWithUnexpectedKeys()
static::assertNotNull($charge);
static::assertSame('ch_123', $charge->id);
}

public function testRequestWithClientStripeVersion()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_version' => '2020-03-02',
'api_base' => MOCK_URL,
]);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
static::assertSame('2020-03-02', $this->optsReflector->getValue($charge)->headers['Stripe-Version']);
}

public function testRequestWithOptsStripeVersion()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_version' => '2020-03-02',
'api_base' => MOCK_URL,
]);
$charge = $client->request('get', '/v1/charges/ch_123', [], ['stripe_version' => '2019-12-03']);
static::assertNotNull($charge);
static::assertSame('2019-12-03', $this->optsReflector->getValue($charge)->headers['Stripe-Version']);
}

public function testRequestWithClientStripeAccount()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
static::assertSame('acct_123', $this->optsReflector->getValue($charge)->headers['Stripe-Account']);
}

public function testRequestWithOptsStripeAccount()
{
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'stripe_account' => 'acct_123',
'api_base' => MOCK_URL,
]);
$charge = $client->request('get', '/v1/charges/ch_123', [], ['stripe_account' => 'acct_456']);
static::assertNotNull($charge);
static::assertSame('acct_456', $this->optsReflector->getValue($charge)->headers['Stripe-Account']);
}
}

0 comments on commit 9cc7a8b

Please sign in to comment.