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