From 851a31b3c038b0b8e5b892619d1295fef1f4b025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Tue, 16 Mar 2021 14:35:57 +0100 Subject: [PATCH] Proof of concept for billing page --- app/Http/Livewire/BillingPortalPurchase.php | 61 ++++++++++++++++--- .../views/billing-portal/purchase.blade.php | 3 +- .../billing-portal-purchase.blade.php | 20 ++++-- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index 8142f607ca51..b313aab9a1e1 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -4,7 +4,7 @@ namespace App\Http\Livewire; use App\Factory\ClientFactory; use App\Models\ClientContact; -use App\Models\Company; +use App\Models\Invoice; use App\Models\User; use App\Repositories\ClientContactRepository; use App\Repositories\ClientRepository; @@ -43,6 +43,8 @@ class BillingPortalPurchase extends Component public $methods = []; + public $invoice; + public function authenticate() { $this->validate(); @@ -72,18 +74,17 @@ class BillingPortalPurchase extends Component protected function createBlankClient() { - $company = Company::first(); - $user = User::first(); + $company = $this->billing_subscription->company; + $user = User::first(); // TODO: What should be a value of $user? $client_repo = new ClientRepository(new ClientContactRepository()); - $client_data = [ + + $client = $client_repo->save([ 'name' => 'Client Name', 'contacts' => [ ['email' => $this->email], ] - ]; - - $client = $client_repo->save($client_data, ClientFactory::create($company->id, $user->id)); + ], ClientFactory::create($company->id, $user->id)); return $client->contacts->first(); } @@ -105,6 +106,52 @@ class BillingPortalPurchase extends Component return $this; } + 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'); + } + public function render() { if ($this->contact instanceof ClientContact) { diff --git a/resources/views/billing-portal/purchase.blade.php b/resources/views/billing-portal/purchase.blade.php index 6ad16312f877..0bb3cba62bb5 100644 --- a/resources/views/billing-portal/purchase.blade.php +++ b/resources/views/billing-portal/purchase.blade.php @@ -10,7 +10,8 @@ function updateGatewayFields(companyGatewayId, paymentMethodId) { document.getElementById('company_gateway_id').value = companyGatewayId; document.getElementById('payment_method_id').value = paymentMethodId; - document.getElementById('payment-method-form').submit(); } + + Livewire.on('beforePaymentEventsCompleted', () => document.getElementById('payment-method-form').submit()); @endpush diff --git a/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php b/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php index 5d028bf1b4b6..713111531ea1 100644 --- a/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php @@ -38,19 +38,27 @@ @if($this->steps['fetched_payment_methods'])
-
@csrf + + @if($invoice instanceof \App\Models\Invoice) + + + + @endif + - - + +
@foreach($this->methods as $method)