PageRenderTime 76ms CodeModel.GetById 17ms RepoModel.GetById 3ms app.codeStats 0ms

/includes/lib/Stripe/lib/BaseStripeClient.php

https://github.com/strangerstudios/paid-memberships-pro
PHP | 266 lines | 126 code | 37 blank | 103 comment | 19 complexity | aaafa7b5018518eaf9fd773ec4efc9b4 MD5 | raw file
  1. <?php
  2. namespace Stripe;
  3. class BaseStripeClient implements StripeClientInterface
  4. {
  5. /** @var string default base URL for Stripe's API */
  6. const DEFAULT_API_BASE = 'https://api.stripe.com';
  7. /** @var string default base URL for Stripe's OAuth API */
  8. const DEFAULT_CONNECT_BASE = 'https://connect.stripe.com';
  9. /** @var string default base URL for Stripe's Files API */
  10. const DEFAULT_FILES_BASE = 'https://files.stripe.com';
  11. /** @var array<string, mixed> */
  12. private $config;
  13. /** @var \Stripe\Util\RequestOptions */
  14. private $defaultOpts;
  15. /**
  16. * Initializes a new instance of the {@link BaseStripeClient} class.
  17. *
  18. * The constructor takes a single argument. The argument can be a string, in which case it
  19. * should be the API key. It can also be an array with various configuration settings.
  20. *
  21. * Configuration settings include the following options:
  22. *
  23. * - api_key (null|string): the Stripe API key, to be used in regular API requests.
  24. * - client_id (null|string): the Stripe client ID, to be used in OAuth requests.
  25. * - stripe_account (null|string): a Stripe account ID. If set, all requests sent by the client
  26. * will automatically use the {@code Stripe-Account} header with that account ID.
  27. * - stripe_version (null|string): a Stripe API verion. If set, all requests sent by the client
  28. * will include the {@code Stripe-Version} header with that API version.
  29. *
  30. * The following configuration settings are also available, though setting these should rarely be necessary
  31. * (only useful if you want to send requests to a mock server like stripe-mock):
  32. *
  33. * - api_base (string): the base URL for regular API requests. Defaults to
  34. * {@link DEFAULT_API_BASE}.
  35. * - connect_base (string): the base URL for OAuth requests. Defaults to
  36. * {@link DEFAULT_CONNECT_BASE}.
  37. * - files_base (string): the base URL for file creation requests. Defaults to
  38. * {@link DEFAULT_FILES_BASE}.
  39. *
  40. * @param array<string, mixed>|string $config the API key as a string, or an array containing
  41. * the client configuration settings
  42. */
  43. public function __construct($config = [])
  44. {
  45. if (\is_string($config)) {
  46. $config = ['api_key' => $config];
  47. } elseif (!\is_array($config)) {
  48. throw new \Stripe\Exception\InvalidArgumentException('$config must be a string or an array');
  49. }
  50. $config = \array_merge($this->getDefaultConfig(), $config);
  51. $this->validateConfig($config);
  52. $this->config = $config;
  53. $this->defaultOpts = \Stripe\Util\RequestOptions::parse([
  54. 'stripe_account' => $config['stripe_account'],
  55. 'stripe_version' => $config['stripe_version'],
  56. ]);
  57. }
  58. /**
  59. * Gets the API key used by the client to send requests.
  60. *
  61. * @return null|string the API key used by the client to send requests
  62. */
  63. public function getApiKey()
  64. {
  65. return $this->config['api_key'];
  66. }
  67. /**
  68. * Gets the client ID used by the client in OAuth requests.
  69. *
  70. * @return null|string the client ID used by the client in OAuth requests
  71. */
  72. public function getClientId()
  73. {
  74. return $this->config['client_id'];
  75. }
  76. /**
  77. * Gets the base URL for Stripe's API.
  78. *
  79. * @return string the base URL for Stripe's API
  80. */
  81. public function getApiBase()
  82. {
  83. return $this->config['api_base'];
  84. }
  85. /**
  86. * Gets the base URL for Stripe's OAuth API.
  87. *
  88. * @return string the base URL for Stripe's OAuth API
  89. */
  90. public function getConnectBase()
  91. {
  92. return $this->config['connect_base'];
  93. }
  94. /**
  95. * Gets the base URL for Stripe's Files API.
  96. *
  97. * @return string the base URL for Stripe's Files API
  98. */
  99. public function getFilesBase()
  100. {
  101. return $this->config['files_base'];
  102. }
  103. /**
  104. * Sends a request to Stripe's API.
  105. *
  106. * @param string $method the HTTP method
  107. * @param string $path the path of the request
  108. * @param array $params the parameters of the request
  109. * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
  110. *
  111. * @return \Stripe\StripeObject the object returned by Stripe's API
  112. */
  113. public function request($method, $path, $params, $opts)
  114. {
  115. $opts = $this->defaultOpts->merge($opts, true);
  116. $baseUrl = $opts->apiBase ?: $this->getApiBase();
  117. $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
  118. list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
  119. $opts->discardNonPersistentHeaders();
  120. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  121. $obj->setLastResponse($response);
  122. return $obj;
  123. }
  124. /**
  125. * Sends a request to Stripe's API.
  126. *
  127. * @param string $method the HTTP method
  128. * @param string $path the path of the request
  129. * @param array $params the parameters of the request
  130. * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
  131. *
  132. * @return \Stripe\Collection of ApiResources
  133. */
  134. public function requestCollection($method, $path, $params, $opts)
  135. {
  136. $obj = $this->request($method, $path, $params, $opts);
  137. if (!($obj instanceof \Stripe\Collection)) {
  138. $received_class = \get_class($obj);
  139. $msg = "Expected to receive `Stripe\\Collection` object from Stripe API. Instead received `{$received_class}`.";
  140. throw new \Stripe\Exception\UnexpectedValueException($msg);
  141. }
  142. $obj->setFilters($params);
  143. return $obj;
  144. }
  145. /**
  146. * @param \Stripe\Util\RequestOptions $opts
  147. *
  148. * @throws \Stripe\Exception\AuthenticationException
  149. *
  150. * @return string
  151. */
  152. private function apiKeyForRequest($opts)
  153. {
  154. $apiKey = $opts->apiKey ?: $this->getApiKey();
  155. if (null === $apiKey) {
  156. $msg = 'No API key provided. Set your API key when constructing the '
  157. . 'StripeClient instance, or provide it on a per-request basis '
  158. . 'using the `api_key` key in the $opts argument.';
  159. throw new \Stripe\Exception\AuthenticationException($msg);
  160. }
  161. return $apiKey;
  162. }
  163. /**
  164. * TODO: replace this with a private constant when we drop support for PHP < 5.
  165. *
  166. * @return array<string, mixed>
  167. */
  168. private function getDefaultConfig()
  169. {
  170. return [
  171. 'api_key' => null,
  172. 'client_id' => null,
  173. 'stripe_account' => null,
  174. 'stripe_version' => null,
  175. 'api_base' => self::DEFAULT_API_BASE,
  176. 'connect_base' => self::DEFAULT_CONNECT_BASE,
  177. 'files_base' => self::DEFAULT_FILES_BASE,
  178. ];
  179. }
  180. /**
  181. * @param array<string, mixed> $config
  182. *
  183. * @throws \Stripe\Exception\InvalidArgumentException
  184. */
  185. private function validateConfig($config)
  186. {
  187. // api_key
  188. if (null !== $config['api_key'] && !\is_string($config['api_key'])) {
  189. throw new \Stripe\Exception\InvalidArgumentException('api_key must be null or a string');
  190. }
  191. if (null !== $config['api_key'] && ('' === $config['api_key'])) {
  192. $msg = 'api_key cannot be the empty string';
  193. throw new \Stripe\Exception\InvalidArgumentException($msg);
  194. }
  195. if (null !== $config['api_key'] && (\preg_match('/\s/', $config['api_key']))) {
  196. $msg = 'api_key cannot contain whitespace';
  197. throw new \Stripe\Exception\InvalidArgumentException($msg);
  198. }
  199. // client_id
  200. if (null !== $config['client_id'] && !\is_string($config['client_id'])) {
  201. throw new \Stripe\Exception\InvalidArgumentException('client_id must be null or a string');
  202. }
  203. // stripe_account
  204. if (null !== $config['stripe_account'] && !\is_string($config['stripe_account'])) {
  205. throw new \Stripe\Exception\InvalidArgumentException('stripe_account must be null or a string');
  206. }
  207. // stripe_version
  208. if (null !== $config['stripe_version'] && !\is_string($config['stripe_version'])) {
  209. throw new \Stripe\Exception\InvalidArgumentException('stripe_version must be null or a string');
  210. }
  211. // api_base
  212. if (!\is_string($config['api_base'])) {
  213. throw new \Stripe\Exception\InvalidArgumentException('api_base must be a string');
  214. }
  215. // connect_base
  216. if (!\is_string($config['connect_base'])) {
  217. throw new \Stripe\Exception\InvalidArgumentException('connect_base must be a string');
  218. }
  219. // files_base
  220. if (!\is_string($config['files_base'])) {
  221. throw new \Stripe\Exception\InvalidArgumentException('files_base must be a string');
  222. }
  223. // check absence of extra keys
  224. $extraConfigKeys = \array_diff(\array_keys($config), \array_keys($this->getDefaultConfig()));
  225. if (!empty($extraConfigKeys)) {
  226. throw new \Stripe\Exception\InvalidArgumentException('Found unknown key(s) in configuration array: ' . \implode(',', $extraConfigKeys));
  227. }
  228. }
  229. }