Skip to content

Commit

Permalink
New StripeClient class
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe authored and richardm-stripe committed May 14, 2020
1 parent dccb135 commit 14bf117
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 0 deletions.
4 changes: 4 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
require __DIR__ . '/lib/Exception/OAuth/UnsupportedGrantTypeException.php';
require __DIR__ . '/lib/Exception/OAuth/UnsupportedResponseTypeException.php';

// StripeClient
require __DIR__ . '/lib/StripeClientInterface.php';
require __DIR__ . '/lib/StripeClient.php';

// API operations
require __DIR__ . '/lib/ApiOperations/All.php';
require __DIR__ . '/lib/ApiOperations/Create.php';
Expand Down
151 changes: 151 additions & 0 deletions lib/StripeClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Stripe;

class StripeClient implements StripeClientInterface
{
/**
* @var string default base URL for Stripe's API
*/
const DEFAULT_API_BASE = 'https://api.stripe.com';

/**
* @var string default base URL for Stripe's OAuth API
*/
const DEFAULT_CONNECT_BASE = 'https://connect.stripe.com';

/**
* @var string default base URL for Stripe's Files API
*/
const DEFAULT_FILES_BASE = 'https://files.stripe.com';

private $apiKey;
private $clientId;

private $apiBase;
private $connectBase;
private $filesBase;

/**
* Initializes a new instance of the {@link StripeClient} class.
*
* @param null|string $apiKey the API key used by the client to make requests
* @param null|string $clientId the client ID used by the client in OAuth requests
* @param null|string $apiBase The base URL for Stripe's API. Defaults to {@link DEFAULT_API_BASE}.
* @param null|string $connectBase The base URL for Stripe's OAuth API. Defaults to {@link DEFAULT_CONNECT_BASE}.
* @param null|string $filesBase The base URL for Stripe's Files API. Defaults to {@link DEFAULT_FILES_BASE}.
*/
public function __construct(
$apiKey,
$clientId = null,
$apiBase = null,
$connectBase = null,
$filesBase = null
) {
if (null !== $apiKey && ('' === $apiKey)) {
$msg = 'API key cannot be the empty string.';

throw new \Stripe\Exception\InvalidArgumentException($msg);
}
if (null !== $apiKey && (\preg_match('/\s/', $apiKey))) {
$msg = 'API key cannot contain whitespace.';

throw new \Stripe\Exception\InvalidArgumentException($msg);
}

$this->apiKey = $apiKey;
$this->clientId = $clientId;

$this->apiBase = $apiBase ?: self::DEFAULT_API_BASE;
$this->connectBase = $connectBase ?: self::DEFAULT_CONNECT_BASE;
$this->filesBase = $filesBase ?: self::DEFAULT_FILES_BASE;
}

/**
* Gets the API key used by the client to send requests.
*
* @return null|string the API key used by the client to send requests
*/
public function getApiKey()
{
return $this->apiKey;
}

/**
* Gets the client ID used by the client in OAuth requests.
*
* @return null|string the client ID used by the client in OAuth requests
*/
public function getClientId()
{
return $this->clientId;
}

/**
* Gets the base URL for Stripe's API.
*
* @return string the base URL for Stripe's API
*/
public function getApiBase()
{
return $this->apiBase;
}

/**
* Gets the base URL for Stripe's OAuth API.
*
* @return string the base URL for Stripe's OAuth API
*/
public function getConnectBase()
{
return $this->connectBase;
}

/**
* Gets the base URL for Stripe's Files API.
*
* @return string the base URL for Stripe's Files API
*/
public function getFilesBase()
{
return $this->filesBase;
}

/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
*
* @return \Stripe\StripeObject the object returned by Stripe's API
*/
public function request($method, $path, $params, $opts)
{
$opts = \Stripe\Util\RequestOptions::parse($opts);
$baseUrl = $opts->apiBase ?: $this->getApiBase();
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
$opts->discardNonPersistentHeaders();
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);

return $obj;
}

private function apiKeyForRequest($opts)
{
$apiKey = $opts->apiKey ?: $this->getApiKey();

if (null === $apiKey) {
$msg = 'No API key provided. Set your API key when constructing the '
. 'StripeClient instance, or provide it on a per-request basis '
. 'using the `api_key` key in the $opts argument.';

throw new Exception\AuthenticationException($msg);
}

return $apiKey;
}
}
56 changes: 56 additions & 0 deletions lib/StripeClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Stripe;

/**
* Interface for a Stripe client.
*/
interface StripeClientInterface
{
/**
* Gets the API key used by the client to send requests.
*
* @return null|string the API key used by the client to send requests
*/
public function getApiKey();

/**
* Gets the client ID used by the client in OAuth requests.
*
* @return null|string the client ID used by the client in OAuth requests
*/
public function getClientId();

/**
* Gets the base URL for Stripe's API.
*
* @return string the base URL for Stripe's API
*/
public function getApiBase();

/**
* Gets the base URL for Stripe's OAuth API.
*
* @return string the base URL for Stripe's OAuth API
*/
public function getConnectBase();

/**
* Gets the base URL for Stripe's Files API.
*
* @return string the base URL for Stripe's Files API
*/
public function getFilesBase();

/**
* Sends a request to Stripe's API.
*
* @param string $method the HTTP method
* @param string $path the path of the request
* @param array $params the parameters of the request
* @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
*
* @return \Stripe\StripeObject the object returned by Stripe's API
*/
public function request($method, $path, $params, $opts);
}
64 changes: 64 additions & 0 deletions tests/Stripe/StripeClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Stripe;

/**
* @internal
*/
final class StripeClientTest extends \PHPUnit\Framework\TestCase
{
public function testCtorDoesNotThrowIfApiKeyIsNull()
{
$client = new StripeClient(null);
static::assertNotNull($client);
static::assertNull($client->getApiKey());
}

public function testCtorThrowsIfApiKeyIsEmpty()
{
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('API key cannot be the empty string.');

$client = new StripeClient('');
}

public function testCtorThrowsIfApiKeyContainsWhitespace()
{
$this->expectException(\Stripe\Exception\InvalidArgumentException::class);
$this->expectExceptionMessage('API key cannot contain whitespace.');

$client = new StripeClient("sk_test_123\n");
}

public function testRequestWithClientApiKey()
{
$client = new StripeClient('sk_test_client', null, 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);
}

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

public function testRequestThrowsIfNoApiKeyInClientAndOpts()
{
$this->expectException(\Stripe\Exception\AuthenticationException::class);
$this->expectExceptionMessage('No API key provided.');

$client = new StripeClient(null, null, MOCK_URL);
$charge = $client->request('get', '/v1/charges/ch_123', [], []);
static::assertNotNull($charge);
static::assertSame('ch_123', $charge->id);
}
}

0 comments on commit 14bf117

Please sign in to comment.