PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.2
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Twilio / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.10.2, at includes/Core/Integration/Twilio/RecordApiHelper.php

75 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 public function __construct($integrationDetails, $sid, $token, $from_num, $logID)
25 {
26 $this->_sid = $sid;
27 $this->_from_num = $from_num;
28 $this->_defaultHeader = [
29 'Authorization' => 'Basic ' . base64_encode("$sid:$token"),
30 'Accept' => '*/*',
31 'verify' => false,
32 'Content-Type' => 'application/x-www-form-urlencoded'
33 ];
34 $this->_logResponse = new UtilApiResponse();
35 $this->logID = $logID;
36 }
37
38 public function sendMessage($data)
39 {
40 $data['From'] = $this->_from_num;
41 $apiEndpoint = self::$apiBaseUri . "/Accounts/$this->_sid/Messages.json";
42 $response = HttpHelper::post($apiEndpoint, $data, $this->_defaultHeader);
43 return $response;
44 }
45
46 public function generateReqDataFromFieldMap($data, $fieldMap)
47 {
48 $dataFinal = [];
49
50 foreach ($fieldMap as $value) {
51 $triggerValue = $value->formField;
52 $actionValue = $value->twilioField;
53 if ('custom' === $triggerValue) {
54 $dataFinal[$actionValue] = $value->customValue;
55 } elseif (!is_null($data[$triggerValue])) {
56 $dataFinal[$actionValue] = $data[$triggerValue];
57 }
58 }
59 return $dataFinal;
60 }
61
62 public function executeRecordApi($integId, $fieldValues, $fieldMap)
63 {
64 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
65 $apiResponse = $this->sendMessage($finalData);
66
67 if ((property_exists($apiResponse, 'status') && 400 === $apiResponse->code) || property_exists($apiResponse, 'code')) {
68 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'errors', $apiResponse);
69 } else {
70 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'success', $apiResponse);
71 }
72 return $apiResponse;
73 }
74 }
75