Formit custom hook snippet validation - using hCaptcha

I’m using hcaptcha as a Formit hook like this:

[[FormIt?
    &hooks=`hcaptcha, ...`
    ...]]

<div class="h-captcha" data-sitekey="[YOUR-SITE_KEY]"></div>

<script src='https://www.hCaptcha.com/1/api.js' async defer></script>

Then the hcaptcha Snippet:

<?php
$data = array(
  'secret' => "[YOUR-SECRET-KEY]",
  'response' => $_POST['h-captcha-response']
);$verify = curl_init();

curl_setopt($verify, CURLOPT_URL, "https://hcaptcha.com/siteverify");
curl_setopt($verify, CURLOPT_POST, true);
curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($verify); 
// var_dump($response);

$responseData = json_decode($response);
if($responseData->success) {
  return true;
} 
else {
  //$validator->addError('captcha','Captcha invalid.');
  $hook->addError('captcha','Error: Please complete the hCaptcha.');
  return false;
}
1 Like