1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00
invoiceninja/app/Http/Livewire/BillingPortalPurchase.php

159 lines
4.1 KiB
PHP
Raw Normal View History

2021-03-11 17:20:30 +01:00
<?php
namespace App\Http\Livewire;
2021-03-15 17:33:27 +01:00
use App\Factory\ClientFactory;
2021-03-11 17:20:30 +01:00
use App\Models\ClientContact;
2021-03-15 17:33:27 +01:00
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
2021-03-11 17:20:30 +01:00
use Illuminate\Support\Facades\Auth;
2021-03-17 16:12:25 +01:00
use Illuminate\Support\Facades\Cache;
2021-03-11 17:20:30 +01:00
use Livewire\Component;
class BillingPortalPurchase extends Component
{
2021-03-15 17:33:27 +01:00
public $hash;
public $heading_text = 'Log in';
2021-03-11 17:20:30 +01:00
public $email;
public $password;
2021-03-12 15:00:33 +01:00
public $billing_subscription;
2021-03-15 17:33:27 +01:00
public $contact;
2021-03-12 15:00:33 +01:00
protected $rules = [
'email' => ['required', 'email'],
];
2021-03-15 17:33:27 +01:00
public $company_gateway_id;
public $payment_method_id;
2021-03-11 17:20:30 +01:00
public $steps = [
'passed_email' => false,
'existing_user' => false,
'fetched_payment_methods' => false,
2021-03-15 17:33:27 +01:00
'fetched_client' => false,
2021-03-11 17:20:30 +01:00
];
2021-03-12 15:00:33 +01:00
public $methods = [];
2021-03-11 17:20:30 +01:00
2021-03-16 14:35:57 +01:00
public $invoice;
2021-03-17 16:12:25 +01:00
public $coupon;
2021-03-11 17:20:30 +01:00
public function authenticate()
{
$this->validate();
$contact = ClientContact::where('email', $this->email)->first();
if ($contact && $this->steps['existing_user'] === false) {
return $this->steps['existing_user'] = true;
}
if ($contact && $this->steps['existing_user']) {
$attempt = Auth::guard('contact')->attempt(['email' => $this->email, 'password' => $this->password]);
2021-03-15 17:33:27 +01:00
return $attempt
? $this->getPaymentMethods($contact)
: session()->flash('message', 'These credentials do not match our records.');
2021-03-11 17:20:30 +01:00
}
$this->steps['existing_user'] = false;
2021-03-12 15:00:33 +01:00
2021-03-13 14:51:01 +01:00
$contact = $this->createBlankClient();
2021-03-15 17:33:27 +01:00
if ($contact && $contact instanceof ClientContact) {
$this->getPaymentMethods($contact);
}
2021-03-11 17:20:30 +01:00
}
protected function createBlankClient()
{
2021-03-16 14:35:57 +01:00
$company = $this->billing_subscription->company;
$user = $this->billing_subscription->user;
2021-03-12 15:00:33 +01:00
2021-03-15 17:33:27 +01:00
$client_repo = new ClientRepository(new ClientContactRepository());
2021-03-16 14:35:57 +01:00
$client = $client_repo->save([
2021-03-15 17:33:27 +01:00
'name' => 'Client Name',
'contacts' => [
['email' => $this->email],
]
2021-03-16 14:35:57 +01:00
], ClientFactory::create($company->id, $user->id));
2021-03-12 15:00:33 +01:00
2021-03-15 17:33:27 +01:00
return $client->contacts->first();
2021-03-12 15:00:33 +01:00
}
protected function getPaymentMethods(ClientContact $contact): self
{
$this->steps['fetched_payment_methods'] = true;
$this->methods = $contact->client->service()->getPaymentMethods(1000);
2021-03-11 17:20:30 +01:00
2021-03-15 17:33:27 +01:00
$this->heading_text = 'Pick a payment method';
Auth::guard('contact')->login($contact);
$this->contact = $contact;
2021-03-12 15:00:33 +01:00
return $this;
2021-03-11 17:20:30 +01:00
}
2021-03-16 14:35:57 +01:00
public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id)
{
$this->company_gateway_id = $company_gateway_id;
$this->payment_method_id = $gateway_type_id;
$this->handleBeforePaymentEvents();
}
public function handleBeforePaymentEvents()
{
$data = [
2021-03-16 14:35:57 +01:00
'client_id' => $this->contact->client->id,
'date' => now()->format('Y-m-d'),
'invitations' => [[
'key' => '',
'client_contact_id' => $this->contact->hashed_id,
2021-03-16 14:35:57 +01:00
]],
2021-03-17 16:12:25 +01:00
'user_input_promo_code' => $this->coupon,
'quantity' => 1, // Option to increase quantity
2021-03-16 14:35:57 +01:00
];
$this->invoice = $this->billing_subscription
->service()
->createInvoice($data)
->service()
->markSent()
->save();
2021-03-16 14:35:57 +01:00
2021-03-17 16:12:25 +01:00
Cache::put($this->hash, [
'email' => $this->email ?? $this->contact->email,
'client_id' => $this->contact->client->id,
'invoice_id' => $this->invoice->id],
now()->addMinutes(60)
);
2021-03-16 14:35:57 +01:00
$this->emit('beforePaymentEventsCompleted');
}
2021-03-17 16:12:25 +01:00
public function applyCouponCode()
{
dd('Applying coupon code: ' . $this->coupon);
}
2021-03-11 17:20:30 +01:00
public function render()
{
2021-03-15 17:33:27 +01:00
if ($this->contact instanceof ClientContact) {
$this->getPaymentMethods($this->contact);
}
2021-03-11 17:20:30 +01:00
return render('components.livewire.billing-portal-purchase');
}
}