lasthourcloud/app/Http/Livewire/Subscription/PricingPlans.php

88 lines
3.0 KiB
PHP
Raw Normal View History

2023-08-24 14:14:09 +00:00
<?php
namespace App\Http\Livewire\Subscription;
use Livewire\Component;
use Stripe\Stripe;
use Stripe\Checkout\Session;
class PricingPlans extends Component
{
2023-09-12 15:24:46 +00:00
public bool $isTrial = false;
public function mount() {
$this->isTrial = !data_get(currentTeam(),'subscription.stripe_trial_already_ended');
}
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 'ultimate-monthly':
$priceId = config('subscription.stripe_price_id_ultimate_monthly');
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-yearly':
$priceId = config('subscription.stripe_price_id_ultimate_yearly');
break;
default:
$priceId = config('subscription.stripe_price_id_basic_monthly');
break;
}
if (!$priceId) {
$this->emit('error', 'Price ID not found! Please contact the administrator.');
return;
}
$payload = [
'client_reference_id' => auth()->user()->id . ':' . currentTeam()->id,
'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
];
2023-09-12 09:19:21 +00:00
if (!data_get($team,'subscription.stripe_trial_already_ended')) {
$payload['subscription_data'] = [
2023-09-12 09:23:31 +00:00
'trial_period_days' => config('constants.limits.trial_period'),
2023-09-12 09:19:21 +00:00
'trial_settings' => [
'end_behavior' => [
'missing_payment_method' => 'cancel',
]
],
];
$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'] = [
'name' => 'auto'
];
2023-08-24 14:14:09 +00:00
} else {
$payload['customer_email'] = auth()->user()->email;
}
$session = Session::create($payload);
return redirect($session->url, 303);
}
}