Requirements
PHP 5.6.0 and later.
Dependencies
The bindings require the following extensions in order to work properly:
If you use Composer, these dependencies should be handled automatically. If you install manually, you’ll want to make sure that these extensions are available.
Client/service patterns vs legacy patterns
You can continue to use the legacy integration patterns used prior to version 7.33.0. Review the migration guide for the backwards-compatible client/services pattern changes.
Documentation
See the PHP API docs.
See video demonstrations covering how to use the library.
PHP 5.3
If you are using PHP 5.3, you should upgrade your environment as this version has been past end of life since August 2014. Otherwise, you can download v5.9.2 (zip, tar.gz) from our releases page. This version will continue to work with new versions of the Stripe API for all common uses.
Custom Request Timeouts
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 to avoid executing the same transaction twice as a result of timeout retry logic.
To 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.
// set up your tweaked Curl client
$curl = new \Stripe\HttpClient\CurlClient();
$curl->setTimeout(10); // default is \Stripe\HttpClient\CurlClient::DEFAULT_TIMEOUT
$curl->setConnectTimeout(5); // default is \Stripe\HttpClient\CurlClient::DEFAULT_CONNECT_TIMEOUT
echo $curl->getTimeout(); // 10
echo $curl->getConnectTimeout(); // 5
// tell Stripe to use the tweaked client
\Stripe\ApiRequestor::setHttpClient($curl);
// use the Stripe API client as you normally would
Custom cURL Options (e.g. proxies)
Need 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.
// set up your tweaked Curl client
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_PROXY => 'proxy.local:80']);
// tell Stripe to use the tweaked client
\Stripe\ApiRequestor::setHttpClient($curl);
Alternately, 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.
Configuring a Logger
The library does minimal logging, but it can be configured
with a PSR-3
compatible logger so that messages
end up there instead of error_log
:
\Stripe\Stripe::setLogger($logger);
Accessing response data
You can access the data from the last API response on any object via getLastResponse()
.
$customer = $stripe->customers->create([
'description' => 'example customer',
]);
echo $customer->getLastResponse()->headers['Request-Id'];
Per-request Configuration
For apps that need to use multiple keys during the lifetime of a process, like one that uses Stripe Connect, it’s also possible to set a per-request key and/or account:
$customers = $stripe->customers->all([],[
'api_key' => 'sk_test_...',
'stripe_account' => 'acct_...'
]);
$stripe->customers->retrieve('cus_123456789', [], [
'api_key' => 'sk_test_...',
'stripe_account' => 'acct_...'
]);
Configuring CA Bundles
By default, the library will use its own internal bundle of known CA certificates, but it’s possible to configure your own:
\Stripe\Stripe::setCABundlePath("path/to/ca/bundle");
Configuring Automatic Retries
The library can be configured to automatically retry requests that fail due to an intermittent network problem:
\Stripe\Stripe::setMaxNetworkRetries(2);
Idempotency keys are added to requests to guarantee that retries are safe.
Beta SDKs
Stripe has features in the beta phase that can be accessed via the beta version of this package.
We would love for you to try these and share feedback with us before these features reach the stable phase.
Use the composer require
command with an exact version specified to install the beta version of the stripe-php pacakge.
composer require stripe/stripe-php:v9.2.0-beta.1
Note There can be breaking changes between beta versions. Therefore we recommend pinning the package version to a specific beta version in your composer.json file. This way you can install the same version each time without breaking changes unless you are intentionally looking for the latest beta version.
We highly recommend keeping an eye on when the beta feature you are interested in goes from beta to stable so that you can move from using a beta version of the SDK to the stable version.
If your beta feature requires a Stripe-Version
header to be sent, set the apiVersion
property of config
object by using the function addBetaVersion
:
Stripe::addBetaVersion("feature_beta", "v3");
Development
Get Composer. For example, on Mac OS:
brew install composer
Install dependencies:
composer install
The test suite depends on stripe-mock, so make sure to fetch and run it from a background terminal (stripe-mock’s README also contains instructions for installing via Homebrew and other methods):
go install github.com/stripe/stripe-mock@latest
stripe-mock
Install dependencies as mentioned above (which will resolve PHPUnit), then you can run the test suite:
./vendor/bin/phpunit
Or to run an individual test file:
./vendor/bin/phpunit tests/Stripe/UtilTest.php
Update bundled CA certificates from the Mozilla cURL release:
./update_certs.php
The library uses PHP CS Fixer for code formatting. Code must be formatted before PRs are submitted, otherwise CI will fail. Run the formatter with:
./vendor/bin/php-cs-fixer fix -v .
Attention plugin developers
Are you writing a plugin that integrates Stripe and embeds our library? Then please use the setAppInfo
function to identify your plugin. For example:
\Stripe\Stripe::setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");
The method should be called once, before any request is sent to the API. The second and third parameters are optional.
SSL / TLS configuration option
See 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
.