• Stars
    star
    3,558
  • Rank 11,791 (Top 0.3 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 13 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP library for the Stripe API.

Stripe PHP bindings

Build Status Latest Stable Version Total Downloads License Code Coverage

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:

  • 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 protected]',
    '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");

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.

More Repositories

1

stripe-node

Node.js library for the Stripe API.
TypeScript
3,591
star
2

stripe-ios

Stripe iOS SDK
Swift
2,009
star
3

stripe-go

Go library for the Stripe API.
Go
1,948
star
4

stripe-ruby

Ruby library for the Stripe API.
Ruby
1,866
star
5

veneur

A distributed, fault-tolerant pipeline for observability data
Go
1,690
star
6

react-stripe-js

React components for Stripe.js and Stripe Elements
TypeScript
1,612
star
7

stripe-cli

A command-line tool for Stripe
Go
1,538
star
8

stripe-python

Python library for the Stripe API.
Python
1,507
star
9

stripe-dotnet

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com.
C#
1,307
star
10

stripe-mock

stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's testmode to make test suites integrating with Stripe faster and less brittle.
Go
1,278
star
11

stripe-react-native

React Native library for Stripe.
TypeScript
1,192
star
12

stripe-android

Stripe Android SDK
Kotlin
1,190
star
13

smokescreen

A simple HTTP proxy that fogs over naughty URLs
Go
988
star
14

elements-examples

Stripe Elements examples.
HTML
987
star
15

stripe-java

Java library for the Stripe API.
Java
735
star
16

skycfg

Skycfg is an extension library for the Starlark language that adds support for constructing Protocol Buffer messages.
Go
629
star
17

stripe-connect-rocketrides

Sample on-demand platform built on Stripe: Connect onboarding for pilots, iOS app for passengers to request rides.
Swift
614
star
18

stripe-js

Loading wrapper for Stripe.js
TypeScript
561
star
19

poncho

Easily create REST APIs
Ruby
518
star
20

rainier

Bayesian inference in Scala.
Scala
434
star
21

openapi

An OpenAPI specification for the Stripe API.
351
star
22

stripe-billing-typographic

⚡️Typographic is a webfont service (and demo) built with Stripe Billing.
JavaScript
215
star
23

subprocess

A port of Python's subprocess module to Ruby
Ruby
193
star
24

carbon-removal-source-materials

Source materials supporting Stripe Climate carbon removal purchases (http://stripe.com/climate)
189
star
25

dagon

Tools for rewriting and optimizing DAGs (directed-acyclic graphs) in Scala
Scala
149
star
26

bonsai

Beautiful trees, without the landscaping.
Scala
142
star
27

pg-schema-diff

Go library for diffing Postgres schemas and generating SQL migrations
Go
141
star
28

ios-dashboard-ui

[DEPRECATED] UI components from the Stripe Dashboard iOS app
Swift
136
star
29

stripe-apps

Stripe Apps lets you embed custom user experiences directly in the Stripe Dashboard and orchestrate the Stripe API.
TypeScript
134
star
30

vscode-stripe

Stripe for Visual Studio Code
TypeScript
117
star
31

example-mobile-backend

A simple, easy-to-deploy backend that you can use to demo our example mobile apps.
Ruby
105
star
32

stripe.github.io

A landing page for Stripe's GitHub organization
HTML
92
star
33

stripe-terminal-ios

Stripe Terminal iOS SDK
Objective-C
92
star
34

stripe-demo-connect-roastery-saas-platform

Roastery demo SaaS platform using Stripe Connect
JavaScript
89
star
35

stripe-terminal-react-native

React Native SDK for Stripe Terminal
TypeScript
79
star
36

example-terminal-backend

A simple, easy-to-deploy backend that you can use to run the Stripe Terminal example apps
Ruby
77
star
37

stripe-terminal-android

Stripe Terminal Android SDK
Java
77
star
38

unilog

A logger for use with daemontools.
Go
77
star
39

stripe-terminal-js-demo

Demo app for the Stripe Terminal JS SDK
JavaScript
75
star
40

stripe-postman

Postman collection for Stripe's API
71
star
41

goforit

A feature flags client library for Go
Go
68
star
42

stripe-connect-custom-rocketdeliveries

Sample on-demand platform built on Stripe Connect: Custom Accounts and Connect Onboarding for deliveries. https://rocketdeliveries.io
JavaScript
62
star
43

tracer-objc

Generic record & playback framework for Objective-C
Objective-C
51
star
44

mobile-viewport-control

Dynamically control the mobile viewport
JavaScript
47
star
45

log4j-remediation-tools

Tools for remediating the recent log4j2 RCE vulnerability (CVE-2021-44228)
Go
41
star
46

stripe-reachability

A bash script to test access to the Stripe API
Shell
29
star
47

terminal-js

Loading wrapper for the Terminal JS SDK
TypeScript
29
star
48

krl

OpenSSH Key Revocation List support for Go
Go
29
star
49

stripe-identity-react-native

React Native library for Stripe Identity
TypeScript
28
star
50

stripe-magento2-releases

19
star
51

checkout-sales-demo

Sales demo of Stripe Checkout with different locales around the world.
HTML
12
star
52

stripe-ios-spm

Swift Package Manager mirror for the Stripe iOS SDK. See http://github.com/stripe/stripe-ios for details.
12
star
53

connect-js

Loading wrapper for Connect.js
TypeScript
11
star
54

react-connect-js

React components for Connect.js and Connect embedded components
TypeScript
11
star
55

stripe-mirakl-connector

Official Stripe Mirakl Connector
PHP
10
star
56

salesforce-connector-examples

Stripe Salesforce Connector examples
JavaScript
9
star
57

homebrew-stripe-mock

Homebrew tap for stripestub
Ruby
6
star
58

homebrew-stripe-cli

Ruby
6
star
59

.github

Stripe uses the Contributor Covenant Code of Conduct for our open-source community.
6
star
60

scoop-stripe-cli

4
star
61

ssf-ruby

A Ruby client for the Sensor Sensibility Format
Ruby
4
star
62

stripe-sfcc-b2c-connector

JavaScript
2
star