| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Twilio; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
|
| 8 |
/** |
| 9 |
* Provide functionality for Record insert,upsert |
| 10 |
*/ |
| 11 |
class RecordApiHelper |
| 12 |
{ |
| 13 |
protected $_defaultHeader; |
| 14 |
public static $apiBaseUri = 'https://api.twilio.com/2010-04-01'; |
| 15 |
|
| 16 |
private $_sid; |
| 17 |
|
| 18 |
private $_from_num; |
| 19 |
|
| 20 |
private $logID; |
| 21 |
|
| 22 |
private $_logResponse; |
| 23 |
|
| 24 |
private $_entryID; |
| 25 |
|
| 26 |
private $_formID; |
| 27 |
|
| 28 |
public function __construct($integrationDetails, $sid, $token, $from_num, $logID, $formID, $entryID) |
| 29 |
{ |
| 30 |
$this->_sid = $sid; |
| 31 |
$this->_from_num = $from_num; |
| 32 |
$this->_defaultHeader = [ |
| 33 |
'Authorization' => 'Basic ' . base64_encode("$sid:$token"), |
| 34 |
'Accept' => '*/*', |
| 35 |
'verify' => false, |
| 36 |
'Content-Type' => 'application/x-www-form-urlencoded' |
| 37 |
]; |
| 38 |
$this->_logResponse = new UtilApiResponse(); |
| 39 |
$this->logID = $logID; |
| 40 |
|
| 41 |
$this->_entryID = $entryID; |
| 42 |
$this->_formID = $formID; |
| 43 |
} |
| 44 |
|
| 45 |
public function sendMessage($data) |
| 46 |
{ |
| 47 |
$data['From'] = $this->_from_num; |
| 48 |
$apiEndpoint = self::$apiBaseUri . "/Accounts/$this->_sid/Messages.json"; |
| 49 |
$response = HttpHelper::post($apiEndpoint, $data, $this->_defaultHeader); |
| 50 |
return $response; |
| 51 |
} |
| 52 |
|
| 53 |
public function generateReqDataFromFieldMap($data, $fieldMap) |
| 54 |
{ |
| 55 |
$dataFinal = []; |
| 56 |
|
| 57 |
foreach ($fieldMap as $value) { |
| 58 |
$triggerValue = $value->formField; |
| 59 |
$actionValue = $value->twilioField; |
| 60 |
if ('custom' === $triggerValue) { |
| 61 |
$dataFinal[$actionValue] = $value->customValue; |
| 62 |
} elseif (!is_null($data[$triggerValue])) { |
| 63 |
$dataFinal[$actionValue] = $data[$triggerValue]; |
| 64 |
} |
| 65 |
} |
| 66 |
return $dataFinal; |
| 67 |
} |
| 68 |
|
| 69 |
public function executeRecordApi($integId, $fieldValues, $fieldMap) |
| 70 |
{ |
| 71 |
$finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); |
| 72 |
$apiResponse = $this->sendMessage($finalData); |
| 73 |
|
| 74 |
$entryDetails = [ |
| 75 |
'formId' => $this->_formID, |
| 76 |
'entryId' => $this->_entryID, |
| 77 |
'fieldValues' => $fieldValues |
| 78 |
]; |
| 79 |
|
| 80 |
if ((property_exists($apiResponse, 'status') && 400 === $apiResponse->code) || property_exists($apiResponse, 'code')) { |
| 81 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'errors', $apiResponse, $entryDetails); |
| 82 |
} else { |
| 83 |
$this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'success', $apiResponse, $entryDetails); |
| 84 |
} |
| 85 |
return $apiResponse; |
| 86 |
} |
| 87 |
} |
| 88 |
|