* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\CommandHandler; use Exception; use PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\Command\DeletePaymentTokenCommand; use PrestaShop\Module\PrestashopCheckout\PayPal\PaymentToken\PaymentMethodTokenService; use PrestaShop\Module\PrestashopCheckout\Repository\PaymentTokenRepository; class DeletePaymentTokenCommandHandler { /** @var PaymentTokenRepository */ private $paymentTokenRepository; /** * @var PaymentMethodTokenService */ private $paymentMethodTokenService; public function __construct(PaymentMethodTokenService $paymentMethodTokenService, PaymentTokenRepository $paymentTokenRepository) { $this->paymentTokenRepository = $paymentTokenRepository; $this->paymentMethodTokenService = $paymentMethodTokenService; } /** * @throws Exception */ public function handle(DeletePaymentTokenCommand $command) { $tokenBelongsToCustomer = false; $tokens = $this->paymentTokenRepository->findByPrestaShopCustomerId($command->getCustomerId()->getValue()); foreach ($tokens as $token) { $tokenBelongsToCustomer |= $token->getId()->getValue() === $command->getPaymentTokenId()->getValue(); } if ($tokenBelongsToCustomer) { $this->paymentMethodTokenService->deletePaymentToken($command->getPaymentTokenId()); $this->paymentTokenRepository->deleteById($command->getPaymentTokenId()); } else { throw new Exception('Failed to remove saved payment token'); } } }