From 43e6ba2ec67a026a4f45b4affbe0a8a18fbc5c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 7 Apr 2021 13:26:50 +0200 Subject: [PATCH] Check if payment is eligible to process --- app/Http/Livewire/BillingPortalPurchase.php | 12 ++++++++++++ app/Services/Subscription/SubscriptionService.php | 11 +++++------ .../livewire/billing-portal-purchase.blade.php | 6 +++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index 546817e2eb..6fbb439cfb 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -109,6 +109,8 @@ class BillingPortalPurchase extends Component 'passwordless_login_sent' => false, 'started_payment' => false, 'discount_applied' => false, + 'show_loading_bar' => false, + 'not_eligible' => null, ]; /** @@ -299,6 +301,7 @@ class BillingPortalPurchase extends Component public function handleBeforePaymentEvents() { $this->steps['started_payment'] = true; + $this->steps['show_loading_bar'] = true; $data = [ 'client_id' => $this->contact->client->id, @@ -320,6 +323,15 @@ class BillingPortalPurchase extends Component ->fillDefaults() ->save(); + $is_eligible = $this->subscription->service()->isEligible($this->contact); + + if (is_array($is_eligible)) { + $this->steps['not_eligible'] = true; + $this->steps['show_loading_bar'] = false; + + return; + } + Cache::put($this->hash, [ 'subscription_id' => $this->subscription->id, 'email' => $this->email ?? $this->contact->email, diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index bb9e1d9563..d78a530247 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -118,7 +118,6 @@ class SubscriptionService /* Hits the client endpoint to determine whether the user is able to access this subscription */ public function isEligible($contact) { - $context = [ 'context' => 'is_eligible', 'subscription' => $this->subscription->hashed_id, @@ -131,7 +130,7 @@ class SubscriptionService return $response; } - /* Starts the process to create a trial + /* Starts the process to create a trial - we create a recurring invoice, which is has its next_send_date as now() + trial_duration - we then hit the client API end point to advise the trial payload - we then return the user to either a predefined user endpoint, OR we return the user to the recurring invoice page. @@ -229,10 +228,10 @@ class SubscriptionService $response = false; $body = array_merge($context, [ - 'company_key' => $this->subscription->company->company_key, + 'company_key' => $this->subscription->company->company_key, 'account_key' => $this->subscription->company->account->key, 'db' => $this->subscription->company->db, - ]); + ]); $response = $this->sendLoad($this->subscription, $body); @@ -240,10 +239,10 @@ class SubscriptionService if(is_array($response)){ $body = $response; - + } else { - + $status = $response->getStatusCode(); $response_body = $response->getBody(); $body = array_merge($body, ['status' => $status, 'response_body' => $response_body]); 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 c8685e93dd..004e8100c8 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 @@ -124,7 +124,7 @@ @endforeach @endif - @if($steps['started_payment']) + @if($steps['started_payment'] && $steps['show_loading_bar']) Apply @endif + + @if($steps['not_eligible'] && !is_null($steps['not_eligible'])) +

{{ ctrans('texts.payment_error') }}

+ @endif