99
1010use Magento \Framework \App \Action \Action ;
1111use Magento \Framework \App \ActionFlag ;
12+ use Magento \Framework \App \ObjectManager ;
1213use Magento \Framework \App \ResponseInterface ;
1314use Magento \Framework \Event \Observer ;
1415use Magento \Framework \Event \ObserverInterface ;
1516use Magento \Framework \Exception \InputException ;
1617use Magento \Framework \Exception \LocalizedException ;
1718use Magento \Framework \Serialize \SerializerInterface ;
1819use Magento \ReCaptchaUi \Model \CaptchaResponseResolverInterface ;
20+ use Magento \ReCaptchaUi \Model \ErrorMessageConfigInterface ;
1921use Magento \ReCaptchaUi \Model \IsCaptchaEnabledInterface ;
2022use Magento \ReCaptchaUi \Model \ValidationConfigResolverInterface ;
2123use Magento \ReCaptchaValidationApi \Api \ValidatorInterface ;
24+ use Magento \ReCaptchaValidationApi \Model \ValidationErrorMessagesProvider ;
2225use Psr \Log \LoggerInterface ;
2326
2427/**
@@ -61,6 +64,16 @@ class PayPalObserver implements ObserverInterface
6164 */
6265 private $ logger ;
6366
67+ /**
68+ * @var ErrorMessageConfigInterface
69+ */
70+ private $ errorMessageConfig ;
71+
72+ /**
73+ * @var ValidationErrorMessagesProvider
74+ */
75+ private $ validationErrorMessagesProvider ;
76+
6477 /**
6578 * @param CaptchaResponseResolverInterface $captchaResponseResolver
6679 * @param ValidationConfigResolverInterface $validationConfigResolver
@@ -69,6 +82,8 @@ class PayPalObserver implements ObserverInterface
6982 * @param SerializerInterface $serializer
7083 * @param IsCaptchaEnabledInterface $isCaptchaEnabled
7184 * @param LoggerInterface $logger
85+ * @param ErrorMessageConfigInterface|null $errorMessageConfig
86+ * @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
7287 */
7388 public function __construct (
7489 CaptchaResponseResolverInterface $ captchaResponseResolver ,
@@ -77,7 +92,9 @@ public function __construct(
7792 ActionFlag $ actionFlag ,
7893 SerializerInterface $ serializer ,
7994 IsCaptchaEnabledInterface $ isCaptchaEnabled ,
80- LoggerInterface $ logger
95+ LoggerInterface $ logger ,
96+ ?ErrorMessageConfigInterface $ errorMessageConfig = null ,
97+ ?ValidationErrorMessagesProvider $ validationErrorMessagesProvider = null
8198 ) {
8299 $ this ->captchaResponseResolver = $ captchaResponseResolver ;
83100 $ this ->validationConfigResolver = $ validationConfigResolver ;
@@ -86,6 +103,10 @@ public function __construct(
86103 $ this ->serializer = $ serializer ;
87104 $ this ->isCaptchaEnabled = $ isCaptchaEnabled ;
88105 $ this ->logger = $ logger ;
106+ $ this ->errorMessageConfig = $ errorMessageConfig
107+ ?? ObjectManager::getInstance ()->get (ErrorMessageConfigInterface::class);
108+ $ this ->validationErrorMessagesProvider = $ validationErrorMessagesProvider
109+ ?? ObjectManager::getInstance ()->get (ValidationErrorMessagesProvider::class);
89110 }
90111
91112 /**
@@ -110,24 +131,53 @@ public function execute(Observer $observer): void
110131 $ reCaptchaResponse = $ this ->captchaResponseResolver ->resolve ($ request );
111132 } catch (InputException $ e ) {
112133 $ this ->logger ->error ($ e );
113- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
134+ $ this ->processError (
135+ $ response ,
136+ [],
137+ $ key
138+ );
114139 return ;
115140 }
116141
117142 $ validationResult = $ this ->captchaValidator ->isValid ($ reCaptchaResponse , $ validationConfig );
118143 if (false === $ validationResult ->isValid ()) {
119- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
144+ $ this ->processError (
145+ $ response ,
146+ $ validationResult ->getErrors (),
147+ $ key
148+ );
120149 }
121150 }
122151 }
123152
124153 /**
154+ * Process errors from reCAPTCHA response.
155+ *
125156 * @param ResponseInterface $response
126- * @param string $message
157+ * @param array $errorMessages
158+ * @param string $sourceKey
127159 * @return void
128160 */
129- private function processError (ResponseInterface $ response , string $ message ): void
161+ private function processError (ResponseInterface $ response , array $ errorMessages , string $ sourceKey ): void
130162 {
163+ $ validationErrorText = $ this ->errorMessageConfig ->getValidationFailureMessage ();
164+ $ technicalErrorText = $ this ->errorMessageConfig ->getTechnicalFailureMessage ();
165+
166+ $ message = $ errorMessages ? $ validationErrorText : $ technicalErrorText ;
167+
168+ foreach ($ errorMessages as $ errorMessageCode => $ errorMessageText ) {
169+ if (!$ this ->isValidationError ($ errorMessageCode )) {
170+ $ message = $ technicalErrorText ;
171+ $ this ->logger ->error (
172+ __ (
173+ 'reCAPTCHA \'%1 \' form error: %2 ' ,
174+ $ sourceKey ,
175+ $ errorMessageText
176+ )
177+ );
178+ }
179+ }
180+
131181 $ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
132182
133183 $ jsonPayload = $ this ->serializer ->serialize ([
@@ -137,4 +187,15 @@ private function processError(ResponseInterface $response, string $message): voi
137187 ]);
138188 $ response ->representJson ($ jsonPayload );
139189 }
190+
191+ /**
192+ * Check if error code present in validation errors list.
193+ *
194+ * @param string $errorMessageCode
195+ * @return bool
196+ */
197+ private function isValidationError (string $ errorMessageCode ): bool
198+ {
199+ return $ errorMessageCode !== $ this ->validationErrorMessagesProvider ->getErrorMessage ($ errorMessageCode );
200+ }
140201}
0 commit comments