Uploading inital module

This commit is contained in:
TheLeo 2024-08-29 15:14:04 +00:00
parent f1dc2680a3
commit 0aed3e8196
3 changed files with 64 additions and 0 deletions

51
FDShim_Controller.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace Drupal\FreshdeskShim\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class FDShim_Controller extends ControllerBase {
public function fd_route(Request $request) {
if ($request->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);
}
}
}

6
FreshdeskShim.info.yml Normal file
View File

@ -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.*

View File

@ -0,0 +1,7 @@
FresdeskShim.content:
path: '/FreshdeskShim'
defaults:
_controller: '\Drupal\FreshdeskShim\Controller\FDShim_Controller::fd_route'
_title: 'Freshdesk Shim'
requirements:
_permission: 'access content'