coolify/app/Livewire/Subscription/PricingPlans.php

71 lines
2.2 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 Illuminate\Support\Facades\Auth;
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
{
public function subscribeStripe($type)
{
Stripe::setApiKey(config('subscription.stripe_api_key'));
2024-10-31 14:14:30 +00:00
$priceId = match ($type) {
'dynamic-monthly' => config('subscription.stripe_price_id_dynamic_monthly'),
'dynamic-yearly' => config('subscription.stripe_price_id_dynamic_yearly'),
default => config('subscription.stripe_price_id_dynamic_monthly'),
2024-10-31 14:14:30 +00:00
};
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',
'client_reference_id' => Auth::id().':'.currentTeam()->id,
2023-08-24 14:14:09 +00:00
'line_items' => [[
'price' => $priceId,
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 2,
],
'quantity' => 2,
2023-08-24 14:14:09 +00:00
]],
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,
],
'subscription_data' => [
'metadata' => [
'user_id' => Auth::id(),
'team_id' => currentTeam()->id,
],
],
'payment_method_collection' => 'if_required',
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-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;
2023-08-24 14:14:09 +00:00
}
$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);
}
}