PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / Encharge / EnchargeHandler.php

EnchargeHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Core/Integration/Encharge/EnchargeHandler.php

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