2023-07-13 20:03:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-08-24 14:14:09 +00:00
|
|
|
use App\Models\Team;
|
|
|
|
|
use Stripe\Stripe;
|
2023-07-13 20:03:27 +00:00
|
|
|
|
2023-08-24 14:14:09 +00:00
|
|
|
function isSubscriptionActive()
|
2023-07-14 09:27:08 +00:00
|
|
|
{
|
2025-12-08 12:39:33 +00:00
|
|
|
return once(function () {
|
|
|
|
|
if (! isCloud()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$team = currentTeam();
|
|
|
|
|
if (! $team) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-12-27 15:17:56 +00:00
|
|
|
// Root team (id=0) doesn't require subscription
|
|
|
|
|
if ($team->id === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-12-08 12:39:33 +00:00
|
|
|
$subscription = $team?->subscription;
|
2023-08-14 14:56:13 +00:00
|
|
|
|
2025-12-08 12:39:33 +00:00
|
|
|
if (is_null($subscription)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (isStripe()) {
|
|
|
|
|
return $subscription->stripe_invoice_paid === true;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-12-08 12:39:33 +00:00
|
|
|
return false;
|
|
|
|
|
});
|
2023-08-14 13:22:29 +00:00
|
|
|
}
|
2025-12-08 12:39:33 +00:00
|
|
|
|
2023-08-24 14:14:09 +00:00
|
|
|
function isSubscriptionOnGracePeriod()
|
2023-08-14 13:22:29 +00:00
|
|
|
{
|
2025-12-08 12:39:33 +00:00
|
|
|
return once(function () {
|
|
|
|
|
$team = currentTeam();
|
|
|
|
|
if (! $team) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$subscription = $team?->subscription;
|
|
|
|
|
if (! $subscription) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (isStripe()) {
|
|
|
|
|
return $subscription->stripe_cancel_at_period_end;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-12-08 12:39:33 +00:00
|
|
|
return false;
|
|
|
|
|
});
|
2023-08-24 14:14:09 +00:00
|
|
|
}
|
|
|
|
|
function subscriptionProvider()
|
|
|
|
|
{
|
|
|
|
|
return config('subscription.provider');
|
|
|
|
|
}
|
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-24 14:14:09 +00:00
|
|
|
function getStripeCustomerPortalSession(Team $team)
|
|
|
|
|
{
|
|
|
|
|
Stripe::setApiKey(config('subscription.stripe_api_key'));
|
2024-02-23 10:21:14 +00:00
|
|
|
$return_url = route('subscription.show');
|
2024-06-10 20:43:34 +00:00
|
|
|
$stripe_customer_id = data_get($team, 'subscription.stripe_customer_id');
|
|
|
|
|
if (! $stripe_customer_id) {
|
2023-10-11 07:55:05 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-10-31 17:20:11 +00:00
|
|
|
|
|
|
|
|
return \Stripe\BillingPortal\Session::create([
|
2023-08-24 14:14:09 +00:00
|
|
|
'customer' => $stripe_customer_id,
|
|
|
|
|
'return_url' => $return_url,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2023-08-24 15:41:11 +00:00
|
|
|
function allowedPathsForUnsubscribedAccounts()
|
2023-08-24 14:14:09 +00:00
|
|
|
{
|
|
|
|
|
return [
|
2024-02-23 10:21:14 +00:00
|
|
|
'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
|
|
|
'force-password-reset',
|
2026-02-24 09:17:16 +00:00
|
|
|
'two-factor-challenge',
|
2024-06-10 20:43:34 +00:00
|
|
|
'livewire/update',
|
2024-12-16 12:00:29 +00:00
|
|
|
'admin',
|
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(),
|
2024-03-13 11:11:37 +00:00
|
|
|
'onboarding',
|
2024-06-10 20:43:34 +00:00
|
|
|
'livewire/update',
|
2023-08-24 15:41:11 +00:00
|
|
|
];
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
function allowedPathsForInvalidAccounts()
|
|
|
|
|
{
|
2023-10-09 12:20:55 +00:00
|
|
|
return [
|
|
|
|
|
'logout',
|
|
|
|
|
'verify',
|
2023-10-26 18:45:38 +00:00
|
|
|
'force-password-reset',
|
2026-02-24 09:17:16 +00:00
|
|
|
'two-factor-challenge',
|
2024-06-10 20:43:34 +00:00
|
|
|
'livewire/update',
|
2023-10-09 12:20:55 +00:00
|
|
|
];
|
|
|
|
|
}
|
2025-08-18 12:54:08 +00:00
|
|
|
|
|
|
|
|
function updateStripeCustomerEmail(Team $team, string $newEmail): void
|
|
|
|
|
{
|
|
|
|
|
if (! isStripe()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$stripe_customer_id = data_get($team, 'subscription.stripe_customer_id');
|
|
|
|
|
if (! $stripe_customer_id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Stripe::setApiKey(config('subscription.stripe_api_key'));
|
|
|
|
|
|
|
|
|
|
\Stripe\Customer::update(
|
|
|
|
|
$stripe_customer_id,
|
|
|
|
|
['email' => $newEmail]
|
|
|
|
|
);
|
|
|
|
|
}
|