PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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 2.10.2 All 137 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.0, at includes/Core/Integration/Encharge/EnchargeHandler.php

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