1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Livewire/SubscriptionPlanSwitch.php

197 lines
4.8 KiB
PHP
Raw Normal View History

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
use App\Libraries\MultiDB;
use App\Models\ClientContact;
use App\Models\Subscription;
2021-04-14 04:40:16 +02:00
use Illuminate\Support\Facades\Cache;
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
/**
* @var \App\Models\RecurringInvoice
2021-04-12 13:56:08 +02:00
*/
public $recurring_invoice;
/**
* @var Subscription
*/
2021-04-07 18:08:26 +02:00
public $subscription;
2021-04-12 13:56:08 +02:00
/**
* @var ?float
*/
public $amount;
/**
* @var Subscription
*/
public $target;
2021-04-07 18:08:26 +02:00
/**
* @var ClientContact
*/
public ClientContact $contact;
2021-04-07 18:08:26 +02:00
/**
* @var array
*/
2021-04-07 18:08:26 +02:00
public $methods = [];
/**
* @var string
*/
2021-04-07 18:08:26 +02:00
public $total;
2022-09-02 02:38:27 +02:00
public $hide_button = false;
/**
* @var array
*/
public $state = [
'payment_initialised' => false,
'show_loading_bar' => false,
'invoice' => null,
'company_gateway_id' => null,
'payment_method_id' => null,
'show_rff' => false,
];
/**
* @var mixed|string
*/
public $hash;
public $company;
public ?string $first_name;
public ?string $last_name;
public ?string $email;
2021-04-07 18:08:26 +02:00
public function mount()
{
MultiDB::setDb($this->company->db);
2021-04-12 13:56:08 +02:00
$this->total = $this->amount;
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
$this->hash = Str::uuid()->toString();
$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;
}
public function handleBeforePaymentEvents(): void
{
$this->state['show_loading_bar'] = true;
$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,
]);
2023-02-16 02:36:09 +01:00
Cache::put(
$this->hash,
[
'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,
'context' => 'change_plan',
now()->addMinutes(60), ]
2023-02-16 02:36:09 +01:00
);
2021-06-27 13:55:15 +02:00
$this->state['payment_initialised'] = true;
} else {
$this->handlePaymentNotRequired();
}
2023-12-13 17:52:49 +01:00
$this->dispatch('beforePaymentEventsCompleted');
2021-04-07 18:08:26 +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
{
$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;
$response = $this->target->service()->createChangePlanCreditV2([
'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');
}
}