• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-php / #7109

pending completion
#7109

push

php-coveralls

pakrym-stripe
Bump version to 10.14.0

1802 of 2534 relevant lines covered (71.11%)

3.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.61
/lib/BaseStripeClient.php
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)) {
18✔
53
            $config = ['api_key' => $config];
2✔
54
        } elseif (!\is_array($config)) {
16✔
55
            throw new \Stripe\Exception\InvalidArgumentException('$config must be a string or an array');
1✔
56
        }
57

58
        $config = \array_merge($this->getDefaultConfig(), $config);
17✔
59
        $this->validateConfig($config);
17✔
60

61
        $this->config = $config;
13✔
62

63
        $this->defaultOpts = \Stripe\Util\RequestOptions::parse([
13✔
64
            'stripe_account' => $config['stripe_account'],
13✔
65
            'stripe_version' => $config['stripe_version'],
13✔
66
        ]);
13✔
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'];
10✔
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'];
10✔
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);
12✔
132
        $baseUrl = $opts->apiBase ?: $this->getApiBase();
10✔
133
        $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
10✔
134
        list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
9✔
135
        $opts->discardNonPersistentHeaders();
8✔
136
        $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
8✔
137
        $obj->setLastResponse($response);
8✔
138

139
        return $obj;
8✔
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);
2✔
174
        if (!($obj instanceof \Stripe\Collection)) {
2✔
175
            $received_class = \get_class($obj);
1✔
176
            $msg = "Expected to receive `Stripe\\Collection` object from Stripe API. Instead received `{$received_class}`.";
1✔
177

178
            throw new \Stripe\Exception\UnexpectedValueException($msg);
1✔
179
        }
180
        $obj->setFilters($params);
1✔
181

182
        return $obj;
1✔
183
    }
184

185
    /**
186
     * Sends a request to Stripe's API.
187
     *
188
     * @param string $method the HTTP method
189
     * @param string $path the path of the request
190
     * @param array $params the parameters of the request
191
     * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
192
     *
193
     * @return \Stripe\SearchResult of ApiResources
194
     */
195
    public function requestSearchResult($method, $path, $params, $opts)
196
    {
197
        $obj = $this->request($method, $path, $params, $opts);
×
198
        if (!($obj instanceof \Stripe\SearchResult)) {
×
199
            $received_class = \get_class($obj);
×
200
            $msg = "Expected to receive `Stripe\\SearchResult` object from Stripe API. Instead received `{$received_class}`.";
×
201

202
            throw new \Stripe\Exception\UnexpectedValueException($msg);
×
203
        }
204
        $obj->setFilters($params);
×
205

206
        return $obj;
×
207
    }
208

209
    /**
210
     * @param \Stripe\Util\RequestOptions $opts
211
     *
212
     * @throws \Stripe\Exception\AuthenticationException
213
     *
214
     * @return string
215
     */
216
    private function apiKeyForRequest($opts)
217
    {
218
        $apiKey = $opts->apiKey ?: $this->getApiKey();
10✔
219

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

225
            throw new \Stripe\Exception\AuthenticationException($msg);
1✔
226
        }
227

228
        return $apiKey;
9✔
229
    }
230

231
    /**
232
     * TODO: replace this with a private constant when we drop support for PHP < 5.
233
     *
234
     * @return array<string, mixed>
235
     */
236
    private function getDefaultConfig()
237
    {
238
        return [
17✔
239
            'api_key' => null,
17✔
240
            'client_id' => null,
17✔
241
            'stripe_account' => null,
17✔
242
            'stripe_version' => null,
17✔
243
            'api_base' => self::DEFAULT_API_BASE,
17✔
244
            'connect_base' => self::DEFAULT_CONNECT_BASE,
17✔
245
            'files_base' => self::DEFAULT_FILES_BASE,
17✔
246
        ];
17✔
247
    }
248

249
    /**
250
     * @param array<string, mixed> $config
251
     *
252
     * @throws \Stripe\Exception\InvalidArgumentException
253
     */
254
    private function validateConfig($config)
255
    {
256
        // api_key
257
        if (null !== $config['api_key'] && !\is_string($config['api_key'])) {
17✔
258
            throw new \Stripe\Exception\InvalidArgumentException('api_key must be null or a string');
1✔
259
        }
260

261
        if (null !== $config['api_key'] && ('' === $config['api_key'])) {
16✔
262
            $msg = 'api_key cannot be the empty string';
1✔
263

264
            throw new \Stripe\Exception\InvalidArgumentException($msg);
1✔
265
        }
266

267
        if (null !== $config['api_key'] && (\preg_match('/\s/', $config['api_key']))) {
15✔
268
            $msg = 'api_key cannot contain whitespace';
1✔
269

270
            throw new \Stripe\Exception\InvalidArgumentException($msg);
1✔
271
        }
272

273
        // client_id
274
        if (null !== $config['client_id'] && !\is_string($config['client_id'])) {
14✔
275
            throw new \Stripe\Exception\InvalidArgumentException('client_id must be null or a string');
×
276
        }
277

278
        // stripe_account
279
        if (null !== $config['stripe_account'] && !\is_string($config['stripe_account'])) {
14✔
280
            throw new \Stripe\Exception\InvalidArgumentException('stripe_account must be null or a string');
×
281
        }
282

283
        // stripe_version
284
        if (null !== $config['stripe_version'] && !\is_string($config['stripe_version'])) {
14✔
285
            throw new \Stripe\Exception\InvalidArgumentException('stripe_version must be null or a string');
×
286
        }
287

288
        // api_base
289
        if (!\is_string($config['api_base'])) {
14✔
290
            throw new \Stripe\Exception\InvalidArgumentException('api_base must be a string');
×
291
        }
292

293
        // connect_base
294
        if (!\is_string($config['connect_base'])) {
14✔
295
            throw new \Stripe\Exception\InvalidArgumentException('connect_base must be a string');
×
296
        }
297

298
        // files_base
299
        if (!\is_string($config['files_base'])) {
14✔
300
            throw new \Stripe\Exception\InvalidArgumentException('files_base must be a string');
×
301
        }
302

303
        // check absence of extra keys
304
        $extraConfigKeys = \array_diff(\array_keys($config), \array_keys($this->getDefaultConfig()));
14✔
305
        if (!empty($extraConfigKeys)) {
14✔
306
            // Wrap in single quote to more easily catch trailing spaces errors
307
            $invalidKeys = "'" . \implode("', '", $extraConfigKeys) . "'";
1✔
308

309
            throw new \Stripe\Exception\InvalidArgumentException('Found unknown key(s) in configuration array: ' . $invalidKeys);
1✔
310
        }
311
    }
312
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc