PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.6.0
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.6.0
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / admin / includes / Connectors / Connector.php
commercebird / admin / includes / Connectors Last commit date
Connector.php 9 months ago index.php 1 year ago
Connector.php
351 lines
1 <?php
2
3 namespace CommerceBird\Admin\Connectors;
4
5 use CommerceBird\Admin\Actions\Ajax\SettingsAjax;
6 use CommerceBird\Admin\Traits\LogWriter;
7 use WP_Error;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Connector class for interacting with the CommerceBird API and Exact Online.
15 *
16 * Provides methods for retrieving and saving data such as subscriptions, account settings,
17 * products, orders, and webhooks, as well as handling API requests and logging.
18 */
19 final class Connector {
20 use LogWriter;
21
22 const COST_CENTERS = 'customs/exact/cost-centers';
23 const COST_UNITS = 'customs/exact/cost-units';
24 const GL_ACCOUNTS = 'customs/exact/gl-accounts';
25 const INVOICE_LAYOUTS = 'customs/exact/invoice-layouts';
26 const JOURNAL_LIST = 'customs/exact/journal';
27 const EXACT_WAREHOUSES = 'customs/exact/warehouses';
28 const EXACT_GENERAL_SETTINGS = 'customs/exact/gen-settings';
29 const ITEM = 'customs/exact/bulk-items';
30 const CUSTOMER = 'customs/exact/bulk-customers';
31 const ORDER = 'customs/exact/bulk-orders';
32 const PAYMENT_STATUS = 'customs/exact/invoice-payment-status';
33 const WEBHOOKS = 'customs/exact/webhooks';
34 const WOO_WEBHOOKS = 'customs/woo/webhooks';
35 const CONNECT_EXACT = 'webapp/exact/auth-url/plugin';
36 const EXACT_DIVISIONS = 'webapp/exact/divisions/plugin';
37 const ACCOUNT_SETTINGS = 'customs/settings';
38 const SUBSCRIPTION = 'customs/subscriptions';
39 const EXACT_TAXMAPPING = 'customs/exact/settings/taxes';
40 const EXACT_VATCODES = 'customs/exact/settings/vat-codes';
41 const EXACT_PAYMENT_CONDITIONS = 'customs/exact/settings/payment-conditions';
42 const EXACT_PAYMENT_STATUS_MAPPING = 'customs/exact/settings/payment-status';
43 const API = 'https://api.commercebird.com';
44 // const API = 'http://localhost:3000';
45
46 /**
47 * Get Subscription data from CommerceBird API.
48 *
49 * @param integer $subscription_id
50 * @param string $store_token
51 * @return array|WP_Error
52 */
53 public function get_subscription( $data ) {
54 $response = $this->request(
55 self::SUBSCRIPTION,
56 'POST',
57 $data
58 );
59 if ( is_wp_error( $response ) ) {
60 $this->write_log( $response->get_error_message(), 'commercebird-connector' );
61 return new WP_Error( 'request_failed', $response->get_error_message() );
62 }
63 if ( isset( $response['code'] ) && $response['code'] !== 200 ) {
64 $this->write_log( $response['message'], 'commercebird-connector' );
65 return new WP_Error( 'request_failed', $response['message'] );
66 }
67 return $response['data'];
68 }
69
70 /**
71 * Connect to exact online and get authorization URL.
72 *
73 * @param array $data array ( callback_url, topic )
74 * @return array|WP_Error array ( webhook_id, topic )
75 */
76 public function create_woo_webbhook() {
77 return $this->request( self::WOO_WEBHOOKS, 'POST' );
78 }
79
80 /**
81 * Connect to exact online and get authorization URL.
82 *
83 * @param array $data array ( callback_url, topic )
84 * @return array|WP_Error array ( webhook_id, topic )
85 */
86 public function connect_exact( $domain_suffix ) {
87 return $this->request( self::CONNECT_EXACT, 'GET', array(), array( 'domain' => $domain_suffix ) );
88 }
89
90 public function get_account_settings() {
91 return $this->request( self::ACCOUNT_SETTINGS );
92 }
93 public function save_account_settings( array $data ) {
94 return $this->request( self::ACCOUNT_SETTINGS, 'POST', $data );
95 }
96
97 public function get_exact_divisions() {
98 return $this->request( self::EXACT_DIVISIONS );
99 }
100
101 public function cost_centers() {
102 return $this->request( self::COST_CENTERS );
103 }
104
105 public function gl_accounts() {
106 return $this->request( self::GL_ACCOUNTS );
107 }
108
109 /**
110 * Get the list of journals from Exact Online.
111 *
112 * @return array|WP_Error
113 */
114 public function journals() {
115 return $this->request( self::JOURNAL_LIST );
116 }
117
118 /**
119 * Get the list of invoice layouts from Exact Online.
120 *
121 * @return array|WP_Error
122 */
123 public function invoice_layouts() {
124 return $this->request( self::INVOICE_LAYOUTS );
125 }
126
127 /**
128 * Get the list warehouses from Exact Online.
129 *
130 * @return array|WP_Error
131 */
132 public function exact_warehouses() {
133 return $this->request( self::EXACT_WAREHOUSES );
134 }
135
136 /**
137 * Get mapped general settings for Exact Online.
138 *
139 * @return array|WP_Error
140 */
141 public function exact_general_settings() {
142 return $this->request( self::EXACT_GENERAL_SETTINGS );
143 }
144
145 /**
146 * Save general settings for Exact Online.
147 *
148 * @param array $data mapped general settings data
149 * @return array|WP_Error
150 */
151 public function save_exact_general_settings( array $data ) {
152 return $this->request( self::EXACT_GENERAL_SETTINGS, 'POST', $data );
153 }
154
155 /**
156 * Get exact vat codes.
157 *
158 * @return array|WP_Error
159 */
160 public function get_exact_vat_codes() {
161 return $this->request( self::EXACT_VATCODES );
162 }
163
164 /**
165 * Get mapped taxes for Exact Online.
166 *
167 * @return array|WP_Error
168 */
169 public function get_exact_tax_mapping() {
170 return $this->request( self::EXACT_TAXMAPPING );
171 }
172
173 /**
174 * Save mapped taxes for Exact Online.
175 *
176 * @param array $data mapped taxes data
177 * @return array|WP_Error
178 */
179 public function save_exact_tax_mapping( array $data ) {
180 return $this->request( self::EXACT_TAXMAPPING, 'POST', $data );
181 }
182
183 /**
184 * Get mapped payment conditions for Exact Online.
185 *
186 * @return array|WP_Error
187 */
188 public function get_exact_payment_conditions() {
189 return $this->request( self::EXACT_PAYMENT_CONDITIONS );
190 }
191
192 /**
193 * Get mapped payment staus for Exact Online.
194 *
195 * @return array|WP_Error
196 */
197 public function get_exact_payment_status_mapping() {
198 return $this->request( self::EXACT_PAYMENT_STATUS_MAPPING );
199 }
200
201 /**
202 * Save mapped payment status for Exact Online.
203 *
204 * @param array $data mapped taxes data
205 * @return array|WP_Error
206 */
207 public function save_exact_payment_status_mapping( array $data ) {
208 return $this->request( self::EXACT_PAYMENT_STATUS_MAPPING, 'POST', $data );
209 }
210
211 /**
212 * Suscribe to Exact Online Webhooks. Pass the webhook URL to Exact Online and Topic
213 *
214 * @param array $data array ( callback_url, topic )
215 * @return array|WP_Error array ( webhook_id, topic )
216 */
217 public function subscribe_exact_webhooks( array $data ) {
218 $response = $this->request( self::WEBHOOKS, 'POST', $data );
219 return $response['code'] === 200 ? $response['data'] : $response['message'];
220 }
221
222 /**
223 * Collect customer or account id
224 *
225 * @param array $customer array ( customer_email, company_name )
226 *
227 * @return array|WP_Error array ( account_id, company_id )
228 * @throws WP_Error Invalid customer if empty
229 */
230 public function customer() {
231 $response = $this->request( self::CUSTOMER );
232 return $response['code'] === 200 ? $response['data'] : $response['message'];
233 }
234 /**
235 * Collect customer or account id
236 *
237 * @param array $customer array ( customer_email, company_name )
238 *
239 * @return array|WP_Error array ( account_id, company_id )
240 * @throws WP_Error Invalid customer if empty
241 */
242 public function order( array $range ) {
243
244 $response = $this->request(
245 self::ORDER,
246 'GET',
247 array(),
248 $range
249 );
250 return $response['code'] === 200 ? $response['data'] : $response['message'];
251 }
252
253 /**
254 * Send Order Ids to Exact Online using the bulk endpoint
255 *
256 * @param array $data array ( order_ids )
257 * @return array|WP_Error array ( order_ids )
258 * @throws WP_Error Invalid order if empty
259 */
260 public function send_orders( array $data ) {
261 $response = $this->request( self::ORDER, 'POST', $data );
262 return $response['code'] === 200 ? $response['data'] : $response['message'];
263 }
264
265 /**
266 * Get item ID by product ID
267 *
268 * @param int $id of the item
269 *
270 * @return array|WP_Error The ID of the item or an error object.
271 */
272 public function products() {
273 $response = $this->request( self::ITEM );
274 return $response['code'] === 200 ? $response['data'] : $response['message'];
275 }
276
277 public function cost_units() {
278 return $this->request( self::COST_UNITS );
279 }
280
281 /**
282 * Get payment status
283 *
284 * @param array
285 *
286 * @return array|WP_Error array ( payment_status )
287 */
288 public function payment_status( array $data ) {
289 $response = $this->request( self::PAYMENT_STATUS, 'POST', $data, array() );
290 return $response['code'] === 200 ? $response['data'] : $response['message'];
291 }
292
293 /**
294 * Generate request URL
295 *
296 * @param string $endpoint
297 * @param string $method
298 * @param array $data
299 * @param array $params
300 * @return array|WP_Error
301 */
302 public function request( string $endpoint, string $method = 'GET', array $data = array(), array $params = array() ) {
303 // $token = ExactOnlineAjax::instance()->get_token();
304 $token = SettingsAjax::instance()->get_token();
305
306 $token = ! empty( $token ) ? $token : '';
307 $url = sprintf( '%s/%s?token=%s', self::API, $endpoint, $token );
308 if ( ! empty( $params ) ) {
309 $url .= '&' . http_build_query( $params );
310 }
311
312 // if current site contains localhost, use https://dev.commercebird.com.
313 $current_site_url = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
314 $site_url = empty( $current_site_url ) || str_contains( $current_site_url, 'localhost' ) ? 'https://dev.commercebird.com' : site_url();
315 if ( 'POST' === $method ) {
316 $response = wp_remote_post(
317 $url,
318 array(
319 'headers' => array(
320 'Accept' => 'application/json',
321 'Content-Type' => 'application/json',
322 'zohowooagent' => $site_url,
323 ),
324 'timeout' => 60,
325 'sslverify' => false,
326 'body' => wp_json_encode( $data ),
327 )
328 );
329 } else {
330 $response = wp_remote_get(
331 $url,
332 array(
333 'headers' => array(
334 'Accept' => 'application/json',
335 'zohowooagent' => $site_url,
336 'x-woozo-module' => $data['module'] ?? '',
337 ),
338 'timeout' => 60,
339 'sslverify' => false,
340 )
341 );
342 }
343 if ( is_wp_error( $response ) ) {
344 $this->write_log( $response->get_error_message(), 'commercebird-connector' );
345 return new WP_Error( 'request_failed', $response->get_error_message() );
346 }
347 $response = wp_remote_retrieve_body( $response );
348 return json_decode( $response, true );
349 }
350 }
351