diff --git a/src/Config/Config.php b/src/Config/Config.php index f9dd481a1..2d04d8844 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -239,6 +239,11 @@ class Config const MOLLIE_VOUCHER_METHOD_ID = 'voucher'; const MOLLIE_in3_METHOD_ID = 'in3'; + /** + * @see https://www.mollie.com/en/payments/billie + */ + const MOLLIE_PAYMENT_METHOD_BILLIE = 'billie'; + const MOLLIE_VOUCHER_CATEGORY_NULL = 'null'; const MOLLIE_VOUCHER_CATEGORY_MEAL = 'meal'; const MOLLIE_VOUCHER_CATEGORY_GIFT = 'gift'; diff --git a/src/Entity/MolPaymentMethod.php b/src/Entity/MolPaymentMethod.php index f960ca063..8c175099c 100644 --- a/src/Entity/MolPaymentMethod.php +++ b/src/Entity/MolPaymentMethod.php @@ -139,8 +139,8 @@ class MolPaymentMethod extends ObjectModel ], ]; - public function getPaymentMethodName() + public function getPaymentMethodName(): string { - return $this->id_method; + return (string) $this->id_method; } } diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php index 749912f0e..a926bf857 100644 --- a/src/Repository/AbstractRepository.php +++ b/src/Repository/AbstractRepository.php @@ -26,7 +26,7 @@ class AbstractRepository implements ReadOnlyRepositoryInterface /** * @param string $fullyClassifiedClassName */ - public function __construct($fullyClassifiedClassName) + public function __construct(string $fullyClassifiedClassName) { $this->fullyClassifiedClassName = $fullyClassifiedClassName; } 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 @@ +getValue($sql); diff --git a/src/Repository/PendingOrderCartRepository.php b/src/Repository/PendingOrderCartRepository.php index 2b9aa54b1..9be7ac5f6 100644 --- a/src/Repository/PendingOrderCartRepository.php +++ b/src/Repository/PendingOrderCartRepository.php @@ -14,4 +14,8 @@ final class PendingOrderCartRepository extends AbstractRepository { + public function __construct() + { + parent::__construct(\MolPendingOrderCart::class); + } } diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/AmountPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/AmountPaymentMethodRestrictionValidator.php index 6276aa587..b18512be3 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/AmountPaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/AmountPaymentMethodRestrictionValidator.php @@ -37,7 +37,6 @@ namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; use Mollie\Adapter\Context; -use MolPaymentMethod; use PrestaShop\Decimal\Number; /** Validator to check if cart total is valid for amount restrictions */ @@ -55,7 +54,7 @@ public function __construct( /** * {@inheritDoc} */ - public function isValid(MolPaymentMethod $paymentMethod) + public function isValid(\MolPaymentMethod $paymentMethod): bool { $orderTotal = $this->context->getCart()->getOrderTotal(); @@ -77,7 +76,7 @@ public function isValid(MolPaymentMethod $paymentMethod) /** * {@inheritDoc} */ - public function supports(MolPaymentMethod $paymentMethod) + public function supports(\MolPaymentMethod $paymentMethod): bool { return true; } diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentMethodRestrictionValidator.php index 8da680e41..d83fcf7bc 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentMethodRestrictionValidator.php @@ -37,8 +37,7 @@ namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; use Mollie\Adapter\ConfigurationAdapter; -use Mollie\Config\Config; -use MolPaymentMethod; +use Mollie\Api\Types\PaymentMethod; class ApplePayPaymentMethodRestrictionValidator implements PaymentMethodRestrictionValidatorInterface { @@ -56,7 +55,7 @@ public function __construct( /** * {@inheritDoc} */ - public function isValid(MolPaymentMethod $paymentMethod) + public function isValid(\MolPaymentMethod $paymentMethod): bool { if (!$this->isSslEnabledEverywhere()) { return false; @@ -72,15 +71,15 @@ public function isValid(MolPaymentMethod $paymentMethod) /** * {@inheritDoc} */ - public function supports(MolPaymentMethod $paymentMethod) + public function supports(\MolPaymentMethod $paymentMethod): bool { - return $paymentMethod->getPaymentMethodName() == Config::MOLLIE_METHOD_ID_APPLE_PAY; + return $paymentMethod->getPaymentMethodName() === PaymentMethod::APPLEPAY; } /** * @return bool */ - private function isSslEnabledEverywhere() + private function isSslEnabledEverywhere(): bool { return (bool) $this->configurationAdapter->get('PS_SSL_ENABLED_EVERYWHERE'); } @@ -88,7 +87,7 @@ private function isSslEnabledEverywhere() /** * @return bool */ - private function isPaymentMethodInCookie() + private function isPaymentMethodInCookie(): bool { if (!isset($_COOKIE['isApplePayMethod'])) { return false; diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php new file mode 100644 index 000000000..8c1338e25 --- /dev/null +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/B2bPaymentMethodRestrictionValidator.php @@ -0,0 +1,104 @@ +context = $context; + $this->addressRepository = $addressRepository; + $this->customerRepository = $customerRepository; + $this->configuration = $configuration; + $this->addressFormatRepository = $addressFormatRepository; + } + + /** + * {@inheritDoc} + */ + public function isValid(\MolPaymentMethod $paymentMethod): bool + { + if (!$this->isB2bEnabled()) { + return false; + } + + if (!$this->isIdentificationNumberValid()) { + return false; + } + + if (!$this->isVatNumberValid()) { + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function supports(\MolPaymentMethod $paymentMethod): bool + { + return $paymentMethod->getPaymentMethodName() === Config::MOLLIE_PAYMENT_METHOD_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->getAddressInvoiceId(); + + /** @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/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/BasePaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/BasePaymentMethodRestrictionValidator.php index 8dafb669f..e5415d983 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/BasePaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/BasePaymentMethodRestrictionValidator.php @@ -36,15 +36,13 @@ namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; -use MolPaymentMethod; - /** Validator to check all cases for every payment method */ class BasePaymentMethodRestrictionValidator implements PaymentMethodRestrictionValidatorInterface { /** * {@inheritDoc} */ - public function isValid(MolPaymentMethod $paymentMethod) + public function isValid(\MolPaymentMethod $paymentMethod): bool { if (!$this->isPaymentMethodEnabled($paymentMethod)) { return false; @@ -56,17 +54,17 @@ public function isValid(MolPaymentMethod $paymentMethod) /** * {@inheritDoc} */ - public function supports(MolPaymentMethod $paymentMethod) + public function supports(\MolPaymentMethod $paymentMethod): bool { return true; } /** - * @param MolPaymentMethod $paymentMethod + * @param \MolPaymentMethod $paymentMethod * * @return bool */ - private function isPaymentMethodEnabled($paymentMethod) + private function isPaymentMethodEnabled(\MolPaymentMethod $paymentMethod): bool { return (bool) $paymentMethod->enabled; } diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/EnvironmentVersionSpecificPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/EnvironmentVersionSpecificPaymentMethodRestrictionValidator.php index 71d49b106..29072b3f2 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/EnvironmentVersionSpecificPaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/EnvironmentVersionSpecificPaymentMethodRestrictionValidator.php @@ -39,7 +39,6 @@ use Mollie\Adapter\Context; use Mollie\Provider\EnvironmentVersionProviderInterface; use Mollie\Repository\MethodCountryRepository; -use MolPaymentMethod; /** Validator to check specific cases by environment version for every payment method */ class EnvironmentVersionSpecificPaymentMethodRestrictionValidator implements PaymentMethodRestrictionValidatorInterface @@ -72,7 +71,7 @@ public function __construct( /** * {@inheritDoc} */ - public function isValid(MolPaymentMethod $paymentMethod) + public function isValid(\MolPaymentMethod $paymentMethod): bool { if (version_compare($this->prestashopVersionProvider->getPrestashopVersion(), '1.6.0.9', '>')) { if (!$this->isCountryAvailable($paymentMethod)) { @@ -86,12 +85,12 @@ public function isValid(MolPaymentMethod $paymentMethod) /** * {@inheritDoc} */ - public function supports(MolPaymentMethod $paymentMethod) + public function supports(\MolPaymentMethod $paymentMethod): bool { return true; } - private function isCountryAvailable(MolPaymentMethod $paymentMethod) + private function isCountryAvailable(\MolPaymentMethod $paymentMethod) { if ($paymentMethod->is_countries_applicable) { return $this->methodCountryRepository->checkIfMethodIsAvailableInCountry( diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/PaymentMethodRestrictionValidatorInterface.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/PaymentMethodRestrictionValidatorInterface.php index 102a6aa3d..b721815f7 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/PaymentMethodRestrictionValidatorInterface.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/PaymentMethodRestrictionValidatorInterface.php @@ -36,25 +36,23 @@ namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; -use MolPaymentMethod; - interface PaymentMethodRestrictionValidatorInterface { /** * Returns if payment is valid * - * @param MolPaymentMethod $paymentMethod + * @param \MolPaymentMethod $paymentMethod * * @return bool */ - public function isValid(MolPaymentMethod $paymentMethod); + public function isValid(\MolPaymentMethod $paymentMethod): bool; /** * Returns if payment restriction validator is supported by payment name * - * @param MolPaymentMethod $paymentMethod + * @param \MolPaymentMethod $paymentMethod * * @return bool */ - public function supports(MolPaymentMethod $paymentMethod); + public function supports(\MolPaymentMethod $paymentMethod): bool; } diff --git a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/VoucherPaymentMethodRestrictionValidator.php b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/VoucherPaymentMethodRestrictionValidator.php index a04b45b32..6d2e51a3e 100644 --- a/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/VoucherPaymentMethodRestrictionValidator.php +++ b/src/Service/PaymentMethod/PaymentMethodRestrictionValidation/VoucherPaymentMethodRestrictionValidator.php @@ -39,7 +39,6 @@ use Mollie\Adapter\Context; use Mollie\Config\Config; use Mollie\Validator\VoucherValidator; -use MolPaymentMethod; class VoucherPaymentMethodRestrictionValidator implements PaymentMethodRestrictionValidatorInterface { @@ -63,7 +62,7 @@ public function __construct(Context $context, VoucherValidator $voucherValidator * TODO extract voucher validator internals into this class + tests. * {@inheritDoc} */ - public function isValid(MolPaymentMethod $paymentMethod) + public function isValid(\MolPaymentMethod $paymentMethod): bool { if (!$this->voucherValidator->validate($this->context->getCart()->getProducts())) { return false; @@ -75,8 +74,8 @@ public function isValid(MolPaymentMethod $paymentMethod) /** * {@inheritDoc} */ - public function supports(MolPaymentMethod $paymentMethod) + public function supports(\MolPaymentMethod $paymentMethod): bool { - return $paymentMethod->getPaymentMethodName() == Config::MOLLIE_VOUCHER_METHOD_ID; + return $paymentMethod->getPaymentMethodName() === Config::MOLLIE_VOUCHER_METHOD_ID; } } diff --git a/src/ServiceProvider/BaseServiceProvider.php b/src/ServiceProvider/BaseServiceProvider.php index d574272c2..fb08fa359 100644 --- a/src/ServiceProvider/BaseServiceProvider.php +++ b/src/ServiceProvider/BaseServiceProvider.php @@ -43,17 +43,23 @@ 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\CartRuleRepository; use Mollie\Repository\CartRuleRepositoryInterface; use Mollie\Repository\CurrencyRepository; use Mollie\Repository\CurrencyRepositoryInterface; +use Mollie\Repository\CustomerRepository; +use Mollie\Repository\CustomerRepositoryInterface; use Mollie\Repository\GenderRepository; use Mollie\Repository\GenderRepositoryInterface; use Mollie\Repository\MolCustomerRepository; use Mollie\Repository\MolOrderPaymentFeeRepository; use Mollie\Repository\MolOrderPaymentFeeRepositoryInterface; +use Mollie\Repository\OrderCartRuleRepository; +use Mollie\Repository\OrderCartRuleRepositoryInterface; use Mollie\Repository\OrderRepository; use Mollie\Repository\OrderRepositoryInterface; use Mollie\Repository\PaymentMethodRepository; @@ -67,6 +73,8 @@ use Mollie\Repository\TaxRulesGroupRepository; use Mollie\Repository\TaxRulesGroupRepositoryInterface; use Mollie\Service\ApiKeyService; +use Mollie\Service\ApiService; +use Mollie\Service\ApiServiceInterface; use Mollie\Service\Content\SmartyTemplateParser; use Mollie\Service\Content\TemplateParserInterface; use Mollie\Service\EntityManager\EntityManagerInterface; @@ -74,6 +82,7 @@ use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\AmountPaymentMethodRestrictionValidator; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\ApplePayPaymentMethodRestrictionValidator; +use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\B2bPaymentMethodRestrictionValidator; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\BasePaymentMethodRestrictionValidator; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\EnvironmentVersionSpecificPaymentMethodRestrictionValidator; use Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation\VoucherPaymentMethodRestrictionValidator; @@ -112,15 +121,27 @@ public function register(Container $container) $this->addService($container, RetryHandlerInterface::class, $container->get(RetryHandler::class)); + $this->addService($container, UninstallerInterface::class, $container->get(DatabaseTableUninstaller::class)); + + $this->addService($container, DecoderInterface::class, JsonDecoder::class); + + $this->addService($container, AddressRepositoryInterface::class, $container->get(AddressRepository::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)); + $this->addService($container, CustomerRepositoryInterface::class, CustomerRepository::class); + $this->addService($container, AddressFormatRepositoryInterface::class, AddressFormatRepository::class); + $this->addService($container, PendingOrderCartRuleRepositoryInterface::class, $container->get(PendingOrderCartRuleRepository::class)); + $this->addService($container, CartRuleRepositoryInterface::class, $container->get(CartRuleRepository::class)); + $this->addService($container, OrderRepositoryInterface::class, $container->get(OrderRepository::class)); + $this->addService($container, CurrencyRepositoryInterface::class, $container->get(CurrencyRepository::class)); + $this->addService($container, MolOrderPaymentFeeRepositoryInterface::class, $container->get(MolOrderPaymentFeeRepository::class)); $this->addService($container, PaymentMethodRepositoryInterface::class, $container->get(PaymentMethodRepository::class)); $this->addService($container, GenderRepositoryInterface::class, $container->get(GenderRepository::class)); + $this->addService($container, OrderCartRuleRepositoryInterface::class, $container->get(OrderCartRuleRepository::class)); $this->addService($container, MolCustomerRepository::class, MolCustomerRepository::class) ->withArgument('MolCustomer'); - $this->addService($container, UninstallerInterface::class, $container->get(DatabaseTableUninstaller::class)); - - $this->addService($container, DecoderInterface::class, JsonDecoder::class); - /* shipping */ $this->addService($container, PaymentTypeIdentificationProviderInterface::class, $container->get(RegularPaymentTypeIdentification::class)); $this->addService($container, ShipmentServiceInterface::class, $container->get(ShipmentService::class)); @@ -137,23 +158,13 @@ public function register(Container $container) ] ); - $this->addService($container, AddressRepositoryInterface::class, $container->get(AddressRepository::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)); + $this->addService($container, CartRuleQuantityChangeHandlerInterface::class, $container->get(CartRuleQuantityChangeHandler::class)); $this->addService($container, OrderTotalProviderInterface::class, $container->get(OrderTotalProvider::class)); $this->addService($container, PaymentFeeProviderInterface::class, $container->get(PaymentFeeProvider::class)); $this->addService($container, EnvironmentVersionProviderInterface::class, $container->get(EnvironmentVersionProvider::class)); - $this->addService($container, PendingOrderCartRuleRepositoryInterface::class, $container->get(PendingOrderCartRuleRepository::class)); - $this->addService($container, CartRuleRepositoryInterface::class, $container->get(CartRuleRepository::class)); - $this->addService($container, OrderRepositoryInterface::class, $container->get(OrderRepository::class)); - $this->addService($container, CurrencyRepositoryInterface::class, $container->get(CurrencyRepository::class)); - $this->addService($container, MolOrderPaymentFeeRepositoryInterface::class, $container->get(MolOrderPaymentFeeRepository::class)); - $this->addService($container, CartRuleQuantityChangeHandlerInterface::class, $container->get(CartRuleQuantityChangeHandler::class)); - $this->addService($container, TemplateParserInterface::class, SmartyTemplateParser::class); $this->addService($container, UpdateMessageProviderInterface::class, $container->get(UpdateMessageProvider::class)); @@ -167,8 +178,11 @@ public function register(Container $container) $container->get(EnvironmentVersionSpecificPaymentMethodRestrictionValidator::class), $container->get(ApplePayPaymentMethodRestrictionValidator::class), $container->get(AmountPaymentMethodRestrictionValidator::class), + $container->get(B2bPaymentMethodRestrictionValidator::class), ]); + $this->addService($container, ApiServiceInterface::class, $container->get(ApiService::class)); + $this->addService($container, CustomLogoProviderInterface::class, $container->get(CreditCardLogoProvider::class)); $this->addService($container, PaymentMethodPositionHandlerInterface::class, PaymentMethodPositionHandler::class) diff --git a/tests/Integration/Application/CommandHandler/RequestApplePayPaymentSessionHandlerTest.php b/tests/Integration/Application/CommandHandler/RequestApplePayPaymentSessionHandlerTest.php index 5aca037a2..35aa30672 100644 --- a/tests/Integration/Application/CommandHandler/RequestApplePayPaymentSessionHandlerTest.php +++ b/tests/Integration/Application/CommandHandler/RequestApplePayPaymentSessionHandlerTest.php @@ -1,7 +1,10 @@ getService(ModuleFactory::class); $apiServiceMock = new ApiServiceMock(); - $handler = new RequestApplePayPaymentSessionHandler($mollie, $apiServiceMock); + $handler = new RequestApplePayPaymentSessionHandler($moduleFactory, $apiServiceMock); $result = $handler->handle($command); $this->assertArrayHasKey('cartId', $result); diff --git a/tests/Integration/Factory/AddressFactory.php b/tests/Integration/Factory/AddressFactory.php new file mode 100644 index 000000000..7d3321697 --- /dev/null +++ b/tests/Integration/Factory/AddressFactory.php @@ -0,0 +1,28 @@ +firstname = $data['first_name'] ?? 'test-first-name'; + $address->lastname = $data['last_name'] ?? 'test-last-name'; + $address->country = $data['country'] ?? 'test-country'; + $address->id_country = $data['id_country'] ?? \Configuration::get('PS_COUNTRY_DEFAULT'); + $address->city = $data['city'] ?? 'test-city'; + $address->postcode = $data['postcode'] ?? '97222'; //max 12 chars + $address->address1 = $data['address1'] ?? 'test-address1'; + $address->address2 = $data['address2'] ?? 'test-address2'; + $address->phone_mobile = $data['phone_mobile'] ?? '5555555'; //letters or symbols cause errors + $address->alias = $data['alias'] ?? 'test-alias'; + $address->vat_number = $data['vat_number'] ?? 'test-vat'; + $address->company = $data['company'] ?? 'test-company'; + + $address->save(); + + return $address; + } +} diff --git a/tests/Integration/Factory/CarrierFactory.php b/tests/Integration/Factory/CarrierFactory.php new file mode 100644 index 000000000..e77ce0852 --- /dev/null +++ b/tests/Integration/Factory/CarrierFactory.php @@ -0,0 +1,55 @@ +name = $data['name'] ?? 'test-name'; + $carrier->active = $data['active'] ?? true; + $carrier->delay = $data['delay'] ?? '28 days later'; + + //NOTE to if true would add PS_SHIPPING_HANDLING from configuration to shipping price. + $carrier->shipping_handling = $data['shipping_handling'] ?? false; + + //NOTE need to do it like this because otherwise it would not show up as option. + if (isset($data['price']) && !empty((int) $data['price'])) { + $carrier->shipping_method = \Carrier::SHIPPING_METHOD_PRICE; + } else { + $carrier->shipping_method = \Carrier::SHIPPING_METHOD_FREE; + } + + $carrier->shipping_method = $data['shipping_method'] ?? $carrier->shipping_method; + + $carrier->save(); + + $rangePrice = new \RangePrice(); + $rangePrice->id_carrier = $carrier->id; + $rangePrice->delimiter1 = 0; + $rangePrice->delimiter2 = 1; + + $rangePrice->save(); + + $zones = \Zone::getZones(); + $prices = []; + + foreach ($zones as $zone) { + $carrier->addZone($zone['id_zone']); + + $prices[] = [ + 'id_range_price' => $rangePrice->id, + 'id_range_weight' => null, + 'id_carrier' => (int) $carrier->id, + 'id_zone' => (int) $zone['id_zone'], + 'price' => $data['price'] ?? 0, + ]; + } + // enable all zones + $carrier->addDeliveryPrice($prices); + + return $carrier; + } +} diff --git a/tests/Integration/Factory/CartFactory.php b/tests/Integration/Factory/CartFactory.php new file mode 100644 index 000000000..54698b77c --- /dev/null +++ b/tests/Integration/Factory/CartFactory.php @@ -0,0 +1,24 @@ +id_lang = $data['id_lang'] ?? \Configuration::get('PS_LANG_DEFAULT'); + $cart->id_currency = $data['id_currency'] ?? \Configuration::get('PS_CURRENCY_DEFAULT'); + $cart->id_carrier = $data['id_carrier'] ?? CarrierFactory::create()->id; + $cart->id_address_delivery = $data['id_address_delivery'] ?? AddressFactory::create()->id; + $cart->id_address_invoice = $data['id_address_invoice'] ?? AddressFactory::create()->id; + $cart->id_customer = $data['id_customer'] ?? CustomerFactory::create()->id; + + $cart->save(); + + \Context::getContext()->cart = $cart; + + return $cart; + } +} diff --git a/tests/Integration/Factory/CustomerFactory.php b/tests/Integration/Factory/CustomerFactory.php new file mode 100644 index 000000000..0906629b4 --- /dev/null +++ b/tests/Integration/Factory/CustomerFactory.php @@ -0,0 +1,24 @@ +firstname = $data['first_name'] ?? 'test-first-name'; + $customer->lastname = $data['last_name'] ?? 'test-last-name'; + $customer->email = $data['email'] ?? 'test-email@email.com'; + $customer->passwd = $data['passwd'] ?? 'test-passwd'; + $customer->is_guest = $data['is_guest'] ?? false; + $customer->siret = $data['siret'] ?? 'test-siret'; + + $customer->save(); + + \Context::getContext()->customer = $customer; + + return $customer; + } +} diff --git a/tests/Integration/Factory/FactoryInterface.php b/tests/Integration/Factory/FactoryInterface.php new file mode 100644 index 000000000..2b91d48c9 --- /dev/null +++ b/tests/Integration/Factory/FactoryInterface.php @@ -0,0 +1,8 @@ +originalB2bValue = (int) Configuration::get('PS_B2B_ENABLE'); + + parent::setUp(); + } + + public function tearDown() + { + Configuration::set('PS_B2B_ENABLE', $this->originalB2bValue); + + parent::tearDown(); + } + + public function testItSuccessfullyValidatedIsValid() + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => 'test-siret-number', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => 'vat-number', + ]); + + $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); + + $this->assertEquals(true, $supports); + $this->assertEquals(true, $valid); + } + + public function testItSuccessfullyValidatedIsValidMissingVatNumberInFormat() + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_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() + { + 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', + ]); + + $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); + + $this->assertEquals(false, $supports); + $this->assertEquals(true, $valid); + } + + public function testItUnsuccessfullyValidatedIsValidMissingSiretNumber() + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => '', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => 'vat-number', + ]); + + $this->contextBuilder->setCart(CartFactory::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() + { + Configuration::set('PS_B2B_ENABLE', 0); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => 'test-siret', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => 'vat-number', + ]); + + $this->contextBuilder->setCart(CartFactory::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() + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => 'test-siret', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => '', + ]); + + $this->contextBuilder->setCart(CartFactory::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() + { + Configuration::set('PS_B2B_ENABLE', 1); + + $molPaymentMethod = new \MolPaymentMethod(); + $molPaymentMethod->id_method = Config::MOLLIE_PAYMENT_METHOD_BILLIE; + + $customer = CustomerFactory::create([ + 'siret' => 'test-siret', + ]); + + $billingAddress = AddressFactory::create([ + 'vat_number' => '', + ]); + + $shippingAddress = AddressFactory::create([ + 'vat_number' => 'test-vat-number', + ]); + + $this->contextBuilder->setCart(CartFactory::create([ + 'id_customer' => $customer->id, + 'id_address_delivery' => $shippingAddress->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); + } +} diff --git a/tests/Unit/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentRestrictionValidationTest.php b/tests/Unit/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentRestrictionValidationTest.php index db90a1b49..50b3ee69c 100644 --- a/tests/Unit/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentRestrictionValidationTest.php +++ b/tests/Unit/Service/PaymentMethod/PaymentMethodRestrictionValidation/ApplePayPaymentRestrictionValidationTest.php @@ -1,6 +1,7 @@ getMockBuilder(Context::class) - ->disableOriginalConstructor() - ->getMock(); $applePayValidation = new ApplePayPaymentMethodRestrictionValidator( - $contextMock, $configurationAdapter ); @@ -60,7 +57,6 @@ public function getApplePayPaymentRestrictionValidationDataProvider() public function testIsSupported($paymentName, $expectedResult) { $applePayValidation = new ApplePayPaymentMethodRestrictionValidator( - $this->mockContext('AT', 'AUD'), $this->mockConfigurationAdapter([ 'PS_SSL_ENABLED_EVERYWHERE' => true, ])