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

stripe / stripe-php / 6471862601

10 Oct 2023 04:02PM UTC coverage: 69.665% (-0.5%) from 70.141%
6471862601

push

github

web-flow
Merge pull request #1570 from localheinz/feature/coveralls

Enhancement: Use `coverallsapp/github-action` to report code coverage

2393 of 3435 relevant lines covered (69.67%)

3.5 hits per line

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

70.59
/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, null|string> */
17
    const DEFAULT_CONFIG = [
18
        'api_key' => null,
19
        'client_id' => null,
20
        'stripe_account' => null,
21
        'stripe_version' => \Stripe\Util\ApiVersion::CURRENT,
22
        'api_base' => self::DEFAULT_API_BASE,
23
        'connect_base' => self::DEFAULT_CONNECT_BASE,
24
        'files_base' => self::DEFAULT_FILES_BASE,
25
    ];
26

27
    /** @var array<string, mixed> */
28
    private $config;
29

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

33
    /**
34
     * Initializes a new instance of the {@link BaseStripeClient} class.
35
     *
36
     * The constructor takes a single argument. The argument can be a string, in which case it
37
     * should be the API key. It can also be an array with various configuration settings.
38
     *
39
     * Configuration settings include the following options:
40
     *
41
     * - api_key (null|string): the Stripe API key, to be used in regular API requests.
42
     * - client_id (null|string): the Stripe client ID, to be used in OAuth requests.
43
     * - stripe_account (null|string): a Stripe account ID. If set, all requests sent by the client
44
     *   will automatically use the {@code Stripe-Account} header with that account ID.
45
     * - stripe_version (null|string): a Stripe API verion. If set, all requests sent by the client
46
     *   will include the {@code Stripe-Version} header with that API version.
47
     *
48
     * The following configuration settings are also available, though setting these should rarely be necessary
49
     * (only useful if you want to send requests to a mock server like stripe-mock):
50
     *
51
     * - api_base (string): the base URL for regular API requests. Defaults to
52
     *   {@link DEFAULT_API_BASE}.
53
     * - connect_base (string): the base URL for OAuth requests. Defaults to
54
     *   {@link DEFAULT_CONNECT_BASE}.
55
     * - files_base (string): the base URL for file creation requests. Defaults to
56
     *   {@link DEFAULT_FILES_BASE}.
57
     *
58
     * @param array<string, mixed>|string $config the API key as a string, or an array containing
59
     *   the client configuration settings
60
     */
61
    public function __construct($config = [])
19✔
62
    {
63
        if (\is_string($config)) {
19✔
64
            $config = ['api_key' => $config];
2✔
65
        } elseif (!\is_array($config)) {
17✔
66
            throw new \Stripe\Exception\InvalidArgumentException('$config must be a string or an array');
1✔
67
        }
68

69
        $config = \array_merge(self::DEFAULT_CONFIG, $config);
18✔
70
        $this->validateConfig($config);
18✔
71

72
        $this->config = $config;
14✔
73

74
        $this->defaultOpts = \Stripe\Util\RequestOptions::parse([
14✔
75
            'stripe_account' => $config['stripe_account'],
14✔
76
            'stripe_version' => $config['stripe_version'],
14✔
77
        ]);
14✔
78
    }
79

80
    /**
81
     * Gets the API key used by the client to send requests.
82
     *
83
     * @return null|string the API key used by the client to send requests
84
     */
85
    public function getApiKey()
11✔
86
    {
87
        return $this->config['api_key'];
11✔
88
    }
89

90
    /**
91
     * Gets the client ID used by the client in OAuth requests.
92
     *
93
     * @return null|string the client ID used by the client in OAuth requests
94
     */
95
    public function getClientId()
×
96
    {
97
        return $this->config['client_id'];
×
98
    }
99

100
    /**
101
     * Gets the base URL for Stripe's API.
102
     *
103
     * @return string the base URL for Stripe's API
104
     */
105
    public function getApiBase()
11✔
106
    {
107
        return $this->config['api_base'];
11✔
108
    }
109

110
    /**
111
     * Gets the base URL for Stripe's OAuth API.
112
     *
113
     * @return string the base URL for Stripe's OAuth API
114
     */
115
    public function getConnectBase()
×
116
    {
117
        return $this->config['connect_base'];
×
118
    }
119

120
    /**
121
     * Gets the base URL for Stripe's Files API.
122
     *
123
     * @return string the base URL for Stripe's Files API
124
     */
125
    public function getFilesBase()
×
126
    {
127
        return $this->config['files_base'];
×
128
    }
129

130
    /**
131
     * Sends a request to Stripe's API.
132
     *
133
     * @param 'delete'|'get'|'post' $method the HTTP method
134
     * @param string $path the path of the request
135
     * @param array $params the parameters of the request
136
     * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
137
     *
138
     * @return \Stripe\StripeObject the object returned by Stripe's API
139
     */
140
    public function request($method, $path, $params, $opts)
13✔
141
    {
142
        $opts = $this->defaultOpts->merge($opts, true);
13✔
143
        $baseUrl = $opts->apiBase ?: $this->getApiBase();
11✔
144
        $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
11✔
145
        list($response, $opts->apiKey) = $requestor->request($method, $path, $params, $opts->headers);
10✔
146
        $opts->discardNonPersistentHeaders();
9✔
147
        $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
9✔
148
        $obj->setLastResponse($response);
9✔
149

150
        return $obj;
9✔
151
    }
152

