diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php index 049bab609..2d9891b1d 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php @@ -13,11 +13,7 @@ namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; use Mollie\Adapter\ConfigurationAdapter; -use Mollie\Adapter\Context; use Mollie\Api\Types\PaymentMethod; -use Mollie\Repository\AddressFormatRepositoryInterface; -use Mollie\Repository\AddressRepositoryInterface; -use Mollie\Repository\CustomerRepositoryInterface; use MolPaymentMethod; if (!defined('_PS_VERSION_')) { @@ -26,29 +22,12 @@ class B2bPaymentMethodRestrictionValidator implements PaymentMethodRestrictionValidatorInterface { - /** @var Context */ - private $context; - /** @var AddressRepositoryInterface */ - private $addressRepository; - /** @var CustomerRepositoryInterface */ - private $customerRepository; /** @var ConfigurationAdapter */ private $configuration; - /** @var AddressFormatRepositoryInterface */ - private $addressFormatRepository; - public function __construct( - Context $context, - AddressRepositoryInterface $addressRepository, - CustomerRepositoryInterface $customerRepository, - ConfigurationAdapter $configuration, - AddressFormatRepositoryInterface $addressFormatRepository - ) { - $this->context = $context; - $this->addressRepository = $addressRepository; - $this->customerRepository = $customerRepository; + public function __construct(ConfigurationAdapter $configuration) + { $this->configuration = $configuration; - $this->addressFormatRepository = $addressFormatRepository; } /** @@ -60,14 +39,6 @@ public function isValid(MolPaymentMethod $paymentMethod): bool return false; } - if (!$this->isIdentificationNumberValid()) { - return false; - } - - if (!$this->isVatNumberValid()) { - return false; - } - return true; } @@ -79,39 +50,6 @@ public function supports(MolPaymentMethod $paymentMethod): bool return $paymentMethod->getPaymentMethodName() === PaymentMethod::BILLIE; } - private function isIdentificationNumberValid(): bool - { - $customerId = $this->context->getCustomerId(); - - /** @var \Customer $customer */ - $customer = $this->customerRepository->findOneBy([ - 'id_customer' => $customerId, - ]); - - return !empty($customer->siret); - } - - private function isVatNumberValid(): bool - { - $billingAddressId = $this->context->getInvoiceAddressId(); - - /** @var \Address $billingAddress */ - $billingAddress = $this->addressRepository->findOneBy([ - '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); - } - private function isB2bEnabled(): bool { return (bool) (int) $this->configuration->get('PS_B2B_ENABLE'); diff --git a/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php b/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php index 90000cf62..ec7174e32 100644 --- a/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php +++ b/tests/Integration/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidatorTest.php @@ -16,9 +16,6 @@ use Mollie\Api\Types\PaymentMethod; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\B2bPaymentMethodRestrictionValidator; use Mollie\Tests\Integration\BaseTestCase; -use Mollie\Tests\Integration\Factory\AddressFactory; -use Mollie\Tests\Integration\Factory\CartFactory; -use Mollie\Tests\Integration\Factory\CustomerFactory; class B2bPaymentMethodRestrictionValidatorTest extends BaseTestCase { @@ -46,57 +43,6 @@ public function testItSuccessfullyValidatedIsValid(): void $molPaymentMethod = new \MolPaymentMethod(); $molPaymentMethod->id_method = PaymentMethod::BILLIE; - $customer = CustomerFactory::create([ - 'siret' => 'test-siret-number', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => 'vat-number', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_invoice' => $billingAddress->id, - ]); - - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ - $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); - - $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); - - $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); - - $this->assertEquals(true, $supports); - $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(); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_invoice' => $billingAddress->id, - ]); - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); @@ -104,76 +50,10 @@ public function testItSuccessfullyValidatedIsValidMissingVatNumberInFormat(): vo $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); - - $molPaymentMethod = new \MolPaymentMethod(); - $molPaymentMethod->id_method = 'not-supported-method'; - - $customer = CustomerFactory::create([ - 'siret' => 'test-siret-number', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => 'vat-number', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_invoice' => $billingAddress->id, - ]); - - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ - $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); - - $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); - - $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); - - $this->assertEquals(false, $supports); - $this->assertEquals(true, $valid); - } - - public function testItUnsuccessfullyValidatedIsValidMissingSiretNumber(): void - { - Configuration::set('PS_B2B_ENABLE', 1); - - $molPaymentMethod = new \MolPaymentMethod(); - $molPaymentMethod->id_method = PaymentMethod::BILLIE; - - $customer = CustomerFactory::create([ - 'siret' => '', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => 'vat-number', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_delivery' => $billingAddress->id, - 'id_address_invoice' => $billingAddress->id, - ]); - - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ - $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); - - $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); - - $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); - - $this->assertEquals(true, $supports); - $this->assertEquals(false, $valid); - } - public function testItUnsuccessfullyValidatedIsValidB2bNotEnabled(): void { Configuration::set('PS_B2B_ENABLE', 0); @@ -181,88 +61,6 @@ public function testItUnsuccessfullyValidatedIsValidB2bNotEnabled(): void $molPaymentMethod = new \MolPaymentMethod(); $molPaymentMethod->id_method = PaymentMethod::BILLIE; - $customer = CustomerFactory::create([ - 'siret' => 'test-siret', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => 'vat-number', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_delivery' => $billingAddress->id, - 'id_address_invoice' => $billingAddress->id, - ]); - - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ - $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); - - $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); - - $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); - - $this->assertEquals(true, $supports); - $this->assertEquals(false, $valid); - } - - public function testItUnsuccessfullyValidatedIsValidMissingVatNumberInBothAddresses(): void - { - Configuration::set('PS_B2B_ENABLE', 1); - - $molPaymentMethod = new \MolPaymentMethod(); - $molPaymentMethod->id_method = PaymentMethod::BILLIE; - - $customer = CustomerFactory::create([ - 'siret' => 'test-siret', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => '', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_delivery' => $billingAddress->id, - 'id_address_invoice' => $billingAddress->id, - ]); - - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ - $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class); - - $supports = $b2bPaymentMethodRestrictionValidator->supports($molPaymentMethod); - - $valid = $b2bPaymentMethodRestrictionValidator->isValid($molPaymentMethod); - - $this->assertEquals(true, $supports); - $this->assertEquals(false, $valid); - } - - public function testItUnsuccessfullyValidatedIsValidMissingVatNumberInBillingAddress(): void - { - Configuration::set('PS_B2B_ENABLE', 1); - - $molPaymentMethod = new \MolPaymentMethod(); - $molPaymentMethod->id_method = PaymentMethod::BILLIE; - - $customer = CustomerFactory::create([ - 'siret' => 'test-siret', - ]); - - $billingAddress = AddressFactory::create([ - 'vat_number' => '', - ]); - - $shippingAddress = AddressFactory::create([ - 'vat_number' => 'test-vat-number', - ]); - - CartFactory::initialize()->create([ - 'id_customer' => $customer->id, - 'id_address_delivery' => $billingAddress->id, - 'id_address_invoice' => $billingAddress->id, - ]); - /** @var B2bPaymentMethodRestrictionValidator $b2bPaymentMethodRestrictionValidator */ $b2bPaymentMethodRestrictionValidator = $this->getService(B2bPaymentMethodRestrictionValidator::class);