Skip to content

Commit

Permalink
Merge pull request #25 from sprankhub/verification-response-processing
Browse files Browse the repository at this point in the history
Verification response processing
  • Loading branch information
DanieliMi authored Apr 4, 2023
2 parents 06100b1 + 845970f commit 99d298d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Model/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace IMI\FriendlyCaptcha\Model;

use IMI\FriendlyCaptcha\Api\ValidateInterface;
use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\HTTP\Client\CurlFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function validate(string $friendlyCaptchaSolution): bool
$curl->post($this->getSiteVerifyUrl(), $parameters);
$response = $this->serializer->unserialize($curl->getBody());

if ($curl->getStatus() === 200) {
if ($this->shouldUseResponse($curl, $response)) {
return $response['success'];
} else {
$this->logger->error('Error validating captcha solution.', ['response' => var_export($response, true)]);
Expand All @@ -93,12 +94,22 @@ public function validate(string $friendlyCaptchaSolution): bool
return true;
}

public function getSiteVerifyUrl(): string
private function getSiteVerifyUrl(): string
{
if ($this->config->useEuEndpoint()) {
return 'https://eu-api.friendlycaptcha.eu/api/v1/siteverify';
}

return 'https://api.friendlycaptcha.com/api/v1/siteverify';
}

private function shouldUseResponse(Curl $curl, $response): bool
{
$isResponseOk = $curl->getStatus() === 200;
$isSolutionMissingOrBadRequest = $curl->getStatus() === 400
&& isset($response['success'], $response['errors'])
&& array_intersect($response['errors'], ['solution_missing', 'bad_request']);

return $isResponseOk || $isSolutionMissingOrBadRequest;
}
}

0 comments on commit 99d298d

Please sign in to comment.