Skip to content

Commit

Permalink
PIPRES-113: Billie payment method additional check for vat number
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Sep 19, 2023
1 parent 556480d commit a16eb5f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Repository/AddressFormatRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Mollie\Repository;

class AddressFormatRepository extends AbstractRepository implements AddressFormatRepositoryInterface
{
public function __construct()
{
parent::__construct(\AddressFormat::class);
}
}
7 changes: 7 additions & 0 deletions src/Repository/AddressFormatRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Mollie\Repository;

interface AddressFormatRepositoryInterface extends ReadOnlyRepositoryInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -19,17 +20,21 @@ class B2bPaymentMethodRestrictionValidator implements PaymentMethodRestrictionVa
private $customerRepository;
/** @var ConfigurationAdapter */
private $configuration;
/** @var AddressFormatRepositoryInterface */
private $addressFormatRepository;

public function __construct(
Context $context,
AddressRepositoryInterface $addressRepository,
CustomerRepositoryInterface $customerRepository,
ConfigurationAdapter $configuration
ConfigurationAdapter $configuration,
AddressFormatRepositoryInterface $addressFormatRepository
) {
$this->context = $context;
$this->addressRepository = $addressRepository;
$this->customerRepository = $customerRepository;
$this->configuration = $configuration;
$this->addressFormatRepository = $addressFormatRepository;
}

/**
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions src/ServiceProvider/BaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a16eb5f

Please sign in to comment.