From ec1d4e05c13df7ecf2f496032b25bfcce97d6b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 8 Apr 2021 16:53:54 +0200 Subject: [PATCH 1/8] - Rename: $target_subscription to $target and all references - Ability to pay for the upgrade --- .../SubscriptionPlanSwitchController.php | 6 +- app/Http/Livewire/SubscriptionPlanSwitch.php | 68 +++++++++++++++++-- .../Subscription/SubscriptionService.php | 23 +++++-- .../subscription-plan-switch.blade.php | 60 ++++++++++++---- .../recurring_invoices/show.blade.php | 2 +- .../ninja2020/subscriptions/switch.blade.php | 8 ++- routes/client.php | 2 +- 7 files changed, 141 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php b/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php index 9dfd6da209..9f5a4275a9 100644 --- a/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php +++ b/app/Http/Controllers/ClientPortal/SubscriptionPlanSwitchController.php @@ -24,14 +24,14 @@ class SubscriptionPlanSwitchController extends Controller * * @param ShowPlanSwitchRequest $request * @param Subscription $subscription - * @param string $target_subscription + * @param string $target * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function index(ShowPlanSwitchRequest $request, Subscription $subscription, Subscription $target_subscription) + public function index(ShowPlanSwitchRequest $request, Subscription $subscription, Subscription $target) { return render('subscriptions.switch', [ 'subscription' => $subscription, - 'target_subscription' => $target_subscription, + 'target' => $target, ]); } } diff --git a/app/Http/Livewire/SubscriptionPlanSwitch.php b/app/Http/Livewire/SubscriptionPlanSwitch.php index 8b26fd4ab8..a6afc69c44 100644 --- a/app/Http/Livewire/SubscriptionPlanSwitch.php +++ b/app/Http/Livewire/SubscriptionPlanSwitch.php @@ -12,30 +12,90 @@ namespace App\Http\Livewire; +use App\Models\ClientContact; +use App\Models\Subscription; +use Illuminate\Support\Str; use Livewire\Component; class SubscriptionPlanSwitch extends Component { + /** + * @var Subscription + */ public $subscription; - public $target_subscription; + /** + * @var Subscription + */ + public $target; + /** + * @var ClientContact + */ public $contact; + /** + * @var array + */ public $methods = []; + /** + * @var string + */ public $total; + /** + * @var array + */ + public $state = [ + 'payment_initialised' => false, + 'show_loading_bar' => false, + 'invoice' => null, + 'company_gateway_id' => null, + 'payment_method_id' => null, + ]; + + /** + * @var mixed|string + */ + public $hash; + public function mount() { + $this->total = $this->subscription->service()->getPriceBetweenSubscriptions($this->subscription, $this->target); + $this->methods = $this->contact->client->service()->getPaymentMethods(100); - $this->total = 1; + $this->hash = Str::uuid()->toString(); } - public function handleBeforePaymentEvents() + public function handleBeforePaymentEvents(): void { - // .. + $this->state['show_loading_bar'] = true; + + $this->state['invoice'] = $this->subscription->service()->createChangePlanInvoice([ + 'subscription' => $this->subscription, + 'target' => $this->target, + ]); + + $this->state['payment_initialised'] = true; + + $this->emit('beforePaymentEventsCompleted'); + } + + /** + * Middle method between selecting payment method & + * submitting the from to the backend. + * + * @param $company_gateway_id + * @param $gateway_type_id + */ + public function handleMethodSelectingEvent($company_gateway_id, $gateway_type_id) + { + $this->state['company_gateway_id'] = $company_gateway_id; + $this->state['payment_method_id'] = $gateway_type_id; + + $this->handleBeforePaymentEvents(); } public function render() diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 6e4ce1ed96..5734e23634 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -184,7 +184,7 @@ class SubscriptionService public function createChangePlanInvoice($data) { - + return Invoice::where('status_id', Invoice::STATUS_SENT)->first(); } public function createInvoice($data): ?\App\Models\Invoice @@ -275,9 +275,9 @@ class SubscriptionService } /** - * Get the single charge products for the + * Get the single charge products for the * subscription - * + * * @return ?Product Collection */ public function products() @@ -286,9 +286,9 @@ class SubscriptionService } /** - * Get the recurring products for the + * Get the recurring products for the * subscription - * + * * @return ?Product Collection */ public function recurring_products() @@ -319,4 +319,17 @@ class SubscriptionService { // .. } + + /** + * Get pro rata calculation between subscriptions. + * + * @param Subscription $current + * @param Subscription $target + */ + public function getPriceBetweenSubscriptions(Subscription $current, Subscription $target): int + { + // Calculate the pro rata. Return negative value if credits needed. + + return 1; + } } 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 1f4daa4215..ff43981e91 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 @@ -7,24 +7,58 @@
-

- {{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($total, $subscription->company) }} - {{-- {{ \App\Utils\Number::formatMoney($subscription->price, $subscription->company) }}--}} -

+ Select a payment method: + {{--

--}} + {{-- {{ ctrans('texts.total') }}: {{ \App\Utils\Number::formatMoney($total, $subscription->company) }}--}} + {{-- {{ \App\Utils\Number::formatMoney($subscription->price, $subscription->company) }}--}} + {{--

--}}
- + @if($state['invoice']) +
+ @csrf + + @if($state['invoice'] instanceof \App\Models\Invoice) + + + + @endif + + + + +
+ @endif + +
- Select a payment method:
- @foreach($this->methods as $method) - - @endforeach + + @if(!$state['payment_initialised']) + @foreach($this->methods as $method) + + @endforeach + @endif + + @if($state['show_loading_bar']) +
+ + + + +
+ @endif
diff --git a/resources/views/portal/ninja2020/recurring_invoices/show.blade.php b/resources/views/portal/ninja2020/recurring_invoices/show.blade.php index d61a011f01..e7a64b07ae 100644 --- a/resources/views/portal/ninja2020/recurring_invoices/show.blade.php +++ b/resources/views/portal/ninja2020/recurring_invoices/show.blade.php @@ -92,7 +92,7 @@
@foreach($invoice->subscription->service()->getPlans() as $subscription) - {{ $subscription->name }} + {{ $subscription->name }} @endforeach
diff --git a/resources/views/portal/ninja2020/subscriptions/switch.blade.php b/resources/views/portal/ninja2020/subscriptions/switch.blade.php index 1e36648cae..3cb6b87c51 100644 --- a/resources/views/portal/ninja2020/subscriptions/switch.blade.php +++ b/resources/views/portal/ninja2020/subscriptions/switch.blade.php @@ -42,6 +42,12 @@ - @livewire('subscription-plan-switch', compact('subscription', 'target_subscription', 'contact')) + @livewire('subscription-plan-switch', compact('subscription', 'target', 'contact')) @endsection + +@push('footer') + +@endpush diff --git a/routes/client.php b/routes/client.php index 78bb546556..32ba6eb1b9 100644 --- a/routes/client.php +++ b/routes/client.php @@ -72,7 +72,7 @@ Route::group(['middleware' => ['auth:contact', 'locale', 'check_client_existence Route::get('documents/{document}/download', 'ClientPortal\DocumentController@download')->name('documents.download'); Route::resource('documents', 'ClientPortal\DocumentController')->only(['index', 'show']); - Route::get('subscriptions/{subscription}/plan_switch/{target_subscription}', 'ClientPortal\SubscriptionPlanSwitchController@index')->name('subscription.plan_switch'); + Route::get('subscriptions/{subscription}/plan_switch/{target}', 'ClientPortal\SubscriptionPlanSwitchController@index')->name('subscription.plan_switch'); Route::resource('subscriptions', 'ClientPortal\SubscriptionController')->only(['index']); From c0f57801725f33f6d2b3e98b80bf864dd5c2c78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Thu, 8 Apr 2021 17:11:14 +0200 Subject: [PATCH 2/8] wip --- .../Livewire/RecurringInvoiceCancellation.php | 38 +++++++++++++++++++ .../Subscription/SubscriptionService.php | 2 + .../recurring-invoice-cancellation.blade.php | 3 ++ .../includes/modals/cancellation.blade.php | 8 ++-- .../recurring_invoices/show.blade.php | 2 +- 5 files changed, 47 insertions(+), 6 deletions(-) create mode 100644 app/Http/Livewire/RecurringInvoiceCancellation.php create mode 100644 resources/views/portal/ninja2020/components/livewire/recurring-invoice-cancellation.blade.php diff --git a/app/Http/Livewire/RecurringInvoiceCancellation.php b/app/Http/Livewire/RecurringInvoiceCancellation.php new file mode 100644 index 0000000000..4e03e3b097 --- /dev/null +++ b/app/Http/Livewire/RecurringInvoiceCancellation.php @@ -0,0 +1,38 @@ +invoice->subscription) { + return $this->invoice->subscription->service()->handleCancellation(); + } + + return redirect()->route('client.recurring_invoices.request_cancellation', ['recurring_invoice' => $this->invoice->hashed_id]); + } + + public function render() + { + return render('components.livewire.recurring-invoice-cancellation'); + } +} diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index 5734e23634..ec08670656 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -317,6 +317,8 @@ class SubscriptionService public function handleCancellation() { + dd('Cancelling using SubscriptionService'); + // .. } diff --git a/resources/views/portal/ninja2020/components/livewire/recurring-invoice-cancellation.blade.php b/resources/views/portal/ninja2020/components/livewire/recurring-invoice-cancellation.blade.php new file mode 100644 index 0000000000..f133a0a8ec --- /dev/null +++ b/resources/views/portal/ninja2020/components/livewire/recurring-invoice-cancellation.blade.php @@ -0,0 +1,3 @@ + diff --git a/resources/views/portal/ninja2020/recurring_invoices/includes/modals/cancellation.blade.php b/resources/views/portal/ninja2020/recurring_invoices/includes/modals/cancellation.blade.php index df1b98e6fd..c75064b298 100644 --- a/resources/views/portal/ninja2020/recurring_invoices/includes/modals/cancellation.blade.php +++ b/resources/views/portal/ninja2020/recurring_invoices/includes/modals/cancellation.blade.php @@ -1,4 +1,5 @@ -