From 81f5808bf67c05de4195d4d91fae94ca0419deab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 17 Mar 2021 12:06:58 +0100 Subject: [PATCH] - Use user from $billing_subscription to create client - Disable generating the cache for temporary state - Generate the blank invoice based on the product --- app/Http/Livewire/BillingPortalPurchase.php | 48 ++++++------------- app/Models/BillingSubscription.php | 1 - .../BillingSubscriptionService.php | 24 +++++----- .../ninja2020/layout/payments.blade.php | 11 +++-- 4 files changed, 34 insertions(+), 50 deletions(-) diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index b313aab9a1..dd39084d08 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -4,12 +4,9 @@ namespace App\Http\Livewire; use App\Factory\ClientFactory; use App\Models\ClientContact; -use App\Models\Invoice; -use App\Models\User; use App\Repositories\ClientContactRepository; use App\Repositories\ClientRepository; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Cache; use Livewire\Component; class BillingPortalPurchase extends Component @@ -75,7 +72,7 @@ class BillingPortalPurchase extends Component protected function createBlankClient() { $company = $this->billing_subscription->company; - $user = User::first(); // TODO: What should be a value of $user? + $user = $this->billing_subscription->user; $client_repo = new ClientRepository(new ClientContactRepository()); @@ -91,7 +88,7 @@ class BillingPortalPurchase extends Component protected function getPaymentMethods(ClientContact $contact): self { - Cache::put($this->hash, ['email' => $this->email ?? $this->contact->email, 'url' => url()->current()]); +// Cache::put($this->hash, ['email' => $this->email ?? $this->contact->email, 'url' => url()->current()]); $this->steps['fetched_payment_methods'] = true; @@ -116,38 +113,23 @@ class BillingPortalPurchase extends Component public function handleBeforePaymentEvents() { - $company = $this->billing_subscription->company; - $user = User::first(); // TODO: What should be a value of $user? - - $invoice = [ + $data = [ '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' => '', + 'date' => now()->format('Y-m-d'), + 'invitations' => [[ + 'key' => '', + 'client_contact_id' => $this->contact->hashed_id, ]], + 'user_input_promo_code' => '', // Field to input the promo code, + 'quantity' => 1, // Option to increase quantity ]; - // 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->invoice = $this->billing_subscription + ->service() + ->createInvoice($data) + ->service() + ->markSent() + ->save(); $this->emit('beforePaymentEventsCompleted'); } diff --git a/app/Models/BillingSubscription.php b/app/Models/BillingSubscription.php index 10f6d15564..50b68eae79 100644 --- a/app/Models/BillingSubscription.php +++ b/app/Models/BillingSubscription.php @@ -73,5 +73,4 @@ class BillingSubscription extends BaseModel { return $this->belongsTo(Product::class); } - } diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index e464f4d9ea..b9e509a034 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/BillingSubscription/BillingSubscriptionService.php @@ -13,13 +13,14 @@ namespace App\Services\BillingSubscription; use App\DataMapper\InvoiceItem; use App\Factory\InvoiceFactory; +use App\Models\BillingSubscription; use App\Models\ClientSubscription; use App\Models\Product; use App\Repositories\InvoiceRepository; class BillingSubscriptionService { - + /** @var BillingSubscription */ private $billing_subscription; public function __construct(BillingSubscription $billing_subscription) @@ -27,9 +28,8 @@ class BillingSubscriptionService $this->billing_subscription = $billing_subscription; } - public function createInvoice($data) + public function createInvoice($data): ?\App\Models\Invoice { - $invoice_repo = new InvoiceRepository(); // $data = [ @@ -38,31 +38,31 @@ class BillingSubscriptionService // 'invitations' => [ // 'client_contact_id' => hashed_id // ], - // 'line_items' => [], + // 'line_items' => [], // ]; + $data['line_items'] = $this->createLineItems($data['quantity']); - $invoice = $invoice_repo->save($data, InvoiceFactory::create($billing_subscription->company_id, $billing_subscription->user_id)); /* - + If trial_enabled -> return early -- what we need to know that we don't already -- Has a promo code been entered, and does it match -- Is this a recurring subscription - -- + -- 1. Is this a recurring product? 2. What is the quantity? ie is this a multi seat product ( does this mean we need this value stored in the client sub?) */ - - return $invoice; + + return $invoice_repo->save($data, InvoiceFactory::create($this->billing_subscription->company_id, $this->billing_subscription->user_id)); } - private function createLineItems($quantity) + private function createLineItems($quantity): array { $line_items = []; - + $product = $this->billing_subscription->product; $item = new InvoiceItem; @@ -88,7 +88,7 @@ class BillingSubscriptionService public function createClientSubscription($payment_hash, $recurring_invoice_id = null) { //create the client sub record - + //?trial enabled? $cs = new ClientSubscription(); $cs->subscription_id = $this->billing_subscription->id; diff --git a/resources/views/portal/ninja2020/layout/payments.blade.php b/resources/views/portal/ninja2020/layout/payments.blade.php index 49bf823d62..f72f97a85c 100644 --- a/resources/views/portal/ninja2020/layout/payments.blade.php +++ b/resources/views/portal/ninja2020/layout/payments.blade.php @@ -31,10 +31,13 @@
@yield('gateway_content')
- - - Secure 256-bit encryption - + + @if(Request::isSecure()) + + + Secure 256-bit encryption + + @endif @endsection