introduce VatValidator.php
This commit is contained in:
parent
98632b0de8
commit
7429d32072
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Customer\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use Webkul\Customer\Rules\VatValidator;
|
||||
|
||||
/**
|
||||
* Class VatIdRule
|
||||
*
|
||||
* @see https://laravel.com/docs/5.8/validation#using-rule-objects
|
||||
* @package App\Rules
|
||||
*/
|
||||
class VatIdRule implements Rule
|
||||
{
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* The rules are borrowed from:
|
||||
* @see https://raw.githubusercontent.com/danielebarbaro/laravel-vat-eu-validator/master/src/VatValidator.php
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$validator = new VatValidator();
|
||||
|
||||
return $validator->validate($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return trans('shop::app.invalid_vat_format');
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Webkul\Customer\Rules;
|
||||
|
||||
/**
|
||||
* Class VatValidator
|
||||
*
|
||||
* This class is borrowed from:
|
||||
*
|
||||
* @see https://raw.githubusercontent.com/danielebarbaro/laravel-vat-eu-validator
|
||||
*
|
||||
* @package Danielebarbaro\LaravelVatEuValidator
|
||||
*/
|
||||
class VatValidator
|
||||
{
|
||||
/**
|
||||
* Regular expression patterns per country code
|
||||
*
|
||||
* @var array
|
||||
* @link http://ec.europa.eu/taxation_customs/vies/faq.html?locale=en#item_11
|
||||
*/
|
||||
protected static $pattern_expression = array(
|
||||
'AT' => '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),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'invalid_vat_format' => 'The given vat id has a wrong format',
|
||||
'security-warning' => 'Suspicious activity found!!!',
|
||||
'nothing-to-delete' => 'Nothing to delete',
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,14 @@ class CustomerCest
|
|||
// we need to use this css selector to hit the correct <form>. 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.');
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue