diff --git a/README.md b/README.md index 790d608..8f8da72 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,32 @@ imports: - { resource: "@SetonoSyliusQuickpayPlugin/Resources/config/app/fixtures.yaml" } ```` +### 5: Add validator constraint + +Create `config/validator/Address.xml`: + +```xml + + + + + + + + + + + +``` + +Like it [configured](tests/Application/config/validator/Address.xml) at example application. + ## Configuration Create a new Payment method of the type *Quickpay* and fill out the required form fields. diff --git a/src/Action/ConvertPaymentAction.php b/src/Action/ConvertPaymentAction.php index 7a66b44..2b2c25a 100644 --- a/src/Action/ConvertPaymentAction.php +++ b/src/Action/ConvertPaymentAction.php @@ -25,6 +25,7 @@ use Sylius\Component\Core\Model\ProductVariantInterface; use Symfony\Component\Intl\Countries; use VIISON\AddressSplitter\AddressSplitter; +use VIISON\AddressSplitter\Exceptions\SplittingException; use Webmozart\Assert\Assert; /** @@ -107,17 +108,43 @@ protected function convertAddress(AddressInterface $address, CustomerInterface $ $street = $address->getStreet(); Assert::notNull($street); - $splittedStreet = AddressSplitter::splitAddress($street); - $details = []; + switch (mb_strtoupper($countryCode)) { + case 'DE': + try { + $splittedStreet = AddressSplitter::splitAddress($street); + + $details['street'] = $splittedStreet['streetName']; + $details['house_number'] = $splittedStreet['houseNumber']; + } catch (SplittingException $e) { + $details['street'] = $street; + $details['house_number'] = ''; + } + + break; + case 'NL': + try { + $splittedStreet = AddressSplitter::splitAddress($street); + + $details['street'] = $splittedStreet['streetName']; + $details['house_number'] = $splittedStreet['houseNumberParts']['base']; + $details['house_extension'] = $splittedStreet['houseNumberParts']['extension']; + } catch (SplittingException $e) { + $details['street'] = $street; + $details['house_number'] = ''; + $details['house_extension'] = ''; + } + + break; + default: + $details['street'] = $street; + } + $details['name'] = sprintf( '%s %s', $address->getFirstName(), $address->getLastName() ); - $details['street'] = $splittedStreet['streetName']; - $details['house_number'] = $splittedStreet['houseNumberParts']['base']; - $details['house_extension'] = $splittedStreet['houseNumberParts']['extension']; $details['city'] = $address->getCity(); $details['zip_code'] = $address->getPostcode(); $details['region'] = $address->getProvinceName() ?? $address->getProvinceCode(); diff --git a/src/Checker/StreetEligibilityChecker.php b/src/Checker/StreetEligibilityChecker.php new file mode 100644 index 0000000..c3c45f8 --- /dev/null +++ b/src/Checker/StreetEligibilityChecker.php @@ -0,0 +1,49 @@ +getCountryCode(); + Assert::notNull($countryCode); + + $street = $address->getStreet(); + if (null === $street) { + return false; + } + + try { + switch (mb_strtoupper($countryCode)) { + case 'DE': + $splittedStreet = AddressSplitter::splitAddress($street); + if ('' === $splittedStreet['houseNumber']) { + return false; + } + + break; + case 'NL': + $splittedStreet = AddressSplitter::splitAddress($street); + if ('' === $splittedStreet['houseNumberParts']['base']) { + return false; + } + + if ('' === $splittedStreet['houseNumberParts']['extension']) { + return false; + } + } + } catch (SplittingException $e) { + return false; + } + + return true; + } +} diff --git a/src/Checker/StreetEligibilityCheckerInterface.php b/src/Checker/StreetEligibilityCheckerInterface.php new file mode 100644 index 0000000..bc7bed3 --- /dev/null +++ b/src/Checker/StreetEligibilityCheckerInterface.php @@ -0,0 +1,12 @@ + 'Hausmannstätten', 'postcode' => $options['approved'] ? '8071' : '8070', 'gender' => $options['approved'] ? CustomerInterface::MALE_GENDER : CustomerInterface::FEMALE_GENDER, - 'birthday' => $options['approved'] ? '1960-04-14' : '1980-04-14', + 'birthday' => $options['approved'] ? '1960-04-14 00:00:00' : '1980-04-14 00:00:00', ], 'FI' => [ 'phone_number' => '0401234567', @@ -149,7 +149,7 @@ protected function getKlarnaTestDataByCountry(string $countryCode, array $option 'city' => 'Neuss', 'postcode' => '41460', 'gender' => CustomerInterface::MALE_GENDER, - 'birthday' => '1960-07-07', + 'birthday' => '1960-07-07 00:00:00', ], 'NO' => [ 'phone_number' => '40 123 456', @@ -163,7 +163,7 @@ protected function getKlarnaTestDataByCountry(string $countryCode, array $option 'city' => 'Gravenhage', 'postcode' => '2521VA', 'gender' => CustomerInterface::MALE_GENDER, - 'birthday' => '1960-07-10', + 'birthday' => '1960-07-10 00:00:00', ], 'CH' => [ 'first_name' => $options['approved'] ? $this->faker->firstName : 'test', @@ -173,7 +173,7 @@ protected function getKlarnaTestDataByCountry(string $countryCode, array $option 'city' => 'Zürich', 'postcode' => '8001', 'gender' => CustomerInterface::MALE_GENDER, - 'birthday' => '1960-01-01', + 'birthday' => '1960-01-01 00:00:00', ], ]; diff --git a/src/Resources/config/app/fixtures.yaml b/src/Resources/config/app/fixtures.yaml index 0b2e567..1025d65 100644 --- a/src/Resources/config/app/fixtures.yaml +++ b/src/Resources/config/app/fixtures.yaml @@ -3,6 +3,19 @@ sylius_fixtures: default: fixtures: + geographical: + options: + countries: + - 'US' + - 'DK' + - 'NL' + zones: + EU: + name: "EU" + scope: "all" + countries: + - 'DK' + - 'NL' currency: options: currencies: @@ -30,12 +43,28 @@ sylius_fixtures: - "DKK" enabled: true hostname: "localhost" + nl_web_store: + name: "NL Web Store" + code: 'NL_WEB' + locales: + - "%locale%" + currencies: + - "EUR" + enabled: true + hostname: "localhost" - setono_quickpay_klarna_test_shop_user: + setono_quickpay_klarna_test_shop_user_dk: + name: setono_quickpay_klarna_test_shop_user options: amount: 10 country: DK + setono_quickpay_klarna_test_shop_user_nl: + name: setono_quickpay_klarna_test_shop_user + options: + amount: 10 + country: NL + # Temporary fix for https://github.com/Sylius/Sylius/issues/10719 # @todo Remove this lines once issue will be fixed order: @@ -72,6 +101,7 @@ sylius_fixtures: - 'FASHION_WEB' - 'EU_WEB' - 'DK_WEB' + - 'NL_WEB' enabled: true quickpay_klarna: code: 'quickpay_klarna' @@ -97,6 +127,7 @@ sylius_fixtures: use_authorize: true channels: - 'DK_WEB' + - 'NL_WEB' enabled: true shipping_method: @@ -105,7 +136,24 @@ sylius_fixtures: eu_courier: code: 'eu_courier' name: 'EU courier' + zone: 'EU' enabled: true channels: - 'EU_WEB' - 'DK_WEB' + - 'NL_WEB' + + cap_product_nl: + name: product + options: + custom: + - name: 'Winter cap' + tax_category: 'other' + channels: + - 'NL_WEB' + main_taxon: 'caps_with_pompons' + taxons: + - 'caps' + - 'caps_with_pompons' + images: + - { path: '@SyliusCoreBundle/Resources/fixtures/caps/cap_01.jpg', type: 'main' } diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 3227727..f8bf06b 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -16,6 +16,20 @@ alias="setono_sylius_quickpay.quickpay.convert_payment" /> + + + + + + + + + + + streetEligibilityChecker = $streetEligibilityChecker; + } + + public function validate($address, Constraint $constraint): void + { + Assert::isInstanceOf($address, AddressInterface::class); + Assert::isInstanceOf($constraint, AddressStreetEligibility::class); + + if (!$this->streetEligibilityChecker->isEligible($address)) { + $this->context + ->buildViolation($constraint->message) + ->atPath('street') + ->addViolation() + ; + } + } +} diff --git a/src/Validator/Constraints/AddressStreetEligibility.php b/src/Validator/Constraints/AddressStreetEligibility.php new file mode 100644 index 0000000..635226f --- /dev/null +++ b/src/Validator/Constraints/AddressStreetEligibility.php @@ -0,0 +1,23 @@ + + + + + + + + + +