-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLEX_apex_controller_cls
54 lines (51 loc) · 1.96 KB
/
LEX_apex_controller_cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
*author: Chris Ludovice
*@date : 06.08.2020
*description: apex class controller responsible for:
- verification of token validity in the hcapatcha API endpoint.
- creation of web case in the database.
@revision(s)
*/
public class HcaptchaCreateCase {
@AuraEnabled
public static Boolean verifyForm(String captchaResponse,String subject,String description){
//Instantiate an httpRequest
HttpRequest request = new HttpRequest();
String key='SECRET_KEY'; // SECRET_KEY replace with your secret key
String url='https://hcaptcha.com/siteverify';
request.setEndpoint(url);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String payload = 'response='+EncodingUtil.urlEncode(token,'UTF-8')+'&secret='+EncodingUtil.urlEncode(key,'UTF-8');
request.setBody(payload);
Http http = new Http();
HttpResponse response = new HttpResponse();
response = http.send(request);
Json2Apex res = new Json2Apex();
if (response.getStatusCode() == 200) {
try{
res = parse(response.getBody()); // parse hcaptcha json response to apex object
}
catch(Exception e) {
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
Case c = new Case();
c.Subject=subject;
c.Type='TEST';
c.Status='New';
c.Description=description;
c.Origin='Web';
if(res.success)
insert c;
return res.success;
}
public class JSON2Apex{ // create wrapper class to parse hcaptcha payload to apex objects
public Boolean success;
public String challenge_ts;
public String hostname;
}
public static JSON2Apex parse(String json){
return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}
}