* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ if (!defined('_PS_VERSION_')) { exit; } use PsCheckout\Core\Exception\PsCheckoutException; use PsCheckout\Core\PaymentToken\Action\DeletePaymentTokenAction; use PsCheckout\Infrastructure\Controller\AbstractFrontController; use PsCheckout\Infrastructure\Repository\PaymentTokenRepository; use PsCheckout\Utility\Common\InputStreamUtility; /** * This controller receive ajax call to manage the Customer PayPal Payment Method tokens */ class Ps_CheckoutVaultModuleFrontController extends AbstractFrontController { /** * @see FrontController::postProcess() */ public function postProcess() { try { $bodyValues = []; /** @var InputStreamUtility $inputStreamUtility */ $inputStreamUtility = $this->module->getService(InputStreamUtility::class); $bodyContent = $inputStreamUtility->getBodyContent(); if (!empty($bodyContent)) { $bodyValues = json_decode($bodyContent, true); } $customerId = $this->context->customer->isLogged() ? $this->context->customer->id : null; if (isset($bodyValues['action'])) { $action = $bodyValues['action']; switch ($action) { case 'deleteToken': $vaultId = $bodyValues['vaultId']; /** @var DeletePaymentTokenAction $deletePaymentTokenAction */ $deletePaymentTokenAction = $this->module->getService(DeletePaymentTokenAction::class); $deletePaymentTokenAction->execute($vaultId, $customerId); $this->exitWithResponse([ 'status' => true, 'httpCode' => 200, ]); break; default: $this->exitWithResponse([ 'status' => false, 'httpCode' => 400, ]); } } /** @var PaymentTokenRepository $paymentTokenRepository */ $paymentTokenRepository = $this->module->getService(PaymentTokenRepository::class); $tokens = $paymentTokenRepository->getAllByCustomerId($customerId); $this->exitWithResponse([ 'status' => true, 'httpCode' => 200, 'body' => [ 'customerId' => $customerId, 'paymentTokens' => $tokens, 'totalItems' => null, 'totalPages' => null, ], ]); } catch (Exception $exception) { $this->exitWithExceptionMessage($exception); } catch (Throwable $exception) { $this->exitWithExceptionMessage(new PsCheckoutException( 'An error occurred while processing the vault request.', PsCheckoutException::UNKNOWN, $exception )); } } }