· 7 years ago · Sep 12, 2018, 04:26 PM
1<div data-sitekey="<?php echo $this->getSiteKey() ?>"
2 class="g-recaptcha required-entry"
3 style="margin-top:10px">
4</div>
5
6...
7 function checkCaptchaAndSubscribe(thisContext)
8 {
9 var captchaResponse = grecaptcha.getResponse();
10
11 if (captchaResponse == "") {
12 $captchaRequired.css('display', 'block');
13 return false;
14 } else {
15
16 $captchaRequired.css('display', 'none');
17 $status.html("<h2 class='nl2go_h2'>Die Captcha Validierung ist fehlgeschlagen!</h2>");
18 $statusContainer.show();
19
20 grecaptcha.reset();
21
22 //do something...
23 subscribe(thisContext);
24
25 }
26 }
27
28function checkCaptchaAndSubscribe(thisContext)
29 {
30 var captchaResponse = grecaptcha.getResponse("reCAPTCHA_newsletter");
31
32 if (captchaResponse == "") {
33 $captchaRequired.css('display', 'block');
34 return false;
35 }
36
37 grecaptcha.reset();
38 $captchaRequired.css('display', 'none');
39
40 jQuery.ajax({
41 url: "/fekete_newsletter2go/index/verify",
42 method: "POST",
43 async: "true",
44 data: {
45 recaptchaResponse: captchaResponse
46 },
47 success: function(response) {
48
49 $statusContainer.show();
50
51 if (response != "success") {
52 $status.html("<h2 class='nl2go_h2'>Die Captcha Validierung ist fehlgeschlagen!</h2>");
53 return false;
54 }
55
56 subscribe(thisContext);
57 }
58 });
59 }
60
61public function verifyAction()
62 {
63 $captchaResponse = $this->getRequest()->getParam('recaptchaResponse');
64
65 if (!isset($captchaResponse) || empty($captchaResponse)) {
66 return "captcha response is empty";
67 }
68
69 $secretKey = Mage::Helper("recaptcha")->getSecretKey();
70
71 $url = 'https://www.google.com/recaptcha/api/siteverify';
72 $data = array(
73 'secret' => $secretKey,
74 'response' => $captchaResponse,
75 );
76
77 // use key 'http' even if you send the request to https://...
78 $options = array(
79 'http' => array(
80 'header' => "Content-type: application/x-www-form-urlencodedrn",
81 'method' => 'POST',
82 'content' => http_build_query($data)
83 )
84 );
85
86 $context = stream_context_create($options);
87
88 $result = file_get_contents($url, false, $context);
89 $result = json_decode($result);
90
91 if ($result->success) {
92 echo "success";
93 } else {
94 echo "fail";
95 }
96 }