-
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 2 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,124 @@ | ||
| <?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->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 (HttpHelper::$responseCode == 200) { | ||
|
Comment on lines
+38
to
+44
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 |
||
| 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->auth_token) || empty($refreshFieldsRequestParams->projectId)) { | ||
|
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. Accessing properties on if (empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->projectId)) {
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 — the guard now checks the request object itself before accessing properties: |
||
| 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 (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 ?? ''; | ||
|
|
||
| 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 | ||
| ); | ||
|
|
||
| if (is_wp_error($instasentApiResponse)) { | ||
| return $instasentApiResponse; | ||
| } | ||
|
|
||
| return $instasentApiResponse; | ||
|
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.
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 — removed the redundant |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?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 | ||
| ) { | ||
| $fieldData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap); | ||
|
|
||
| $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; | ||
| } | ||
|
|
||
| $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.
Accessing properties on
$refreshFieldsRequestParamswhen it is null or not an object will trigger a fatal error in PHP 8.0+. Guard against null/empty request parameters before accessing properties.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 — the guard now checks the request object itself before accessing properties:
empty($refreshFieldsRequestParams) || empty($refreshFieldsRequestParams->auth_token).