lasthourcloud/bootstrap/helpers/subscriptions.php
2023-08-30 16:01:38 +02:00

129 lines
3.3 KiB
PHP

<?php
use App\Models\Team;
use Illuminate\Support\Carbon;
use Stripe\Stripe;
function getSubscriptionLink($type)
{
$checkout_id = config("subscription.lemon_squeezy_checkout_id_$type");
if (!$checkout_id) {
return null;
}
$user_id = auth()->user()->id;
$team_id = currentTeam()->id ?? null;
$email = auth()->user()->email ?? null;
$name = auth()->user()->name ?? null;
$url = "https://store.coollabs.io/checkout/buy/$checkout_id?";
if ($user_id) {
$url .= "&checkout[custom][user_id]={$user_id}";
}
if (isset($team_id)) {
$url .= "&checkout[custom][team_id]={$team_id}";
}
if ($email) {
$url .= "&checkout[email]={$email}";
}
if ($name) {
$url .= "&checkout[name]={$name}";
}
return $url;
}
function getPaymentLink()
{
return currentTeam()->subscription->lemon_update_payment_menthod_url;
}
function getRenewDate()
{
return Carbon::parse(currentTeam()->subscription->lemon_renews_at)->format('Y-M-d H:i:s');
}
function getEndDate()
{
return Carbon::parse(currentTeam()->subscription->lemon_renews_at)->format('Y-M-d H:i:s');
}
function isSubscriptionActive()
{
$team = currentTeam();
if (!$team) {
return false;
}
$subscription = $team?->subscription;
if (!$subscription) {
return false;
}
if (config('subscription.provider') === 'lemon') {
return $subscription->lemon_status === 'active';
}
if (config('subscription.provider') === 'stripe') {
return $subscription->stripe_invoice_paid === true && $subscription->stripe_cancel_at_period_end === false;
}
return false;
// if (config('subscription.provider') === 'paddle') {
// return $subscription->paddle_status === 'active';
// }
}
function isSubscriptionOnGracePeriod()
{
$team = currentTeam();
if (!$team) {
return false;
}
$subscription = $team?->subscription;
if (!$subscription) {
return false;
}
if (config('subscription.provider') === 'lemon') {
$is_still_grace_period = $subscription->lemon_ends_at &&
Carbon::parse($subscription->lemon_ends_at) > Carbon::now();
return $is_still_grace_period;
}
if (config('subscription.provider') === 'stripe') {
return $subscription->stripe_cancel_at_period_end;
}
return false;
}
function subscriptionProvider()
{
return config('subscription.provider');
}
function getStripeCustomerPortalSession(Team $team)
{
Stripe::setApiKey(config('subscription.stripe_api_key'));
$return_url = route('team.index');
$stripe_customer_id = $team->subscription->stripe_customer_id;
$session = \Stripe\BillingPortal\Session::create([
'customer' => $stripe_customer_id,
'return_url' => $return_url,
]);
return $session;
}
function allowedPathsForUnsubscribedAccounts()
{
return [
'subscription',
'login',
'register',
'waitlist',
'force-password-reset',
'logout',
'livewire/message/force-password-reset',
'livewire/message/check-license',
'livewire/message/switch-team',
'livewire/message/subscription.pricing-plans'
];
}
function allowedPathsForBoardingAccounts()
{
return [
...allowedPathsForUnsubscribedAccounts(),
'boarding',
'livewire/message/boarding',
];
}