79 lines
3.1 KiB
PHP
79 lines
3.1 KiB
PHP
<?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();
|
|
|
|
// Data Validation
|
|
if (!isset($request['name']) || !isset($request['phone']) || !isset($request['email'])) {
|
|
return new JsonResponse(['error' => 'Bad Request'], 400);
|
|
}
|
|
|
|
// 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
|
|
];
|
|
$descriptionString =
|
|
'<div>Name: ' . $request['name'] . '</div>' .
|
|
'<div>Date: ' . $request['date'] . '</div>' .
|
|
'<div>Phone: ' . $request['phone_num'] . '</div>' .
|
|
'<div>Hinge Type: ' . $request['hinge_type'] . '</div>' .
|
|
'<div>Ceiling Greater than 14ft: ' . $request['ceiling_height'] . '</div>' .
|
|
'<div>Loading Dock: ' . $request['loading_dock'] . '</div>' .
|
|
'<div>Basement Under Baler: ' . $request['baler_over_basement'] . '</div>' .
|
|
'<div>Voltage: ' . $request['voltage'] . '</div>';
|
|
|
|
// Converting the POST request data into the correct format for Fresdesk ticket creation
|
|
$postData = [
|
|
'name' => $request['name'],
|
|
'email' => $request['email'],
|
|
'phone' => $request['phone'],
|
|
'subject' => '[Webform Submission] ' . $request['name'] . ' Baler Request',
|
|
'description' => $descriptionString,
|
|
'attachments[]' => $request['attachments[]'],
|
|
'status' => '2',
|
|
'priority' => '1',
|
|
'group' => '154000339633'
|
|
];
|
|
|
|
|
|
// cURL Options
|
|
curl_setopt($fd, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($fd, CURLOPT_POST, true);
|
|
curl_setopt($fd, CURLOPT_POSTFIELDS, $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);
|
|
}
|
|
}
|
|
} |