diff --git a/app/Http/Livewire/RequiredClientInfo.php b/app/Http/Livewire/RequiredClientInfo.php index a19e51d64368..c325704ec5dd 100644 --- a/app/Http/Livewire/RequiredClientInfo.php +++ b/app/Http/Livewire/RequiredClientInfo.php @@ -27,14 +27,6 @@ class RequiredClientInfo extends Component */ public $contact; - - /** - * Instance of payment gateway. Used for getting the required fields. - * - * @var mixed - */ - private $gateway; - /** * Mappings for updating the database. Left side is mapping from gateway, * right side is column in database. @@ -66,6 +58,8 @@ class RequiredClientInfo extends Component 'contact_phone' => 'phone', ]; + public $show_form = true; + public function handleSubmit(array $data): bool { $rules = []; @@ -124,24 +118,28 @@ class RequiredClientInfo extends Component 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() { - // This will be coming from the gateway itself. Something like $gateway->getRequiredRules(); - - $this->fields = [ - [ - 'name' => 'client_name', - 'label' => ctrans('texts.name'), - 'type' => 'text', - 'validation_rules' => 'required|min:3' - ], - [ - 'name' => 'contact_phone', - 'label' => ctrans('texts.phone'), - 'type' => 'number', - 'validation_rules' => 'required|min:2', - ], - ]; + $this->checkFields(); return render('components.livewire.required-client-info'); } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 8366c4f5f627..4a15e185aafc 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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. * diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 12b0bf766e5e..e243be301c6e 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -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); } diff --git a/resources/views/portal/ninja2020/components/livewire/required-client-info.blade.php b/resources/views/portal/ninja2020/components/livewire/required-client-info.blade.php index 6fc5543b5e26..d686b3d3f747 100644 --- a/resources/views/portal/ninja2020/components/livewire/required-client-info.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/required-client-info.blade.php @@ -30,4 +30,13 @@ @endcomponent + + @if(!$show_form) + + @endif diff --git a/resources/views/portal/ninja2020/layout/payments.blade.php b/resources/views/portal/ninja2020/layout/payments.blade.php index 34c0e9df2527..7209b47184d7 100644 --- a/resources/views/portal/ninja2020/layout/payments.blade.php +++ b/resources/views/portal/ninja2020/layout/payments.blade.php @@ -11,7 +11,7 @@ @endpush @section('body') - @livewire('required-client-info', ['gateway' => $gateway, 'contact' => auth('contact')->user()]) + @livewire('required-client-info', ['fields' => $gateway->getClientRequiredFields(), 'contact' => auth('contact')->user()])