· 7 years ago · Aug 08, 2018, 10:56 AM
1<form name="freeeventForm" id="freeeventForm">
2 <div id="RecaptchaField"></div>
3 <input type="hidden" class="validate-reCAPTCHA">
4</form>
5
6<script src="https://www.google.com/recaptcha/api.js?onload=CaptchaCallback&render=explicit" async defer></script>
7<script type="text/javascript">
8 //< ![CDATA[
9 var CaptchaCallback = function() {
10 grecaptcha.render('RecaptchaField', {'sitekey' : '6LeuiDwUAAAAALByt-xxxxxxxxxxx-xUsZHFkeEP'});
11 };
12 var customForm = new VarienForm('freeeventForm');
13 Validation.add('validate-reCAPTCHA','reCAPTCHA is mandatory',function(){
14 var response = grecaptcha.getResponse();
15 if (response.length === 0) {
16 return false;
17 }
18 return true;
19});
20 //]]>
21</script>
22
23public function checkCaptcha(array $post)
24{
25 if ($post['g-recaptcha-response']) {
26 $captcha = $post['g-recaptcha-response'];
27
28 $secretKey = Mage::helper('gorilla_recaptcha')->getRecaptchaPrivateKey(); // replace this with private key
29 $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $captcha);
30 $responseKeys = json_decode($response, true);
31
32 if (intval($responseKeys["success"]) !== 1) {
33 return "failed";
34 } else {
35 return "success";
36 }
37 }
38 else {
39 return "failed";
40 }
41}