diff --git a/app/Http/Livewire/RequiredClientInfo.php b/app/Http/Livewire/RequiredClientInfo.php new file mode 100644 index 000000000000..c68b1ef35416 --- /dev/null +++ b/app/Http/Livewire/RequiredClientInfo.php @@ -0,0 +1,148 @@ + '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'); + } +} diff --git a/app/PaymentDrivers/AuthorizePaymentDriver.php b/app/PaymentDrivers/AuthorizePaymentDriver.php index a95fe0705568..3f516c9f81a9 100644 --- a/app/PaymentDrivers/AuthorizePaymentDriver.php +++ b/app/PaymentDrivers/AuthorizePaymentDriver.php @@ -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(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 168d96e435e5..d99ca01a11c0 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/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 04a82cb15eeb..726dac89b8d4 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -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); } diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php index 9a18837687ec..387b29c1ce9a 100644 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ b/app/PaymentDrivers/PayPalExpressPaymentDriver.php @@ -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 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/lang/en/texts.php b/resources/lang/en/texts.php index 9d67109ff33b..d6829802ec1f 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -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.', ]; 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 new file mode 100644 index 000000000000..d686b3d3f747 --- /dev/null +++ b/resources/views/portal/ninja2020/components/livewire/required-client-info.blade.php @@ -0,0 +1,42 @@ +
+ {{ ctrans('texts.required_payment_information_more') }} +
+