2021-03-11 14:05:23 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-03-11 14:05:23 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-03-11 14:05:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\ClientPortal;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-03-25 11:55:59 +01:00
|
|
|
use App\Models\Subscription;
|
2021-03-11 14:05:23 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-03-18 15:41:26 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2021-03-22 14:09:29 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-03-15 17:33:27 +01:00
|
|
|
use Illuminate\Support\Str;
|
2021-03-11 14:05:23 +01:00
|
|
|
|
2021-03-25 11:55:59 +01:00
|
|
|
class SubscriptionPurchaseController extends Controller
|
2021-03-11 14:05:23 +01:00
|
|
|
{
|
2021-03-25 11:55:59 +01:00
|
|
|
public function index(Subscription $subscription, Request $request)
|
2021-03-11 14:05:23 +01:00
|
|
|
{
|
2023-03-01 03:42:31 +01:00
|
|
|
App::setLocale($subscription->company->locale());
|
|
|
|
|
2024-03-09 06:35:00 +01:00
|
|
|
if ($subscription->trashed()) {
|
2024-03-27 10:08:55 +01:00
|
|
|
return $this->render('generic.not_available', ['passed_account' => $subscription->company->account, 'passed_company' => $subscription->company]);
|
2024-03-09 06:35:00 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 22:42:48 +01:00
|
|
|
/* Make sure the contact is logged into the correct company for this subscription */
|
2022-06-21 11:57:17 +02:00
|
|
|
if (auth()->guard('contact')->user() && auth()->guard('contact')->user()->company_id != $subscription->company_id) {
|
2022-02-01 22:42:48 +01:00
|
|
|
auth()->guard('contact')->logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:41:26 +01:00
|
|
|
if ($request->has('locale')) {
|
|
|
|
$this->setLocale($request->query('locale'));
|
|
|
|
}
|
|
|
|
|
2021-03-15 17:33:27 +01:00
|
|
|
return view('billing-portal.purchase', [
|
2021-03-25 11:55:59 +01:00
|
|
|
'subscription' => $subscription,
|
2021-03-15 17:33:27 +01:00
|
|
|
'hash' => Str::uuid()->toString(),
|
2021-03-18 15:41:26 +01:00
|
|
|
'request_data' => $request->all(),
|
2021-03-15 17:33:27 +01:00
|
|
|
]);
|
2021-03-11 14:05:23 +01:00
|
|
|
}
|
2021-03-18 15:41:26 +01:00
|
|
|
|
2022-11-23 07:27:43 +01:00
|
|
|
public function upgrade(Subscription $subscription, Request $request)
|
|
|
|
{
|
2023-03-01 03:42:31 +01:00
|
|
|
App::setLocale($subscription->company->locale());
|
|
|
|
|
2022-11-23 07:27:43 +01:00
|
|
|
/* Make sure the contact is logged into the correct company for this subscription */
|
|
|
|
if (auth()->guard('contact')->user() && auth()->guard('contact')->user()->company_id != $subscription->company_id) {
|
|
|
|
auth()->guard('contact')->logout();
|
|
|
|
$request->session()->invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('locale')) {
|
|
|
|
$this->setLocale($request->query('locale'));
|
|
|
|
}
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!auth()->guard('contact')->check() && $subscription->registration_required && $subscription->company->client_can_register) {
|
|
|
|
session()->put('url.intended', route('client.subscription.upgrade', ['subscription' => $subscription->hashed_id]));
|
2022-12-20 05:50:02 +01:00
|
|
|
|
|
|
|
return redirect()->route('client.register', ['company_key' => $subscription->company->company_key]);
|
2023-02-16 02:36:09 +01:00
|
|
|
} elseif (!auth()->guard('contact')->check() && $subscription->registration_required && ! $subscription->company->client_can_register) {
|
2022-12-20 05:50:02 +01:00
|
|
|
return render('generic.subscription_blocked', ['account' => $subscription->company->account, 'company' => $subscription->company]);
|
|
|
|
}
|
|
|
|
|
2022-11-23 07:27:43 +01:00
|
|
|
return view('billing-portal.purchasev2', [
|
|
|
|
'subscription' => $subscription,
|
|
|
|
'hash' => Str::uuid()->toString(),
|
|
|
|
'request_data' => $request->all(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-02-08 19:55:29 +01:00
|
|
|
public function v3(Subscription $subscription, Request $request)
|
|
|
|
{
|
|
|
|
// Todo: Prerequirement checks for subscription.
|
|
|
|
|
|
|
|
return view('billing-portal.v3.index', [
|
|
|
|
'subscription' => $subscription,
|
|
|
|
'hash' => Str::uuid()->toString(),
|
|
|
|
'request_data' => $request->all(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-03-18 15:41:26 +01:00
|
|
|
/**
|
|
|
|
* Set locale for incoming request.
|
|
|
|
*
|
|
|
|
* @param string $locale
|
2024-06-18 02:24:03 +02:00
|
|
|
* @return string
|
2021-03-18 15:41:26 +01:00
|
|
|
*/
|
2024-06-18 02:24:03 +02:00
|
|
|
private function setLocale(string $locale): string
|
2021-03-18 15:41:26 +01:00
|
|
|
{
|
2024-06-18 02:24:03 +02:00
|
|
|
|
|
|
|
/** @var \Illuminate\Support\Collection<\App\Models\Language> */
|
|
|
|
$languages = app('languages');
|
|
|
|
|
|
|
|
$record = $languages->first(function ($item) use ($locale) {
|
|
|
|
/** @var \App\Models\Language $item */
|
2021-03-22 14:09:29 +01:00
|
|
|
return $item->locale == $locale;
|
2024-06-18 02:24:03 +02:00
|
|
|
});
|
2021-03-18 15:41:26 +01:00
|
|
|
|
2024-06-18 02:24:03 +02:00
|
|
|
return $record ? $record->locale : 'en';
|
|
|
|
|
2021-03-18 15:41:26 +01:00
|
|
|
}
|
2021-03-11 14:05:23 +01:00
|
|
|
}
|