PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.3.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.3.1
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 3.3.1, at includes/Core/Integration/Encharge/EnchargeHandler.php

187 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 GlobalHelper::requirePostMethod();
57
58 try {
59 $requestsParams = GlobalHelper::formatRequestData();
60 } catch (\InvalidArgumentException $e) {
61 wp_send_json_error($e->getMessage(), 400);
62 }
63
64 if (
65 empty($requestsParams->api_key)
66 ) {
67 wp_send_json_error(
68 __(
69 'Requested parameter is empty',
70 'bit-form'
71 ),
72 400
73 );
74 }
75
76 $apiEndpoint = self::$api_endpoint . 'accounts/info';
77 $authorizationHeader['Accept'] = 'application/json';
78 $authorizationHeader['X-Encharge-Token'] = $requestsParams->api_key;
79 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
80
81 if (is_wp_error($apiResponse) || $apiResponse->error) {
82 wp_send_json_error(
83 empty($apiResponse->code) ? 'Unknown' : $apiResponse->error->message,
84 400
85 );
86 }
87
88 wp_send_json_success(true);
89 } else {
90 wp_send_json_error(
91 __(
92 'Token expired',
93 'bit-form'
94 ),
95 401
96 );
97 }
98 }
99
100 /**
101 * Process ajax request for refresh crm modules
102 *
103 * @return JSON Encharge field
104 */
105 public static function enchargeHeaders()
106 {
107 $authorizationHeader = null;
108 $response = null;
109 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
110 GlobalHelper::requirePostMethod();
111
112 try {
113 $queryParams = GlobalHelper::formatRequestData();
114 } catch (\InvalidArgumentException $e) {
115 wp_send_json_error($e->getMessage(), 400);
116 }
117
118 if (
119 empty($queryParams->api_key)
120 ) {
121 wp_send_json_error(
122 __(
123 'Requested parameter is empty',
124 'bit-form'
125 ),
126 400
127 );
128 }
129 $apiEndpoint = self::$api_endpoint . 'fields';
130 $authorizationHeader['Accept'] = 'application/json';
131 $authorizationHeader['X-Encharge-Token'] = $queryParams->api_key;
132 $enChargeResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
133 $fields = [];
134 if (!is_wp_error($enChargeResponse)) {
135 $allFields = $enChargeResponse->items;
136 // wp_send_json_success($allFields);
137 foreach ($allFields as $field) {
138 $required = 'email' === $field->name ? true : false;
139 $fields[$field->name] = (object) [
140 'fieldId' => $field->name,
141 'fieldName' => ucfirst($field->name),
142 'required' => $required
143 ];
144 }
145 $response['enChargeFields'] = $fields;
146 wp_send_json_success($response);
147 }
148 } else {
149 wp_send_json_error(
150 __(
151 'Token expired',
152 'bit-form'
153 ),
154 401
155 );
156 }
157 }
158
159 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
160 {
161 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
162
163 $api_key = $integrationDetails->api_key;
164 $fieldMap = $integrationDetails->field_map;
165 $tags = property_exists($integrationDetails, 'tags') ? $integrationDetails->tags : null;
166
167 if (
168 empty($api_key)
169 || empty($fieldMap)
170 ) {
171 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Encharge api', 'bit-form'));
172 }
173 $recordApiHelper = new RecordApiHelper($api_key, $this->_integrationID, $logID, $entryID);
174 $enchagreApiResponse = $recordApiHelper->executeRecordApi(
175 $fieldValues,
176 $fieldMap,
177 $tags,
178 $this->formId
179 );
180
181 if (is_wp_error($enchagreApiResponse)) {
182 return $enchagreApiResponse;
183 }
184 return $enchagreApiResponse;
185 }
186 }
187