1
1
<?php
2
- // Salesforce REST API: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_list.htm
3
- // Salesforce Cases API: https://developer.salesforce.com/docs/atlas.en-us.226.0.object_reference.meta/object_reference/sforce_api_objects_case.htm
4
- // Salesforce JWT OAuth: https://help.salesforce.com/articleView?id=remoteaccess_oauth_jwt_flow.htm&type=5
5
- // OAuth Authorization: https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=3MVG9Kip4IKAZQEXRsS0YD5c1R6FtIVV6IrGlckdJRiGd.B0bIIxaFZ7m9BzSGlkpdTWKLeAz4fIkAlXM4bV7&redirect_uri=https://login.salesforce.com/services/oauth2/success
6
-
7
- use Firebase \JWT \JWT ;
8
-
9
- require ('../vendor/firebase/php-jwt/src/JWT.php ' );
10
-
11
2
function validate (): bool {
12
3
return isset ($ _POST ["name " ]) && strlen ($ _POST ["name " ]) > 0 && isset ($ _POST ["email " ])
13
4
&& strlen ($ _POST ["email " ]) > 0 && isset ($ _POST ["message " ]) && strlen ($ _POST ["message " ]) > 0
@@ -30,104 +21,71 @@ function verifyRecaptcha($endpoint, $config): int {
30
21
$ code = 2 ;
31
22
}
32
23
33
- curl_close ($ ch );
34
24
return $ code ;
35
25
}
36
26
37
- function getToken ($ endpoint , $ config , $ privateKey ) {
38
- $ payload = array (
39
- "iss " => $ config ['sfClientId ' ],
40
- "aud " => "https://login.salesforce.com " ,
41
- "sub " => $ config ['sfUser ' ],
42
- "exp " => strval (time () + (3 * 60 ))
43
- );
44
-
45
- $ jwt = JWT ::encode ($ payload , $ privateKey , 'RS256 ' );
46
-
47
- $ data = http_build_query (array (
48
- 'grant_type ' => 'urn:ietf:params:oauth:grant-type:jwt-bearer ' ,
49
- 'assertion ' => $ jwt
27
+ function createCustomer ($ baseUrl , $ token , $ name , $ email ): bool {
28
+ $ data = json_encode (array (
29
+ 'displayName ' => $ name ,
30
+ 'fullName ' => $ name ,
31
+ 'email ' => $ email ,
50
32
));
51
33
52
- $ ch = curl_init ($ endpoint );
34
+ $ ch = curl_init ($ baseUrl . ' /customer ' );
53
35
curl_setopt ($ ch , CURLOPT_POST , 1 );
54
36
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
55
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
56
37
curl_setopt ($ ch , CURLOPT_HTTPHEADER , array (
57
- 'Content-Type: application/x-www-form-urlencoded ' ,
58
- 'Content-Length: ' . strlen ($ data )
38
+ 'Authorization: Bearer ' . $ token ,
39
+ 'Content-Type: application/json ' ,
40
+ 'Content-Length: ' . strlen ($ data )
59
41
));
60
- $ response = curl_exec ($ ch );
61
- $ token = null ;
42
+ curl_exec ($ ch );
62
43
63
- if (!curl_errno ($ ch ) && curl_getinfo ($ ch , CURLINFO_RESPONSE_CODE ) === 200 ) {
64
- $ token = json_decode ($ response , true );
44
+ $ http_code = curl_getinfo ($ ch , CURLINFO_RESPONSE_CODE );
45
+ if (!curl_errno ($ ch ) && ($ http_code === 200 || $ http_code === 409 )) {
46
+ return true ;
65
47
}
66
48
67
- curl_close ($ ch );
68
- return $ token ;
49
+ return false ;
69
50
}
70
51
71
- function createCase ($ endpoint , $ token ) {
52
+ function createRequest ($ baseUrl , $ token , $ email , $ message ): bool {
53
+ $ summary = $ message ;
54
+ if (strlen ($ summary ) > 50 ) {
55
+ $ summary = substr ($ summary , 0 , 47 ) . "... " ;
56
+ }
57
+
72
58
$ data = json_encode (array (
73
- 'SuppliedName ' => $ _POST ["name " ],
74
- 'SuppliedEmail ' => $ _POST ["email " ],
75
- 'Subject ' => "Contact Form Submission " ,
76
- 'Description ' => $ _POST ["message " ],
77
- 'Origin ' => 'Contact Form '
59
+ 'isAdfRequest ' => false ,
60
+ 'requestFieldValues ' => array (
61
+ 'summary ' => $ summary ,
62
+ 'description ' => $ message ,
63
+ ),
64
+ 'raiseOnBehalfOf ' => $ email ,
65
+ 'requestTypeId ' => "10013 " ,
66
+ 'serviceDeskId ' => '1 '
78
67
));
79
68
80
- $ ch = curl_init ($ endpoint );
69
+ $ ch = curl_init ($ baseUrl . ' /request ' );
81
70
curl_setopt ($ ch , CURLOPT_POST , 1 );
82
71
curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
83
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
84
72
curl_setopt ($ ch , CURLOPT_HTTPHEADER , array (
85
73
'Authorization: Bearer ' . $ token ,
86
74
'Content-Type: application/json ' ,
87
75
'Content-Length: ' . strlen ($ data )
88
76
));
89
- $ response = curl_exec ($ ch );
90
-
91
- $ id = null ;
92
- if (!curl_errno ($ ch ) && curl_getinfo ($ ch , CURLINFO_RESPONSE_CODE ) == 201 ) {
93
- $ id = json_decode ($ response , true )["id " ];
94
- }
95
-
96
- curl_close ($ ch );
97
- return $ id ;
98
- }
99
-
100
- function notifyRecipient ($ endpoint , $ token , $ id ): bool {
101
- $ data = json_encode (array (
102
- 'inputs ' => array (
103
- array ('SObjectRowId ' => $ id )
104
- )
105
- ));
106
-
107
- $ ch = curl_init ($ endpoint );
108
- curl_setopt ($ ch , CURLOPT_POST , 1 );
109
- curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ data );
110
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
111
- curl_setopt ($ ch , CURLOPT_HTTPHEADER , array (
112
- 'Authorization: Bearer ' . $ token ,
113
- 'Content-Type: application/json ' ,
114
- 'Content-Length: ' . strlen ($ data )
115
- ));
116
77
curl_exec ($ ch );
117
78
118
- $ status = false ;
119
- if (!curl_errno ($ ch ) && curl_getinfo ($ ch , CURLINFO_RESPONSE_CODE ) == 200 ) {
120
- $ status = true ;
79
+ if (!curl_errno ($ ch ) && curl_getinfo ($ ch , CURLINFO_RESPONSE_CODE ) == 201 ) {
80
+ return true ;
121
81
}
122
82
123
- curl_close ($ ch );
124
- return $ status ;
83
+ return false ;
125
84
}
126
85
127
86
$ config = include ('../../config.php ' );
128
87
129
88
$ recaptchaEndpoint = "https://www.google.com/recaptcha/api/siteverify " ; // reCAPTCHA API
130
- $ oauthEndpoint = "https://login.salesforce.com/services/oauth2/token " ; // OAuth 2.0 Token API
131
89
132
90
if (!validate ()) { // Check if request had all required info
133
91
http_response_code (400 );
@@ -143,27 +101,15 @@ function notifyRecipient($endpoint, $token, $id): bool {
143
101
exit ('reCAPTCHA verification failed. Are you a robot? ' );
144
102
}
145
103
146
- $ response = getToken ($ oauthEndpoint , $ config , file_get_contents ('../../private ' ));
147
- if (is_null ($ response )) { // Check if application is OAuth authenticated
148
- http_response_code (500 );
149
- exit ('There was an error authenticating your request. ' );
150
- }
151
-
152
- $ token = $ response ["access_token " ];
153
- $ caseEndpoint = $ response ["instance_url " ] . "/services/data/v53.0/sobjects/Case/ " ; // Authenticated Case API
154
- $ notifyEndpoint = $ response ["instance_url " ] . "/services/data/v53.0/actions/custom/emailAlert/Case/Auto_Response/ " ;
104
+ $ baseUrl = 'https://sebsscholarship.atlassian.net/rest/servicedeskapi ' ;
105
+ $ token = base64_encode ($ config ['jiraUser ' ] . ": " . $ config ['jiraApiKey ' ]);
155
106
156
- $ id = createCase ( $ caseEndpoint , $ token); // Submit the case to Salesforce
157
- if ( is_null ( $ id )) {
107
+ if (! createCustomer ( $ baseUrl , $ token, $ _POST [ " name " ], $ _POST [ " email " ])
108
+ || ! createRequest ( $ baseUrl , $ token , $ _POST [ " email " ], $ _POST [ " message " ] )) {
158
109
http_response_code (500 );
159
110
exit ('There was an error submitting your message. ' );
160
111
}
161
112
162
- if (!notifyRecipient ($ notifyEndpoint , $ token , $ id )) {
163
- http_response_code (500 );
164
- exit ('There was an error sending your confirmation message. ' );
165
- }
166
-
167
113
exit ('Message has been sent! ' );
168
114
169
115
0 commit comments