function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pooja GLPooja GL 

Issue with reCaptcha functionality

Hi,

I have a Google reCaptcha class and VF page created separately in the Org. i want to use this reCaptcha on one other VF page (register page) existing on the same org for which I have embedded the below line in the VF page. This is somehow making the captcha images available on the register page, but the functionality is not at all working.

<apex:include pageName="reCAPTCHA"/>

I would need your expertise on this.

 
Swaraj Behera 7Swaraj Behera 7
Can you please post the Vf Page reCAPTCHA and Class.
Thanks
Pooja GLPooja GL
Hi Swaraj,

Please find the below codes.

VF page :

<apex:page controller="reCAPTCHA"> <script src='https://www.google.com/recaptcha/api.js'></script> <apex:pageBlock title="Captcha"> <apex:form id="formid"> <apex:pageBlockSection columns="1"> <!-- <apex:pageBlockSectionItem > <apex:outputLabel for="inputName" value="Name"/> <apex:inputText value="{!myName}" id="inputName"/> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:outputLabel for="inputEmail" value="Email"/> <apex:inputText value="{!myEmail}" id="inputEmail"/> </apex:pageBlockSectionItem> --> <!-- Show the reCAPTCHA form if they are not yet verified --> <apex:pageBlockSectionItem rendered="false"> <!-- reCAPTCHA verification Source: https://developers.google.com/recaptcha/docs/display --> <script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k={!publicKey}"> </script> <noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k={!publicKey}" height="300" width="500" frameborder="0"></iframe><br/> <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> <textarea name="g-recaptcha-response" rows="3" cols="40"></textarea> <input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> </noscript> </apex:pageBlockSectionItem> <apex:pageBlockSectionItem > <apex:commandButton action="{!verify}" value="Check If I'm Human" rendered="{! NOT(verified)}" reRender="formid"/> </apex:pageBlockSectionItem> <!-- Otherwise, they are verified, show a confirmation message --> <apex:pageBlockSectionItem rendered="{!verified}"> <p>reCAPTCHA verification suggests that you're not a 'bot.</p> </apex:pageBlockSectionItem> </apex:pageBlockSection> <div class="g-recaptcha" data-sitekey="6LfZBhwTAAAAAOFFthnln_xT2Z0sUePJ_omPXvVz"></div> </apex:form> </apex:pageBlock> </apex:page>

Controller Class :

/* Public class for reCAPTCHA */ public class reCAPTCHA {     /* Configuration */     // The API endpoint for the reCAPTCHA service     private static String baseUrl = 'https://www.google.com/recaptcha/api/siteverify';      // The keys you get by signing up for reCAPTCHA for your domain     private static String privateKey = '6LfZBhwTAAAAAE1Y7BOIEsPPtBWW_pHslYSEcYca';     public String publicKey {          get { return '6LfZBhwTAAAAAOFFthnln_xT2Z0sUePJ_omPXvVz'; }     }      /* Implementation */          // Simple form fields for the example form     public String myName { get; set; }     public String myEmail { get; set; }          // Create properties for the non-VF component input fields generated     // by the reCAPTCHA JavaScript.     public String challenge {          get {             return ApexPages.currentPage().getParameters().get('recaptcha_challenge_field');         }     }     public String response  {          get {             return ApexPages.currentPage().getParameters().get('g-recaptcha-response');         }     }          // Whether the submission has passed reCAPTCHA validation or not     public Boolean verified { get; private set; }     /* Constructor call for reCAPTCHA */     public reCAPTCHA() {         this.verified = false;     }     /* Calling Verify method */     public void verify() {         System.debug('reCAPTCHA verification attempt');         // On first page load, form is empty, so no request to make yet        /* if ( challenge == null || response == null ) {              System.debug('reCAPTCHA verification attempt with empty form');             return null;          }                              HttpResponse r = makeRequest(baseUrl,             'privatekey=' + privateKey +              '&remoteip='  + remoteHost +              '&challenge=' + challenge +             '&response='  + response         );*/         HttpResponse r = makeRequest(baseUrl,'secret=' + privateKey +'&response='+response);         system.debug('Response'+r);                  if ( r!= null ) {             this.verified = (r.getBody().startsWithIgnoreCase('true'));         }                  if(this.verified) {             // If they pass verification, you might do something interesting here             // Or simply return a PageReference to the "next" page             //return null;         }         else {             // stay on page to re-try reCAPTCHA             //return null;          }     } /* define PageReference reset method */     public PageReference reset() {         return null;      }        /* Private helper methods */     private static HttpResponse makeRequest(string url, string body)  {         HttpResponse response = null;         HttpRequest req = new HttpRequest();            req.setEndpoint(url);         req.setMethod('POST');         req.setBody (body);         try {             Http http = new Http();             if(test.isRunningTest()){                 response.setBody('True');                 return response;             }                          response = http.send(req);             System.debug('reCAPTCHA response: ' + response);             System.debug('reCAPTCHA body: ' + response.getBody());         } catch(System.Exception e) {             System.debug('ERROR: ' + e);         }         return response;     }             /* define remoteHost */     private String remoteHost {          get {              String ret = '127.0.0.1';             // also could use x-original-remote-host              Map<String, String> hdrs = ApexPages.currentPage().getHeaders();             if (hdrs.get('x-original-remote-addr')!= null)                 ret =  hdrs.get('x-original-remote-addr');             else if (hdrs.get('X-Salesforce-SIP')!= null)                 ret =  hdrs.get('X-Salesforce-SIP');             return ret;         }     }       }
Wei Hua 5Wei Hua 5
Hi Pooja, is reCAPTCHA working in your original VFP?  If yes, what domian did you use when registered?  salesforce.com?  Thanks.
Yogendra JangidYogendra Jangid
Hi 
I would recommend using LWC and this removes the use of VF page for implementing google recaptcha
https://inevitableyogendra.blogspot.com/2021/09/using-google-recaptcha-v2-in-lightning-web-component.html