trypost/app/Models/Account.php
Paulo Castellano 3e642ef520 refactor: redesign billing settings + upgrade dialog with indies aesthetic
- Billing settings page restructured around the design system: dropped
  the 280px-label / 1fr-content split for stacked HeadingSmall sections;
  current plan rendered as a hero card with display-font name, price,
  amber sticker tile, and an inline primary "Change plan" CTA; payment
  method consolidated into a single sticker row with a violet card-icon
  tile and the manage CTA on the same line; empty payment-method state
  handled. Invoices keep the sticker-row treatment.
- Upgrade dialog mirrors Subscribe.vue end-to-end: planTones backgrounds
  per slug,  POPULAR / CURRENT badges absolute-positioned, ink-bordered
  rounded-full CTA pill, sticker check icons, "Everything in {plan}"
  copy, IconInfoCircle tooltip on credits, yearly/monthly toggle pill
  with rotating amber save-months badge. While a request is in flight
  every plan button is disabled and the active one shows IconLoader2
  spinner.
- Account model gains `displayablePaymentMethod()` returning the
  card array used by the UI. Resolves the customer-level default first,
  falls back to the first attached payment method (Stripe Checkout trials
  anchor the card to the subscription rather than the customer, so the
  customer-level lookup returns null even when a card exists).
- i18n: added `billing.subscription.expires_on` and `no_payment_method`
  in en/pt-BR/es.
2026-05-06 17:00:25 -03:00

121 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use App\Features\MemberLimit;
use App\Features\MonthlyCreditsLimit;
use App\Features\SocialAccountLimit;
use App\Features\WorkspaceLimit;
use App\Models\Traits\HasUsage;
use Database\Factories\AccountFactory;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Laravel\Cashier\Billable;
use Laravel\Pennant\Feature;
class Account extends Model
{
/** @use HasFactory<AccountFactory> */
use Billable, HasFactory, HasUsage, HasUuids;
public const SUBSCRIPTION_NAME = 'default';
protected $fillable = [
'owner_id',
'name',
'billing_email',
'plan_id',
];
protected static function booted(): void
{
static::updated(function (Account $account): void {
if ($account->wasChanged('plan_id')) {
Feature::for($account)->forget([
WorkspaceLimit::class,
SocialAccountLimit::class,
MemberLimit::class,
MonthlyCreditsLimit::class,
]);
}
});
}
public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id');
}
public function plan(): BelongsTo
{
return $this->belongsTo(Plan::class);
}
public function users(): HasMany
{
return $this->hasMany(User::class);
}
public function workspaces(): HasMany
{
return $this->hasMany(Workspace::class);
}
public function invites(): HasMany
{
return $this->hasMany(Invite::class);
}
public function hasActiveSubscription(): bool
{
if (config('trypost.self_hosted')) {
return true;
}
return $this->subscribed(self::SUBSCRIPTION_NAME);
}
public function isOnTrial(): bool
{
return $this->subscription(self::SUBSCRIPTION_NAME)?->onTrial() ?? false;
}
/**
* Returns the displayable card for the billing UI. Falls back to the first
* attached payment method when the customer has no `invoice_settings.default_payment_method`
* (Stripe Checkout trials anchor the card to the subscription, not the customer).
*
* @return array{brand: string, last4: string, exp_month: int, exp_year: int}|null
*/
public function displayablePaymentMethod(): ?array
{
$paymentMethod = $this->defaultPaymentMethod() ?? $this->paymentMethods()->first();
$card = $paymentMethod?->card;
if (! $card) {
return null;
}
return [
'brand' => $card->brand,
'last4' => $card->last4,
'exp_month' => $card->exp_month,
'exp_year' => $card->exp_year,
];
}
public function stripeEmail(): string
{
return $this->billing_email ?? $this->owner?->email ?? '';
}
public function stripeName(): string
{
return $this->name;
}
}