coolify/app/Models/Subscription.php

49 lines
1.4 KiB
PHP
Raw Normal View History

2023-07-13 15:07:42 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Subscription extends Model
{
protected $guarded = [];
2023-07-13 15:07:42 +02:00
public function team()
{
return $this->belongsTo(Team::class);
}
2024-06-10 22:43:34 +02:00
2023-08-14 15:22:29 +02:00
public function type()
{
2024-06-25 13:54:58 +02:00
if (isStripe()) {
2024-06-10 22:43:34 +02:00
if (! $this->stripe_plan_id) {
2023-10-09 14:20:55 +02:00
return 'zero';
2023-08-30 18:23:55 +02:00
}
$subscription = Subscription::where('id', $this->id)->first();
2024-06-10 22:43:34 +02:00
if (! $subscription) {
2023-08-30 18:23:55 +02:00
return null;
}
2023-10-27 10:17:13 +02:00
$subscriptionPlanId = data_get($subscription, 'stripe_plan_id');
2024-06-10 22:43:34 +02:00
if (! $subscriptionPlanId) {
2023-08-30 18:23:55 +02:00
return null;
}
2023-10-27 10:17:13 +02:00
$subscriptionInvoicePaid = data_get($subscription, 'stripe_invoice_paid');
2024-06-10 22:43:34 +02:00
if (! $subscriptionInvoicePaid) {
2023-10-27 10:17:13 +02:00
return null;
}
2023-08-30 18:23:55 +02:00
$subscriptionConfigs = collect(config('subscription'));
$stripePlanId = null;
$subscriptionConfigs->map(function ($value, $key) use ($subscriptionPlanId, &$stripePlanId) {
2023-10-27 10:17:13 +02:00
if ($value === $subscriptionPlanId) {
2023-08-30 18:23:55 +02:00
$stripePlanId = $key;
2024-06-10 22:43:34 +02:00
}
2023-08-30 18:23:55 +02:00
})->first();
if ($stripePlanId) {
2024-02-23 12:59:14 +01:00
return str($stripePlanId)->after('stripe_price_id_')->before('_')->lower();
2023-08-30 18:23:55 +02:00
}
2023-08-14 15:22:29 +02:00
}
2024-06-10 22:43:34 +02:00
2023-09-12 11:19:21 +02:00
return 'zero';
2023-08-14 15:22:29 +02:00
}
2023-07-13 15:07:42 +02:00
}