stripe/stripe-php
Official Stripe PHP SDK for accessing the Stripe API. Install via Composer, configure your API key, and use resource classes that map to Stripe objects and endpoints. Supports PHP 7.2+ (older versions being phased out).
Installation:
composer require stripe/stripe-php
Add to composer.json autoload if not using Composer's default:
"autoload": {
"psr-4": {
"App\\": "src/",
"Stripe\\": "vendor/stripe/stripe-php/src/"
}
}
Initialize Client:
require __DIR__ . '/vendor/autoload.php';
\Stripe\Stripe::setApiKey(config('services.stripe.key')); // Laravel config
First Use Case: Create a customer and charge:
$customer = \Stripe\Customer::create([
'email' => 'user@example.com',
'name' => 'John Doe',
'payment_method' => 'pm_123',
]);
$charge = \Stripe\Charge::create([
'amount' => 1000,
'currency' => 'usd',
'customer' => $customer->id,
]);
StripeClient class for service-based patterns (preferred in modern versions).Service-Based Pattern (Recommended)
$stripe = new \Stripe\StripeClient(config('services.stripe.key'));
$customer = $stripe->customers->create([...]);
\Stripe\Customer::create() otherwise).Legacy Static Pattern
\Stripe\Stripe::setApiKey(config('services.stripe.key'));
$customer = \Stripe\Customer::create([...]);
Webhook Handling
use Stripe\Webhook;
$payload = file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = Webhook::constructEvent($payload, $sig_header, config('services.stripe.webhook_secret'));
switch ($event->type) {
case 'payment_intent.succeeded':
// Handle success
break;
}
stripe/webhooks package for built-in middleware.Subscription Management
$subscription = $stripe->subscriptions->create([
'customer' => $customer->id,
'items' => [['price' => 'price_123']],
]);
subscription_items for dynamic updates.php artisan vendor:publish --tag=stripe-config.stripe-mock for local testing:
stripe-mock
Mock requests in tests:
$this->mockStripe('customers/create', ['id' => 'cus_123']);
$charge = \Stripe\Charge::create([...], ['idempotency_key' => 'unique_key']);
TLS 1.2 Requirement
invalid_request_error with TLS 1.0/1.1.\Stripe\Stripe::setCABundlePath('/path/to/ca-bundle.crt');
Or set cURL options:
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2]);
\Stripe\ApiRequestor::setHttpClient($curl);
Legacy vs. Modern SDK
\Stripe\Customer::create() (legacy) with $stripe->customers->create() (modern).Webhook Verification
Webhook::constructEvent() and use HTTP_STRIPE_SIGNATURE.Rate Limits
429 Too Many Requests.Stripe::setMaxNetworkRetries(3).Undocumented Fields
rawRequest for beta features or undocumented endpoints:
$response = $stripe->rawRequest('post', '/v1/beta_endpoint', [...]);
\Stripe\Stripe::setLogger(new \Monolog\Logger('stripe', [...]));
$customer = \Stripe\Customer::create([...]);
$lastResponse = $customer->getLastResponse();
print_r($lastResponse->headers); // Debug headers
stripe-mock to avoid hitting live API during development.Custom HTTP Client
$client = new \Stripe\HttpClient\CurlClient([
CURLOPT_PROXY => 'http://proxy:8080',
CURLOPT_TIMEOUT => 30,
]);
\Stripe\ApiRequestor::setHttpClient($client);
Telemetry
\Stripe\Stripe::setEnableTelemetry(false);
Beta Features
-beta.X versions for public previews:
composer require stripe/stripe-php:v15.0.0-beta.1
\Stripe\Stripe::addBetaVersion('feature_beta', 'v3');
Laravel Service Provider
StripeClient to the container:
$this->app->singleton(\Stripe\StripeClient::class, function ($app) {
return new \Stripe\StripeClient(config('services.stripe.key'));
});
public function __construct(private \Stripe\StripeClient $stripe) {}
How can I help you explore Laravel packages today?