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
|
|
|
|
2024-11-04 13:18:16 +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'),
|
2024-10-31 22:25:16 +00:00
|
|
|
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 = [
|
2024-04-13 12:57:11 +00:00
|
|
|
'allow_promotion_codes' => true,
|
2023-09-14 18:42:12 +00:00
|
|
|
'billing_address_collection' => 'required',
|
2024-11-04 13:18:16 +00:00
|
|
|
'client_reference_id' => Auth::id().':'.currentTeam()->id,
|
2023-08-24 14:14:09 +00:00
|
|
|
'line_items' => [[
|
|
|
|
|
'price' => $priceId,
|
2024-10-31 22:25:16 +00:00
|
|
|
'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,
|
|
|
|
|
],
|
2024-10-31 22:25:16 +00:00
|
|
|
'subscription_data' => [
|
|
|
|
|
'metadata' => [
|
2024-11-04 13:18:16 +00:00
|
|
|
'user_id' => Auth::id(),
|
2024-10-31 22:25:16 +00:00
|
|
|
'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
|
|
|
];
|
2024-02-25 13:00:35 +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 {
|
2024-11-04 13:18:16 +00:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
}
|