PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
V3.3.3 V3.3.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 All 140 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.0, at includes/Core/Integration/Twilio/RecordApiHelper.php

70 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 protected $_defaultHeader;
13 public static $apiBaseUri = 'https://api.twilio.com/2010-04-01';
14
15 private $_sid;
16
17 private $_from_num;
18
19 private $logID;
20
21 private $_logResponse;
22
23 public function __construct($integrationDetails, $sid, $token, $from_num, $logID) {
24 $this->_sid = $sid;
25 $this->_from_num = $from_num;
26 $this->_defaultHeader = [
27 'Authorization' => 'Basic ' . base64_encode("$sid:$token"),
28 'Accept' => '*/*',
29 'verify' => false,
30 'Content-Type' => 'application/x-www-form-urlencoded'
31 ];
32 $this->_logResponse = new UtilApiResponse();
33 $this->logID = $logID;
34 }
35
36 public function sendMessage($data) {
37 $data['From'] = $this->_from_num;
38 $apiEndpoint = self::$apiBaseUri . "/Accounts/$this->_sid/Messages.json";
39 $response = HttpHelper::post($apiEndpoint, $data, $this->_defaultHeader);
40 return $response;
41 }
42
43 public function generateReqDataFromFieldMap($data, $fieldMap) {
44 $dataFinal = [];
45
46 foreach ($fieldMap as $value) {
47 $triggerValue = $value->formField;
48 $actionValue = $value->twilioField;
49 if ('custom' === $triggerValue) {
50 $dataFinal[$actionValue] = $value->customValue;
51 } elseif (!is_null($data[$triggerValue])) {
52 $dataFinal[$actionValue] = $data[$triggerValue];
53 }
54 }
55 return $dataFinal;
56 }
57
58 public function executeRecordApi($integId, $fieldValues, $fieldMap) {
59 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
60 $apiResponse = $this->sendMessage($finalData);
61
62 if ((property_exists($apiResponse, 'status') && 400 === $apiResponse->code) || property_exists($apiResponse, 'code')) {
63 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'errors', $apiResponse);
64 } else {
65 $this->_logResponse->apiResponse($this->logID, $integId, ['type' => 'twilio sms sending', 'type_name' => 'sms sent'], 'success', $apiResponse);
66 }
67 return $apiResponse;
68 }
69 }
70