May 4, 2024
0
Level 2
Laravel Cashier (Stripe) and Filament Error: The payment attempt failed because of an invalid payment method.
I'm using:
"laravel/framework": "^11.0",
"livewire/livewire": "^3.4",
"laravel/cashier": "^15.3",
"filament/filament": "^3.2",
Plan.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'stripe_price',
'name',
'short_description',
'price',
'periodicity',
'features',
'is_featured',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'features' => 'array',
];
}
}
0000_00_00_000000_create_plans_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->string('stripe_price');
$table->string('name');
$table->tinyText('short_description', 73);
$table->decimal('price', 10, 2);
$table->enum('periodicity', ['monthly', 'yearly']);
$table->string('features');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('plans');
}
};
PlanSeeder.php
<?php
namespace Database\Seeders;
use App\Models\Plan;
use Illuminate\Database\Seeder;
class PlanSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$plans = [
[
'stripe_price' => 'price_123xxxxxxxxxxx',
'name' => 'Individual',
'short_description' => 'Lorem Ipsum is simply dummy text.',
'price' => 19.99,
'periodicity' => 'monthly',
'features' => [
'Feature 1',
'Feature 2',
'Feature 3',
'Feature 4',
'Feature 5',
],
],
[
'stripe_price' => 'price_456xxxxxxxxxxxxx',
'name' => 'Individual',
'short_description' => 'Lorem Ipsum is simply dummy text.',
'price' => 129.99,
'periodicity' => 'yearly',
'features' => [
'Feature 1',
'Feature 2',
'Feature 3',
'Feature 4',
'Feature 5',
],
],
];
foreach ($plans as $plan) {
Plan::create($plan);
}
}
}
ListPlans.php
<?php
namespace App\Filament\App\Pages;
use Filament\Pages\Page;
use Filament\Forms\Contracts\HasForms;
use Filament\Tables\Contracts\HasTable;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Actions\Action;
use Filament\Notifications\Notification;
use App\Models\Plan;
class ListPlans extends Page implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;
// ...
public function table(Table $table): Table
{
return $table
->query(Plan::query())
->columns([
TextColumn::make('name'),
TextColumn::make('short_description'),
// ...
])
->actions([
Action::make('subscribeNow')
->label('Subscribe')
->button()
->action(function (SubscriptionPlan $plan) {
// Get the currently logged-in user
$user = auth()->user();
try {
// Create a new subscription using Laravel Cashier
$subscription = $user->newSubscription('default', $plan->stripe_price)
->create();
} catch (Exception $exception) {
// Handle potential errors during subscription creation
Notification::make()
->title($exception->getMessage())
->danger()
->persistent()
->send();
}
}),
]);
}
}
Please or to participate in this conversation.