From 8ee1f8eaf1bad14ec9796afdf14d6659f219ae39 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 18 Jan 2023 21:06:09 +1100 Subject: [PATCH] Improve subscriptions v2 for free subscriptions --- app/Http/Livewire/BillingPortalPurchasev2.php | 75 +------------------ .../Subscription/SubscriptionService.php | 44 +++++++++++ 2 files changed, 47 insertions(+), 72 deletions(-) diff --git a/app/Http/Livewire/BillingPortalPurchasev2.php b/app/Http/Livewire/BillingPortalPurchasev2.php index 87f4827907b8..209e4a4068b8 100644 --- a/app/Http/Livewire/BillingPortalPurchasev2.php +++ b/app/Http/Livewire/BillingPortalPurchasev2.php @@ -587,7 +587,6 @@ class BillingPortalPurchasev2 extends Component $this->is_eligible = false; $this->not_eligible_message = $eligibility_check['message']; - return $this; } @@ -601,40 +600,9 @@ class BillingPortalPurchasev2 extends Component ->markPaid() ->save(); - if (strlen($this->subscription->recurring_product_ids) >= 1) { - - $recurring_invoice = $this->subscription->service()->convertInvoiceToRecurringBundle($this->contact->client_id, $this->bundle); - - /* Start the recurring service */ - $recurring_invoice->service() - ->start() - ->save(); - - $invoice->recurring_id = $recurring_invoice->id; - $invoice->save(); - - $context = [ - 'context' => 'recurring_purchase', - 'recurring_invoice' => $recurring_invoice->hashed_id, - 'invoice' => $invoice->hashed_id, - 'client' => $recurring_invoice->client->hashed_id, - 'subscription' => $this->subscription->hashed_id, - 'contact' => auth()->guard('contact')->user()->hashed_id, - 'redirect_url' => "/client/recurring_invoices/{$recurring_invoice->hashed_id}", - ]; - - return $context; - } - - - - - - - $redirect_url = "/client/invoices/{$invoice->hashed_id}"; - - return $this->handleRedirect($redirect_url); - + return $this->subscription + ->service() + ->handleNoPaymentFlow($invoice, $this->bundle, $this->contact); } @@ -672,43 +640,6 @@ class BillingPortalPurchasev2 extends Component } - // /** - // * Handle user authentication - // * - // * @return $this|bool|void - // */ - // public function authenticate() - // { - // $this->validate(); - - // $contact = ClientContact::where('email', $this->email) - // ->where('company_id', $this->subscription->company_id) - // ->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, 'company_id' => $this->subscription->company_id]); - - // return $attempt - // ? $this->getPaymentMethods($contact) - // : session()->flash('message', 'These credentials do not match our records.'); - // } - - // $this->steps['existing_user'] = false; - - // $contact = $this->createBlankClient(); - - // if ($contact && $contact instanceof ClientContact) { - // $this->getPaymentMethods($contact); - // } - // } - - - - /** * Create a blank client. Used for new customers purchasing. * diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 732fca034623..0068173408ee 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -1358,6 +1358,50 @@ class SubscriptionService } + /** + * Handle case where no payment is required + * @param Invoice $invoice The Invoice + * @param array $bundle The bundle array + * @param ClientContact $contact The Client Contact + * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse + */ + public function handleNoPaymentFlow(Invoice $invoice, $bundle, ClientContact $contact) + { + + if (strlen($this->subscription->recurring_product_ids) >= 1) { + + $recurring_invoice = $this->convertInvoiceToRecurringBundle($contact->client_id, collect($bundle)->map(function ($bund){ return (object) $bund;})); + + /* Start the recurring service */ + $recurring_invoice->service() + ->start() + ->save(); + + $invoice->recurring_id = $recurring_invoice->id; + $invoice->save(); + + $context = [ + 'context' => 'recurring_purchase', + 'recurring_invoice' => $recurring_invoice->hashed_id, + 'invoice' => $invoice->hashed_id, + 'client' => $recurring_invoice->client->hashed_id, + 'subscription' => $this->subscription->hashed_id, + 'contact' => $contact->hashed_id, + 'redirect_url' => "/client/recurring_invoices/{$recurring_invoice->hashed_id}", + ]; + + $this->triggerWebhook($context); + + return $this->handleRedirect($context['redirect_url']); + + } + + $redirect_url = "/client/invoices/{$invoice->hashed_id}"; + + return $this->handleRedirect($redirect_url); + + } + /** * 'email' => $this->email ?? $this->contact->email, * 'quantity' => $this->quantity,