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-16 14:35:57 +01:00
|
|
|
use App\Models\Invoice;
|
2021-03-15 17:33:27 +01:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\ClientContactRepository;
|
|
|
|
use App\Repositories\ClientRepository;
|
2021-03-11 17:20:30 +01:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-03-15 17:33:27 +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-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 = User::first(); // TODO: What should be a value of $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
|
|
|
|
{
|
2021-03-15 17:33:27 +01:00
|
|
|
Cache::put($this->hash, ['email' => $this->email ?? $this->contact->email, 'url' => url()->current()]);
|
|
|
|
|
2021-03-12 15:00:33 +01:00
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$company = $this->billing_subscription->company;
|
|
|
|
$user = User::first(); // TODO: What should be a value of $user?
|
|
|
|
|
|
|
|
$invoice = [
|
|
|
|
'client_id' => $this->contact->client->id,
|
|
|
|
'line_items' => [[
|
|
|
|
'quantity' => 1,
|
|
|
|
'cost' => 10,
|
|
|
|
'product_key' => 'example',
|
|
|
|
'notes' => 'example',
|
|
|
|
'discount' => 0,
|
|
|
|
'is_amount_discount' => true,
|
|
|
|
'tax_rate1' => 0,
|
|
|
|
'tax_rate2' => 0,
|
|
|
|
'tax_rate3' => 0,
|
|
|
|
'tax_name1' => '',
|
|
|
|
'tax_name2' => '',
|
|
|
|
'tax_name3' => '',
|
|
|
|
'sort_id' => 0,
|
|
|
|
'line_total' => 1,
|
|
|
|
'custom_value1' => 'example',
|
|
|
|
'custom_value2' => 'example',
|
|
|
|
'custom_value3' => 'example',
|
|
|
|
'custom_value4' => 'example',
|
|
|
|
'type_id' => 1,
|
|
|
|
'date' => '',
|
|
|
|
]],
|
|
|
|
];
|
|
|
|
|
|
|
|
// TODO: Only for testing.
|
|
|
|
$this->invoice = Invoice::where('status_id', Invoice::STATUS_SENT)->first();
|
|
|
|
// $this->invoice = (new \App\Repositories\InvoiceRepository)->save($invoice, InvoiceFactory::create($company->id, $user->id));
|
|
|
|
|
|
|
|
$this->emit('beforePaymentEventsCompleted');
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|