Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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_')) {
Expand All @@ -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;
}

/**
Expand All @@ -60,14 +39,6 @@ public function isValid(MolPaymentMethod $paymentMethod): bool
return false;
}

if (!$this->isIdentificationNumberValid()) {
return false;
}

if (!$this->isVatNumberValid()) {
return false;
}

return true;
}

Expand All @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -46,223 +43,24 @@ 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);

$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);

$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);

$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);

Expand Down
Loading