coolify/app/Models/Subscription.php

63 lines
2.0 KiB
PHP
Raw Normal View History

2023-07-13 15:07:42 +02:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
2023-08-30 18:23:55 +02:00
use Illuminate\Support\Str;
2023-07-13 15:07:42 +02:00
class Subscription extends Model
{
protected $guarded = [];
2023-07-13 15:07:42 +02:00
public function team()
{
return $this->belongsTo(Team::class);
}
2023-08-14 15:22:29 +02:00
public function type()
{
2023-08-30 18:23:55 +02: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 15:22:29 +02:00
2023-08-30 18:23:55 +02: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-02-23 12:59:14 +01:00
} else if (isStripe()) {
2023-08-30 18:23:55 +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();
if (!$subscription) {
return null;
}
2023-10-27 10:17:13 +02:00
$subscriptionPlanId = data_get($subscription, 'stripe_plan_id');
2023-08-30 18:23:55 +02:00
if (!$subscriptionPlanId) {
return null;
}
2023-10-27 10:17:13 +02:00
$subscriptionInvoicePaid = data_get($subscription, 'stripe_invoice_paid');
if (!$subscriptionInvoicePaid) {
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;
};
})->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
}
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
}