lasthourcloud/app/Livewire/Subscription/PricingPlans.php

120 lines
4.1 KiB
PHP
Raw Normal View History

2023-08-24 14:14:09 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Subscription;
2023-08-24 14:14:09 +00:00
use Livewire\Component;
use Stripe\Checkout\Session;
2024-06-10 20:43:34 +00:00
use Stripe\Stripe;
2023-08-24 14:14:09 +00:00
class PricingPlans extends Component
{
2023-09-12 15:24:46 +00:00
public bool $isTrial = false;
2024-06-10 20:43:34 +00:00
public function mount()
{
2024-06-10 20:43:34 +00:00
$this->isTrial = ! data_get(currentTeam(), 'subscription.stripe_trial_already_ended');
2023-11-29 15:34:31 +00:00
if (config('constants.limits.trial_period') == 0) {
$this->isTrial = false;
}
2023-09-12 15:24:46 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-24 14:14:09 +00:00
public function subscribeStripe($type)
{
2023-09-12 09:19:21 +00:00
$team = currentTeam();
2023-08-24 14:14:09 +00:00
Stripe::setApiKey(config('subscription.stripe_api_key'));
switch ($type) {
case 'basic-monthly':
$priceId = config('subscription.stripe_price_id_basic_monthly');
break;
case 'basic-yearly':
$priceId = config('subscription.stripe_price_id_basic_yearly');
break;
case 'pro-monthly':
$priceId = config('subscription.stripe_price_id_pro_monthly');
break;
case 'pro-yearly':
$priceId = config('subscription.stripe_price_id_pro_yearly');
break;
case 'ultimate-monthly':
$priceId = config('subscription.stripe_price_id_ultimate_monthly');
break;
2023-08-24 14:14:09 +00:00
case 'ultimate-yearly':
$priceId = config('subscription.stripe_price_id_ultimate_yearly');
break;
2024-04-12 13:48:56 +00:00
case 'dynamic-monthly':
$priceId = config('subscription.stripe_price_id_dynamic_monthly');
break;
case 'dynamic-yearly':
$priceId = config('subscription.stripe_price_id_dynamic_yearly');
break;
2023-08-24 14:14:09 +00:00
default:
$priceId = config('subscription.stripe_price_id_basic_monthly');
break;
}
2024-06-10 20:43:34 +00:00
if (! $priceId) {
2023-12-07 18:06:32 +00:00
$this->dispatch('error', 'Price ID not found! Please contact the administrator.');
2024-06-10 20:43:34 +00:00
2023-08-24 14:14:09 +00:00
return;
}
$payload = [
'allow_promotion_codes' => true,
2023-09-14 18:42:12 +00:00
'billing_address_collection' => 'required',
2024-06-10 20:43:34 +00:00
'client_reference_id' => auth()->user()->id.':'.currentTeam()->id,
2023-08-24 14:14:09 +00:00
'line_items' => [[
'price' => $priceId,
'quantity' => 1,
]],
2023-09-05 08:57:49 +00:00
'tax_id_collection' => [
'enabled' => true,
],
2023-09-05 08:49:17 +00:00
'automatic_tax' => [
2023-08-24 14:14:09 +00:00
'enabled' => true,
],
2023-09-12 09:19:21 +00:00
2023-08-24 14:14:09 +00:00
'mode' => 'subscription',
2023-08-30 16:23:55 +00:00
'success_url' => route('dashboard', ['success' => true]),
'cancel_url' => route('subscription.index', ['cancelled' => true]),
2023-08-24 14:14:09 +00:00
];
if (str($type)->contains('ultimate')) {
$payload['line_items'][0]['adjustable_quantity'] = [
'enabled' => true,
'minimum' => 10,
2023-09-12 09:19:21 +00:00
];
$payload['line_items'][0]['quantity'] = 10;
2023-11-29 15:34:31 +00:00
}
2024-04-12 13:48:56 +00:00
if (str($type)->contains('dynamic')) {
$payload['line_items'][0]['adjustable_quantity'] = [
'enabled' => true,
'minimum' => 2,
];
$payload['line_items'][0]['quantity'] = 2;
}
2024-06-10 20:43:34 +00:00
if (! data_get($team, 'subscription.stripe_trial_already_ended')) {
if (config('constants.limits.trial_period') > 0) {
$payload['subscription_data'] = [
'trial_period_days' => config('constants.limits.trial_period'),
'trial_settings' => [
'end_behavior' => [
'missing_payment_method' => 'cancel',
2024-06-10 20:43:34 +00:00
],
],
];
}
2023-09-12 09:19:21 +00:00
$payload['payment_method_collection'] = 'if_required';
}
2023-08-24 14:14:09 +00:00
$customer = currentTeam()->subscription?->stripe_customer_id ?? null;
if ($customer) {
$payload['customer'] = $customer;
2023-08-24 15:41:11 +00:00
$payload['customer_update'] = [
2024-06-10 20:43:34 +00:00
'name' => 'auto',
2023-08-24 15:41:11 +00:00
];
2023-08-24 14:14:09 +00:00
} else {
$payload['customer_email'] = auth()->user()->email;
}
$session = Session::create($payload);
2024-06-10 20:43:34 +00:00
2023-08-24 14:14:09 +00:00
return redirect($session->url, 303);
}
}