153
    /**
154
     * Sends a request to Stripe's API, passing chunks of the streamed response
155
     * into a user-provided $readBodyChunkCallable callback.
156
     *
157
     * @param 'delete'|'get'|'post' $method the HTTP method
158
     * @param string $path the path of the request
159
     * @param callable $readBodyChunkCallable a function that will be called
160
     * @param array $params the parameters of the request
161
     * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
162
     * with chunks of bytes from the body if the request is successful
163
     */
164
    public function requestStream($method, $path, $readBodyChunkCallable, $params, $opts)
×
165
    {
166
        $opts = $this->defaultOpts->merge($opts, true);
×
167
        $baseUrl = $opts->apiBase ?: $this->getApiBase();
×
168
        $requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
×
169
        list($response, $opts->apiKey) = $requestor->requestStream($method, $path, $readBodyChunkCallable, $params, $opts->headers);
×
170
    }
171

172
    /**
173
     * Sends a request to Stripe's API.
174
     *
175
     * @param 'delete'|'get'|'post' $method the HTTP method
176
     * @param string $path the path of the request
177
     * @param array $params the parameters of the request
178
     * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
179
     *
180
     * @return \Stripe\Collection of ApiResources
181
     */
182
    public function requestCollection($method, $path, $params, $opts)
2✔
183
    {
184
        $obj = $this->request($method, $path, $params, $opts);
2✔
185
        if (!($obj instanceof \Stripe\Collection)) {
2✔
186
            $received_class = \get_class($obj);
1✔
187
            $msg = "Expected to receive `Stripe\\Collection` object from Stripe API. Instead received `{$received_class}`.";
1✔
188

189
            throw new \Stripe\Exception\UnexpectedValueException($msg);
1✔
190
        }
191
        $obj->setFilters($params);
1✔
192

193
        return $obj;
1✔
194
    }
195

196
    /**
197
     * Sends a request to Stripe's API.
198
     *
199
     * @param 'delete'|'get'|'post' $method the HTTP method
200
     * @param string $path the path of the request
201
     * @param array $params the parameters of the request
202
     * @param array|\Stripe\Util\RequestOptions $opts the special modifiers of the request
203
     *
204
     * @return \Stripe\SearchResult of ApiResources
205
     */
206
    public function requestSearchResult($method, $path, $params, $opts)
×
207
    {
208
        $obj = $this->request($method, $path, $params, $opts);
×
209
        if (!($obj instanceof \Stripe\SearchResult)) {
×
210
            $received_class = \get_class($obj);
×
211
            $msg = "Expected to receive `Stripe\\SearchResult` object from Stripe API. Instead received `{$received_class}`.";
×
212

213
            throw new \Stripe\Exception\UnexpectedValueException($msg);
×
214
        }
215
        $obj->setFilters($params);
×
216

217
        return $obj;
×
218
    }
219

220
    /**
221
     * @param \Stripe\Util\RequestOptions $opts
222
     *
223
     * @throws \Stripe\Exception\AuthenticationException
224
     *
225
     * @return string
226
     */
227
    private function apiKeyForRequest($opts)
11✔
228
    {
229
        $apiKey = $opts->apiKey ?: $this->getApiKey();
11✔
230

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

236
            throw new \Stripe\Exception\AuthenticationException($msg);
1✔
237
        }
238

239
        return $apiKey;
10✔
240
    }
241

242
    /**
243
     * @param array<string, mixed> $config
244
     *
245
     * @throws \Stripe\Exception\InvalidArgumentException
246
     */
247
    private function validateConfig($config)
18✔
248
    {
249
        // api_key
250
        if (null !== $config['api_key'] && !\is_string($config['api_key'])) {
18✔
251
            throw new \Stripe\Exception\InvalidArgumentException('api_key must be null or a string');
1✔
252
        }
253

254
        if (null !== $config['api_key'] && ('' === $config['api_key'])) {
17✔
255
            $msg = 'api_key cannot be the empty string';
1✔
256

257
            throw new \Stripe\Exception\InvalidArgumentException($msg);
1✔
258
        }
259

260
        if (null !== $config['api_key'] && (\preg_match('/\s/', $config['api_key']))) {
16✔
261
            $msg = 'api_key cannot contain whitespace';
1✔
262

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

266
        // client_id
267
        if (null !== $config['client_id'] && !\is_string($config['client_id'])) {
15✔
268
            throw new \Stripe\Exception\InvalidArgumentException('client_id must be null or a string');
×
269
        }
270

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

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

281
        // api_base
282
        if (!\is_string($config['api_base'])) {
15✔
283
            throw new \Stripe\Exception\InvalidArgumentException('api_base must be a string');
×
284
        }
285

286
        // connect_base
287
        if (!\is_string($config['connect_base'])) {
15✔
288
            throw new \Stripe\Exception\InvalidArgumentException('connect_base must be a string');
×
289
        }
290

291
        // files_base
292
        if (!\is_string($config['files_base'])) {
15✔
293
            throw new \Stripe\Exception\InvalidArgumentException('files_base must be a string');
×
294
        }
295

296
        // check absence of extra keys
297
        $extraConfigKeys = \array_diff(\array_keys($config), \array_keys(self::DEFAULT_CONFIG));
15✔
298
        if (!empty($extraConfigKeys)) {
15✔
299
            // Wrap in single quote to more easily catch trailing spaces errors
300
            $invalidKeys = "'" . \implode("', '", $extraConfigKeys) . "'";
1✔
301

302
            throw new \Stripe\Exception\InvalidArgumentException('Found unknown key(s) in configuration array: ' . $invalidKeys);
1✔
303
        }
304
    }
305
}
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