DeleteAction.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Vault\Controller\Cards;
  7. use Magento\Customer\Model\Session;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Framework\App\Request\Http;
  10. use Magento\Framework\App\ResponseInterface;
  11. use Magento\Framework\Controller\Result\JsonFactory;
  12. use Magento\Framework\Controller\ResultInterface;
  13. use Magento\Framework\Data\Form\FormKey\Validator;
  14. use Magento\Framework\Exception\NotFoundException;
  15. use Magento\Vault\Api\Data\PaymentTokenInterface;
  16. use Magento\Vault\Api\PaymentTokenRepositoryInterface;
  17. use Magento\Vault\Controller\CardsManagement;
  18. use Magento\Vault\Model\PaymentTokenManagement;
  19. /**
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class DeleteAction extends CardsManagement
  23. {
  24. const WRONG_REQUEST = 1;
  25. const WRONG_TOKEN = 2;
  26. const ACTION_EXCEPTION = 3;
  27. /**
  28. * @var array
  29. */
  30. private $errorsMap = [];
  31. /**
  32. * @var JsonFactory
  33. */
  34. private $jsonFactory;
  35. /**
  36. * @var Validator
  37. */
  38. private $fkValidator;
  39. /**
  40. * @var PaymentTokenRepositoryInterface
  41. */
  42. private $tokenRepository;
  43. /**
  44. * @var PaymentTokenManagement
  45. */
  46. private $paymentTokenManagement;
  47. /**
  48. * @param Context $context
  49. * @param Session $customerSession
  50. * @param JsonFactory $jsonFactory
  51. * @param Validator $fkValidator
  52. * @param PaymentTokenRepositoryInterface $tokenRepository
  53. * @param PaymentTokenManagement $paymentTokenManagement
  54. */
  55. public function __construct(
  56. Context $context,
  57. Session $customerSession,
  58. JsonFactory $jsonFactory,
  59. Validator $fkValidator,
  60. PaymentTokenRepositoryInterface $tokenRepository,
  61. PaymentTokenManagement $paymentTokenManagement
  62. ) {
  63. parent::__construct($context, $customerSession);
  64. $this->jsonFactory = $jsonFactory;
  65. $this->fkValidator = $fkValidator;
  66. $this->tokenRepository = $tokenRepository;
  67. $this->paymentTokenManagement = $paymentTokenManagement;
  68. $this->errorsMap = [
  69. self::WRONG_TOKEN => __('No token found.'),
  70. self::WRONG_REQUEST => __('Wrong request.'),
  71. self::ACTION_EXCEPTION => __('Deletion failure. Please try again.')
  72. ];
  73. }
  74. /**
  75. * Dispatch request
  76. *
  77. * @return ResultInterface|ResponseInterface
  78. * @throws NotFoundException
  79. */
  80. public function execute()
  81. {
  82. $request = $this->_request;
  83. if (!$request instanceof Http) {
  84. return $this->createErrorResponse(self::WRONG_REQUEST);
  85. }
  86. if (!$this->fkValidator->validate($request)) {
  87. return $this->createErrorResponse(self::WRONG_REQUEST);
  88. }
  89. $paymentToken = $this->getPaymentToken($request);
  90. if ($paymentToken === null) {
  91. return $this->createErrorResponse(self::WRONG_TOKEN);
  92. }
  93. try {
  94. $this->tokenRepository->delete($paymentToken);
  95. } catch (\Exception $e) {
  96. return $this->createErrorResponse(self::ACTION_EXCEPTION);
  97. }
  98. return $this->createSuccessMessage();
  99. }
  100. /**
  101. * @param int $errorCode
  102. * @return ResponseInterface
  103. */
  104. private function createErrorResponse($errorCode)
  105. {
  106. $this->messageManager->addErrorMessage(
  107. $this->errorsMap[$errorCode]
  108. );
  109. return $this->_redirect('vault/cards/listaction');
  110. }
  111. /**
  112. * @return ResponseInterface
  113. */
  114. private function createSuccessMessage()
  115. {
  116. $this->messageManager->addSuccessMessage(
  117. __('Stored Payment Method was successfully removed')
  118. );
  119. return $this->_redirect('vault/cards/listaction');
  120. }
  121. /**
  122. * @param Http $request
  123. * @return PaymentTokenInterface|null
  124. */
  125. private function getPaymentToken(Http $request)
  126. {
  127. $publicHash = $request->getPostValue(PaymentTokenInterface::PUBLIC_HASH);
  128. if ($publicHash === null) {
  129. return null;
  130. }
  131. return $this->paymentTokenManagement->getByPublicHash(
  132. $publicHash,
  133. $this->customerSession->getCustomerId()
  134. );
  135. }
  136. }