-
Notifications
You must be signed in to change notification settings - Fork 1
feat(instasent): add Instasent SMS action integration #184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6863930
33d5027
9d1675b
247c6ab
8b5c557
40b6b4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Instasent Integration | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\Instasent; | ||
|
|
||
| use BitApps\Integrations\Core\Util\HttpHelper; | ||
| use WP_Error; | ||
|
|
||
| /** | ||
| * Provide functionality for Instasent integration | ||
| */ | ||
| class InstasentController | ||
| { | ||
| private static $_baseUrl = 'https://api.instasent.com'; | ||
|
|
||
| public function authorize($refreshFieldsRequestParams) | ||
| { | ||
| if (empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token)) { | ||
| wp_send_json_error( | ||
| __( | ||
| 'Requested parameter is empty', | ||
| 'bit-integrations' | ||
| ), | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| $endpoint = self::$_baseUrl . '/organization/account'; | ||
| $header = [ | ||
| 'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token, | ||
| 'Content-Type' => 'application/json', | ||
| 'Accept' => 'application/json', | ||
| ]; | ||
|
|
||
| $response = HttpHelper::get($endpoint, null, $header); | ||
|
|
||
| if (is_wp_error($response)) { | ||
| wp_send_json_error($response->get_error_message(), 400); | ||
| } | ||
|
|
||
| if (HttpHelper::$responseCode == 200) { | ||
| wp_send_json_success('Authorization Successful', 200); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| wp_send_json_error( | ||
| $response->message ?? $response ?? 'Authorization Failed', | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| public function refreshDatasources($refreshFieldsRequestParams) | ||
| { | ||
| if (empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->projectId)) { | ||
| wp_send_json_error( | ||
| __('Requested parameter is empty', 'bit-integrations'), | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| $project = rawurlencode($refreshFieldsRequestParams->projectId); | ||
| $endpoint = self::$_baseUrl . "/v1/project/{$project}/datasource"; | ||
| $header = [ | ||
| 'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token, | ||
| 'Content-Type' => 'application/json', | ||
| 'Accept' => 'application/json', | ||
| ]; | ||
|
|
||
| $response = HttpHelper::get($endpoint, null, $header); | ||
|
|
||
| if (is_wp_error($response)) { | ||
| wp_send_json_error($response->get_error_message(), 400); | ||
| } | ||
|
|
||
| if (HttpHelper::$responseCode == 200) { | ||
|
Comment on lines
+73
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always validate the response with $response = HttpHelper::get($endpoint, null, $header);
if (is_wp_error($response)) {
wp_send_json_error($response->get_error_message(), 400);
}
if (HttpHelper::$responseCode == 200) {References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added an |
||
| $entities = $response->entities ?? []; | ||
| $formattedResponse = []; | ||
| foreach ($entities as $entity) { | ||
| $formattedResponse[] = [ | ||
| 'id' => $entity->id ?? '', | ||
| 'name' => $entity->name ?? ($entity->id ?? ''), | ||
| ]; | ||
| } | ||
|
|
||
| wp_send_json_success($formattedResponse, 200); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| wp_send_json_error( | ||
| $response->message ?? $response ?? __('Failed to fetch data sources', 'bit-integrations'), | ||
| 400 | ||
| ); | ||
| } | ||
|
|
||
| public function execute($integrationData, $fieldValues) | ||
| { | ||
| $integrationDetails = $integrationData->flow_details; | ||
| $integId = $integrationData->id; | ||
| $auth_token = $integrationDetails->auth_token ?? ''; | ||
| $fieldMap = $integrationDetails->field_map ?? ''; | ||
| $action = $integrationDetails->action ?? ''; | ||
| $actions = isset($integrationDetails->actions) ? $integrationDetails->actions : (object) []; | ||
|
|
||
| if ( | ||
| empty($fieldMap) | ||
| || empty($auth_token) | ||
| || empty($action) | ||
| ) { | ||
| // translators: %s: Placeholder value | ||
| return new WP_Error('REQ_FIELD_EMPTY', wp_sprintf(__('module, fields are required for %s api', 'bit-integrations'), 'Instasent')); | ||
| } | ||
|
|
||
| $recordApiHelper = new RecordApiHelper($auth_token, $integrationDetails, $integId); | ||
| $instasentApiResponse = $recordApiHelper->execute( | ||
| $integrationDetails, | ||
| $fieldValues, | ||
| $fieldMap, | ||
| $auth_token, | ||
| $action, | ||
| $actions | ||
| ); | ||
|
|
||
| return $instasentApiResponse; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Instasent Record Api | ||
| */ | ||
|
|
||
| namespace BitApps\Integrations\Actions\Instasent; | ||
|
|
||
| use BitApps\Integrations\Config; | ||
| use BitApps\Integrations\Core\Util\Common; | ||
| use BitApps\Integrations\Core\Util\Hooks; | ||
| use BitApps\Integrations\Log\LogHandler; | ||
|
|
||
| /** | ||
| * Provide functionality for Record insert, upsert | ||
| */ | ||
| class RecordApiHelper | ||
| { | ||
| private $_integrationID; | ||
|
|
||
| private $_integrationDetails; | ||
|
|
||
| private $_authToken; | ||
|
|
||
| public function __construct($auth_token, $integrationDetails, $integId) | ||
| { | ||
| $this->_authToken = $auth_token; | ||
| $this->_integrationDetails = $integrationDetails; | ||
| $this->_integrationID = $integId; | ||
| } | ||
|
|
||
| public function generateReqDataFromFieldMap($data, $fieldMap) | ||
| { | ||
| $dataFinal = []; | ||
|
|
||
| foreach ($fieldMap as $key => $value) { | ||
| $triggerValue = $value->formField; | ||
| $actionValue = $value->instasentFormField; | ||
|
|
||
| if (empty($actionValue)) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($triggerValue === 'custom' && isset($value->customValue)) { | ||
| $dataFinal[$actionValue] = Common::replaceFieldWithValue($value->customValue, $data); | ||
| } elseif (isset($data[$triggerValue])) { | ||
| $dataFinal[$actionValue] = $data[$triggerValue]; | ||
| } | ||
| } | ||
|
|
||
| return $dataFinal; | ||
| } | ||
|
|
||
| public function execute( | ||
| $integrationDetails, | ||
| $fieldValues, | ||
| $fieldMap, | ||
| $auth_token, | ||
| $action, | ||
| $utilities = null | ||
| ) { | ||
| $fieldData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); | ||
|
|
||
| if ($action === 'send_sms' && isset($utilities->allowUnicode)) { | ||
| $fieldData['allowUnicode'] = $utilities->allowUnicode; | ||
| } | ||
|
|
||
| $default = [ | ||
| 'success' => false, | ||
| 'message' => __('Bit Integrations Pro is required.', 'bit-integrations'), | ||
| ]; | ||
|
|
||
| switch ($action) { | ||
| case 'send_sms': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_send_sms'), $default, $fieldData, $auth_token); | ||
| $typeName = 'send-sms'; | ||
|
|
||
| break; | ||
|
|
||
| case 'create_lookup': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_create_lookup'), $default, $fieldData, $auth_token); | ||
| $typeName = 'create-lookup'; | ||
|
|
||
| break; | ||
|
|
||
| case 'create_datasource': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_create_datasource'), $default, $fieldData, $integrationDetails, $auth_token); | ||
| $typeName = 'create-datasource'; | ||
|
|
||
| break; | ||
|
|
||
| case 'create_or_update_contact': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_create_or_update_contact'), $default, $fieldData, $integrationDetails, $auth_token); | ||
| $typeName = 'create-or-update-contact'; | ||
|
|
||
| break; | ||
|
|
||
| case 'delete_contact': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_delete_contact'), $default, $fieldData, $integrationDetails, $auth_token); | ||
| $typeName = 'delete-contact'; | ||
|
|
||
| break; | ||
|
|
||
| case 'create_contact_event': | ||
| $apiResponse = Hooks::apply(Config::withPrefix('instasent_create_contact_event'), $default, $fieldData, $integrationDetails, $auth_token); | ||
| $typeName = 'create-contact-event'; | ||
|
|
||
| break; | ||
|
|
||
| default: | ||
| $apiResponse = $default; | ||
| $typeName = $action; | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| if (is_wp_error($apiResponse)) { | ||
| LogHandler::save( | ||
| $this->_integrationID, | ||
| wp_json_encode(['type' => 'action', 'type_name' => $typeName]), | ||
| 'error', | ||
| wp_json_encode(['message' => $apiResponse->get_error_message()]) | ||
| ); | ||
|
|
||
| return $apiResponse; | ||
| } | ||
|
|
||
| $apiResponse = \is_array($apiResponse) ? (object) $apiResponse : $apiResponse; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always validate the response with if (is_wp_error($apiResponse)) {
LogHandler::save(
$this->_integrationID,
wp_json_encode(['type' => 'action', 'type_name' => $typeName]),
'error',
wp_json_encode(['message' => $apiResponse->get_error_message()])
);
return $apiResponse;
}
$apiResponse = \is_array($apiResponse) ? (object) $apiResponse : $apiResponse;References
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — added an |
||
|
|
||
| if ( | ||
| (isset($apiResponse->success) && $apiResponse->success) | ||
| || isset($apiResponse->id) | ||
| || isset($apiResponse->data) | ||
| ) { | ||
| LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'action', 'type_name' => $typeName]), 'success', wp_json_encode($apiResponse)); | ||
| } else { | ||
| LogHandler::save($this->_integrationID, wp_json_encode(['type' => 'action', 'type_name' => $typeName]), 'error', wp_json_encode($apiResponse)); | ||
| } | ||
|
|
||
| return $apiResponse; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
|
|
||
| if (!defined('ABSPATH')) { | ||
| exit; | ||
| } | ||
|
|
||
| use BitApps\Integrations\Actions\Instasent\InstasentController; | ||
| use BitApps\Integrations\Core\Util\Route; | ||
|
|
||
| Route::post('instasent_authorize', [InstasentController::class, 'authorize']); | ||
| Route::post('refresh_instasent_datasources', [InstasentController::class, 'refreshDatasources']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always validate the response with
is_wp_error()before checking response codes or accessing properties to prevent fatal errors in PHP 8.0+.References
is_wp_error()function to validate the response before accessing it as an array or object. This prevents fatal errors in PHP 8.0+.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added an
is_wp_error($response)guard right after the request, before reading the response code/properties.