Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PHP library for the Stripe API.

License

NotificationsYou must be signed in to change notification settings

stripe/stripe-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusLatest Stable VersionTotal DownloadsLicense

The Stripe PHP library provides convenient access to the Stripe API fromapplications written in the PHP language. It includes a pre-defined set ofclasses for API resources that initialize themselves dynamically from APIresponses which makes it compatible with a wide range of versions of the StripeAPI.

Requirements

PHP 5.6.0 and later.

Composer

You can install the bindings viaComposer. Run the following command:

composer require stripe/stripe-php

To use the bindings, use Composer'sautoload:

require_once'vendor/autoload.php';

Manual Installation

If you do not wish to use Composer, you can download thelatest release. Then, to use the bindings, include theinit.php file.

require_once'/path/to/stripe-php/init.php';

Dependencies

The bindings require the following extensions in order to work properly:

  • curl, although you can use your own non-cURL client if you prefer
  • json
  • mbstring (Multibyte String)

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 version7.33.0. Review themigration guide for the backwards-compatible client/services pattern changes.

Documentation

See thePHP API docs.

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 ourreleases 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 ourreleases page. This version will continue to work with new versions of the Stripe API for all common uses.

Custom Request Timeouts

NoteWe 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 useidempotency 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_TIMEOUTecho$curl->getTimeout();// 10echo$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 requisiteCURLOPT_* array to the CurlClient constructor, using the same syntax ascurl_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. SeetestDefaultOptions() intests/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 configuredwith aPSR-3 compatible logger so that messagesend up there instead oferror_log:

\Stripe\Stripe::setLogger($logger);

Accessing response data

You can access the data from the last API response on any object viagetLastResponse().

$customer =$stripe->customers->create(['description' =>'example customer',]);echo$customer->getLastResponse()->headers['Request-Id'];

SSL / TLS compatibility issues

Stripe's API now requires thatall 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 aninvalid_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 athttps://stripe.com/blog/upgrading-tls.".

The recommended course of action is toupgrade 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 theCURLOPT_SSLVERSION option to eitherCURL_SSLVERSION_TLSv1 orCURL_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, likeone that usesStripe Connect, it's also possible to set aper-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 CAcertificates, 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 toan intermittent network problem:

\Stripe\Stripe::setMaxNetworkRetries(2);

Idempotency keys are added to requests to guarantee thatretries are safe.

Telemetry

By default, the library sends telemetry to Stripe regarding request latency and feature usage. Thesenumbers help Stripe improve the overall latency of its API for all users, andimprove popular features.

You can disable this behavior if you prefer:

\Stripe\Stripe::setEnableTelemetry(false);

Public Preview SDKs

Stripe has features in thepublic preview phase that can be accessed via versions of this package that have the-beta.X suffix like12.2.0-beta.2.We would love for you to try these as we incrementally release new features and improve them based on your feedback.

The public preview SDKs are a different version of the same package as the stable SDKs. These versions are appended with-beta.X such as15.0.0-beta.1. To install, choose the version that includes support for the preview feature you are interested in by reviewing thereleases page and then use it in thecomposer require command:

composer require stripe/stripe-php:v<replace-with-the-version-of-your-choice>

NoteThere can be breaking changes between two versions of the public preview SDKs without a bump in the major version. Therefore we recommend pinning the package version to a specific 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 version of the public preview SDK.

Some preview features require a name and version to be set in theStripe-Version header likefeature_beta=v3. If the preview feature you are interested in has this requirement, use the functionaddBetaVersion (available only in the public preview SDKs):

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 therawRequest 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

Contribution guidelines for this project

We usejust for conveniently running development tasks. You can use them directly, or copy the commands out of thejustfile. To our help docs, runjust.

To get started, installComposer. For example, on Mac OS:

brew install composer

Install dependencies:

just install# or: composer install

The test suite depends onstripe-mock, so make sure to fetch and run it from abackground terminal (stripe-mock's README also containsinstructions for installing via Homebrew and other methods):

go install github.com/stripe/stripe-mock@lateststripe-mock

Install dependencies as mentioned above (which will resolvePHPUnit), then you can run the test suite:

justtest# or: ./vendor/bin/phpunit

Or to run an individual test file:

justtest tests/Stripe/UtilTest.php# or: ./vendor/bin/phpunit tests/Stripe/UtilTest.php

Update bundled CA certificates from theMozilla cURL release:

./update_certs.php

The library usesPHP CS Fixer for code formatting. Code must be formatted before PRs are submitted, otherwise CI will fail. Run the formatter with:

just format# or: ./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 thesetAppInfo 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 forCURLOPT_SSLVERSION: none (default),CURL_SSLVERSION_TLSv1 andCURL_SSLVERSION_TLSv1_2.

About

PHP library for the Stripe API.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp