From 7429d32072f6994f2c188a932ec1c8b13414d5ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gro=C3=9Fe?= Date: Fri, 10 Jan 2020 14:07:58 +0100 Subject: [PATCH] introduce VatValidator.php --- .../Http/Controllers/AddressController.php | 34 ++++--- .../Webkul/Customer/src/Rules/VatIdRule.php | 52 ++++++++++ .../Customer/src/Rules/VatValidator.php | 95 +++++++++++++++++++ .../Webkul/Shop/src/Resources/lang/en/app.php | 1 + tests/functional/Customer/CustomerCest.php | 8 ++ 5 files changed, 175 insertions(+), 15 deletions(-) create mode 100644 packages/Webkul/Customer/src/Rules/VatIdRule.php create mode 100644 packages/Webkul/Customer/src/Rules/VatValidator.php diff --git a/packages/Webkul/Customer/src/Http/Controllers/AddressController.php b/packages/Webkul/Customer/src/Http/Controllers/AddressController.php index b7fefec45..bbe9d90a7 100755 --- a/packages/Webkul/Customer/src/Http/Controllers/AddressController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/AddressController.php @@ -5,6 +5,7 @@ namespace Webkul\Customer\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use Webkul\Customer\Repositories\CustomerAddressRepository; +use Webkul\Customer\Rules\VatIdRule; use Auth; /** @@ -26,7 +27,8 @@ class AddressController extends Controller /** * CustomerAddressRepository object * - * @param \Webkul\Customer\Repositories\CustomerAddressRepository $customerAddressRepository + * @param \Webkul\Customer\Repositories\CustomerAddressRepository $customerAddressRepository + * * @var Object */ protected $customerAddressRepository; @@ -75,11 +77,12 @@ class AddressController extends Controller $this->validate(request(), [ 'address1' => 'string|required', - 'country' => 'string|required', - 'state' => 'string|required', - 'city' => 'string|required', + 'country' => 'string|required', + 'state' => 'string|required', + 'city' => 'string|required', 'postcode' => 'required', - 'phone' => 'required' + 'phone' => 'required', + 'vat_id' => new VatIdRule(), ]); $cust_id['customer_id'] = $this->customer->id; @@ -108,8 +111,8 @@ class AddressController extends Controller public function edit($id) { $address = $this->customerAddressRepository->findOneWhere([ - 'id' => $id, - 'customer_id' => auth()->guard('customer')->user()->id + 'id' => $id, + 'customer_id' => auth()->guard('customer')->user()->id, ]); if (! $address) @@ -130,18 +133,18 @@ class AddressController extends Controller $this->validate(request(), [ 'address1' => 'string|required', - 'country' => 'string|required', - 'state' => 'string|required', - 'city' => 'string|required', + 'country' => 'string|required', + 'state' => 'string|required', + 'city' => 'string|required', 'postcode' => 'required', - 'phone' => 'required' + 'phone' => 'required', ]); $data = collect(request()->input())->except('_token')->toArray(); $addresses = $this->customer->addresses; - foreach($addresses as $address) { + foreach ($addresses as $address) { if ($id == $address->id) { session()->flash('success', trans('shop::app.customer.account.address.edit.success')); @@ -157,7 +160,8 @@ class AddressController extends Controller } /** - * To change the default address or make the default address, by default when first address is created will be the default address + * To change the default address or make the default address, by default when first address is + * created will be the default address * * @return Response */ @@ -185,8 +189,8 @@ class AddressController extends Controller public function destroy($id) { $address = $this->customerAddressRepository->findOneWhere([ - 'id' => $id, - 'customer_id' => auth()->guard('customer')->user()->id + 'id' => $id, + 'customer_id' => auth()->guard('customer')->user()->id, ]); if (! $address) diff --git a/packages/Webkul/Customer/src/Rules/VatIdRule.php b/packages/Webkul/Customer/src/Rules/VatIdRule.php new file mode 100644 index 000000000..0098a32f5 --- /dev/null +++ b/packages/Webkul/Customer/src/Rules/VatIdRule.php @@ -0,0 +1,52 @@ +validate($value); + } + + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return trans('shop::app.invalid_vat_format'); + } +} diff --git a/packages/Webkul/Customer/src/Rules/VatValidator.php b/packages/Webkul/Customer/src/Rules/VatValidator.php new file mode 100644 index 000000000..92245f08b --- /dev/null +++ b/packages/Webkul/Customer/src/Rules/VatValidator.php @@ -0,0 +1,95 @@ + 'U[A-Z\d]{8}', + 'BE' => '(0\d{9}|\d{10})', + 'BG' => '\d{9,10}', + 'CY' => '\d{8}[A-Z]', + 'CZ' => '\d{8,10}', + 'DE' => '\d{9}', + 'DK' => '(\d{2} ?){3}\d{2}', + 'EE' => '\d{9}', + 'EL' => '\d{9}', + 'ES' => '[A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8}', + 'FI' => '\d{8}', + 'FR' => '([A-Z]{2}|\d{2})\d{9}', + 'GB' => '\d{9}|\d{12}|(GD|HA)\d{3}', + 'HR' => '\d{11}', + 'HU' => '\d{8}', + 'IE' => '[A-Z\d]{8}|[A-Z\d]{9}', + 'IT' => '\d{11}', + 'LT' => '(\d{9}|\d{12})', + 'LU' => '\d{8}', + 'LV' => '\d{11}', + 'MT' => '\d{8}', + 'NL' => '\d{9}B\d{2}', + 'PL' => '\d{10}', + 'PT' => '\d{9}', + 'RO' => '\d{2,10}', + 'SE' => '\d{12}', + 'SI' => '\d{8}', + 'SK' => '\d{10}', + ); + + /** + * Validate a VAT number format. + * + * @param string $vatNumber + * + * @return boolean + */ + public function validate(string $vatNumber): bool + { + $vatNumber = $this->vatCleaner($vatNumber); + list($country, $number) = $this->splitVat($vatNumber); + + if (! isset(self::$pattern_expression[$country])) { + return false; + } + + return preg_match('/^' . self::$pattern_expression[$country] . '$/', $number) > 0; + } + + /** + * @param string $vatNumber + * + * @return string + */ + private function vatCleaner(string $vatNumber): string + { + $vatNumber_no_spaces = trim($vatNumber); + return strtoupper($vatNumber_no_spaces); + } + + /** + * @param string $vatNumber + * + * @return array + */ + private function splitVat(string $vatNumber): array + { + return [ + substr($vatNumber, 0, 2), + substr($vatNumber, 2), + ]; + } +} diff --git a/packages/Webkul/Shop/src/Resources/lang/en/app.php b/packages/Webkul/Shop/src/Resources/lang/en/app.php index c742af1e7..ad160f423 100755 --- a/packages/Webkul/Shop/src/Resources/lang/en/app.php +++ b/packages/Webkul/Shop/src/Resources/lang/en/app.php @@ -1,6 +1,7 @@ 'The given vat id has a wrong format', 'security-warning' => 'Suspicious activity found!!!', 'nothing-to-delete' => 'Nothing to delete', diff --git a/tests/functional/Customer/CustomerCest.php b/tests/functional/Customer/CustomerCest.php index d6715eb0e..09ab339e5 100644 --- a/tests/functional/Customer/CustomerCest.php +++ b/tests/functional/Customer/CustomerCest.php @@ -77,6 +77,14 @@ class CustomerCest // we need to use this css selector to hit the correct
. There is another one at the // page header (search) $I->submitForm($formCssSelector, $fields); + $I->seeInSource('The given vat id has a wrong format'); + + // valid vat id: + $fields['vat_id'] = 'DE123456789'; + + $I->submitForm($formCssSelector, $fields); + + $I->seeInSource('Address have been successfully added.'); $I->seeInSource('Address have been successfully added.');