• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..17-Oct-2021-

data/H17-Oct-2021-3,4773,336

lib/H17-Oct-2021-17,6856,929

.coveralls.github-actions.ymlH A D17-Oct-202190 43

.php_cs.distH A D17-Oct-20211.8 KiB4637

CHANGELOG.mdH A D17-Oct-202147.3 KiB1,317764

CODE_OF_CONDUCT.mdH A D17-Oct-20213.3 KiB7857

LICENSEH A D17-Oct-20211.1 KiB2217

MakefileH A D17-Oct-20211,007 3727

README.mdH A D17-Oct-202110.5 KiB267180

VERSIONH A D17-Oct-20217 21

build.phpH A D17-Oct-2021640 2619

composer.jsonH A D17-Oct-2021923 4948

init.phpH A D17-Oct-202110.8 KiB239210

phpdoc.dist.xmlH A D17-Oct-2021975 3231

phpstan-baseline.neonH A D17-Oct-2021315 1310

phpstan.neon.distH A D17-Oct-2021156 128

update_certs.phpH A D17-Oct-2021392 2015

README.md

1# Stripe PHP bindings
2
3[![Build Status](https://travis-ci.org/stripe/stripe-php.svg?branch=master)](https://travis-ci.org/stripe/stripe-php)
4[![Latest Stable Version](https://poser.pugx.org/stripe/stripe-php/v/stable.svg)](https://packagist.org/packages/stripe/stripe-php)
5[![Total Downloads](https://poser.pugx.org/stripe/stripe-php/downloads.svg)](https://packagist.org/packages/stripe/stripe-php)
6[![License](https://poser.pugx.org/stripe/stripe-php/license.svg)](https://packagist.org/packages/stripe/stripe-php)
7[![Code Coverage](https://coveralls.io/repos/stripe/stripe-php/badge.svg?branch=master)](https://coveralls.io/r/stripe/stripe-php?branch=master)
8
9The Stripe PHP library provides convenient access to the Stripe API from
10applications written in the PHP language. It includes a pre-defined set of
11classes for API resources that initialize themselves dynamically from API
12responses which makes it compatible with a wide range of versions of the Stripe
13API.
14
15## Requirements
16
17PHP 5.6.0 and later.
18
19## Composer
20
21You can install the bindings via [Composer](http://getcomposer.org/). Run the following command:
22
23```bash
24composer require stripe/stripe-php
25```
26
27To use the bindings, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):
28
29```php
30require_once('vendor/autoload.php');
31```
32
33## Manual Installation
34
35If you do not wish to use Composer, you can download the [latest release](https://github.com/stripe/stripe-php/releases). Then, to use the bindings, include the `init.php` file.
36
37```php
38require_once('/path/to/stripe-php/init.php');
39```
40
41## Dependencies
42
43The bindings require the following extensions in order to work properly:
44
45-   [`curl`](https://secure.php.net/manual/en/book.curl.php), although you can use your own non-cURL client if you prefer
46-   [`json`](https://secure.php.net/manual/en/book.json.php)
47-   [`mbstring`](https://secure.php.net/manual/en/book.mbstring.php) (Multibyte String)
48
49If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.
50
51## Getting Started
52
53Simple usage looks like:
54
55```php
56$stripe = new \Stripe\StripeClient('sk_test_BQokikJOvBiI2HlWgH4olfQ2');
57$customer = $stripe->customers->create([
58    'description' => 'example customer',
59    'email' => 'email@example.com',
60    'payment_method' => 'pm_card_visa',
61]);
62echo $customer;
63```
64
65### Client/service patterns vs legacy patterns
66
67You can continue to use the legacy integration patterns used prior to version [7.33.0](https://github.com/stripe/stripe-php/blob/master/CHANGELOG.md#7330---2020-05-14). Review the [migration guide](https://github.com/stripe/stripe-php/wiki/Migration-to-StripeClient-and-services-in-7.33.0) for the backwards-compatible client/services pattern changes.
68
69## Documentation
70
71See the [PHP API docs](https://stripe.com/docs/api/php#intro).
72
73## Legacy Version Support
74
75### PHP 5.4 & 5.5
76
77If you are using PHP 5.4 or 5.5, you can download v6.21.1 ([zip](https://github.com/stripe/stripe-php/archive/v6.21.1.zip), [tar.gz](https://github.com/stripe/stripe-php/archive/v6.21.1.tar.gz)) from our [releases page](https://github.com/stripe/stripe-php/releases). This version will continue to work with new versions of the Stripe API for all common uses.
78
79### PHP 5.3
80
81If you are using PHP 5.3, you can download v5.9.2 ([zip](https://github.com/stripe/stripe-php/archive/v5.9.2.zip), [tar.gz](https://github.com/stripe/stripe-php/archive/v5.9.2.tar.gz)) from our [releases page](https://github.com/stripe/stripe-php/releases). This version will continue to work with new versions of the Stripe API for all common uses.
82
83## Custom Request Timeouts
84
85_NOTE:_ We do not recommend decreasing the timeout for non-read-only calls (e.g. charge creation), since even if you locally timeout, the request on Stripe's side can still complete. If you are decreasing timeouts on these calls, make sure to use [idempotency tokens](https://stripe.com/docs/api/php#idempotent_requests) to avoid executing the same transaction twice as a result of timeout retry logic.
86
87To modify request timeouts (connect or total, in seconds) you'll need to tell the API client to use a CurlClient other than its default. You'll set the timeouts in that CurlClient.
88
89```php
90// set up your tweaked Curl client
91$curl = new \Stripe\HttpClient\CurlClient();
92$curl->setTimeout(10); // default is \Stripe\HttpClient\CurlClient::DEFAULT_TIMEOUT
93$curl->setConnectTimeout(5); // default is \Stripe\HttpClient\CurlClient::DEFAULT_CONNECT_TIMEOUT
94
95echo $curl->getTimeout(); // 10
96echo $curl->getConnectTimeout(); // 5
97
98// tell Stripe to use the tweaked client
99\Stripe\ApiRequestor::setHttpClient($curl);
100
101// use the Stripe API client as you normally would
102```
103
104## Custom cURL Options (e.g. proxies)
105
106Need to set a proxy for your requests? Pass in the requisite `CURLOPT_*` array to the CurlClient constructor, using the same syntax as `curl_stopt_array()`. This will set the default cURL options for each HTTP request made by the SDK, though many more common options (e.g. timeouts; see above on how to set those) will be overridden by the client even if set here.
107
108```php
109// set up your tweaked Curl client
110$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_PROXY => 'proxy.local:80']);
111// tell Stripe to use the tweaked client
112\Stripe\ApiRequestor::setHttpClient($curl);
113```
114
115Alternately, a callable can be passed to the CurlClient constructor that returns the above array based on request inputs. See `testDefaultOptions()` in `tests/CurlClientTest.php` for an example of this behavior. Note that the callable is called at the beginning of every API request, before the request is sent.
116
117### Configuring a Logger
118
119The library does minimal logging, but it can be configured
120with a [`PSR-3` compatible logger][psr3] so that messages
121end up there instead of `error_log`:
122
123```php
124\Stripe\Stripe::setLogger($logger);
125```
126
127### Accessing response data
128
129You can access the data from the last API response on any object via `getLastResponse()`.
130
131```php
132$customer = $stripe->customers->create([
133    'description' => 'example customer',
134]);
135echo $customer->getLastResponse()->headers['Request-Id'];
136```
137
138### SSL / TLS compatibility issues
139
140Stripe's API now requires that [all connections use TLS 1.2](https://stripe.com/blog/upgrading-tls). Some systems (most notably some older CentOS and RHEL versions) are capable of using TLS 1.2 but will use TLS 1.0 or 1.1 by default. In this case, you'd get an `invalid_request_error` with the following error message: "Stripe no longer supports API requests made with TLS 1.0. Please initiate HTTPS connections with TLS 1.2 or later. You can learn more about this at [https://stripe.com/blog/upgrading-tls](https://stripe.com/blog/upgrading-tls).".
141
142The recommended course of action is to [upgrade your cURL and OpenSSL packages](https://support.stripe.com/questions/how-do-i-upgrade-my-stripe-integration-from-tls-1-0-to-tls-1-2#php) so that TLS 1.2 is used by default, but if that is not possible, you might be able to solve the issue by setting the `CURLOPT_SSLVERSION` option to either `CURL_SSLVERSION_TLSv1` or `CURL_SSLVERSION_TLSv1_2`:
143
144```php
145$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1]);
146\Stripe\ApiRequestor::setHttpClient($curl);
147```
148
149### Per-request Configuration
150
151For apps that need to use multiple keys during the lifetime of a process, like
152one that uses [Stripe Connect][connect], it's also possible to set a
153per-request key and/or account:
154
155```php
156$customers = $stripe->customers->all([],[
157    'api_key' => 'sk_test_...',
158    'stripe_account' => 'acct_...'
159]);
160
161$stripe->customers->retrieve('cus_123456789', [], [
162    'api_key' => 'sk_test_...',
163    'stripe_account' => 'acct_...'
164]);
165```
166
167### Configuring CA Bundles
168
169By default, the library will use its own internal bundle of known CA
170certificates, but it's possible to configure your own:
171
172```php
173\Stripe\Stripe::setCABundlePath("path/to/ca/bundle");
174```
175
176### Configuring Automatic Retries
177
178The library can be configured to automatically retry requests that fail due to
179an intermittent network problem:
180
181```php
182\Stripe\Stripe::setMaxNetworkRetries(2);
183```
184
185[Idempotency keys][idempotency-keys] are added to requests to guarantee that
186retries are safe.
187
188### Request latency telemetry
189
190By default, the library sends request latency telemetry to Stripe. These
191numbers help Stripe improve the overall latency of its API for all users.
192
193You can disable this behavior if you prefer:
194
195```php
196\Stripe\Stripe::setEnableTelemetry(false);
197```
198
199## Development
200
201Get [Composer][composer]. For example, on Mac OS:
202
203```bash
204brew install composer
205```
206
207Install dependencies:
208
209```bash
210composer install
211```
212
213The test suite depends on [stripe-mock], so make sure to fetch and run it from a
214background terminal ([stripe-mock's README][stripe-mock] also contains
215instructions for installing via Homebrew and other methods):
216
217```bash
218go get -u github.com/stripe/stripe-mock
219stripe-mock
220```
221
222Install dependencies as mentioned above (which will resolve [PHPUnit](http://packagist.org/packages/phpunit/phpunit)), then you can run the test suite:
223
224```bash
225./vendor/bin/phpunit
226```
227
228Or to run an individual test file:
229
230```bash
231./vendor/bin/phpunit tests/UtilTest.php
232```
233
234Update bundled CA certificates from the [Mozilla cURL release][curl]:
235
236```bash
237./update_certs.php
238```
239
240The library uses [PHP CS Fixer][php-cs-fixer] for code formatting. Code must be formatted before PRs are submitted, otherwise CI will fail. Run the formatter with:
241
242```bash
243./vendor/bin/php-cs-fixer fix -v .
244```
245
246## Attention plugin developers
247
248Are you writing a plugin that integrates Stripe and embeds our library? Then please use the `setAppInfo` function to identify your plugin. For example:
249
250```php
251\Stripe\Stripe::setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");
252```
253
254The method should be called once, before any request is sent to the API. The second and third parameters are optional.
255
256### SSL / TLS configuration option
257
258See the "SSL / TLS compatibility issues" paragraph above for full context. If you want to ensure that your plugin can be used on all systems, you should add a configuration option to let your users choose between different values for `CURLOPT_SSLVERSION`: none (default), `CURL_SSLVERSION_TLSv1` and `CURL_SSLVERSION_TLSv1_2`.
259
260[composer]: https://getcomposer.org/
261[connect]: https://stripe.com/connect
262[curl]: http://curl.haxx.se/docs/caextract.html
263[idempotency-keys]: https://stripe.com/docs/api/php#idempotent_requests
264[php-cs-fixer]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
265[psr3]: http://www.php-fig.org/psr/psr-3/
266[stripe-mock]: https://github.com/stripe/stripe-mock
267