From 7c8bf53951ee1966c533acba6a7383dfb0ad305f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 15 Apr 2021 13:40:03 +1000 Subject: [PATCH] updgrade and downgrade --- app/Http/Livewire/SubscriptionPlanSwitch.php | 21 ++--- .../Subscription/SubscriptionService.php | 87 ++++++++++++++++--- .../subscription-plan-switch.blade.php | 17 ++-- 3 files changed, 92 insertions(+), 33 deletions(-) diff --git a/app/Http/Livewire/SubscriptionPlanSwitch.php b/app/Http/Livewire/SubscriptionPlanSwitch.php index 53d5b489b015..9ad9f35dc672 100644 --- a/app/Http/Livewire/SubscriptionPlanSwitch.php +++ b/app/Http/Livewire/SubscriptionPlanSwitch.php @@ -85,26 +85,12 @@ class SubscriptionPlanSwitch extends Component $this->state['show_loading_bar'] = true; - $change_cost = $this->target->service()->calculateUpgradePrice($this->recurring_invoice, $this->target); - - if($change_cost > 0) - { $this->state['invoice'] = $this->target->service()->createChangePlanInvoice([ 'recurring_invoice' => $this->recurring_invoice, 'subscription' => $this->subscription, 'target' => $this->target, 'hash' => $this->hash, ]); - } - else - { - $this->state['credit'] = $this->target->service()->createChangePlanCredit([ - 'recurring_invoice' => $this->recurring_invoice, - 'subscription' => $this->subscription, - 'target' => $this->target, - 'hash' => $this->hash, - ]); - } Cache::put($this->hash, [ 'subscription_id' => $this->target->id, @@ -139,6 +125,13 @@ class SubscriptionPlanSwitch extends Component public function handlePaymentNotRequired() { + return $this->target->service()->createChangePlanCredit([ + 'recurring_invoice' => $this->recurring_invoice, + 'subscription' => $this->subscription, + 'target' => $this->target, + 'hash' => $this->hash, + ]); + } public function render() diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 1f366f913765..f26bdf42f736 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -250,9 +250,11 @@ class SubscriptionService * @param Invoice $invoice * @return array */ - private function calculateProRataRefundItems($invoice) :array + private function calculateProRataRefundItems($invoice, $is_credit = false) :array { - + /* depending on whether we are creating an invoice or a credit*/ + $multiplier = $is_credit ? 1 : -1; + $start_date = Carbon::parse($invoice->date); $current_date = now(); @@ -271,7 +273,7 @@ class SubscriptionService if($item->product_key != ctrans('texts.refund')) { - $item->cost = ($item->cost*$ratio*-1); + $item->cost = ($item->cost*$ratio*$multiplier); $item->product_key = ctrans('texts.refund'); $item->notes = ctrans('texts.refund') . ": ". $item->notes; @@ -343,8 +345,40 @@ class SubscriptionService nlog("total payable = {$total_payable}"); - return $this->createCredit($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription); + $credit = $this->createCredit($pro_rata_refund_amount, $last_invoice, $target_subscription, $old_subscription); +/////////////////////////// + $old_subscription_recurring_invoice = $recurring_invoice; + $old_subscription_recurring_invoice->service()->stop()->save(); + + $recurring_invoice_repo = new RecurringInvoiceRepository(); + $recurring_invoice_repo->archive($old_subscription_recurring_invoice); + + $recurring_invoice = $this->convertInvoiceToRecurring($recurring_invoice->client_id); + $recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice); + $recurring_invoice->next_send_date = now(); + $recurring_invoice->next_send_date = $recurring_invoice->nextSendDate(); + + /* Start the recurring service */ + $recurring_invoice->service() + ->start() + ->save(); +//////////////////////////// + + $context = [ + 'context' => 'change_plan', + 'recurring_invoice' => $recurring_invoice->hashed_id, + 'credit' => $credit->hashed_id, + 'client' => $recurring_invoice->client->hashed_id, + 'subscription' => $target_subscription->hashed_id, + 'contact' => auth('contact')->user()->hashed_id, + ]; + + $response = $this->triggerWebhook($context); + + nlog($response); + + return $this->handleRedirect('/client/credits/'.$credit->hashed_id); } @@ -424,16 +458,49 @@ class SubscriptionService nlog($response); - if(array_key_exists('post_purchase_url', $this->subscription->webhook_configuration) && strlen($this->subscription->webhook_configuration['post_purchase_url']) >=1) - return redirect($this->subscription->webhook_configuration['post_purchase_url']); - - return redirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id); + return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id); } - public function handlePlanChangeNoPayment() + public function handlePlanChangeNoPayment($data) { + /* + 'recurring_invoice' => $this->recurring_invoice, + 'subscription' => $this->subscription, + 'target' => $this->target, + 'hash' => $this->hash, + */ + + $old_subscription_recurring_invoice = $data['recurring_invoice']; + $old_subscription_recurring_invoice->service()->stop()->save(); + $recurring_invoice_repo = new RecurringInvoiceRepository(); + $recurring_invoice_repo->archive($old_subscription_recurring_invoice); + + $recurring_invoice = $this->convertInvoiceToRecurring($old_subscription_recurring_invoice->client_id); + $recurring_invoice = $recurring_invoice_repo->save([], $recurring_invoice); + $recurring_invoice->next_send_date = now(); + $recurring_invoice->next_send_date = $recurring_invoice->nextSendDate(); + + /* Start the recurring service */ + $recurring_invoice->service() + ->start() + ->save(); + + $context = [ + 'context' => 'change_plan', + 'recurring_invoice' => $recurring_invoice->hashed_id, + 'invoice' => $this->encodePrimaryKey($payment_hash->fee_invoice_id), + 'client' => $recurring_invoice->client->hashed_id, + 'subscription' => $this->subscription->hashed_id, + 'contact' => auth('contact')->user()->hashed_id, + ]; + + $response = $this->triggerWebhook($context); + + nlog($response); + + return $this->handleRedirect('/client/recurring_invoices/'.$recurring_invoice->hashed_id); } private function createCredit($refund_amount, $last_invoice, $target, $old_subscription) @@ -448,7 +515,7 @@ class SubscriptionService $line_items = $subscription_repo->generateLineItems($target); - $credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice)); + $credit->line_items = array_merge($line_items, $this->calculateProRataRefundItems($last_invoice, true)); $data = [ 'client_id' => $last_invoice->client_id, diff --git a/resources/views/portal/ninja2020/components/livewire/subscription-plan-switch.blade.php b/resources/views/portal/ninja2020/components/livewire/subscription-plan-switch.blade.php index 01730dcf3956..47cf8f129928 100644 --- a/resources/views/portal/ninja2020/components/livewire/subscription-plan-switch.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/subscription-plan-switch.blade.php @@ -1,6 +1,9 @@
+ + @if(isset($state['invoice'])) +
@@ -15,7 +18,6 @@
- @if($state['invoice'])
@csrf @@ -32,14 +34,6 @@
- @elseif($state['credit']) -
- @csrf - -
- @endif
@@ -68,5 +62,10 @@ @endif
+ @elseif($amount < 0) + + @endif