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 =
'
Name: ' . $request['name'] . '
' .
'Date: ' . $request['date'] . '
' .
'Phone: ' . $request['phone_num'] . '
' .
'Hinge Type: ' . $request['hinge_type'] . '
' .
'Ceiling Greater than 14ft: ' . $request['ceiling_height'] . '
' .
'Loading Dock: ' . $request['loading_dock'] . '
' .
'Basement Under Baler: ' . $request['baler_over_basement'] . '
' .
'Voltage: ' . $request['voltage'] . '
';
// 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);
}
}
}