mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Merge pull request #4670 from beganovich/v5-jan12-required-fields
(v5) Required fields for gateways
This commit is contained in:
commit
ee85a6487c
148
app/Http/Livewire/RequiredClientInfo.php
Normal file
148
app/Http/Livewire/RequiredClientInfo.php
Normal file
@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
|
||||
namespace App\Http\Livewire;
|
||||
|
||||
use App\Models\ClientContact;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Livewire\Component;
|
||||
|
||||
class RequiredClientInfo extends Component
|
||||
{
|
||||
public $fields = [];
|
||||
|
||||
/**
|
||||
* @var ClientContact
|
||||
*/
|
||||
public $contact;
|
||||
|
||||
/**
|
||||
* Mappings for updating the database. Left side is mapping from gateway,
|
||||
* right side is column in database.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $mappings = [
|
||||
'client_name' => 'name',
|
||||
'client_website' => 'website',
|
||||
'client_phone' => 'phone',
|
||||
|
||||
'client_address_line_1' => 'address1',
|
||||
'client_address_line_2' => 'address2',
|
||||
'client_city' => 'city',
|
||||
'client_state' => 'state',
|
||||
'client_postal_code' => 'postal_code',
|
||||
'client_country_id' => 'country_id',
|
||||
|
||||
'client_shipping_address_line_1' => 'shipping_address1',
|
||||
'client_shipping_address_line_2' => 'shipping_address2',
|
||||
'client_shipping_city' => 'shipping_city',
|
||||
'client_shipping_state' => 'shipping_state',
|
||||
'client_shipping_postal_code' => 'shipping_postal_code',
|
||||
'client_shipping_country_id' => 'shipping_country_id',
|
||||
|
||||
'contact_first_name' => 'first_name',
|
||||
'contact_last_name' => 'last_name',
|
||||
'contact_email' => 'email',
|
||||
'contact_phone' => 'phone',
|
||||
];
|
||||
|
||||
public $show_form = true;
|
||||
|
||||
public function handleSubmit(array $data): bool
|
||||
{
|
||||
$rules = [];
|
||||
|
||||
collect($this->fields)->map(function ($field) use (&$rules) {
|
||||
$rules[$field['name']] = array_key_exists('validation_rules', $field)
|
||||
? $field['validation_rules']
|
||||
: 'required';
|
||||
});
|
||||
|
||||
$validator = Validator::make($data, $rules);
|
||||
|
||||
if ($validator->fails()) {
|
||||
session()->flash('validation_errors', $validator->getMessageBag()->getMessages());
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->updateClientDetails($data)) {
|
||||
$this->emit('passed-required-fields-check');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: Throw an exception about not being able to update the profile.
|
||||
return false;
|
||||
}
|
||||
|
||||
private function updateClientDetails(array $data): bool
|
||||
{
|
||||
$client = [];
|
||||
$contact = [];
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (Str::startsWith($field, 'client_')) {
|
||||
$client[$this->mappings[$field]] = $value;
|
||||
}
|
||||
|
||||
if (Str::startsWith($field, 'contact_')) {
|
||||
$contact[$this->mappings[$field]] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$contact_update = $this->contact
|
||||
->fill($contact)
|
||||
->push();
|
||||
|
||||
$client_update = $this->contact->client
|
||||
->fill($client)
|
||||
->push();
|
||||
|
||||
if ($contact_update && $client_update) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function checkFields()
|
||||
{
|
||||
foreach ($this->fields as $field) {
|
||||
$_field = $this->mappings[$field['name']];
|
||||
|
||||
if (Str::startsWith($field['name'], 'client_')) {
|
||||
(empty($this->contact->client->{$_field}) || is_null($this->contact->client->{$_field}))
|
||||
? $this->show_form = true
|
||||
: $this->show_form = false;
|
||||
}
|
||||
|
||||
if (Str::startsWith($field['name'], 'contact_')) {
|
||||
(empty($this->contact->{$_field}) || is_null($this->contact->{$_field}))
|
||||
? $this->show_form = true
|
||||
: $this->show_form = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
count($this->fields) > 0
|
||||
? $this->checkFields()
|
||||
: $this->show_form = false;
|
||||
|
||||
return render('components.livewire.required-client-info');
|
||||
}
|
||||
}
|
@ -63,6 +63,14 @@ class AuthorizePaymentDriver extends BaseDriver
|
||||
return $types;
|
||||
}
|
||||
|
||||
public function getClientRequiredFields(): array
|
||||
{
|
||||
return [
|
||||
['name' => 'client_name', 'label' => ctrans('texts.name'), 'type' => 'text', 'validation' => 'required|min:2'],
|
||||
['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required|email:rfc'],
|
||||
];
|
||||
}
|
||||
|
||||
public function authorizeView($payment_method)
|
||||
{
|
||||
return (new AuthorizePaymentMethod($this))->authorizeView();
|
||||
|
@ -69,7 +69,7 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
|
||||
/* Array of payment methods */
|
||||
public static $methods = [];
|
||||
|
||||
|
||||
/** @var array */
|
||||
public $required_fields = [];
|
||||
|
||||
@ -80,6 +80,16 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Required fields for client to fill, to proceed with gateway actions.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function getClientRequiredFields(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorize a payment method.
|
||||
*
|
||||
|
@ -121,23 +121,11 @@ class CheckoutComPaymentDriver extends BaseDriver
|
||||
|
||||
public function authorizeView($data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->authorizeView($data);
|
||||
}
|
||||
|
||||
public function authorizeResponse($data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->authorizeResponse($data);
|
||||
}
|
||||
|
||||
@ -149,12 +137,6 @@ class CheckoutComPaymentDriver extends BaseDriver
|
||||
*/
|
||||
public function processPaymentView(array $data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->paymentView($data);
|
||||
}
|
||||
|
||||
@ -166,12 +148,6 @@ class CheckoutComPaymentDriver extends BaseDriver
|
||||
*/
|
||||
public function processPaymentResponse($request)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->paymentResponse($request);
|
||||
}
|
||||
|
||||
|
@ -80,12 +80,6 @@ class PayPalExpressPaymentDriver extends BaseDriver
|
||||
|
||||
public function processPaymentView($data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
$this->initializeOmnipayGateway();
|
||||
|
||||
$this->payment_hash->data = array_merge((array) $this->payment_hash->data, ['amount' => $data['total']['amount_with_fee']]);
|
||||
@ -120,12 +114,6 @@ class PayPalExpressPaymentDriver extends BaseDriver
|
||||
|
||||
public function processPaymentResponse($request)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
$this->initializeOmnipayGateway();
|
||||
|
||||
$response = $this->omnipay_gateway
|
||||
|
@ -149,6 +149,13 @@ class StripePaymentDriver extends BaseDriver
|
||||
}
|
||||
}
|
||||
|
||||
public function getClientRequiredFields(): array
|
||||
{
|
||||
return [
|
||||
['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Proxy method to pass the data into payment method authorizeView().
|
||||
*
|
||||
@ -157,12 +164,6 @@ class StripePaymentDriver extends BaseDriver
|
||||
*/
|
||||
public function authorizeView(array $data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->authorizeView($data);
|
||||
}
|
||||
|
||||
@ -174,12 +175,6 @@ class StripePaymentDriver extends BaseDriver
|
||||
*/
|
||||
public function authorizeResponse($request)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->authorizeResponse($request);
|
||||
}
|
||||
|
||||
@ -191,23 +186,11 @@ class StripePaymentDriver extends BaseDriver
|
||||
*/
|
||||
public function processPaymentView(array $data)
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->paymentView($data);
|
||||
}
|
||||
|
||||
public function processPaymentResponse($request) //We never have to worry about unsuccessful payments as failures are handled at the front end for this driver.
|
||||
{
|
||||
if (count($this->required_fields) > 0) {
|
||||
return redirect()
|
||||
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
|
||||
->with('missing_required_fields', $this->required_fields);
|
||||
}
|
||||
|
||||
return $this->payment_method->paymentResponse($request);
|
||||
}
|
||||
|
||||
|
@ -3366,4 +3366,7 @@ return [
|
||||
|
||||
'no_action_provided' => 'No action provided. If you believe this is wrong, please contact the support.',
|
||||
'no_payable_invoices_selected' => 'No payable invoices selected. Make sure you are not trying to pay draft invoice or invoice with zero balance due.',
|
||||
|
||||
'required_payment_information' => 'Required payment details',
|
||||
'required_payment_information_more' => 'To complete a payment we need more details about you.',
|
||||
];
|
||||
|
@ -0,0 +1,42 @@
|
||||
<div class="container mx-auto grid grid-cols-12" data-ref="required-fields-container">
|
||||
<div class="col-span-12 lg:col-span-6 lg:col-start-4 overflow-hidden bg-white shadow rounded-lg">
|
||||
<div class="px-4 py-5 border-b border-gray-200 sm:px-6">
|
||||
<h3 class="text-lg font-medium leading-6 text-gray-900">
|
||||
{{ ctrans('texts.required_payment_information') }}
|
||||
</h3>
|
||||
|
||||
<p class="max-w-2xl mt-1 text-sm leading-5 text-gray-500">
|
||||
{{ ctrans('texts.required_payment_information_more') }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<form wire:submit.prevent="handleSubmit(Object.fromEntries(new FormData($event.target)))">
|
||||
@foreach($fields as $field)
|
||||
@component('portal.ninja2020.components.general.card-element', ['title' => $field['label']])
|
||||
<input class="input w-full" type="{{ $field['type'] }}" name="{{ $field['name'] }}">
|
||||
|
||||
@if(session()->has('validation_errors') && array_key_exists($field['name'], session('validation_errors')))
|
||||
<p class="mt-2 text-gray-900 border-red-300 px-2 py-1 bg-gray-100">{{ session('validation_errors')[$field['name']][0] }}</p>
|
||||
@endif
|
||||
@endcomponent
|
||||
@endforeach
|
||||
|
||||
@component('portal.ninja2020.components.general.card-element-single')
|
||||
<div class="flex justify-end">
|
||||
<button class="button button-primary bg-primary">
|
||||
{{ trans('texts.next_step') }}
|
||||
</button>
|
||||
</div>
|
||||
@endcomponent
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@if(!$show_form)
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
document.querySelector('div[data-ref="required-fields-container"]').classList.add('hidden');
|
||||
document.querySelector('div[data-ref="gateway-container"]').classList.remove('hidden');
|
||||
});
|
||||
</script>
|
||||
@endif
|
||||
</div>
|
@ -11,7 +11,9 @@
|
||||
@endpush
|
||||
|
||||
@section('body')
|
||||
<div class="container mx-auto grid grid-cols-12">
|
||||
@livewire('required-client-info', ['fields' => $gateway->getClientRequiredFields(), 'contact' => auth('contact')->user()])
|
||||
|
||||
<div class="container mx-auto grid grid-cols-12 hidden" data-ref="gateway-container">
|
||||
<div class="col-span-12 lg:col-span-6 lg:col-start-4 overflow-hidden bg-white shadow rounded-lg">
|
||||
<div class="px-4 py-5 border-b border-gray-200 sm:px-6">
|
||||
@isset($card_title)
|
||||
@ -19,6 +21,7 @@
|
||||
{{ $card_title }}
|
||||
</h3>
|
||||
@endisset
|
||||
|
||||
@isset($card_description)
|
||||
<p class="max-w-2xl mt-1 text-sm leading-5 text-gray-500">
|
||||
{{ $card_description }}
|
||||
@ -34,4 +37,11 @@
|
||||
|
||||
@push('footer')
|
||||
@yield('gateway_footer')
|
||||
|
||||
<script>
|
||||
Livewire.on('passed-required-fields-check', () => {
|
||||
document.querySelector('div[data-ref="required-fields-container"]').classList.add('hidden');
|
||||
document.querySelector('div[data-ref="gateway-container"]').classList.remove('hidden');
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
Loading…
x
Reference in New Issue
Block a user