9
9
10
10
use Magento \Framework \App \Action \Action ;
11
11
use Magento \Framework \App \ActionFlag ;
12
+ use Magento \Framework \App \ObjectManager ;
12
13
use Magento \Framework \App \ResponseInterface ;
13
14
use Magento \Framework \Event \Observer ;
14
15
use Magento \Framework \Event \ObserverInterface ;
15
16
use Magento \Framework \Exception \InputException ;
16
17
use Magento \Framework \Exception \LocalizedException ;
17
18
use Magento \Framework \Serialize \SerializerInterface ;
18
19
use Magento \ReCaptchaUi \Model \CaptchaResponseResolverInterface ;
20
+ use Magento \ReCaptchaUi \Model \ErrorMessageConfigInterface ;
19
21
use Magento \ReCaptchaUi \Model \IsCaptchaEnabledInterface ;
20
22
use Magento \ReCaptchaUi \Model \ValidationConfigResolverInterface ;
21
23
use Magento \ReCaptchaValidationApi \Api \ValidatorInterface ;
24
+ use Magento \ReCaptchaValidationApi \Model \ValidationErrorMessagesProvider ;
22
25
use Psr \Log \LoggerInterface ;
23
26
24
27
/**
@@ -61,6 +64,16 @@ class PayPalObserver implements ObserverInterface
61
64
*/
62
65
private $ logger ;
63
66
67
+ /**
68
+ * @var ErrorMessageConfigInterface
69
+ */
70
+ private $ errorMessageConfig ;
71
+
72
+ /**
73
+ * @var ValidationErrorMessagesProvider
74
+ */
75
+ private $ validationErrorMessagesProvider ;
76
+
64
77
/**
65
78
* @param CaptchaResponseResolverInterface $captchaResponseResolver
66
79
* @param ValidationConfigResolverInterface $validationConfigResolver
@@ -69,6 +82,8 @@ class PayPalObserver implements ObserverInterface
69
82
* @param SerializerInterface $serializer
70
83
* @param IsCaptchaEnabledInterface $isCaptchaEnabled
71
84
* @param LoggerInterface $logger
85
+ * @param ErrorMessageConfigInterface|null $errorMessageConfig
86
+ * @param ValidationErrorMessagesProvider|null $validationErrorMessagesProvider
72
87
*/
73
88
public function __construct (
74
89
CaptchaResponseResolverInterface $ captchaResponseResolver ,
@@ -77,7 +92,9 @@ public function __construct(
77
92
ActionFlag $ actionFlag ,
78
93
SerializerInterface $ serializer ,
79
94
IsCaptchaEnabledInterface $ isCaptchaEnabled ,
80
- LoggerInterface $ logger
95
+ LoggerInterface $ logger ,
96
+ ?ErrorMessageConfigInterface $ errorMessageConfig = null ,
97
+ ?ValidationErrorMessagesProvider $ validationErrorMessagesProvider = null
81
98
) {
82
99
$ this ->captchaResponseResolver = $ captchaResponseResolver ;
83
100
$ this ->validationConfigResolver = $ validationConfigResolver ;
@@ -86,6 +103,10 @@ public function __construct(
86
103
$ this ->serializer = $ serializer ;
87
104
$ this ->isCaptchaEnabled = $ isCaptchaEnabled ;
88
105
$ this ->logger = $ logger ;
106
+ $ this ->errorMessageConfig = $ errorMessageConfig
107
+ ?? ObjectManager::getInstance ()->get (ErrorMessageConfigInterface::class);
108
+ $ this ->validationErrorMessagesProvider = $ validationErrorMessagesProvider
109
+ ?? ObjectManager::getInstance ()->get (ValidationErrorMessagesProvider::class);
89
110
}
90
111
91
112
/**
@@ -110,24 +131,53 @@ public function execute(Observer $observer): void
110
131
$ reCaptchaResponse = $ this ->captchaResponseResolver ->resolve ($ request );
111
132
} catch (InputException $ e ) {
112
133
$ this ->logger ->error ($ e );
113
- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
134
+ $ this ->processError (
135
+ $ response ,
136
+ [],
137
+ $ key
138
+ );
114
139
return ;
115
140
}
116
141
117
142
$ validationResult = $ this ->captchaValidator ->isValid ($ reCaptchaResponse , $ validationConfig );
118
143
if (false === $ validationResult ->isValid ()) {
119
- $ this ->processError ($ response , $ validationConfig ->getValidationFailureMessage ());
144
+ $ this ->processError (
145
+ $ response ,
146
+ $ validationResult ->getErrors (),
147
+ $ key
148
+ );
120
149
}
121
150
}
122
151
}
123
152
124
153
/**
154
+ * Process errors from reCAPTCHA response.
155
+ *
125
156
* @param ResponseInterface $response
126
- * @param string $message
157
+ * @param array $errorMessages
158
+ * @param string $sourceKey
127
159
* @return void
128
160
*/
129
- private function processError (ResponseInterface $ response , string $ message ): void
161
+ private function processError (ResponseInterface $ response , array $ errorMessages , string $ sourceKey ): void
130
162
{
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
+
131
181
$ this ->actionFlag ->set ('' , Action::FLAG_NO_DISPATCH , true );
132
182
133
183
$ jsonPayload = $ this ->serializer ->serialize ([
@@ -137,4 +187,15 @@ private function processError(ResponseInterface $response, string $message): voi
137
187
]);
138
188
$ response ->representJson ($ jsonPayload );
139
189
}
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
+ }
140
201
}
0 commit comments