Aug 10, 2024
composer require laravel/cashier
php artisan vendor:publish --tag="cashier-migrations"
php artisan migrate
php 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:webhook
php artisan make:view pricing
navigation.blade.php
.web.php
:
Route::view('/pricing', 'pricing')->name('pricing');
php artisan make:controller CheckoutController --invokable
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 */ ]);
}
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