Skip to content

Commit 30ec19a

Browse files
committed
Switch contact form from Salesforce to Jira
1 parent 92f6f57 commit 30ec19a

File tree

4 files changed

+39
-165
lines changed

4 files changed

+39
-165
lines changed

composer.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
{
2-
"require": {
3-
"firebase/php-jwt": "v6.10.0",
4-
"ext-json": "*",
5-
"ext-curl": "*"
6-
},
72
"name": "sebs-scholarship/website",
83
"description": "Website for the SEBS Scholarship Foundation"
94
}

composer.lock

Lines changed: 3 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.phar

630 KB
Binary file not shown.

email/contact.php

Lines changed: 36 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
<?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-
112
function validate(): bool {
123
return isset($_POST["name"]) && strlen($_POST["name"]) > 0 && isset($_POST["email"])
134
&& strlen($_POST["email"]) > 0 && isset($_POST["message"]) && strlen($_POST["message"]) > 0
@@ -30,104 +21,71 @@ function verifyRecaptcha($endpoint, $config): int {
3021
$code = 2;
3122
}
3223

33-
curl_close($ch);
3424
return $code;
3525
}
3626

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,
5032
));
5133

52-
$ch = curl_init($endpoint);
34+
$ch = curl_init($baseUrl . '/customer');
5335
curl_setopt($ch, CURLOPT_POST, 1);
5436
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
55-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5637
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)
5941
));
60-
$response = curl_exec($ch);
61-
$token = null;
42+
curl_exec($ch);
6243

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;
6547
}
6648

67-
curl_close($ch);
68-
return $token;
49+
return false;
6950
}
7051

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+
7258
$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'
7867
));
7968

80-
$ch = curl_init($endpoint);
69+
$ch = curl_init($baseUrl . '/request');
8170
curl_setopt($ch, CURLOPT_POST, 1);
8271
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
83-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
8472
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
8573
'Authorization: Bearer ' . $token,
8674
'Content-Type: application/json',
8775
'Content-Length: ' . strlen($data)
8876
));
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-
));
11677
curl_exec($ch);
11778

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;
12181
}
12282

123-
curl_close($ch);
124-
return $status;
83+
return false;
12584
}
12685

12786
$config = include('../../config.php');
12887

12988
$recaptchaEndpoint = "https://www.google.com/recaptcha/api/siteverify"; // reCAPTCHA API
130-
$oauthEndpoint = "https://login.salesforce.com/services/oauth2/token"; // OAuth 2.0 Token API
13189

13290
if (!validate()) { // Check if request had all required info
13391
http_response_code(400);
@@ -143,27 +101,15 @@ function notifyRecipient($endpoint, $token, $id): bool {
143101
exit('reCAPTCHA verification failed. Are you a robot?');
144102
}
145103

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']);
155106

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"])) {
158109
http_response_code(500);
159110
exit('There was an error submitting your message.');
160111
}
161112

162-
if (!notifyRecipient($notifyEndpoint, $token, $id)) {
163-
http_response_code(500);
164-
exit('There was an error sending your confirmation message.');
165-
}
166-
167113
exit('Message has been sent!');
168114

169115

0 commit comments

Comments
 (0)