2021-04-07 18:08:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. 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
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* @var RecurringInvoice
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2021-04-07 18:08:26 +02:00
|
|
|
public $contact;
|
|
|
|
|
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;
|
|
|
|
|
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,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed|string
|
|
|
|
*/
|
|
|
|
public $hash;
|
|
|
|
|
2021-06-07 03:06:31 +02:00
|
|
|
public $company;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
Cache::put($this->hash, [
|
|
|
|
'subscription_id' => $this->target->id,
|
|
|
|
'target_id' => $this->target->id,
|
|
|
|
'recurring_invoice' => $this->recurring_invoice->id,
|
|
|
|
'client_id' => $this->recurring_invoice->client->id,
|
|
|
|
'invoice_id' => $this->state['invoice']->id,
|
|
|
|
'context' => 'change_plan',
|
|
|
|
now()->addMinutes(60), ]
|
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
|
|
|
|
|
|
|
$this->emit('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()
|
|
|
|
{
|
2021-04-15 05:40:03 +02:00
|
|
|
return $this->target->service()->createChangePlanCredit([
|
2022-06-21 11:57:17 +02:00
|
|
|
'recurring_invoice' => $this->recurring_invoice,
|
|
|
|
'subscription' => $this->subscription,
|
|
|
|
'target' => $this->target,
|
|
|
|
'hash' => $this->hash,
|
|
|
|
]);
|
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');
|
|
|
|
}
|
|
|
|
}
|