|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meta\Sales\Controller\Checkout; |
| 6 | + |
| 7 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 8 | +use Magento\Framework\App\Action\HttpGetActionInterface; |
| 9 | +use Magento\Framework\App\RequestInterface; |
| 10 | +use Magento\Framework\App\ResponseInterface; |
| 11 | +use Magento\Framework\Controller\Result\RedirectFactory; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 14 | +use Magento\Quote\Model\QuoteRepository; |
| 15 | +use Meta\BusinessExtension\Helper\FBEHelper; |
| 16 | +use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator; |
| 17 | + |
| 18 | +/** |
| 19 | + * Controller for loading cart based on masked ID. |
| 20 | + */ |
| 21 | +class LoadMetaCart implements HttpGetActionInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var RedirectFactory |
| 25 | + */ |
| 26 | + private $resultRedirectFactory; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var QuoteIdMaskFactory |
| 30 | + */ |
| 31 | + private $quoteIdMaskFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var QuoteRepository |
| 35 | + */ |
| 36 | + private $quoteRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var CheckoutSession |
| 40 | + */ |
| 41 | + private $checkoutSession; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var RequestInterface |
| 45 | + */ |
| 46 | + private $request; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var FBEHelper |
| 50 | + */ |
| 51 | + private $fbeHelper; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var Authenticator |
| 55 | + */ |
| 56 | + private $authenticator; |
| 57 | + |
| 58 | + /** |
| 59 | + * Constructor. |
| 60 | + * |
| 61 | + * @param RequestInterface $request |
| 62 | + * @param RedirectFactory $resultRedirectFactory |
| 63 | + * @param QuoteIdMaskFactory $quoteIdMaskFactory |
| 64 | + * @param QuoteRepository $quoteRepository |
| 65 | + * @param CheckoutSession $checkoutSession |
| 66 | + * @param FBEHelper $fbeHelper |
| 67 | + * @param Authenticator $authenticator |
| 68 | + */ |
| 69 | + public function __construct( |
| 70 | + RequestInterface $request, |
| 71 | + RedirectFactory $resultRedirectFactory, |
| 72 | + QuoteIdMaskFactory $quoteIdMaskFactory, |
| 73 | + QuoteRepository $quoteRepository, |
| 74 | + CheckoutSession $checkoutSession, |
| 75 | + FBEHelper $fbeHelper, |
| 76 | + Authenticator $authenticator |
| 77 | + ) { |
| 78 | + $this->request = $request; |
| 79 | + $this->resultRedirectFactory = $resultRedirectFactory; |
| 80 | + $this->quoteIdMaskFactory = $quoteIdMaskFactory; |
| 81 | + $this->quoteRepository = $quoteRepository; |
| 82 | + $this->checkoutSession = $checkoutSession; |
| 83 | + $this->fbeHelper = $fbeHelper; |
| 84 | + $this->authenticator = $authenticator; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Execute action based on request. |
| 89 | + * |
| 90 | + * IMPORTANT: Signatures must be URL-Encoded after being Base64 Encoded, or verification will fail. |
| 91 | + * |
| 92 | + * @return ResponseInterface |
| 93 | + */ |
| 94 | + public function execute() |
| 95 | + { |
| 96 | + try { |
| 97 | + |
| 98 | + $cartId = $this->request->getParam('cart_id'); |
| 99 | + $signature = $this->request->getParam('signature'); |
| 100 | + |
| 101 | + if (!$this->authenticator->verifySignature($cartId, $signature)) { |
| 102 | + $this->fbeHelper->log( |
| 103 | + "RSA Verification Failed for cartId: {$cartId} with signature: {$signature}" |
| 104 | + ); |
| 105 | + throw new LocalizedException(__('RSA Signature Validation Failed')); |
| 106 | + } |
| 107 | + |
| 108 | + $quoteId = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id')->getQuoteId(); |
| 109 | + $quote = $this->quoteRepository->get($quoteId); |
| 110 | + $this->checkoutSession->replaceQuote($quote); |
| 111 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 112 | + $resultRedirect->setPath('checkout/cart'); |
| 113 | + return $resultRedirect; |
| 114 | + } catch (\Exception $e) { |
| 115 | + $this->fbeHelper->logExceptionImmediatelyToMeta( |
| 116 | + $e, |
| 117 | + [ |
| 118 | + 'event' => 'guest_cart_link', |
| 119 | + 'event_type' => 'cart_redirect', |
| 120 | + 'extra_data' => [ |
| 121 | + 'signature' => $signature, |
| 122 | + 'cart_id' => $cartId, |
| 123 | + ], |
| 124 | + ] |
| 125 | + ); |
| 126 | + $resultRedirect = $this->resultRedirectFactory->create(); |
| 127 | + $resultRedirect->setPath('checkout/cart'); |
| 128 | + return $resultRedirect; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments