Installation
composer require crs/stripe
Ensure CRS\StripeBundle\CRSStripeBundle() is registered in app/Kernel.php under $bundles.
Configuration
Add Stripe keys to config/packages/crs_stripe.yaml (or config.yml in older Laravel/Symfony versions):
crs_stripe:
publishable_key: "pk_test_your_key"
secret_key: "sk_test_your_key"
currency: "USD"
First Use Case: Direct Charge
use CRS\StripeBundle\Stripe;
$stripe = new Stripe();
$stripe->setCardNumber('4242424242424242')
->setName('John Doe')
->setExpMonth(12)
->setExpYear(2030)
->setCVC('123');
$charge = $stripe->Charge()->Pay(100); // $1.00 (amount in cents)
Customer Creation & Subscription
// Create a customer
$customer = $stripe->Customer()->Create([
'email' => 'user@example.com',
'source' => 'tok_visa' // Token from Stripe.js
]);
// Subscribe the customer
$subscription = $stripe->Subscription()->Create([
'customer' => $customer->id,
'plan' => 'monthly_plan_id'
]);
Webhook Handling
Use Laravel’s route:web middleware to handle Stripe webhooks (e.g., payment_intent.succeeded):
Route::post('/stripe/webhook', [StripeWebhookController::class, 'handle']);
Verify signatures with:
$event = \Stripe\Webhook::constructEvent(
$request->getContent(),
$request->header('Stripe-Signature'),
'your_webhook_secret'
);
Token-Based Payments (Recommended) Use Stripe.js to generate tokens client-side, then pass to Laravel:
// Frontend (Stripe.js)
Stripe('pk_test_key').createToken(card).then(function(result) {
if (result.error) { /* Handle error */ }
else {
fetch('/charge', { method: 'POST', body: JSON.stringify({ token: result.token.id }) });
}
});
// Backend
$charge = $stripe->Charge()->PayWithToken($request->token, 100);
// In StripeServiceProvider
$this->app->singleton(Stripe::class, function ($app) {
return new Stripe($app['config']['crs_stripe']);
});
use CRS\StripeBundle\Validators\CardValidator;
$validator = new CardValidator();
if (!$validator->validate($stripe)) {
throw new \InvalidArgumentException('Invalid card details');
}
\Log::info('Stripe Charge', [
'amount' => $charge->amount,
'customer' => $charge->customer,
'status' => $charge->status
]);
Deprecated Methods
stripe/stripe-php (v7+) for modern features:
composer require stripe/stripe-php
CRS\StripeBundle\Stripe with Laravel’s facade or direct SDK calls:
\Stripe\Stripe::setApiKey(config('crs_stripe.secret_key'));
$charge = \Stripe\Charge::create(['amount' => 100, 'currency' => 'usd']);
Amount Handling
$1.00 = 100). Forgetting this causes 402 Validation Error from Stripe.$amountInCents = $amount * 100;
Bundle Stagnation
Webhook Security
4242424242424242) but never hardcode them in production.\Stripe\Stripe::setApiKey(config('crs_stripe.secret_key'));
\Stripe\Stripe::setAppInfo('MyApp', '1.0');
\Stripe\Log::setLogLevel(\Stripe\Log::DEBUG); // Logs to storage/logs/stripe.log
Custom Charge Logic
Extend the Charge class to add metadata or business rules:
class CustomCharge extends \CRS\StripeBundle\Charge
{
public function Pay($amount, array $options = [])
{
$options['metadata'] = ['user_id' => auth()->id()];
return parent::Pay($amount, $options);
}
}
Plan Management Dynamically fetch plans from a database:
$plan = Plan::where('name', 'premium')->first();
$subscription = $stripe->Subscription()->Create([
'customer' => $customer->id,
'items' => [['plan' => $plan->stripe_plan_id]]
]);
Refunds & Disputes Use Stripe’s refund API:
$refund = \Stripe\Refund::create([
'charge' => $charge->id,
'amount' => $charge->amount,
]);
How can I help you explore Laravel packages today?