Add tier feature design spec, annual billing, fuel type normalization, and admin subscription management
- Add comprehensive tier feature matrix spec defining Free/Basic/Plus/Pro capabilities across recommendations, predictions, history, logs, tools, and family sharing - Add `stripe_price_id_annual` column to plans table and rename existing column to `stripe_price_id_monthly` - Normalize legacy fuel type aliases (petrol→e10, diesel→b7_standard) in users table - Implement BillingController with checkout, portal, success/cancel routes supporting monthly/annual cadence - Add admin subscription assignment in Filament user edit page with admin-granted subscription support - Add DowngradeUserOnSubscriptionDeleted listener to disable WhatsApp/SMS preferences on subscription cancellation - Add MissedNotificationsOverview widget to Filament user detail page - Add PollFuelPricesTest covering auto-refresh scenarios - Add PriceReliability enum with reliability classification based on price age - Add fuelTypes.js constants file exporting FUEL_TYPES from window global
This commit is contained in:
45
app/Enums/PriceReliability.php
Normal file
45
app/Enums/PriceReliability.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
enum PriceReliability: string
|
||||
{
|
||||
case Reliable = 'reliable';
|
||||
case Stale = 'stale';
|
||||
case Outdated = 'outdated';
|
||||
|
||||
public static function fromUpdatedAt(?Carbon $updatedAt): self
|
||||
{
|
||||
if ($updatedAt === null) {
|
||||
return self::Outdated;
|
||||
}
|
||||
|
||||
$hours = $updatedAt->diffInHours(now());
|
||||
|
||||
return match (true) {
|
||||
$hours <= 72 => self::Reliable,
|
||||
$hours <= 168 => self::Stale,
|
||||
default => self::Outdated,
|
||||
};
|
||||
}
|
||||
|
||||
public function weight(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::Reliable => 0,
|
||||
self::Stale => 1,
|
||||
self::Outdated => 2,
|
||||
};
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::Reliable => 'Reliable',
|
||||
self::Stale => 'Older price — verify before driving',
|
||||
self::Outdated => 'Outdated — may be inaccurate',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\UserResource\Widgets;
|
||||
|
||||
use App\Models\NotificationLog;
|
||||
use App\Models\User;
|
||||
use Filament\Widgets\StatsOverviewWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
|
||||
class MissedNotificationsOverview extends StatsOverviewWidget
|
||||
{
|
||||
public ?User $record = null;
|
||||
|
||||
protected function getStats(): array
|
||||
{
|
||||
if ($this->record === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$userId = $this->record->id;
|
||||
|
||||
$missedTodayByChannel = fn (string $channel): int => NotificationLog::where('user_id', $userId)
|
||||
->where('channel', $channel)
|
||||
->where('sent', false)
|
||||
->whereDate('created_at', today())
|
||||
->count();
|
||||
|
||||
$missedThisMonth = NotificationLog::where('user_id', $userId)
|
||||
->where('sent', false)
|
||||
->whereMonth('created_at', now()->month)
|
||||
->whereYear('created_at', now()->year)
|
||||
->count();
|
||||
|
||||
return [
|
||||
Stat::make('SMS missed today', $missedTodayByChannel('sms'))
|
||||
->color($missedTodayByChannel('sms') > 0 ? 'warning' : 'gray'),
|
||||
Stat::make('WhatsApp missed today', $missedTodayByChannel('whatsapp'))
|
||||
->color($missedTodayByChannel('whatsapp') > 0 ? 'warning' : 'gray'),
|
||||
Stat::make('Total missed this month', $missedThisMonth)
|
||||
->color($missedThisMonth > 0 ? 'warning' : 'gray'),
|
||||
];
|
||||
}
|
||||
}
|
||||
48
app/Http/Controllers/BillingController.php
Normal file
48
app/Http/Controllers/BillingController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\PlanTier;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class BillingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Redirect the user to a Stripe Checkout session for the requested plan + cadence.
|
||||
*/
|
||||
public function checkout(Request $request, string $tier, string $cadence): Response|RedirectResponse
|
||||
{
|
||||
abort_unless(in_array($tier, [PlanTier::Basic->value, PlanTier::Plus->value, PlanTier::Pro->value], true), 404);
|
||||
abort_unless(in_array($cadence, ['monthly', 'annual'], true), 404);
|
||||
|
||||
$priceId = config("services.stripe.prices.{$tier}.{$cadence}");
|
||||
|
||||
abort_if(empty($priceId), 404, "No Stripe price configured for {$tier} {$cadence}");
|
||||
|
||||
return $request->user()
|
||||
->newSubscription('default', $priceId)
|
||||
->allowPromotionCodes()
|
||||
->checkout([
|
||||
'success_url' => route('billing.success').'?session_id={CHECKOUT_SESSION_ID}',
|
||||
'cancel_url' => route('billing.cancel'),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Redirect the user to the Stripe-hosted Customer Billing Portal. */
|
||||
public function portal(Request $request): Response|RedirectResponse
|
||||
{
|
||||
return $request->user()->redirectToBillingPortal(route('dashboard'));
|
||||
}
|
||||
|
||||
public function success(): RedirectResponse
|
||||
{
|
||||
return redirect()->route('dashboard')->with('status', 'subscription_started');
|
||||
}
|
||||
|
||||
public function cancel(): RedirectResponse
|
||||
{
|
||||
return redirect()->route('dashboard')->with('status', 'subscription_cancelled');
|
||||
}
|
||||
}
|
||||
37
app/Listeners/DowngradeUserOnSubscriptionDeleted.php
Normal file
37
app/Listeners/DowngradeUserOnSubscriptionDeleted.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Listeners;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\UserNotificationPreference;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Laravel\Cashier\Events\WebhookReceived;
|
||||
|
||||
class DowngradeUserOnSubscriptionDeleted
|
||||
{
|
||||
public function handle(WebhookReceived $event): void
|
||||
{
|
||||
if (($event->payload['type'] ?? null) !== 'customer.subscription.deleted') {
|
||||
return;
|
||||
}
|
||||
|
||||
$stripeCustomerId = $event->payload['data']['object']['customer'] ?? null;
|
||||
|
||||
if (! $stripeCustomerId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = User::where('stripe_id', $stripeCustomerId)->first();
|
||||
|
||||
if (! $user) {
|
||||
return;
|
||||
}
|
||||
|
||||
UserNotificationPreference::query()
|
||||
->where('user_id', $user->id)
|
||||
->whereIn('channel', ['whatsapp', 'sms'])
|
||||
->update(['enabled' => false]);
|
||||
|
||||
Cache::tags(['plans'])->forget("plan_for_user_{$user->id}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user