lasthourcloud/bootstrap/helpers/subscriptions.php

150 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2023-07-13 20:03:27 +00:00
<?php
2023-08-24 14:14:09 +00:00
use App\Models\Team;
2023-07-13 20:03:27 +00:00
use Illuminate\Support\Carbon;
2023-08-24 14:14:09 +00:00
use Stripe\Stripe;
2023-07-13 20:03:27 +00:00
2023-08-14 12:00:10 +00:00
function getSubscriptionLink($type)
2023-07-13 20:03:27 +00:00
{
2023-08-23 14:40:59 +00:00
$checkout_id = config("subscription.lemon_squeezy_checkout_id_$type");
2023-07-27 14:22:13 +00:00
if (!$checkout_id) {
return null;
}
2023-07-13 20:03:27 +00:00
$user_id = auth()->user()->id;
2023-08-22 15:44:49 +00:00
$team_id = currentTeam()->id ?? null;
2023-07-13 20:03:27 +00:00
$email = auth()->user()->email ?? null;
$name = auth()->user()->name ?? null;
2023-07-14 11:58:05 +00:00
$url = "https://store.coollabs.io/checkout/buy/$checkout_id?";
2023-07-13 20:03:27 +00:00
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;
}
2023-07-14 09:27:08 +00:00
function getPaymentLink()
{
2023-08-22 15:44:49 +00:00
return currentTeam()->subscription->lemon_update_payment_menthod_url;
2023-07-13 20:03:27 +00:00
}
2023-07-14 09:27:08 +00:00
function getRenewDate()
{
2023-08-22 15:44:49 +00:00
return Carbon::parse(currentTeam()->subscription->lemon_renews_at)->format('Y-M-d H:i:s');
2023-07-13 20:03:27 +00:00
}
2023-07-14 09:27:08 +00:00
function getEndDate()
{
2023-08-22 15:44:49 +00:00
return Carbon::parse(currentTeam()->subscription->lemon_renews_at)->format('Y-M-d H:i:s');
2023-07-14 09:27:08 +00:00
}
2023-08-24 14:14:09 +00:00
function isSubscriptionActive()
2023-07-14 09:27:08 +00:00
{
2023-09-02 13:37:25 +00:00
if (!isCloud()) {
return false;
}
2023-08-22 15:44:49 +00:00
$team = currentTeam();
2023-08-14 13:22:29 +00:00
if (!$team) {
return false;
}
$subscription = $team?->subscription;
2023-08-14 14:56:13 +00:00
2023-09-15 09:19:36 +00:00
if (is_null($subscription)) {
2023-08-14 13:22:29 +00:00
return false;
}
2023-08-30 16:23:55 +00:00
if (isLemon()) {
2023-08-24 14:14:09 +00:00
return $subscription->lemon_status === 'active';
}
2023-08-30 16:23:55 +00:00
// if (isPaddle()) {
// return $subscription->paddle_status === 'active';
// }
if (isStripe()) {
2023-08-24 14:14:09 +00:00
return $subscription->stripe_invoice_paid === true && $subscription->stripe_cancel_at_period_end === false;
}
return false;
2023-08-14 13:22:29 +00:00
}
2023-08-24 14:14:09 +00:00
function isSubscriptionOnGracePeriod()
2023-08-14 13:22:29 +00:00
{
2023-08-22 15:44:49 +00:00
$team = currentTeam();
2023-08-14 13:22:29 +00:00
if (!$team) {
return false;
}
$subscription = $team?->subscription;
if (!$subscription) {
return false;
}
2023-08-30 16:23:55 +00:00
if (isLemon()) {
2023-08-24 14:14:09 +00:00
$is_still_grace_period = $subscription->lemon_ends_at &&
Carbon::parse($subscription->lemon_ends_at) > Carbon::now();
return $is_still_grace_period;
}
2023-08-30 16:23:55 +00:00
if (isStripe()) {
2023-08-24 14:14:09 +00:00
return $subscription->stripe_cancel_at_period_end;
}
return false;
}
function subscriptionProvider()
{
return config('subscription.provider');
}
2023-08-31 13:00:59 +00:00
function isLemon()
{
2023-08-30 16:23:55 +00:00
return config('subscription.provider') === 'lemon';
}
2023-08-31 13:00:59 +00:00
function isStripe()
{
2023-08-30 16:23:55 +00:00
return config('subscription.provider') === 'stripe';
}
2023-08-31 13:00:59 +00:00
function isPaddle()
{
2023-08-30 16:23:55 +00:00
return config('subscription.provider') === 'paddle';
}
2023-08-24 14:14:09 +00:00
function getStripeCustomerPortalSession(Team $team)
{
Stripe::setApiKey(config('subscription.stripe_api_key'));
$return_url = route('subscription.show');
$stripe_customer_id = data_get($team,'subscription.stripe_customer_id');
if (!$stripe_customer_id) {
return null;
}
2023-08-24 14:14:09 +00:00
$session = \Stripe\BillingPortal\Session::create([
'customer' => $stripe_customer_id,
'return_url' => $return_url,
]);
return $session;
}
2023-08-24 15:41:11 +00:00
function allowedPathsForUnsubscribedAccounts()
2023-08-24 14:14:09 +00:00
{
return [
'subscription/new',
2023-08-24 14:14:09 +00:00
'login',
2023-10-09 12:20:55 +00:00
'logout',
2023-08-24 14:14:09 +00:00
'waitlist',
'force-password-reset',
2023-12-07 21:56:55 +00:00
'livewire/update'
2023-08-24 14:14:09 +00:00
];
2023-07-14 09:27:08 +00:00
}
2023-08-24 15:41:11 +00:00
function allowedPathsForBoardingAccounts()
{
return [
...allowedPathsForUnsubscribedAccounts(),
'boarding',
2023-12-07 21:56:55 +00:00
'livewire/update'
2023-08-24 15:41:11 +00:00
];
}
2023-10-09 12:20:55 +00:00
function allowedPathsForInvalidAccounts() {
return [
'logout',
'verify',
'force-password-reset',
2023-12-07 21:56:55 +00:00
'livewire/update'
2023-10-09 12:20:55 +00:00
];
}