From 6f5a08f87d03c03f29814bd0b3ce5307f35d1c33 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 2 Jul 2024 14:54:50 +1000 Subject: [PATCH] new payment flow --- app/Livewire/Flow2/PaymentMethod.php | 41 +++++++++--------- app/Livewire/Flow2/ProcessPayment.php | 29 ++++++------- app/Livewire/Flow2/RequiredFields.php | 30 +++++++++++++ app/Livewire/Flow2/UnderOverPayment.php | 42 +++++++++++++++++++ app/Livewire/InvoicePay.php | 36 +++++++++++++++- .../livewire/payment_method-flow2.blade.php | 4 ++ .../livewire/required-fields.blade.php | 3 ++ .../livewire/under-over-payments.blade.php | 37 ++++++++++++++++ 8 files changed, 181 insertions(+), 41 deletions(-) create mode 100644 app/Livewire/Flow2/RequiredFields.php create mode 100644 app/Livewire/Flow2/UnderOverPayment.php create mode 100644 resources/views/portal/ninja2020/components/livewire/required-fields.blade.php create mode 100644 resources/views/portal/ninja2020/components/livewire/under-over-payments.blade.php diff --git a/app/Livewire/Flow2/PaymentMethod.php b/app/Livewire/Flow2/PaymentMethod.php index 2879b6cb91c6..5af240929066 100644 --- a/app/Livewire/Flow2/PaymentMethod.php +++ b/app/Livewire/Flow2/PaymentMethod.php @@ -25,23 +25,23 @@ class PaymentMethod extends Component public $methods = []; - public $isLoading = true; + public $isLoading = false; - public function placeholder() - { - return <<<'HTML' -
- - - - -
- HTML; - } + // public function placeholder() + // { + // return <<<'HTML' + //
+ // + // + // + // + //
+ // HTML; + // } public function mount() { - $this->isLoading = true; + // $this->isLoading = true; $this->invoice = $this->context['invoice']; $this->variables = $this->context['variables']; @@ -49,20 +49,17 @@ class PaymentMethod extends Component $this->methods = $this->invoice->client->service()->getPaymentMethods($this->invoice->balance); + // $this->isLoading = false; + // $this->dispatch('loadingCompleted'); + if(count($this->methods) == 1) { - $this->dispatch('payment-method-selected', company_gateway_id: $this->methods[0]['company_gateway_id'], gateway_type_id: $this->methods[0]['gateway_type_id'], amount: $this->invoice->balance); + $this->dispatch('singlePaymentMethodFound', company_gateway_id: $this->methods[0]['company_gateway_id'], gateway_type_id: $this->methods[0]['gateway_type_id'], amount: $this->invoice->balance); } - else { - $this->isLoading = false; - $this->dispatch('loadingCompleted'); - } - + } - public function render() - { - + { //If there is only one payment method, skip display and push straight to the form!! return render('components.livewire.payment_method-flow2', ['methods' => $this->methods, 'amount' => $this->invoice->balance]); } diff --git a/app/Livewire/Flow2/ProcessPayment.php b/app/Livewire/Flow2/ProcessPayment.php index febffee97b21..fe2375464968 100644 --- a/app/Livewire/Flow2/ProcessPayment.php +++ b/app/Livewire/Flow2/ProcessPayment.php @@ -31,20 +31,9 @@ class ProcessPayment extends Component public $isLoading = true; - - // public function toJSON() - // { - // nlog("why"); - // } - public function mount() { - // $this->loadData(); - } - - public function boot() - { - + MultiDB::setDb($this->context['invoice']->company->db); $invitation = InvoiceInvitation::find($this->context['invitation_id']); @@ -65,11 +54,12 @@ class ProcessPayment extends Component $responder_data = (new LivewireInstantPayment($data))->run(); $company_gateway = CompanyGateway::find($this->context['company_gateway_id']); - + $this->component_view = ''; - if(!$responder_data['success']) + if(!$responder_data['success']) { throw new PaymentFailed($responder_data['error'], 400); + } $driver = $company_gateway ->driver($invitation->contact->client) @@ -92,7 +82,7 @@ class ProcessPayment extends Component } $payment_data['token_billing_string'] = $token_billing_string; - + $this->payment_data_payload = $payment_data; // $this->payment_data_payload['company_gateway'] = $company_gateway; @@ -116,10 +106,15 @@ class ProcessPayment extends Component 'company_gateway' => $this->payment_data_payload['company_gateway'], ]; - // nlog(array_keys($this->payment_data_payload)); - + $this->isLoading = false; + } + + public function boot() + { + + } public function render() diff --git a/app/Livewire/Flow2/RequiredFields.php b/app/Livewire/Flow2/RequiredFields.php new file mode 100644 index 000000000000..3f4bd9a05872 --- /dev/null +++ b/app/Livewire/Flow2/RequiredFields.php @@ -0,0 +1,30 @@ +context['invoice']; + $invoice_amount = $invoice->partial > 0 ? $invoice->partial : $invoice->balance; + $this->currency = $invoice->client->currency(); + $this->payableAmount = Number::formatValue($invoice_amount, $this->currency); + } + + public function render() + { + + return render('components.livewire.under-over-payments',[ + 'settings' => $this->context['settings'], + ]); + } +} diff --git a/app/Livewire/InvoicePay.php b/app/Livewire/InvoicePay.php index 0ae0d0db36b8..336859bfaacc 100644 --- a/app/Livewire/InvoicePay.php +++ b/app/Livewire/InvoicePay.php @@ -22,6 +22,8 @@ use Livewire\Attributes\Computed; use Livewire\Attributes\Reactive; use App\Livewire\Flow2\PaymentMethod; use App\Livewire\Flow2\ProcessPayment; +use App\Livewire\Flow2\UnderOverPayment; +use App\Utils\Number; class InvoicePay extends Component { @@ -37,6 +39,12 @@ class InvoicePay extends Component public $payment_method_accepted = false; + public $under_over_payment = false; + + public $required_fields = false; + + public $ready = true; + public array $context = []; #[On('update.context')] @@ -70,9 +78,17 @@ class InvoicePay extends Component } + #[On('payable-amount')] + public function payableAmount($payable_amount) + { + $this->context['payable_invoices'][0]['amount'] = Number::parseFloat($payable_amount); + $this->under_over_payment = false; + } + #[On('payment-method-selected')] public function paymentMethodSelected($company_gateway_id, $gateway_type_id, $amount) { + $this->context['company_gateway_id'] = $company_gateway_id; $this->context['gateway_type_id'] = $gateway_type_id; $this->context['amount'] = $amount; @@ -83,6 +99,8 @@ class InvoicePay extends Component // $this->invite = \App\Models\InvoiceInvitation::withTrashed()->find($this->invitation_id)->withoutRelations(); $this->payment_method_accepted =true; + + } #[Computed()] @@ -94,10 +112,14 @@ class InvoicePay extends Component if(!$this->signature_accepted) return Signature::class; + if($this->under_over_payment) + return UnderOverPayment::class; + if(!$this->payment_method_accepted) return PaymentMethod::class; - return ProcessPayment::class; + // if($this->ready) + return ProcessPayment::class; } #[Computed()] @@ -116,13 +138,23 @@ class InvoicePay extends Component $client = $invite->contact->client; $variables = ($invite && auth()->guard('contact')->user()->client->getSetting('show_accept_invoice_terms')) ? (new HtmlEngine($invite))->generateLabelsAndValues() : false; $settings = $client->getMergedSettings(); + $this->context['settings'] = $settings; + + //under-over / payment + + //required fields $this->terms_accepted = !$settings->show_accept_invoice_terms; $this->signature_accepted = !$settings->require_invoice_signature; - + $this->under_over_payment = $settings->client_portal_allow_over_payment || $settings->client_portal_allow_under_payment; + $this->required_fields = false; + $this->context['variables'] = $variables; $this->context['invoice'] = $invite->invoice; $this->context['settings'] = $settings; + + $this->context['payable_invoices'] = ['invoice_id' => $this->context['invoice']->hashed_id, 'amount' => $invite->invoice->partial > 0 ? $invite->invoice->partial : $invite->invoice->balance]; + } public function render() diff --git a/resources/views/portal/ninja2020/components/livewire/payment_method-flow2.blade.php b/resources/views/portal/ninja2020/components/livewire/payment_method-flow2.blade.php index 09afec9c68a0..249c7d120e01 100644 --- a/resources/views/portal/ninja2020/components/livewire/payment_method-flow2.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/payment_method-flow2.blade.php @@ -29,6 +29,10 @@ isLoading = false; }); + Livewire.on('singlePaymentMethodFound', (event) => { + $wire.dispatch('payment-method-selected', {company_gateway_id: event.company_gateway_id, gateway_type_id: event.gateway_type_id, amount: event.amount }) + }); + const buttons = document.querySelectorAll('.payment-method'); buttons.forEach(button => { diff --git a/resources/views/portal/ninja2020/components/livewire/required-fields.blade.php b/resources/views/portal/ninja2020/components/livewire/required-fields.blade.php new file mode 100644 index 000000000000..42eb8c531dbf --- /dev/null +++ b/resources/views/portal/ninja2020/components/livewire/required-fields.blade.php @@ -0,0 +1,3 @@ +
+ +
\ No newline at end of file diff --git a/resources/views/portal/ninja2020/components/livewire/under-over-payments.blade.php b/resources/views/portal/ninja2020/components/livewire/under-over-payments.blade.php new file mode 100644 index 000000000000..6001075a453e --- /dev/null +++ b/resources/views/portal/ninja2020/components/livewire/under-over-payments.blade.php @@ -0,0 +1,37 @@ +
+ +
+ {{ ctrans('texts.payment_amount') }} +
+
+ +
+ +
+ + @if($settings->client_portal_allow_under_payment) + {{ ctrans('texts.minimum_payment') }}: {{ $settings->client_portal_under_payment_minimum }} + @endif +
+ +
+ +
+ +
\ No newline at end of file