stripe / stripe-php
Stripe PHP Library
Installs: 100 791 055
Dependents: 504
Suggesters: 9
Security: 0
Stars: 3 754
Watchers: 115
Forks: 848
Open Issues: 16
Requires
- php: >=5.6.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.5.0
- phpstan/phpstan: ^1.2
- phpunit/phpunit: ^5.7 || ^9.0
- v16.4.0-beta.1
- v16.3.0
- v16.3.0-beta.3
- v16.3.0-beta.2
- v16.3.0-beta.1
- v16.2.0
- v16.2.0-beta.3
- v16.2.0-beta.2
- v16.2.0-beta.1
- v16.1.1
- v16.1.0
- v16.0.0
- v15.11.0-beta.1
- v15.10.0
- v15.10.0-beta.1
- v15.9.0
- v15.9.0-beta.1
- v15.8.0
- v15.8.0-beta.1
- v15.7.0
- v15.7.0-beta.1
- v15.6.0
- v15.6.0-beta.1
- v15.5.0
- v15.5.0-beta.1
- v15.4.0
- v15.3.0
- v15.3.0-beta.1
- v15.2.0
- v15.2.0-beta.1
- v15.1.0
- v15.0.0
- v14.11.0-beta.1
- v14.10.0
- v14.10.0-beta.1
- v14.9.0
- v14.9.0-beta.1
- v14.8.0
- v14.8.0-beta.1
- v14.7.0
- v14.7.0-beta.1
- v14.6.0
- v14.6.0-beta.1
- v14.5.0
- v14.5.0-beta.1
- v14.4.0
- v14.4.0-beta.1
- v14.3.0
- v14.2.0
- v14.2.0-beta.1
- v14.1.0
- v14.0.0
- v13.18.0
- v13.18.0-beta.1
- v13.17.0
- v13.16.0
- v13.16.0-beta.1
- v13.15.0
- v13.15.0-beta.1
- v13.14.0
- v13.14.0-beta.1
- v13.13.0
- v13.13.0-beta.1
- v13.12.0
- v13.12.0-beta.1
- v13.11.0
- v13.11.0-beta.1
- v13.10.0
- v13.10.0-beta.3
- v13.10.0-beta.2
- v13.10.0-beta.1
- v13.9.0
- v13.9.0-beta.1
- v13.8.0
- v13.8.0-beta.1
- v13.7.0
- v13.7.0-beta.1
- v13.6.0
- v13.6.0-beta.1
- v13.5.0
- v13.5.0-beta.1
- v13.4.0
- v13.4.0-beta.1
- v13.3.0
- v13.3.0-beta.1
- v13.2.1
- v13.2.0
- v13.2.0-beta.1
- v13.1.0
- v13.1.0-beta.1
- v13.0.0
- v12.9.0-beta.1
- v12.8.0
- v12.8.0-beta.1
- v12.7.0
- v12.7.0-beta.1
- v12.6.0
- v12.6.0-beta.1
- v12.5.0
- v12.5.0-beta.1
- v12.4.0
- v12.4.0-beta.1
- v12.3.0
- v12.3.0-beta.1
- v12.2.0
- v12.2.0-beta.1
- v12.1.0
- v12.0.0
- v12.0.0-beta.1
- v11.0.0
- v10.22.0-beta.1
- v10.21.0
- v10.21.0-beta.1
- v10.20.0
- v10.20.0-beta.2
- v10.20.0-beta.1
- v10.19.0
- v10.18.0
- v10.18.0-beta.1
- v10.17.0
- v10.16.0
- v10.16.0-beta.1
- v10.15.0
- v10.15.0-beta.2
- v10.15.0-beta.1
- v10.14.0
- v10.14.0-beta.2
- v10.14.0-beta.1
- v10.13.0
- v10.13.0-beta.4
- v10.13.0-beta.3
- v10.13.0-beta.2
- v10.13.0-beta.1
- v10.12.1
- v10.12.0
- v10.12.0-beta.1
- v10.11.0
- v10.11.0-beta.1
- v10.10.0
- v10.10.0-beta.1
- v10.9.1
- v10.9.0
- v10.9.0-beta.1
- v10.8.0
- v10.8.0-beta.1
- v10.7.0
- v10.7.0-beta.1
- v10.6.1
- v10.6.0
- v10.6.0-beta.1
- v10.5.0
- v10.5.0-beta.2
- v10.5.0-beta.1
- v10.4.0
- v10.4.0-beta.3
- v10.4.0-beta.2
- v10.4.0-beta.1
- v10.3.0
- v10.3.0-beta.1
- v10.2.0
- v10.2.0-beta.1
- v10.1.0
- v10.0.0
- v9.9.0
- v9.9.0-beta.2
- v9.9.0-beta.1
- v9.8.0
- v9.7.0
- v9.7.0-beta.2
- v9.7.0-beta.1
- v9.6.0
- v9.5.0
- v9.4.0
- v9.4.0-beta.1
- v9.3.0
- v9.3.0-beta.1
- v9.2.0
- v9.2.0-beta.1
- v9.1.0
- v9.1.0-beta.1
- v9.0.0
- v8.125.0-beta.4
- v8.12.0
- v8.12.0-beta.1
- v8.11.0
- v8.10.0
- v8.9.0
- v8.8.0
- v8.7.0
- v8.6.0
- v8.5.0
- v8.4.0
- v8.3.0
- v8.2.0
- v8.1.0
- v8.0.0
- v7.128.0
- v7.127.0
- v7.126.0
- v7.125.0
- v7.125.0-beta.1
- v7.124.0
- v7.124.0-beta.1
- v7.123.0
- v7.123.0-beta.1
- v7.122.0
- v7.121.0
- v7.120.0
- v7.119.0
- v7.118.0
- v7.117.0
- v7.116.0
- v7.115.0
- v7.114.0
- v7.113.0
- v7.112.0
- v7.111.0
- v7.110.0
- v7.109.0
- v7.108.0
- v7.107.0
- v7.106.0
- v7.105.0
- v7.104.0
- v7.103.0
- v7.102.0
- v7.101.0
- v7.100.0
- v7.99.0
- v7.98.0
- v7.97.0
- v7.96.0
- v7.95.0
- v7.94.0
- v7.93.0
- v7.92.0
- v7.91.0
- v7.90.0
- v7.89.0
- v7.88.0
- v7.87.0
- v7.86.0
- v7.85.0
- v7.84.0
- v7.83.0
- v7.82.0
- v7.81.0
- v7.80.0
- v7.79.0
- v7.78.0
- v7.77.0
- v7.76.0
- v7.75.0
- v7.74.0
- v7.73.0
- v7.72.0
- v7.71.0
- v7.70.0
- v7.69.0
- v7.68.0
- v7.67.0
- v7.66.1
- v7.66.0
- v7.65.0
- v7.64.0
- v7.63.0
- v7.62.0
- v7.61.0
- v7.60.0
- v7.59.0
- v7.58.0
- v7.57.0
- v7.56.0
- v7.55.0
- v7.54.0
- v7.53.1
- v7.53.0
- v7.52.0
- v7.51.0
- v7.50.0
- v7.49.0
- v7.48.0
- v7.47.0
- v7.46.1
- v7.46.0
- v7.45.0
- v7.44.0
- v7.43.0
- v7.42.0
- v7.41.1
- v7.41.0
- v7.40.0
- v7.39.0
- v7.38.0
- v7.37.2
- v7.37.1
- v7.37.0
- v7.36.2
- v7.36.1
- v7.36.0
- v7.35.0
- v7.34.0
- v7.33.1
- v7.33.0
- v7.32.1
- v7.32.0
- v7.31.0
- v7.30.0
- v7.29.0
- v7.28.1
- v7.28.0
- v7.27.3
- v7.27.2
- v7.27.1
- v7.27.0
- v7.26.0
- v7.25.0
- v7.24.0
- v7.23.0
- v7.22.0
- v7.21.1
- v7.21.0
- v7.20.0
- v7.19.1
- v7.19.0
- v7.18.0
- v7.17.0
- v7.16.0
- v7.15.0
- v7.14.2
- v7.14.1
- v7.14.0
- v7.13.0
- v7.12.0
- v7.11.0
- v7.10.0
- v7.9.0
- v7.8.0
- v7.7.1
- v7.7.0
- v7.6.0
- v7.5.0
- v7.4.0
- v7.3.1
- v7.3.0
- v7.2.2
- v7.2.1
- v7.2.0
- v7.1.1
- v7.1.0
- v7.0.2
- v7.0.1
- v7.0.0
- v6.43.1
- v6.43.0
- v6.42.0
- v6.41.0
- v6.40.0
- v6.39.2
- v6.39.1
- v6.39.0
- v6.38.0
- v6.37.2
- v6.37.1
- v6.37.0
- v6.36.0
- v6.35.2
- v6.35.1
- v6.35.0
- v6.34.6
- v6.34.5
- v6.34.4
- v6.34.3
- v6.34.2
- v6.34.1
- v6.34.0
- v6.33.0
- v6.32.1
- v6.32.0
- v6.31.5
- v6.31.4
- v6.31.3
- v6.31.2
- v6.31.1
- v6.31.0
- v6.30.5
- v6.30.4
- v6.30.3
- v6.30.2
- v6.30.1
- v6.30.0
- v6.29.3
- v6.29.2
- v6.29.1
- v6.29.0
- v6.28.1
- v6.28.0
- v6.27.0
- v6.26.0
- v6.25.0
- v6.24.0
- v6.23.0
- v6.22.1
- v6.22.0
- v6.21.1
- v6.21.0
- v6.20.1
- v6.20.0
- v6.19.5
- v6.19.4
- v6.19.3
- v6.19.2
- v6.19.1
- v6.19.0
- v6.18.0
- v6.17.2
- v6.17.1
- v6.17.0
- v6.16.0
- v6.15.0
- v6.14.0
- v6.13.0
- v6.12.0
- v6.11.0
- v6.10.4
- v6.10.3
- v6.10.2
- v6.10.1
- v6.10.0
- v6.9.0
- v6.8.2
- v6.8.1
- v6.8.0
- v6.7.4
- v6.7.3
- v6.7.2
- v6.7.1
- v6.7.0
- v6.6.0
- v6.5.0
- v6.4.2
- v6.4.1
- v6.4.0
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.0
- v6.1.0
- v6.0.0
- v5.9.2
- v5.9.1
- v5.9.0
- v5.8.0
- v5.7.0
- v5.6.0
- v5.5.1
- v5.5.0
- v5.4.0
- v5.3.0
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.0
- v4.13.0
- v4.12.0
- v4.11.0
- v4.10.0
- v4.9.1
- v4.9.0
- v4.8.1
- v4.8.0
- v4.7.0
- v4.6.0
- v4.5.1
- v4.5.0
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.0
- v4.2.0
- v4.1.1
- v4.1.0
- v4.0.1
- v4.0.0
- v3.23.0
- v3.22.0
- v3.21.0
- v3.20.0
- v3.19.0
- v3.18.0
- v3.17.1
- v3.17.0
- v3.16.0
- v3.15.0
- v3.14.3
- v3.14.2
- v3.14.1
- v3.14.0
- v3.13.0
- v3.12.1
- v3.12.0
- v3.11.0
- v3.10.1
- v3.10.0
- v3.9.2
- v3.9.1
- v3.9.0
- v3.8.0
- v3.7.1
- v3.7.0
- v3.6.0
- v3.5.0
- v3.4.0
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.0
- v2.3.0
- v2.2.0
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- dev-master / 2.0.x-dev
- v2.0.1
- v2.0.0
- v1.18.0
- v1.17.5
- v1.17.4
- v1.17.3
- v1.17.2
- v1.17.1
- v1.17.0
- v1.16.0
- v1.15.0
- v1.14.1
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.0
- v1.10.1
- v1.9.1
- v1.9.0
- v1.8.3
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.15
- v1.7.14
- v1.7.13
- v1.7.12
- v1.7.11
- v1.7.10
- v1.7.9
- v1.7.8
- v1.7.7
- v1.7.6
- v1.7.5
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- dev-beta
- dev-latest-codegen-beta
- dev-latest-codegen-master
- dev-helenye-next-page
- dev-prathmesh/fix-related-object-fetch
- dev-helenye-invalid-request
- dev-ramya/func_get_args
- dev-prathmesh_raw_request_ga
- dev-prathmesh_remove_search_trait
- dev-prathmesh_deprecate_php_utils
- dev-prathmesh_collection_throws
- dev-ramya/invoiceitem
- dev-ramya/deprecated-events
- dev-ramya/merge-master
- dev-helenye-beta-version
- dev-helenye-beta-version-readme
- dev-anniel-ga-taxids
- dev-anniel-remove-broken-child-methods
- dev-richardm-fix-resources-tax-settings-update
- dev-pakrym/Add_deserialized_object_assertion_to_webhook_test
- dev-helenye-add-paginated-requests
- dev-richardm-rawrequest-usage
- dev-pakrym/Do_not_force_resolution_to_IPV4_addresses
- dev-richardm-merge-master-into-beta
- dev-richardm-bump-api-version
- dev-richardm-phpstan-level-3
- dev-pakrym/Send_request
This package is auto-updated.
Last update: 2024-11-21 21:12:37 UTC
README
The Stripe PHP library provides convenient access to the Stripe API from applications written in the PHP language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the Stripe API.
Requirements
PHP 5.6.0 and later.
Composer
You can install the bindings via Composer. Run the following command:
composer require stripe/stripe-php
To use the bindings, use Composer's autoload:
require_once 'vendor/autoload.php';
Manual Installation
If you do not wish to use Composer, you can download the latest release. Then, to use the bindings, include the init.php
file.
require_once '/path/to/stripe-php/init.php';
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.
Getting Started
Simple usage looks like:
$stripe = new \Stripe\StripeClient('sk_test_BQokikJOvBiI2HlWgH4olfQ2'); $customer = $stripe->customers->create([ 'description' => 'example customer', 'email' => 'email@example.com', 'payment_method' => 'pm_card_visa', ]); echo $customer;
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.
Legacy Version Support
PHP 5.4 & 5.5
If you are using PHP 5.4 or 5.5, you should consider upgrading your environment as those versions have been past end of life since September 2015 and July 2016 respectively. Otherwise, you can still use Stripe by downloading stripe-php v6.43.1 (zip, tar.gz) from our releases page. This version will work but might not support recent features we added since the version was released and upgrading PHP is the best course of action.
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'];
SSL / TLS compatibility issues
Stripe's API now requires that all connections use TLS 1.2. 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.".
The recommended course of action is to upgrade your cURL and OpenSSL packages 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
:
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1]); \Stripe\ApiRequestor::setHttpClient($curl);
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.
Telemetry
By default, the library sends telemetry to Stripe regarding request latency and feature usage. These numbers help Stripe improve the overall latency of its API for all users, and improve popular features.
You can disable this behavior if you prefer:
\Stripe\Stripe::setEnableTelemetry(false);
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");
Custom requests
If you would like to send a request to an undocumented API (for example you are in a private beta), or if you prefer to bypass the method definitions in the library and specify your request details directly, you can use the rawRequest
method on the StripeClient.
$stripe = new \Stripe\StripeClient('sk_test_xyz'); $response = $stripe->rawRequest('post', '/v1/beta_endpoint', [ "caveat": "emptor" ], [ "stripe_version" => "2022-11_15", ]); // $response->body is a string, you can call $stripe->deserialize to get a \Stripe\StripeObject. $obj = $stripe->deserialize($response->body); // For GET requests, the params argument must be null, and you should write the query string explicitly. $get_response = $stripe->rawRequest('get', '/v1/beta_endpoint?caveat=emptor', null, [ "stripe_version" => "2022-11_15", ]);
Support
New features and bug fixes are released on the latest major version of the Stripe PHP library. If you are on an older major version, we recommend that you upgrade to the latest in order to use the new features and bug fixes including those for security vulnerabilities. Older major versions of the package will continue to be available for use, but will not be receiving any updates.
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
.