| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Encharge Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Encharge; |
| 9 |
|
| 10 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Provide functionality for Encharge integration |
| 16 |
*/ |
| 17 |
class EnchargeHandler |
| 18 |
{ |
| 19 |
private $_integrationID; |
| 20 |
public static $api_endpoint = 'https://api.encharge.io/v1/'; |
| 21 |
|
| 22 |
public function __construct($integrationID, $fromID) |
| 23 |
{ |
| 24 |
$this->_integrationID = $integrationID; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Helps to register ajax function's with wp |
| 29 |
* |
| 30 |
* @return null |
| 31 |
*/ |
| 32 |
public static function registerAjax() |
| 33 |
{ |
| 34 |
add_action('wp_ajax_bitforms_encharge_authorize', [__CLASS__, 'enChargeAuthorize']); |
| 35 |
add_action('wp_ajax_bitforms_encharge_headers', [__CLASS__, 'enchargeHeaders']); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Process ajax request for generate_token |
| 40 |
* |
| 41 |
* @return JSON enchagre user Authorization |
| 42 |
*/ |
| 43 |
public static function enChargeAuthorize() |
| 44 |
{ |
| 45 |
$authorizationHeader = null; |
| 46 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 47 |
$inputJSON = file_get_contents('php://input'); |
| 48 |
$requestsParams = json_decode($inputJSON); |
| 49 |
if ( |
| 50 |
empty($requestsParams->api_key) |
| 51 |
) { |
| 52 |
wp_send_json_error( |
| 53 |
__( |
| 54 |
'Requested parameter is empty', |
| 55 |
'bit-form' |
| 56 |
), |
| 57 |
400 |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
$apiEndpoint = self::$api_endpoint . 'accounts/info'; |
| 62 |
$authorizationHeader['Accept'] = 'application/json'; |
| 63 |
$authorizationHeader['X-Encharge-Token'] = $requestsParams->api_key; |
| 64 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 65 |
|
| 66 |
if (is_wp_error($apiResponse) || $apiResponse->error) { |
| 67 |
wp_send_json_error( |
| 68 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->error->message, |
| 69 |
400 |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
wp_send_json_success(true); |
| 74 |
} else { |
| 75 |
wp_send_json_error( |
| 76 |
__( |
| 77 |
'Token expired', |
| 78 |
'bit-form' |
| 79 |
), |
| 80 |
401 |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Process ajax request for refresh crm modules |
| 87 |
* |
| 88 |
* @return JSON Encharge field |
| 89 |
*/ |
| 90 |
public static function enchargeHeaders() |
| 91 |
{ |
| 92 |
$authorizationHeader = null; |
| 93 |
$response = null; |
| 94 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 95 |
$inputJSON = file_get_contents('php://input'); |
| 96 |
$queryParams = json_decode($inputJSON); |
| 97 |
if ( |
| 98 |
empty($queryParams->api_key) |
| 99 |
) { |
| 100 |
wp_send_json_error( |
| 101 |
__( |
| 102 |
'Requested parameter is empty', |
| 103 |
'bit-form' |
| 104 |
), |
| 105 |
400 |
| 106 |
); |
| 107 |
} |
| 108 |
$apiEndpoint = self::$api_endpoint . 'fields'; |
| 109 |
$authorizationHeader['Accept'] = 'application/json'; |
| 110 |
$authorizationHeader['X-Encharge-Token'] = $queryParams->api_key; |
| 111 |
$enChargeResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 112 |
$fields = []; |
| 113 |
if (!is_wp_error($enChargeResponse)) { |
| 114 |
$allFields = $enChargeResponse->items; |
| 115 |
// wp_send_json_success($allFields); |
| 116 |
foreach ($allFields as $field) { |
| 117 |
$required = 'email' === $field->name ? true : false; |
| 118 |
$fields[$field->name] = (object) [ |
| 119 |
'fieldId' => $field->name, |
| 120 |
'fieldName' => ucfirst($field->name), |
| 121 |
'required' => $required |
| 122 |
]; |
| 123 |
} |
| 124 |
$response['enChargeFields'] = $fields; |
| 125 |
wp_send_json_success($response); |
| 126 |
} |
| 127 |
} else { |
| 128 |
wp_send_json_error( |
| 129 |
__( |
| 130 |
'Token expired', |
| 131 |
'bit-form' |
| 132 |
), |
| 133 |
401 |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 139 |
{ |
| 140 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 141 |
|
| 142 |
$api_key = $integrationDetails->api_key; |
| 143 |
$fieldMap = $integrationDetails->field_map; |
| 144 |
$tags = property_exists($integrationDetails, 'tags') ? $integrationDetails->tags : null; |
| 145 |
|
| 146 |
if ( |
| 147 |
empty($api_key) |
| 148 |
|| empty($fieldMap) |
| 149 |
) { |
| 150 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Encharge api', 'bit-form')); |
| 151 |
} |
| 152 |
$recordApiHelper = new RecordApiHelper($api_key, $this->_integrationID, $logID, $entryID); |
| 153 |
$enchagreApiResponse = $recordApiHelper->executeRecordApi( |
| 154 |
$fieldValues, |
| 155 |
$fieldMap, |
| 156 |
$tags |
| 157 |
); |
| 158 |
|
| 159 |
if (is_wp_error($enchagreApiResponse)) { |
| 160 |
return $enchagreApiResponse; |
| 161 |
} |
| 162 |
return $enchagreApiResponse; |
| 163 |
} |
| 164 |
} |
| 165 |
|