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-08-08 09:51:36 +00:00
|
|
|
|
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-10-09 12:20:55 +00:00
|
|
|
return 'zero';
|
2023-08-30 16:23:55 +00:00
|
|
|
}
|
|
|
|
$subscription = Subscription::where('id', $this->id)->first();
|
|
|
|
if (!$subscription) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-27 08:17:13 +00:00
|
|
|
$subscriptionPlanId = data_get($subscription, 'stripe_plan_id');
|
2023-08-30 16:23:55 +00:00
|
|
|
if (!$subscriptionPlanId) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-27 08:17:13 +00:00
|
|
|
$subscriptionInvoicePaid = data_get($subscription, 'stripe_invoice_paid');
|
|
|
|
if (!$subscriptionInvoicePaid) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
})->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
|
|
|
}
|