Aug 10, 2024
composer require laravel/cashierphp artisan vendor:publish --tag="cashier-migrations"php artisan migratephp artisan vendor:publish --tag="cashier-config".env file with Stripe API keys
STRIPE_KEY="your-stripe-key"
STRIPE_SECRET="your-stripe-secret"
STRIPE_WEBHOOK_SECRET="your-stripe-webhook-secret"
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
}
stripe login
stripe listen --forward-to http://localhost/stripe/webhook
.env file.php artisan cashier:webhookphp artisan make:view pricingnavigation.blade.php.web.php:
Route::view('/pricing', 'pricing')->name('pricing');
php artisan make:controller CheckoutController --invokablepublic 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 */ ]);
}
web.php:
Route::get('/checkout/{plan?}', CheckoutController::class)->name('checkout');
Route::view('/success', 'success')->name('success');
```__
php artisan make:view success@if (auth()->user()->subscribedToPrice('price-id', 'product-id'))
<p>You're subscribed to premium ($4.99/month)</p>
@endif