| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
-
- namespace App\Models;
-
- use Carbon\CarbonInterface;
- use Database\Factories\SubscriptionFactory;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
-
- /**
- * A merchant's paid plan, kept in step with Stripe.
- *
- * @property int $id
- * @property int $user_id
- * @property int|null $plan_id
- * @property string|null $stripe_customer_id
- * @property string|null $stripe_subscription_id
- * @property string|null $status
- * @property CarbonInterface|null $current_period_end
- * @property bool $cancel_at_period_end
- * @property-read Plan|null $plan
- * @property-read User $user
- */
- class Subscription extends Model
- {
- /** @use HasFactory<SubscriptionFactory> */
- use HasFactory;
-
- /**
- * Stripe states in which the merchant keeps their plan.
- * "past_due" is included: a card failed, and Stripe is still retrying it.
- *
- * @var list<string>
- */
- public const array ACTIVE_STATUSES = ['active', 'trialing', 'past_due'];
-
- /**
- * @return BelongsTo<User, $this>
- */
- public function user(): BelongsTo
- {
- return $this->belongsTo(User::class);
- }
-
- /**
- * @return BelongsTo<Plan, $this>
- */
- public function plan(): BelongsTo
- {
- return $this->belongsTo(Plan::class);
- }
-
- public function isActive(): bool
- {
- return $this->plan_id !== null && in_array($this->status, self::ACTIVE_STATUSES, true);
- }
-
- /**
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'current_period_end' => 'datetime',
- 'cancel_at_period_end' => 'boolean',
- ];
- }
- }
|