1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Http/Livewire/SubscriptionPlanSwitch.php

147 lines
3.4 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
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;
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 RecurringInvoice
*/
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
*/
2021-04-07 18:08:26 +02:00
public $contact;
/**
* @var array
*/
2021-04-07 18:08:26 +02:00
public $methods = [];
/**
* @var string
*/
2021-04-07 18:08:26 +02:00
public $total;
/**
* @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;
public $company;
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();
}
public function handleBeforePaymentEvents(): void
{
2021-04-14 08:48:32 +02:00
$this->state['show_loading_bar'] = true;
2021-04-15 04:28:31 +02:00
$this->state['invoice'] = $this->target->service()->createChangePlanInvoice([
'recurring_invoice' => $this->recurring_invoice,
'subscription' => $this->subscription,
'target' => $this->target,
'hash' => $this->hash,
]);
2021-04-14 04:40:16 +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)]
);
$this->state['payment_initialised'] = true;
$this->emit('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()
{
2021-04-15 05:40:03 +02:00
return $this->target->service()->createChangePlanCredit([
'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');
}
}