diff --git a/FDShim_Controller.php b/FDShim_Controller.php new file mode 100644 index 0000000..c5835b0 --- /dev/null +++ b/FDShim_Controller.php @@ -0,0 +1,51 @@ +isMethod('POST')) { + $postData = $request->request->all(); + + // Initial variables + $apiUrl = 'https://jvmanufacturing-help.freshdesk.com/api/v2/tickets'; + $apiKey = 'OTBuVjM4cVA0dDdHaXVJeUhoWDpY'; + $fd = curl_init($apiUrl); + $headers = [ + 'Content-Type: multipart/form-data', + 'Authorization: ' . $apiKey + ]; + + // cURL Options + curl_setopt($fd, CURLOPT_RETURNTRANSFER, true); + curl_setopt($fd, CURLOPT_POST, true); + curl_setopt($fd, CURLOPT_POSTFIELDS, http_build_query($postData)); + curl_setopt($fd, CURLOPT_HTTPHEADER, $headers); + + $response = curl_exec($fd); + + // HTTP Response Codes: + // 200 - Request OK + // 405 - Method Not Allowed + // 500 - Internal Error + + if (curl_errno($fd)) { + // cURL error handling + $error = curl_error($fd); + curl_close($fd); + return new JsonResponse(['error' => $error], 500); + } else { + // return cURL response if successful + curl_close($fd); + return new JsonResponse(json_decode($response, true), 200); + } + } else { + // reject all methods that aren't POST requests + return new JsonResponse(['error'=> 'Method Not Allowed'], 405); + } + } +} \ No newline at end of file diff --git a/FreshdeskShim.info.yml b/FreshdeskShim.info.yml new file mode 100644 index 0000000..fcdbcd8 --- /dev/null +++ b/FreshdeskShim.info.yml @@ -0,0 +1,6 @@ +name: Freshdesk Shim API +description: This module takes a POST request from a webform and passess the info back to the Freshdesk API to open a ticket. +package: Custom +type: module +version: 1.0 +core_version_requirement: 10.* diff --git a/FreshdeskShim.routing.yml b/FreshdeskShim.routing.yml new file mode 100644 index 0000000..3a7bb02 --- /dev/null +++ b/FreshdeskShim.routing.yml @@ -0,0 +1,7 @@ +FresdeskShim.content: + path: '/FreshdeskShim' + defaults: + _controller: '\Drupal\FreshdeskShim\Controller\FDShim_Controller::fd_route' + _title: 'Freshdesk Shim' + requirements: + _permission: 'access content' \ No newline at end of file