| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Twilio Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Twilio; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
final class TwilioHandler |
| 15 |
{ |
| 16 |
private $_integrationID; |
| 17 |
public static $apiBaseUri = 'https://api.twilio.com/2010-04-01'; |
| 18 |
protected $_defaultHeader; |
| 19 |
|
| 20 |
public function __construct($integrationID) |
| 21 |
{ |
| 22 |
$this->_integrationID = $integrationID; |
| 23 |
} |
| 24 |
|
| 25 |
public static function registerAjax() |
| 26 |
{ |
| 27 |
add_action('wp_ajax_bitforms_twilio_authorization', [__CLASS__, 'checkAuthorization']); |
| 28 |
} |
| 29 |
|
| 30 |
public static function checkAuthorization() |
| 31 |
{ |
| 32 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 33 |
$inputJSON = file_get_contents('php://input'); |
| 34 |
$tokenRequestParams = json_decode($inputJSON); |
| 35 |
|
| 36 |
if ( |
| 37 |
empty($tokenRequestParams->sid) |
| 38 |
|| empty($tokenRequestParams->token) |
| 39 |
|| empty($tokenRequestParams->from_num) |
| 40 |
) { |
| 41 |
wp_send_json_error( |
| 42 |
__( |
| 43 |
'Requested parameter is empty', |
| 44 |
'bit-integrations' |
| 45 |
), |
| 46 |
400 |
| 47 |
); |
| 48 |
} |
| 49 |
$header = [ |
| 50 |
'Authorization' => 'Basic ' . base64_encode("$tokenRequestParams->sid:$tokenRequestParams->token"), |
| 51 |
'Accept' => '*/*', |
| 52 |
'verify' => false |
| 53 |
]; |
| 54 |
$apiEndpoint = self::$apiBaseUri . '/Accounts'; |
| 55 |
|
| 56 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 57 |
|
| 58 |
$xml = simplexml_load_string($apiResponse); |
| 59 |
$json = wp_json_encode($xml); |
| 60 |
$response = json_decode($json, true); |
| 61 |
|
| 62 |
if (array_key_exists('RestException', $response)) { |
| 63 |
wp_send_json_error( |
| 64 |
'Unauthorize', |
| 65 |
400 |
| 66 |
); |
| 67 |
} else { |
| 68 |
wp_send_json_success($apiResponse, 200); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 74 |
{ |
| 75 |
$integrationDetails = $integrationData->integration_details; |
| 76 |
if (is_string($integrationDetails)) { |
| 77 |
$integrationDetails = json_decode($integrationDetails); |
| 78 |
} |
| 79 |
$fieldMap = $integrationDetails->field_map; |
| 80 |
$sid = $integrationDetails->sid; |
| 81 |
$token = $integrationDetails->token; |
| 82 |
$from_num = $integrationDetails->from_num; |
| 83 |
if ( |
| 84 |
empty($sid) |
| 85 |
|| empty($token) |
| 86 |
|| empty($from_num) |
| 87 |
|| empty($fieldMap) |
| 88 |
) { |
| 89 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('SID, Auth Token,From Number and mapping fields are required for rapidmail api', 'bitform')); |
| 90 |
return $error; |
| 91 |
} |
| 92 |
$recordApiHelper = new RecordApiHelper($integrationDetails, $sid, $token, $from_num, $logID); |
| 93 |
$twilioResponse = $recordApiHelper->executeRecordApi( |
| 94 |
$this->_integrationID, |
| 95 |
$fieldValues, |
| 96 |
$fieldMap |
| 97 |
); |
| 98 |
return $twilioResponse; |
| 99 |
} |
| 100 |
} |
| 101 |
|