Integrating Laravel Cashier with Stripe

Aug 10, 2024

Laravel Cashier and Stripe Integration

Introduction

  • Discusses the experience of finding outdated tutorials on Laravel Cashier and Stripe integrations.
  • Aim: Show how to use Laravel Cashier with Stripe for managing user subscriptions in Laravel 11 (2024).

Agenda

  1. Install Laravel Cashier
  2. Configure Laravel Cashier
  3. Modify User Model
  4. Configure Stripe webhooks
  5. Create Products and Prices in Stripe
  6. Take the user to a Stripe-hosted checkout page
  7. Verify user subscription on the webpage

Steps to Implement

1. Install Laravel Cashier

  • Use the command: composer require laravel/cashier

2. Configure Laravel Cashier

  • Publish the migrations: php artisan vendor:publish --tag="cashier-migrations"
  • Run the migrations: php artisan migrate
  • Publish the configuration file: php artisan vendor:publish --tag="cashier-config"
  • Update .env file with Stripe API keys
    STRIPE_KEY="your-stripe-key"
    STRIPE_SECRET="your-stripe-secret"
    STRIPE_WEBHOOK_SECRET="your-stripe-webhook-secret"
    
  • Update User model to use Billable trait:
    use Laravel\Cashier\Billable;
    class User extends Authenticatable
    {
        use Billable;
    }
    

3. Configure Stripe Webhooks

  • Use Stripe CLI to handle webhooks locally:
    stripe login
    stripe listen --forward-to http://localhost/stripe/webhook
    
  • Add webhook secret key to .env file.
  • Run the command: php artisan cashier:webhook

4. Create Products and Prices in Stripe

  • Go to Stripe Dashboard > Product Catalog and create a new product "Premium"
  • Add multiple pricing options (Monthly, Yearly, Lifetime)
  • Note down the product and price IDs.

5. Create Pricing Page in Laravel

  • Generate a view for the pricing page: php artisan make:view pricing
  • Design the pricing page using a template or custom HTML.
  • Add navigation link to pricing page in navigation.blade.php.
  • Define route in web.php:
    Route::view('/pricing', 'pricing')->name('pricing');
    

6. Implement Checkout Process

  • Create a controller for checkout: php artisan make:controller CheckoutController --invokable
  • Implement the controller to handle Stripe subscription process:
    public function __invoke(Request $request, string $plan = null)
    {
        $user = $request->user();
        $plan = $plan ?? 'default-plan-id';
        return $user->newSubscription('premium', $plan)
            ->checkout([ /* success and cancel URLs */ ]);
    }
    
  • Update routes in web.php:
    Route::get('/checkout/{plan?}', CheckoutController::class)->name('checkout');
    Route::view('/success', 'success')->name('success');
    

7. Verify User Subscription

  • Create a success view: php artisan make:view success
  • In the success view, check for user's subscription status:
    @if (auth()->user()->subscribedToPrice('price-id', 'product-id'))
        <p>You're subscribed to premium ($4.99/month)</p>
    @endif
    
  • Test the entire flow from pricing selection to successful subscription.

Summary

  • Demonstrated steps to integrate Laravel Cashier with Stripe for subscription management.
  • Emphasized the importance of webhooks for keeping database in sync with Stripe.
  • Provided a practical implementation guide for creating, configuring, and using Laravel Cashier and Stripe.