PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.14.0
GiveWP – Donation Plugin and Fundraising Platform v2.14.0
4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / vendor / stripe / stripe-php / lib / BaseStripeClient.php
give / vendor / stripe / stripe-php / lib Last commit date
ApiOperations 4 years ago BillingPortal 4 years ago Checkout 4 years ago Exception 4 years ago HttpClient 4 years ago Identity 4 years ago Issuing 4 years ago Radar 4 years ago Reporting 4 years ago Service 4 years ago Sigma 4 years ago Terminal 4 years ago Util 4 years ago Account.php 4 years ago AccountLink.php 4 years ago AlipayAccount.php 4 years ago ApiRequestor.php 4 years ago ApiResource.php 4 years ago ApiResponse.php 4 years ago ApplePayDomain.php 4 years ago ApplicationFee.php 4 years ago ApplicationFeeRefund.php 4 years ago Balance.php 4 years ago BalanceTransaction.php 4 years ago BankAccount.php 4 years ago BaseStripeClient.php 4 years ago BaseStripeClientInterface.php 4 years ago BitcoinReceiver.php 4 years ago BitcoinTransaction.php 4 years ago Capability.php 4 years ago Card.php 4 years ago Charge.php 4 years ago Collection.php 4 years ago CountrySpec.php 4 years ago Coupon.php 4 years ago CreditNote.php 4 years ago CreditNoteLineItem.php 4 years ago Customer.php 4 years ago CustomerBalanceTransaction.php 4 years ago Discount.php 4 years ago Dispute.php 4 years ago EphemeralKey.php 4 years ago ErrorObject.php 4 years ago Event.php 4 years ago ExchangeRate.php 4 years ago File.php 4 years ago FileLink.php 4 years ago Invoice.php 4 years ago InvoiceItem.php 4 years ago InvoiceLineItem.php 4 years ago LineItem.php 4 years ago LoginLink.php 4 years ago Mandate.php 4 years ago OAuth.php 4 years ago OAuthErrorObject.php 4 years ago Order.php 4 years ago OrderItem.php 4 years ago OrderReturn.php 4 years ago PaymentIntent.php 4 years ago PaymentMethod.php 4 years ago Payout.php 4 years ago Person.php 4 years ago Plan.php 4 years ago Price.php 4 years ago Product.php 4 years ago PromotionCode.php 4 years ago Quote.php 4 years ago Recipient.php 4 years ago RecipientTransfer.php 4 years ago Refund.php 4 years ago RequestTelemetry.php 4 years ago Review.php 4 years ago SKU.php 4 years ago SetupAttempt.php 4 years ago SetupIntent.php 4 years ago SingletonApiResource.php 4 years ago Source.php 4 years ago SourceTransaction.php 4 years ago Stripe.php 4 years ago StripeClient.php 4 years ago StripeClientInterface.php 4 years ago StripeObject.php 4 years ago StripeStreamingClientInterface.php 4 years ago Subscription.php 4 years ago SubscriptionItem.php 4 years ago SubscriptionSchedule.php 4 years ago TaxCode.php 4 years ago TaxId.php 4 years ago TaxRate.php 4 years ago ThreeDSecure.php 4 years ago Token.php 4 years ago Topup.php 4 years ago Transfer.php 4 years ago TransferReversal.php 4 years ago UsageRecord.php 4 years ago UsageRecordSummary.php 4 years ago Webhook.php 4 years ago WebhookEndpoint.php 4 years ago WebhookSignature.php 4 years ago
BaseStripeClient.php
289 lines
1 <?php
2
3 namespace Stripe;
4
5 class BaseStripeClient implements StripeClientInterface, StripeStreamingClientInterface
6 {
7 /** @var string default base URL for Stripe's API */
8 const DEFAULT_API_BASE = 'https://api.stripe.com';
9
10 /** @var string default base URL for Stripe's OAuth API */
11 const DEFAULT_CONNECT_BASE = 'https://connect.stripe.com';
12
13 /** @var string default base URL for Stripe's Files API */
14 const DEFAULT_FILES_BASE = 'https://files.stripe.com';
15
16 /** @var array<string, mixed> */
17 private $config;
18
19 /** @var \Stripe\Util\RequestOptions */
20 private $defaultOpts;
21
22 /**
23 * Initializes a new instance of the {@link BaseStripeClient} class.
24 *
25 * The constructor takes a single argument. The argument can be a string, in which case it
26 * should be the API key. It can also be an array with various configuration settings.
27 *
28 * Configuration settings include the following options:
29 *
30 * - api_key (null|string): the Stripe API key, to be used in regular API requests.
31 * - client_id (null|string): the Stripe client ID, to be used in OAuth requests.
32 * - stripe_account (null|string): a Stripe account ID. If set, all requests sent by the client
33 * will automatically use the {@code Stripe-Account} header with that account ID.
34 * - stripe_version (null|string): a Stripe API verion. If set, all requests sent by the client
35 * will include the {@code Stripe-Version} header with that API version.
36 *
37 * The following configuration settings are also available, though setting these should rarely be necessary
38 * (only useful if you want to send requests to a mock server like stripe-mock):
39 *
40 * - api_base (string): the base URL for regular API requests. Defaults to
41 * {@link DEFAULT_API_BASE}.
42 * - connect_base (string): the base URL for OAuth requests. Defaults to
43 * {@link DEFAULT_CONNECT_BASE}.
44 * - files_base (string): the base URL for file creation requests. Defaults to
45 * {@link DEFAULT_FILES_BASE}.
46 *
47 * @param array<string, mixed>|string $config the API key as a string, or an array containing
48 * the client configuration settings
49 */
50 public function __construct($config = [])
51 {
52 if (\is_string($config)) {
53 $config = ['api_key' => $config];
54 } elseif (!\is_array($config)) {
55 throw new \Stripe\Exception\InvalidArgumentException('$config must be a string or an array');
56 }
57
58 $config = \array_merge($this->getDefaultConfig(), $config);
59 $this->validateConfig($config);
60
61 $this->config = $config;
62
63 $this->defaultOpts = \Stripe\Util\RequestOptions::parse([
64 'stripe_account' => $config['stripe_account'],
65 'stripe_version' => $config['stripe_version'],
66 ]);
67 }
68
69 /**
70 * Gets the API key used by the client to send requests.
71 *
72 * @return null|string the API key used by the client to send requests
73 */
74 public function getApiKey()
75 {
76 return $this->config['api_key'];
77 }
78
79 /**
80 * Gets the client ID used by the client in OAuth requests.
81 *
82 * @return null|string the client ID used by the client in OAuth requests
83 */
84 public function getClientId()
85 {
86 return $this->config['client_id'];
87 }
88
89 /**
90 * Gets the base URL for Stripe's API.
91 *
92 * @return string the base URL for Stripe's API
93 */
94 public function getApiBase()
95 {
96 return $this->config['api_base'];
97 }
98
99 /**
100 * Gets the base URL for Stripe's OAuth API.
101 *
102 * @return string the base URL for Stripe's OAuth API
103 */
104 public function getConnectBase()
105 {
106 return $this->config['connect_base'];
107 }
108
109 /**
110 * Gets the base URL for Stripe's Files API.
111 *
112 * @return string the base URL for Stripe's Files API
113 */
114 public function getFilesBase()
115 {
116 return $this->config['files_base'];
117 }
118
119 /**
120 * Sends a request to Stripe's API.
121 *
122 * @param string $method the HTTP method
123 * @param string $path the path of the request
124 * @param array $params the parameters of the request
125 * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
126 *
127 * @return \Stripe\StripeObject the object returned by Stripe's API
128 */
129 public function request($method, $path, $params, $opts)
130 {
131 $opts = $this->defaultOpts->merge($opts, true);
132 $baseUrl = $opts->apiBase ?: $this->getApiBase();
133 $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
134 list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
135 $opts->discardNonPersistentHeaders();
136 $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
137 $obj->setLastResponse($response);
138
139 return $obj;
140 }
141
142 /**
143 * Sends a request to Stripe's API, passing chunks of the streamed response
144 * into a user-provided $readBodyChunkCallable callback.
145 *
146 * @param string $method the HTTP method
147 * @param string $path the path of the request
148 * @param callable $readBodyChunkCallable a function that will be called
149 * @param array $params the parameters of the request
150 * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
151 * with chunks of bytes from the body if the request is successful
152 */
153 public function requestStream($method, $path, $readBodyChunkCallable, $params, $opts)
154 {
155 $opts = $this->defaultOpts->merge($opts, true);
156 $baseUrl = $opts->apiBase ?: $this->getApiBase();
157 $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
158 list($response, $opts->apiKey) = $requestor->requestStream($method, $path, $readBodyChunkCallable, $params, $opts->headers);
159 }
160
161 /**
162 * Sends a request to Stripe's API.
163 *
164 * @param string $method the HTTP method
165 * @param string $path the path of the request
166 * @param array $params the parameters of the request
167 * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
168 *
169 * @return \Stripe\Collection of ApiResources
170 */
171 public function requestCollection($method, $path, $params, $opts)
172 {
173 $obj = $this->request($method, $path, $params, $opts);
174 if (!($obj instanceof \Stripe\Collection)) {
175 $received_class = \get_class($obj);
176 $msg = "Expected to receive `Stripe\\Collection` object from Stripe API. Instead received `{$received_class}`.";
177
178 throw new \Stripe\Exception\UnexpectedValueException($msg);
179 }
180 $obj->setFilters($params);
181
182 return $obj;
183 }
184
185 /**
186 * @param \Stripe\Util\RequestOptions $opts
187 *
188 * @throws \Stripe\Exception\AuthenticationException
189 *
190 * @return string
191 */
192 private function apiKeyForRequest($opts)
193 {
194 $apiKey = $opts->apiKey ?: $this->getApiKey();
195
196 if (null === $apiKey) {
197 $msg = 'No API key provided. Set your API key when constructing the '
198 . 'StripeClient instance, or provide it on a per-request basis '
199 . 'using the `api_key` key in the $opts argument.';
200
201 throw new \Stripe\Exception\AuthenticationException($msg);
202 }
203
204 return $apiKey;
205 }
206
207 /**
208 * TODO: replace this with a private constant when we drop support for PHP < 5.
209 *
210 * @return array<string, mixed>
211 */
212 private function getDefaultConfig()
213 {
214 return [
215 'api_key' => null,
216 'client_id' => null,
217 'stripe_account' => null,
218 'stripe_version' => null,
219 'api_base' => self::DEFAULT_API_BASE,
220 'connect_base' => self::DEFAULT_CONNECT_BASE,
221 'files_base' => self::DEFAULT_FILES_BASE,
222 ];
223 }
224
225 /**
226 * @param array<string, mixed> $config
227 *
228 * @throws \Stripe\Exception\InvalidArgumentException
229 */
230 private function validateConfig($config)
231 {
232 // api_key
233 if (null !== $config['api_key'] && !\is_string($config['api_key'])) {
234 throw new \Stripe\Exception\InvalidArgumentException('api_key must be null or a string');
235 }
236
237 if (null !== $config['api_key'] && ('' === $config['api_key'])) {
238 $msg = 'api_key cannot be the empty string';
239
240 throw new \Stripe\Exception\InvalidArgumentException($msg);
241 }
242
243 if (null !== $config['api_key'] && (\preg_match('/\s/', $config['api_key']))) {
244 $msg = 'api_key cannot contain whitespace';
245
246 throw new \Stripe\Exception\InvalidArgumentException($msg);
247 }
248
249 // client_id
250 if (null !== $config['client_id'] && !\is_string($config['client_id'])) {
251 throw new \Stripe\Exception\InvalidArgumentException('client_id must be null or a string');
252 }
253
254 // stripe_account
255 if (null !== $config['stripe_account'] && !\is_string($config['stripe_account'])) {
256 throw new \Stripe\Exception\InvalidArgumentException('stripe_account must be null or a string');
257 }
258
259 // stripe_version
260 if (null !== $config['stripe_version'] && !\is_string($config['stripe_version'])) {
261 throw new \Stripe\Exception\InvalidArgumentException('stripe_version must be null or a string');
262 }
263
264 // api_base
265 if (!\is_string($config['api_base'])) {
266 throw new \Stripe\Exception\InvalidArgumentException('api_base must be a string');
267 }
268
269 // connect_base
270 if (!\is_string($config['connect_base'])) {
271 throw new \Stripe\Exception\InvalidArgumentException('connect_base must be a string');
272 }
273
274 // files_base
275 if (!\is_string($config['files_base'])) {
276 throw new \Stripe\Exception\InvalidArgumentException('files_base must be a string');
277 }
278
279 // check absence of extra keys
280 $extraConfigKeys = \array_diff(\array_keys($config), \array_keys($this->getDefaultConfig()));
281 if (!empty($extraConfigKeys)) {
282 // Wrap in single quote to more easily catch trailing spaces errors
283 $invalidKeys = "'" . \implode("', '", $extraConfigKeys) . "'";
284
285 throw new \Stripe\Exception\InvalidArgumentException('Found unknown key(s) in configuration array: ' . $invalidKeys);
286 }
287 }
288 }
289