2021-04-07 18:08:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-04-07 18:08:26 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-04-07 18:08:26 +02:00
|
|
|
*/
|
|
|
|
|
2023-12-13 17:52:49 +01:00
|
|
|
namespace App\Livewire;
|
2021-04-07 18:08:26 +02:00
|
|
|
|
2021-06-07 03:06:31 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2021-04-08 16:53:54 +02:00
|
|
|
use App\Models\ClientContact;
|
|
|
|
use App\Models\Subscription;
|
2021-04-14 04:40:16 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2021-04-08 16:53:54 +02:00
|
|
|
use Illuminate\Support\Str;
|
2021-04-07 18:08:26 +02:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class SubscriptionPlanSwitch extends Component
|
|
|
|
{
|
2021-04-12 13:56:08 +02:00
|
|
|
/**
|
2023-03-09 01:22:59 +01:00
|
|
|
* @var \App\Models\RecurringInvoice
|
2021-04-12 13:56:08 +02:00
|
|
|
*/
|
|
|
|
public $recurring_invoice;
|
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var Subscription
|
|
|
|
*/
|
2021-04-07 18:08:26 +02:00
|
|
|
public $subscription;
|
|
|
|
|
2021-04-12 13:56:08 +02:00
|
|
|
/**
|
|
|
|
* @var ?float
|
|
|
|
*/
|
|
|
|
public $amount;
|
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var Subscription
|
|
|
|
*/
|
|
|
|
public $target;
|
2021-04-07 18:08:26 +02:00
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var ClientContact
|
|
|
|
*/
|
2024-02-09 19:20:33 +01:00
|
|
|
public ClientContact $contact;
|
2021-04-07 18:08:26 +02:00
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2021-04-07 18:08:26 +02:00
|
|
|
public $methods = [];
|
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-04-07 18:08:26 +02:00
|
|
|
public $total;
|
|
|
|
|
2022-09-02 02:38:27 +02:00
|
|
|
public $hide_button = false;
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $state = [
|
|
|
|
'payment_initialised' => false,
|
|
|
|
'show_loading_bar' => false,
|
|
|
|
'invoice' => null,
|
|
|
|
'company_gateway_id' => null,
|
|
|
|
'payment_method_id' => null,
|
2024-02-09 19:20:33 +01:00
|
|
|
'show_rff' => false,
|
2021-04-08 16:53:54 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed|string
|
|
|
|
*/
|
|
|
|
public $hash;
|
|
|
|
|
2021-06-07 03:06:31 +02:00
|
|
|
public $company;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2024-02-09 19:20:33 +01:00
|
|
|
public ?string $first_name;
|
|
|
|
|
|
|
|
public ?string $last_name;
|
|
|
|
|
|
|
|
public ?string $email;
|
|
|
|
|
2021-04-07 18:08:26 +02:00
|
|
|
public function mount()
|
|
|
|
{
|
2021-06-07 03:06:31 +02:00
|
|
|
MultiDB::setDb($this->company->db);
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2021-04-12 13:56:08 +02:00
|
|
|
$this->total = $this->amount;
|
2021-04-08 16:53:54 +02:00
|
|
|
|
2021-04-14 04:40:16 +02:00
|
|
|
$this->methods = $this->contact->client->service()->getPaymentMethods($this->amount);
|
2021-04-07 18:08:26 +02:00
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
$this->hash = Str::uuid()->toString();
|
2024-02-09 19:20:33 +01:00
|
|
|
|
|
|
|
$this->state['show_rff'] = auth()->guard('contact')->user()->showRff();
|
|
|
|
|
|
|
|
$this->first_name = $this->contact->first_name;
|
|
|
|
|
|
|
|
$this->last_name = $this->contact->last_name;
|
|
|
|
|
|
|
|
$this->email = $this->contact->email;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleRff()
|
|
|
|
{
|
|
|
|
$this->validate([
|
|
|
|
'first_name' => ['required'],
|
|
|
|
'last_name' => ['required'],
|
|
|
|
'email' => ['required', 'email'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->contact->update([
|
|
|
|
'first_name' => $this->first_name,
|
|
|
|
'last_name' => $this->last_name,
|
|
|
|
'email' => $this->email,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->state['show_rff'] = false;
|
2021-04-08 16:53:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handleBeforePaymentEvents(): void
|
|
|
|
{
|
|
|
|
$this->state['show_loading_bar'] = true;
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$payment_required = $this->target->service()->changePlanPaymentCheck([
|
|
|
|
'recurring_invoice' => $this->recurring_invoice,
|
|
|
|
'subscription' => $this->subscription,
|
|
|
|
'target' => $this->target,
|
|
|
|
'hash' => $this->hash,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($payment_required) {
|
|
|
|
$this->state['invoice'] = $this->target->service()->createChangePlanInvoice([
|
2021-04-15 04:28:31 +02:00
|
|
|
'recurring_invoice' => $this->recurring_invoice,
|
|
|
|
'subscription' => $this->subscription,
|
|
|
|
'target' => $this->target,
|
|
|
|
'hash' => $this->hash,
|
|
|
|
]);
|
2021-04-08 16:53:54 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
Cache::put(
|
|
|
|
$this->hash,
|
|
|
|
[
|
2023-01-18 06:52:32 +01:00
|
|
|
'subscription_id' => $this->target->hashed_id,
|
|
|
|
'target_id' => $this->target->hashed_id,
|
|
|
|
'recurring_invoice' => $this->recurring_invoice->hashed_id,
|
|
|
|
'client_id' => $this->recurring_invoice->client->hashed_id,
|
|
|
|
'invoice_id' => $this->state['invoice']->hashed_id,
|
2022-06-21 11:57:17 +02:00
|
|
|
'context' => 'change_plan',
|
|
|
|
now()->addMinutes(60), ]
|
2023-02-16 02:36:09 +01:00
|
|
|
);
|
2021-06-27 13:55:15 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->state['payment_initialised'] = true;
|
|
|
|
} else {
|
|
|
|
$this->handlePaymentNotRequired();
|
|
|
|
}
|
2021-04-08 16:53:54 +02:00
|
|
|
|
2023-12-13 17:52:49 +01:00
|
|
|
$this->dispatch('beforePaymentEventsCompleted');
|
2021-04-07 18:08:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 16:53:54 +02:00
|
|
|
/**
|
|
|
|
* Middle method between selecting payment method &
|
|
|
|
* submitting the from to the backend.
|
|
|
|
*
|
|
|
|
* @param $company_gateway_id
|
|
|
|
* @param $gateway_type_id
|
|
|
|
*/
|
|
|
|
public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id)
|
2021-04-07 18:08:26 +02:00
|
|
|
{
|
2021-04-08 16:53:54 +02:00
|
|
|
$this->state['company_gateway_id'] = $company_gateway_id;
|
|
|
|
$this->state['payment_method_id'] = $gateway_type_id;
|
|
|
|
|
|
|
|
$this->handleBeforePaymentEvents();
|
2021-04-07 18:08:26 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
public function handlePaymentNotRequired()
|
|
|
|
{
|
2022-09-02 02:38:27 +02:00
|
|
|
$this->hide_button = true;
|
|
|
|
|
2022-12-22 05:58:18 +01:00
|
|
|
$response = $this->target->service()->createChangePlanCreditV2([
|
2022-06-21 11:57:17 +02:00
|
|
|
'recurring_invoice' => $this->recurring_invoice,
|
|
|
|
'subscription' => $this->subscription,
|
|
|
|
'target' => $this->target,
|
|
|
|
'hash' => $this->hash,
|
|
|
|
]);
|
2022-09-02 02:38:27 +02:00
|
|
|
|
|
|
|
$this->hide_button = true;
|
|
|
|
|
2023-12-13 17:52:49 +01:00
|
|
|
$this->dispatch('redirectRoute', ['route' => $response]);
|
2022-09-02 02:38:27 +02:00
|
|
|
|
2021-04-15 04:28:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 18:08:26 +02:00
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return render('components.livewire.subscription-plan-switch');
|
|
|
|
}
|
|
|
|
}
|