|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ReCaptchaPaypal\Observer; |
| 9 | + |
| 10 | +use Magento\Framework\App\Action\Action; |
| 11 | +use Magento\Framework\App\ActionFlag; |
| 12 | +use Magento\Framework\Event\Observer; |
| 13 | +use Magento\Framework\Event\ObserverInterface; |
| 14 | +use Magento\Framework\Exception\InputException; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\Serialize\SerializerInterface; |
| 17 | +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; |
| 18 | +use Magento\ReCaptchaUi\Model\CaptchaResponseResolverInterface; |
| 19 | +use Magento\ReCaptchaUi\Model\ValidationConfigResolverInterface; |
| 20 | +use Magento\ReCaptchaValidationApi\Api\ValidatorInterface; |
| 21 | +use Psr\Log\LoggerInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * AjaxLoginObserver |
| 25 | + */ |
| 26 | +class PayPalObserver implements ObserverInterface |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var CaptchaResponseResolverInterface |
| 30 | + */ |
| 31 | + private $captchaResponseResolver; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ValidationConfigResolverInterface |
| 35 | + */ |
| 36 | + private $validationConfigResolver; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ValidatorInterface |
| 40 | + */ |
| 41 | + private $captchaValidator; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ActionFlag |
| 45 | + */ |
| 46 | + private $actionFlag; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var SerializerInterface |
| 50 | + */ |
| 51 | + private $serializer; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var IsCaptchaEnabledInterface |
| 55 | + */ |
| 56 | + private $isCaptchaEnabled; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var LoggerInterface |
| 60 | + */ |
| 61 | + private $logger; |
| 62 | + |
| 63 | + /** |
| 64 | + * @param CaptchaResponseResolverInterface $captchaResponseResolver |
| 65 | + * @param ValidationConfigResolverInterface $validationConfigResolver |
| 66 | + * @param ValidatorInterface $captchaValidator |
| 67 | + * @param ActionFlag $actionFlag |
| 68 | + * @param SerializerInterface $serializer |
| 69 | + * @param IsCaptchaEnabledInterface $isCaptchaEnabled |
| 70 | + * @param LoggerInterface $logger |
| 71 | + */ |
| 72 | + public function __construct( |
| 73 | + CaptchaResponseResolverInterface $captchaResponseResolver, |
| 74 | + ValidationConfigResolverInterface $validationConfigResolver, |
| 75 | + ValidatorInterface $captchaValidator, |
| 76 | + ActionFlag $actionFlag, |
| 77 | + SerializerInterface $serializer, |
| 78 | + IsCaptchaEnabledInterface $isCaptchaEnabled, |
| 79 | + LoggerInterface $logger |
| 80 | + ) { |
| 81 | + $this->captchaResponseResolver = $captchaResponseResolver; |
| 82 | + $this->validationConfigResolver = $validationConfigResolver; |
| 83 | + $this->captchaValidator = $captchaValidator; |
| 84 | + $this->actionFlag = $actionFlag; |
| 85 | + $this->serializer = $serializer; |
| 86 | + $this->isCaptchaEnabled = $isCaptchaEnabled; |
| 87 | + $this->logger = $logger; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Validates reCaptcha response. |
| 92 | + * |
| 93 | + * @param Observer $observer |
| 94 | + * @return void |
| 95 | + * @throws LocalizedException |
| 96 | + */ |
| 97 | + public function execute(Observer $observer): void |
| 98 | + { |
| 99 | + $key = 'paypal_payflowpro'; |
| 100 | + if ($this->isCaptchaEnabled->isCaptchaEnabledFor($key)) { |
| 101 | + /** @var Action $controller */ |
| 102 | + $controller = $observer->getControllerAction(); |
| 103 | + $request = $controller->getRequest(); |
| 104 | + $response = $controller->getResponse(); |
| 105 | + |
| 106 | + $validationConfig = $this->validationConfigResolver->get($key); |
| 107 | + |
| 108 | + try { |
| 109 | + $reCaptchaResponse = $this->captchaResponseResolver->resolve($request); |
| 110 | + } catch (InputException $e) { |
| 111 | + $this->logger->error($e); |
| 112 | + |
| 113 | + $jsonPayload = $this->serializer->serialize([ |
| 114 | + 'success' => false, |
| 115 | + 'error' => true, |
| 116 | + 'error_messages' => $validationConfig->getValidationFailureMessage(), |
| 117 | + ]); |
| 118 | + $response->representJson($jsonPayload); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + $validationResult = $this->captchaValidator->isValid($reCaptchaResponse, $validationConfig); |
| 123 | + if (false === $validationResult->isValid()) { |
| 124 | + $this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true); |
| 125 | + |
| 126 | + $jsonPayload = $this->serializer->serialize([ |
| 127 | + 'success' => false, |
| 128 | + 'error' => true, |
| 129 | + 'error_messages' => $validationConfig->getValidationFailureMessage(), |
| 130 | + ]); |
| 131 | + |
| 132 | + $response->representJson($jsonPayload); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments