diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 3b167f65..d3beb143 100755 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -25,9 +25,13 @@ use Invertus\SaferPay\Controller\Front\CheckoutController; use Invertus\SaferPay\Core\Payment\DTO\CheckoutData; use Invertus\SaferPay\Enum\ControllerName; +use Invertus\SaferPay\Exception\Restriction\UnauthenticatedCardUserException; +use Invertus\SaferPay\Exception\SaferPayException; use Invertus\SaferPay\Logger\LoggerInterface; +use Invertus\SaferPay\Repository\SaferPayCardAliasRepository; use Invertus\SaferPay\Repository\SaferPayOrderRepository; use Invertus\SaferPay\Utility\ExceptionUtility; +use Invertus\SaferPay\Validation\CustomerCreditCardValidation; if (!defined('_PS_VERSION_')) { exit; @@ -145,6 +149,37 @@ private function submitHostedFields() /** @var LoggerInterface $logger */ $logger = $this->module->getService(LoggerInterface::class); + /** @var CustomerCreditCardValidation $cardValidation */ + $cardValidation = $this->module->getService(CustomerCreditCardValidation::class); + + try { + $cardValidation->validate(Tools::getValue('selectedCard'), $this->context->customer->id); + } catch (UnauthenticatedCardUserException $e) { + $logger->error($e->getMessage(), [ + 'context' => [], + 'id_customer' => $this->context->customer->id, + 'id_card' => Tools::getValue('selectedCard'), + 'exceptions' => ExceptionUtility::getExceptions($e), + ]); + + $this->ajaxDie(json_encode([ + 'error' => true, + 'message' => $e->getMessage(), + 'url' => $this->getRedirectionToControllerUrl('fail'), + ])); + } catch (SaferPayException $e) { + $logger->error($e->getMessage(), [ + 'context' => [], + 'exceptions' => ExceptionUtility::getExceptions($e), + ]); + + $this->ajaxDie(json_encode([ + 'error' => true, + 'message' => $e->getMessage(), + 'url' => $this->getRedirectionToControllerUrl('fail'), + ])); + } + try { if (Order::getOrderByCartId($this->context->cart->id)) { $this->ajaxDie(json_encode([ diff --git a/src/Exception/ExceptionCode.php b/src/Exception/ExceptionCode.php index aabe86ec..d7a0a14d 100644 --- a/src/Exception/ExceptionCode.php +++ b/src/Exception/ExceptionCode.php @@ -33,6 +33,7 @@ class ExceptionCode // Payment related codes starts from 5*** const PAYMENT_FAILED_TO_FIND_CART = 5001; const PAYMENT_FAILED_TO_CREATE_ORDER = 5002; + const CANNOT_USE_CARD = 5003; // Order related codes starts from 7*** const ORDER_FAILED_TO_FIND_ORDER = 7001; diff --git a/src/Exception/Restriction/UnauthenticatedCardUserException.php b/src/Exception/Restriction/UnauthenticatedCardUserException.php new file mode 100644 index 00000000..bb8c396e --- /dev/null +++ b/src/Exception/Restriction/UnauthenticatedCardUserException.php @@ -0,0 +1,35 @@ + + *@copyright SIX Payment Services + *@license SIX Payment Services + */ + +namespace Invertus\SaferPay\Exception\Restriction; + +use Invertus\SaferPay\Exception\SaferPayException; +use RuntimeException; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class UnauthenticatedCardUserException extends SaferPayException +{ +} diff --git a/src/Exception/SaferPayException.php b/src/Exception/SaferPayException.php index 851bd257..c2d107a1 100644 --- a/src/Exception/SaferPayException.php +++ b/src/Exception/SaferPayException.php @@ -52,4 +52,15 @@ public static function unknownError() ExceptionCode::UNKNOWN_ERROR ); } + + public static function unauthenticatedCard($idCardOwner) + { + return new static( + 'Customer cannot use current saved card at this moment.', + ExceptionCode::CANNOT_USE_CARD, + [ + 'id_card_owner' => $idCardOwner, + ] + ); + } } diff --git a/src/Repository/SaferPayCardAliasRepository.php b/src/Repository/SaferPayCardAliasRepository.php index 5cf24362..d1664313 100755 --- a/src/Repository/SaferPayCardAliasRepository.php +++ b/src/Repository/SaferPayCardAliasRepository.php @@ -74,4 +74,15 @@ public function getSavedCardsByCustomerId($customerId) return Db::getInstance()->executeS($query); } + + public function getCustomerIdByReferenceId($cardAliasId, $idCustomer) + { + $query = new DbQuery(); + $query->select('`id_customer`'); + $query->from('saferpay_card_alias'); + $query->where('id_saferpay_card_alias = "' . pSQL($cardAliasId) . '"'); + $query->where('id_customer = "' . (int) $idCustomer . '"'); + + return Db::getInstance()->getValue($query); + } } diff --git a/src/Validation/CustomerCreditCardValidation.php b/src/Validation/CustomerCreditCardValidation.php new file mode 100644 index 00000000..89f71949 --- /dev/null +++ b/src/Validation/CustomerCreditCardValidation.php @@ -0,0 +1,84 @@ + + *@copyright SIX Payment Services + *@license SIX Payment Services + */ + +namespace Invertus\SaferPay\Validation; + +use Exception; +use Invertus\SaferPay\Exception\Restriction\UnauthenticatedCardUserException; +use Invertus\SaferPay\Exception\SaferPayException; +use Invertus\SaferPay\Logger\LoggerInterface; +use Invertus\SaferPay\Repository\SaferPayCardAliasRepository; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class CustomerCreditCardValidation +{ + /** + * @var SaferPayCardAliasRepository + */ + private $saferPayCardAliasRepository; + /** + * @var mixed + */ + private $logger; + + const FILE_NAME = 'CustomerCreditCardValidation'; + + public function __construct(SaferPayCardAliasRepository $saferPayCardAliasRepository, LoggerInterface $logger) + { + $this->saferPayCardAliasRepository = $saferPayCardAliasRepository; + $this->logger = $logger; + } + + /** + * @return true + * + * @throws UnauthenticatedCardUserException + * @throws SaferPayException + */ + public function validate($idSavedCard, $idCustomer) + { + if ( + !is_numeric($idCustomer) || !is_numeric($idSavedCard) + || empty($idCustomer) || empty($idSavedCard)) + { + $this->logger->error(sprintf('%s - Invalid data or bad types', self::FILE_NAME), [ + 'context' => [], + 'id_saved_card' => $idSavedCard, + 'id_customer' => $idCustomer + ]); + + throw SaferPayException::unknownError(); + } + + $cardOwnerId = $this->saferPayCardAliasRepository->getCustomerIdByReferenceId(pSQL($idSavedCard), pSQL($idCustomer)); + + if (empty($cardOwnerId)) { + throw UnauthenticatedCardUserException::unauthenticatedCard($cardOwnerId); + } + + return true; + } +} \ No newline at end of file