lasthourcloud/app/Models/Subscription.php

60 lines
1.8 KiB
PHP
Raw Normal View History

2023-07-13 13:07:42 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2023-08-30 16:23:55 +00:00
use Illuminate\Support\Str;
2023-07-13 13:07:42 +00:00
class Subscription extends Model
{
protected $guarded = [];
2023-07-13 13:07:42 +00:00
public function team()
{
return $this->belongsTo(Team::class);
}
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';
}
2023-08-14 13:22:29 +00:00
}
2023-08-30 16:23:55 +00:00
if (isStripe()) {
if (!$this->stripe_plan_id) {
2023-09-12 09:19:21 +00:00
return 'zero';
2023-08-30 16:23:55 +00:00
}
$subscription = Subscription::where('id', $this->id)->first();
if (!$subscription) {
return null;
}
$subscriptionPlanId = data_get($subscription,'stripe_plan_id');
if (!$subscriptionPlanId) {
return null;
}
$subscriptionConfigs = collect(config('subscription'));
$stripePlanId = null;
$subscriptionConfigs->map(function ($value, $key) use ($subscriptionPlanId, &$stripePlanId) {
if ($value === $subscriptionPlanId){
$stripePlanId = $key;
};
})->first();
if ($stripePlanId) {
return Str::of($stripePlanId)->after('stripe_price_id_')->before('_')->lower();
}
2023-08-14 13:22:29 +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
}