From 2de7fc9a0f37b5dacb5ab9d250489de1efc3f949 Mon Sep 17 00:00:00 2001 From: mandan2 <61560082+mandan2@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:30:48 +0300 Subject: [PATCH] PIPRES-113: Billie payment method additional check for vat number (#813) --- src/Repository/AddressFormatRepository.php | 11 +++++ .../AddressFormatRepositoryInterface.php | 7 ++++ .../B2bPaymentMethodRestrictionValidator.php | 16 +++++++- src/ServiceProvider/BaseServiceProvider.php | 3 ++ ...bPaymentMethodRestrictionValidatorTest.php | 40 +++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/Repository/AddressFormatRepository.php create mode 100644 src/Repository/AddressFormatRepositoryInterface.php diff --git a/src/Repository/AddressFormatRepository.php b/src/Repository/AddressFormatRepository.php new file mode 100644 index 000000000..de9cad0a6 --- /dev/null +++ b/src/Repository/AddressFormatRepository.php @@ -0,0 +1,11 @@ +context = $context; $this->addressRepository = $addressRepository; $this->customerRepository = $customerRepository; $this->configuration = $configuration; + $this->addressFormatRepository = $addressFormatRepository; } /** @@ -81,6 +86,15 @@ private function isVatNumberValid(): bool 'id_address' => (int) $billingAddressId, ]); + /** @var \AddressFormat $addressFormat */ + $addressFormat = $this->addressFormatRepository->findOneBy([ + 'id_country' => $billingAddress->id_country, + ]); + + if (!str_contains($addressFormat->getFormat($billingAddress->id_country), 'vat_number')) { + return true; + } + return !empty($billingAddress->vat_number); } diff --git a/src/ServiceProvider/BaseServiceProvider.php b/src/ServiceProvider/BaseServiceProvider.php index 9c149eb9b..d50746b53 100644 --- a/src/ServiceProvider/BaseServiceProvider.php +++ b/src/ServiceProvider/BaseServiceProvider.php @@ -41,6 +41,8 @@ use Mollie\Provider\Shipment\AutomaticShipmentSenderStatusesProviderInterface; use Mollie\Provider\UpdateMessageProvider; use Mollie\Provider\UpdateMessageProviderInterface; +use Mollie\Repository\AddressFormatRepository; +use Mollie\Repository\AddressFormatRepositoryInterface; use Mollie\Repository\AddressRepository; use Mollie\Repository\AddressRepositoryInterface; use Mollie\Repository\CartRepository; @@ -158,6 +160,7 @@ public function register(Container $container) ); $this->addService($container, AddressRepositoryInterface::class, $container->get(AddressRepository::class)); + $this->addService($container, AddressFormatRepositoryInterface::class, $container->get(AddressFormatRepository::class)); $this->addService($container, TaxRulesGroupRepositoryInterface::class, $container->get(TaxRulesGroupRepository::class)); $this->addService($container, TaxRuleRepositoryInterface::class, $container->get(TaxRuleRepository::class)); $this->addService($container, TaxRepositoryInterface::class, $container->get(TaxRepository::class)); diff --git a/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php b/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php index d04f1b19a..ad88fa2c7 100644 --- a/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php +++ b/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php @@ -59,6 +59,46 @@ public function testItSuccessfullyValidatedIsValid(): void $this->assertEquals(true, $valid); } + public function testItSuccessfullyValidatedIsValidMissingVatNumberInFormat(): void + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = PaymentMethod::BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => 'test-siret-number', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => 'vat-number', + ]); + + $addressFormat = new \AddressFormat($billingAddress->id_country); + + $originalCountryFormat = $addressFormat->format; + + $addressFormat->format = 'test-format'; + $addressFormat->save(); + + $this->contextBuilder->setCart(CartFactory::create()); + $this->contextBuilder->getContext()->cart->id_address_invoice = $billingAddress->id; + $this->contextBuilder->getContext()->cart->id_customer = $customer->id; + + /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ + $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); + + $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); + + $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); + + $addressFormat->format = $originalCountryFormat; + $addressFormat->save(); + + $this->assertEquals(true, $supports); + $this->assertEquals(true, $valid); + } + public function testItUnsuccessfullyValidatedIsValidMethodNotSupported(): void { Configuration::set('PS_B2B_ENABLE', 1);