Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PIPRES-352: Billie payment method validation #16

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
5 changes: 5 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/MolPaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ class MolPaymentMethod extends ObjectModel
],
];

public function getPaymentMethodName()
public function getPaymentMethodName(): string
{
return $this->id_method;
return (string) $this->id_method;
}
}
2 changes: 1 addition & 1 deletion src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AbstractRepository implements ReadOnlyRepositoryInterface
/**
* @param string $fullyClassifiedClassName
*/
public function __construct($fullyClassifiedClassName)
public function __construct(string $fullyClassifiedClassName)
{
$this->fullyClassifiedClassName = $fullyClassifiedClassName;
}
Expand Down
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
{
}
11 changes: 11 additions & 0 deletions src/Repository/CustomerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Mollie\Repository;

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

namespace Mollie\Repository;

interface CustomerRepositoryInterface extends ReadOnlyRepositoryInterface
{
}
4 changes: 4 additions & 0 deletions src/Repository/MolCustomerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@

class MolCustomerRepository extends AbstractRepository
{
public function __construct()
{
parent::__construct(\MolCustomer::class);
}
}
6 changes: 1 addition & 5 deletions src/Repository/PaymentMethodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
use PDOStatement;
use PrestaShopDatabaseException;

/**
* @deprecated - outside code must always use interface. Use PaymentMethodRepositoryInterface instead.
* In Containers use PaymentMethodRepositoryInterface::class
*/
class PaymentMethodRepository extends AbstractRepository implements PaymentMethodRepositoryInterface
{
public function __construct()
Expand Down Expand Up @@ -88,7 +84,7 @@ public function getPaymentMethodIdByMethodId($paymentMethodId, $environment, $sh
}

$sql = 'SELECT id_payment_method FROM `' . _DB_PREFIX_ . 'mol_payment_method`
WHERE id_method = "' . pSQL($paymentMethodId) . '" AND live_environment = "' . (int) $environment . '"
WHERE id_method = "' . pSQL($paymentMethodId) . '" AND live_environment = "' . (int) $environment . '"
AND id_shop = ' . (int) $shopId;

return Db::getInstance()->getValue($sql);
Expand Down
4 changes: 4 additions & 0 deletions src/Repository/PendingOrderCartRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@

final class PendingOrderCartRepository extends AbstractRepository
{
public function __construct()
{
parent::__construct(\MolPendingOrderCart::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -55,7 +54,7 @@ public function __construct(
/**
* {@inheritDoc}
*/
public function isValid(MolPaymentMethod $paymentMethod)
public function isValid(\MolPaymentMethod $paymentMethod): bool
{
$orderTotal = $this->context->getCart()->getOrderTotal();

Expand All @@ -77,7 +76,7 @@ public function isValid(MolPaymentMethod $paymentMethod)
/**
* {@inheritDoc}
*/
public function supports(MolPaymentMethod $paymentMethod)
public function supports(\MolPaymentMethod $paymentMethod): bool
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -56,7 +55,7 @@ public function __construct(
/**
* {@inheritDoc}
*/
public function isValid(MolPaymentMethod $paymentMethod)
public function isValid(\MolPaymentMethod $paymentMethod): bool
{
if (!$this->isSslEnabledEverywhere()) {
return false;
Expand All @@ -72,23 +71,23 @@ 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');
}

/**
* @return bool
*/
private function isPaymentMethodInCookie()
private function isPaymentMethodInCookie(): bool
{
if (!isset($_COOKIE['isApplePayMethod'])) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Mollie\Service\PaymentMethod\PaymentMethodRestrictionValidation;

use Mollie\Adapter\ConfigurationAdapter;
use Mollie\Adapter\Context;
use Mollie\Config\Config;
use Mollie\Repository\AddressFormatRepositoryInterface;
use Mollie\Repository\AddressRepositoryInterface;
use Mollie\Repository\CustomerRepositoryInterface;

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;
$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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading