PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.2.15
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.2.15
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 / Actions / Ajax / ZohoInventoryAjax.php
commercebird / admin / includes / Actions / Ajax Last commit date
AcfAjax.php 1 year ago ExactOnlineAjax.php 1 year ago ZohoCRMAjax.php 1 year ago ZohoInventoryAjax.php 1 year ago
ZohoInventoryAjax.php
868 lines
1 <?php
2
3 namespace CommerceBird\Admin\Actions\Ajax;
4
5 use CMBIRD_Auth_Zoho;
6 use CMBIRD_API_Handler_Zoho;
7 use CMBIRD_Pricelist_ZI;
8 use CMBIRD_Categories_ZI;
9 use CommerceBird\Admin\Template;
10 use CommerceBird\Admin\Traits\AjaxRequest;
11 use CommerceBird\Admin\Traits\OptionStatus;
12 use CommerceBird\Admin\Traits\Singleton;
13 use CommerceBird\Admin\Traits\LogWriter;
14 use Throwable;
15 use WC_Tax;
16 use WpOrg\Requests\Exception;
17 use function gettype;
18
19 if ( ! defined( 'ABSPATH' ) ) {
20 exit;
21 }
22 final class ZohoInventoryAjax {
23
24 use Singleton;
25 use AjaxRequest;
26 use OptionStatus;
27 use LogWriter;
28
29 private const FORMS = array(
30 'settings' => array(
31 'cors',
32 'id',
33 'email',
34 ),
35 'tax' => array(
36 'selectedTaxRates',
37 ),
38 'product' => array(
39 'item_from_zoho',
40 'disable_stock_sync',
41 'disable_product_sync',
42 'enable_accounting_stock',
43 ),
44 'cron' => array(
45 'form',
46 'categories',
47 ),
48 'order' => array(
49 'disable_sync',
50 'enable_auto_number',
51 'enable_order_status',
52 'order_prefix',
53 'warehouse_id',
54 'enable_warehousestock',
55 ),
56 'contact' => array(
57 'enable_cron',
58 ),
59 'price' => array(
60 'wp_user_role',
61 'zoho_inventory_pricelist',
62 'wcb2b',
63 ),
64 );
65
66 private const ACTIONS = array(
67 'get_subscription' => 'subscription_get',
68 'get_settings' => 'settings_get',
69 'save_settings' => 'settings_set',
70 'reset_settings' => 'settings_reset',
71 'get_zoho_connect' => 'connection_get',
72 'save_zoho_connect' => 'connection_set',
73 'reset_zoho_connect' => 'connection_reset',
74 'get_zoho_tax' => 'tax_get',
75 'save_zoho_tax' => 'tax_set',
76 'reset_zoho_tax' => 'tax_reset',
77 'get_zoho_product' => 'product_get',
78 'save_zoho_product' => 'product_set',
79 'reset_zoho_product' => 'product_reset',
80 'get_zoho_cron' => 'cron_get',
81 'save_zoho_cron' => 'cron_set',
82 'reset_zoho_cron' => 'cron_reset',
83 'get_zoho_order' => 'order_get',
84 'save_zoho_order' => 'order_set',
85 'reset_zoho_order' => 'order_reset',
86 'get_zoho_contact' => 'contact_get',
87 'save_zoho_contact' => 'contact_set',
88 'reset_zoho_contact' => 'contact_reset',
89 'get_zoho_price' => 'price_get',
90 'save_zoho_price' => 'price_set',
91 'reset_zoho_price' => 'price_reset',
92 'get_zoho_fields' => 'fields_get',
93 'save_zoho_fields' => 'fields_set',
94 'reset_zoho_fields' => 'fields_reset',
95 'is_connected' => 'connection_done',
96 'get_wc_taxes' => 'wc_tax_collect',
97 'get_zoho_taxes' => 'zoho_tax_rates_collect',
98 'get_zoho_categories' => 'zoho_categories_collect',
99 'get_zoho_warehouses' => 'zoho_warehouses_collect',
100 'get_zoho_prices' => 'zoho_prices_collect',
101 'get_all_custom_fields' => 'wc_custom_fields_collect',
102 'handle_code' => 'handle_code',
103 );
104
105 private const SOURCE = 'zoho';
106
107 /**
108 * ZohoInventoryAjax constructor.
109 */
110 public function __construct() {
111 $this->load_actions();
112 }
113
114 /**
115 * Collects custom checkout fields for WooCommerce.
116 *
117 * This function verifies the request and collects custom fields from the
118 * THWCFD_Utils::get_checkout_fields() function if the THWCFD class exists.
119 *
120 * @return void
121 */
122 public function wc_custom_fields_collect(): void {
123 $this->verify();
124 $types = array( 'billing', 'shipping', 'additional' );
125 $all_fields = array();
126 $custom_fields = array();
127 // Get all the fields.
128 foreach ( $types as $type ) {
129 // Skip if an unsupported type.
130 if (
131 ! in_array(
132 $type,
133 array(
134 'billing',
135 'shipping',
136 'additional',
137 ),
138 true,
139 )
140 ) {
141 continue;
142 }
143
144 $temp_fields = get_option( 'wc_fields_' . $type );
145 if ( false !== $temp_fields ) {
146 $all_fields = array_merge( $all_fields, $temp_fields );
147 }
148 }
149 // Loop through each field to see if it is a custom field.
150 foreach ( $all_fields as $name => $options ) {
151 if ( isset( $options['custom'] ) && $options['custom'] ) {
152 $label = trim( $options['label'] );
153 $custom_fields[ $name ] = empty( $label ) ? __( 'Please set label', 'commercebird' ) : $label;
154 }
155 }
156
157 $this->response = $custom_fields;
158
159 $this->serve();
160 }
161
162 /**
163 * Retrieves the connection details.
164 *
165 * @return void
166 */
167 public function connection_get(): void {
168 $this->verify();
169 $this->response['account_domain'] = get_option( 'cmbird_zoho_inventory_domain' );
170 $this->response['organization_id'] = get_option( 'cmbird_zoho_inventory_oid' );
171 $this->response['client_id'] = get_option( 'cmbird_zoho_inventory_cid' );
172 $this->response['client_secret'] = get_option( 'cmbird_zoho_inventory_cs' );
173 $this->response['inventory_url'] = get_option( 'cmbird_zoho_inventory_url' );
174 $this->response['redirect_uri'] = get_option( 'cmbird_authorization_redirect_uri' );
175 $this->serve();
176 }
177
178 /**
179 * Retrieves the subscription details.
180 *
181 * @return void
182 */
183 public function subscription_get(): void {
184 $this->verify();
185 $this->response = $this->get_subscription_data();
186 $this->serve();
187 }
188
189 /**
190 * @return array
191 */
192 public function get_subscription_data(): array {
193 $transient = get_transient( 'zoho_subscription' );
194 if ( ! empty( $transient ) ) {
195 return $transient;
196 }
197
198 $data = $this->get_subscription_data_from_api();
199
200 if ( ! empty( $data ) ) {
201 set_transient( 'zoho_subscription', $data, WEEK_IN_SECONDS );
202 }
203
204 return $data;
205 }
206
207 private function get_subscription_data_from_api(): array {
208 $subscription_id = get_option( 'cmbird_zoho_id_status', 0 );
209 if ( ! $subscription_id ) {
210 return array();
211 }
212
213 $response = wp_safe_remote_get(
214 sprintf( 'https://commercebird.com/wp-json/wc/v3/subscriptions/%s', $subscription_id ),
215 array(
216 'headers' => array(
217 'Accept' => 'application/json',
218 'Authorization' => 'Basic Y2tfYjAzMDViODhmNmQ1ZDI2ZTY0MjNjMDczZjZmOTVkZTExOWNjOWU1NTpjc182MDljMTNmMjgxODE2YjkzNzQ5OWIyYTAwNTJlMTE0NTc0NWFjZGMz',
219 ),
220 'timeout' => 15,
221 ),
222 );
223
224 if ( is_wp_error( $response ) ) {
225 $this->errors = array( 'message' => $response->get_error_messages() );
226 return array();
227 }
228
229 $body = wp_remote_retrieve_body( $response );
230 $decode = json_decode( $body, true );
231
232 if ( ! $decode || ! array_key_exists( 'line_items', $decode ) ) {
233 return array();
234 }
235
236 $data = $this->extract_data(
237 $decode,
238 array(
239 'fee_lines',
240 'total',
241 'currency',
242 'next_payment_date_gmt',
243 'needs_payment',
244 'payment_url',
245 'status',
246 'variation_id',
247 'line_items',
248 'billing',
249 ),
250 );
251 $data['variation_id'] = array_column( $data['line_items'], 'variation_id' );
252 $data['plan'] = array_column( $data['line_items'], 'name' );
253
254 return $data;
255 }
256
257 /**
258 * Resets the settings details.
259 *
260 * @return void
261 */
262 public function settings_reset(): void {
263 $this->verify();
264 $this->option_status_remove( self::FORMS['settings'], self::SOURCE );
265 $this->response = array( 'message' => 'Reset successfully!' );
266 $this->serve();
267 }
268
269 /**
270 * Retrieves the settings details.
271 *
272 * @return void
273 */
274 public function settings_get(): void {
275 $this->verify();
276 $this->response = $this->option_status_get( self::FORMS['settings'], self::SOURCE );
277 $this->serve();
278 }
279
280 /**
281 * Sets the settings for the class.
282 *
283 * @return void
284 */
285 public function settings_set(): void {
286 try {
287 $this->verify( self::FORMS['settings'] );
288 if ( $this->data ) {
289 $this->option_status_update( $this->data, self::SOURCE );
290 delete_transient( 'zoho_subscription' );
291 } else {
292 $this->errors = array(
293 'message' => 'Invalid Inputs',
294 $this->data,
295 );
296 }
297 } catch (Exception $exception) {
298 $this->errors = array( 'message' => $exception->getMessage() );
299 }
300 $this->serve();
301 }
302
303 /**
304 * Retrieves the fields and serves the response.
305 *
306 * @return void
307 */
308 public function fields_get(): void {
309 $this->verify();
310 $this->response['form'] = get_option( 'cmbird_wootozoho_custom_fields', array() );
311 $this->serve();
312 }
313
314 /**
315 * Sets the custom fields for the form.
316 *
317 * @return void
318 */
319 public function fields_set(): void {
320 $this->verify( array( 'form' ) );
321 try {
322 update_option( 'cmbird_wootozoho_custom_fields', $this->data['form'] );
323 $this->response = array( 'message' => 'saved' );
324 } catch (Throwable $throwable) {
325 $this->errors = array( 'message' => $throwable->getMessage() );
326 }
327 $this->serve();
328 }
329
330 /**
331 * Resets the fields.
332 *
333 * @return void
334 */
335 public function fields_reset(): void {
336 $this->verify();
337 delete_option( 'cmbird_wootozoho_custom_fields' );
338 $this->response = array( 'message' => 'Reset successfully!' );
339 $this->serve();
340 }
341
342 /**
343 * Resets the price.
344 *
345 * @return void
346 */
347 public function price_reset(): void {
348 $this->verify();
349 delete_option( 'cmbird_zoho_pricelist_id' );
350 delete_option( 'cmbird_zoho_pricelist_role' );
351 delete_option( 'cmbird_zoho_pricelist_wcb2b_groups' );
352 $this->response = array( 'message' => 'Reset successfully!' );
353 $this->serve();
354 }
355
356 /**
357 * Retrieves the price information.
358 *
359 * @return void
360 */
361 public function price_get(): void {
362 $this->verify();
363 $this->response['zoho_inventory_pricelist'] = get_option( 'cmbird_zoho_pricelist_id' );
364 $this->response['wp_user_role'] = get_option( 'cmbird_zoho_pricelist_role' );
365 if ( class_exists( 'WooCommerceB2B' ) ) {
366 $this->response['wcb2b'] = get_option( 'cmbird_zoho_pricelist_wcb2b_groups' );
367 }
368 $this->serve();
369 }
370
371 /**
372 * Sets the price and saves the pricelist.
373 *
374 */
375 public function price_set(): void {
376 $this->verify( self::FORMS['price'] );
377 try {
378 $import_pricelist = new CMBIRD_Pricelist_ZI();
379 $success = $import_pricelist->save_price_list( $this->data );
380 if ( class_exists( 'Addify_B2B_Plugin' ) ) {
381 update_option( 'cmbird_zoho_pricelist_role', $this->data['wp_user_role'] );
382 } else {
383 update_option( 'cmbird_zoho_pricelist_wcb2b_groups', $this->data['wcb2b'] );
384 }
385 if ( $success ) {
386 $this->response = array( 'message' => 'Saved' );
387 if ( class_exists( 'WooCommerceB2B' ) ) {
388 $this->response['wcb2b'] = $this->data['wcb2b'];
389 }
390 } else {
391 $this->errors = array( 'message' => 'Failed to save' );
392 }
393 } catch (Throwable $throwable) {
394 $this->errors = array( 'message' => $throwable->getMessage() );
395 $this->write_log(
396 array(
397 'message' => $throwable->getMessage(),
398 'files' => wp_list_pluck( $throwable->getTrace(), 'file', 'line' ),
399 ),
400 'zoho-ajax-error'
401 );
402 }
403
404 $this->serve();
405 }
406
407 /**
408 * Collects Zoho prices.
409 *
410 * @return void
411 */
412 public function zoho_prices_collect(): void {
413 $this->verify();
414 $price_list_class = new CMBIRD_Pricelist_ZI();
415 $prices = $price_list_class->zi_get_all_pricelist();
416 $this->response = wp_list_pluck( $prices, 'name', 'pricebook_id' );
417 $this->serve();
418 }
419
420 /**
421 * Retrieves the contact details.
422 * @return void
423 */
424 public function contact_get() {
425 $this->verify();
426 $this->response = $this->option_status_get( self::FORMS['contact'], self::SOURCE );
427 $this->serve();
428 }
429 /**
430 * Sets the contact and updates the status.
431 * @return void
432 */
433 public function contact_set() {
434 $this->verify( self::FORMS['contact'] );
435 $this->option_status_update( $this->data, self::SOURCE );
436 if ( isset( $this->data['enable'] ) && ! empty( $this->data['enable'] ) ) {
437 if ( ! wp_next_scheduled( 'zoho_contact_sync' ) ) {
438 wp_schedule_event( time(), 'daily', 'zoho_contact_sync' );
439 }
440 } else {
441 wp_clear_scheduled_hook( 'zoho_contact_sync' );
442 }
443 $this->response = array( 'message' => 'Saved!' );
444 $this->serve();
445 }
446 /**
447 * Resets the contact.
448 * @return void
449 */
450 public function contact_reset() {
451 $this->verify();
452 $this->option_status_remove( self::FORMS['contact'], self::SOURCE );
453 $this->response = array( 'message' => 'Reset successfully!' );
454 $this->serve();
455 }
456
457 /**
458 * Sets the order and updates the status.
459 *
460 * @return void
461 */
462 public function order_set(): void {
463 $this->verify( self::FORMS['order'] );
464 $this->option_status_update( $this->data, self::SOURCE );
465 $this->response = array( 'message' => 'Saved!' );
466 $this->serve();
467 }
468
469 /**
470 * Retrieves the order.
471 *
472 * @return void
473 */
474 public function order_get(): void {
475 $this->verify();
476 $this->response = $this->option_status_get( self::FORMS['order'], self::SOURCE );
477 $this->serve();
478 }
479
480 /**
481 * Resets the order.
482 *
483 * @return void
484 */
485 public function order_reset(): void {
486 $this->verify();
487 $this->option_status_remove( self::FORMS['order'], self::SOURCE );
488 $this->response = array( 'message' => 'Reset successfully!' );
489 $this->serve();
490 }
491
492 /**
493 * Collects Zoho warehouses.
494 *
495 * This function verifies the request, retrieves the Zoho inventory
496 * organization ID and URL from the options, constructs the URL
497 * for the API call, and executes the cURL call to retrieve the
498 * warehouses. The response is then processed to extract the
499 * warehouse names and IDs, and stored in the class property.
500 *
501 * @return void
502 */
503 public function zoho_warehouses_collect(): void {
504 $this->verify();
505 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
506 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
507 $url = $zoho_inventory_url . 'inventory/v1/settings/warehouses?organization_id=' . $zoho_inventory_oid;
508 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
509 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
510 $this->response = wp_list_pluck( $json->warehouses, 'warehouse_name', 'warehouse_id' );
511 $this->serve();
512 }
513
514 /**
515 * Sets the cron for the class.
516 *
517 */
518 public function cron_set(): void {
519 $this->verify( self::FORMS['cron'] );
520 if ( array_key_exists( 'form', $this->data ) ) {
521 $decode = json_decode( $this->data['form'], true );
522 update_option( 'zi_cron_interval', $decode['zi_cron_interval'] );
523 unset( $decode['zi_cron_interval'] );
524 $this->option_status_update( $decode, self::SOURCE );
525 }
526 if ( array_key_exists( 'categories', $this->data ) ) {
527 $decode = json_decode( $this->data['categories'] );
528 update_option( 'cmbird_zoho_item_category', serialize( $decode ) );
529 }
530 $this->response = array( 'message' => 'Saved' );
531 // also set the cron job to sync categories.
532 if ( ! wp_next_scheduled( 'cmbird_zi_category_cron' ) ) {
533 wp_schedule_event( time(), 'daily', 'cmbird_zi_category_cron' );
534 }
535 $this->serve();
536 }
537
538 /**
539 * Executes the cron_get function.
540 *
541 * This function verifies the form status and retrieves the form options
542 * for syncing name, price, image, and description. It also retrieves
543 * the cron interval and the Zoho item categories.
544 *
545 * @return void
546 */
547 public function cron_get(): void {
548 $this->verify();
549 $this->response = array();
550 $this->response['form'] = $this->option_status_get(
551 array(
552 'disable_name_sync',
553 'disable_price_sync',
554 'disable_image_sync',
555 'disable_description_sync',
556 ),
557 self::SOURCE,
558 );
559 $this->response['form']['zi_cron_interval'] = get_option( 'zi_cron_interval', 'none' );
560 $categories = get_option( 'cmbird_zoho_item_category', '' );
561 $this->response['categories'] = maybe_unserialize( $categories );
562 $this->serve();
563 }
564
565 /**
566 * Resets the cron job.
567 *
568 * This function verifies the cron job, removes the specified options,
569 * deletes the 'zoho_item_category' option, and serves the cron job.
570 *
571 * @return void
572 */
573 public function cron_reset(): void {
574 $this->verify();
575 $this->option_status_remove(
576 array(
577 'disable_name_sync',
578 'disable_price_sync',
579 'disable_image_sync',
580 'disable_description_sync',
581 ),
582 self::SOURCE,
583 );
584 delete_option( 'cmbird_zoho_item_category' );
585 // delete all options that contain 'zoho_id_for_term_id_'
586 global $wpdb;
587 $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'zoho_id_for_term_id_%'" );
588 $this->response = array( 'message' => 'Reset successfully!' );
589 $this->serve();
590 }
591
592 /**
593 * Collects Zoho categories.
594 *
595 * This function verifies the data and retrieves the Zoho item categories.
596 * If the categories are successfully retrieved, it filters the data to
597 * include only the 'name' and 'category_id' fields, and removes the
598 * category with ID -1. Finally, it sets the response with the filtered
599 * categories and serves the response.
600 */
601 public function zoho_categories_collect(): void {
602 $this->verify();
603 $category_class = new CMBIRD_Categories_ZI();
604 $categories = $category_class->cmbird_get_zoho_item_categories();
605 if ( gettype( $categories ) === 'array' ) {
606 // $filtered = wp_list_pluck( $categories, 'name', 'category_id' );
607 // unset( $filtered[ -1 ] );
608 $this->response = $categories;
609 }
610 $this->serve();
611 }
612
613 /**
614 * Executes the connection process and handles the response.
615 */
616 public function connection_done(): void {
617 $this->verify();
618 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
619 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
620 $url = $zoho_inventory_url . 'inventory/v1/organizations/?organization_id=' . $zoho_inventory_oid;
621 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
622 $json = $execute_curl_call_handle->execute_curl_call_get( $url );
623 if ( is_wp_error( $json ) ) {
624 $this->errors = array( 'message' => $json->get_error_message() );
625 } elseif ( empty( $json ) ) {
626 $this->errors = array( 'message' => 'We lost connection with zoho. please refresh page.' );
627 } else {
628 $this->response = $json->organizations;
629 }
630 $this->serve();
631 }
632
633 /**
634 * Executes the product_set function.
635 */
636 public function product_set(): void {
637 $this->verify( self::FORMS['product'] );
638 if ( ! empty( $this->data ) ) {
639 $this->option_status_update( $this->data, self::SOURCE );
640 }
641 $this->response = array( 'message' => 'Saved!' );
642 $this->serve();
643 }
644
645 /**
646 * Retrieves the product data.
647 *
648 * @return void
649 */
650 public function product_get(): void {
651 $this->verify();
652 $this->response = $this->option_status_get( self::FORMS['product'], self::SOURCE );
653 $this->serve();
654 }
655
656 /**
657 * Resets the product.
658 *
659 * @return void
660 */
661 public function product_reset(): void {
662 $this->verify();
663 $this->option_status_remove( self::FORMS['product'], self::SOURCE );
664 $this->response = array( 'message' => 'Reset successfully!' );
665 $this->serve();
666 }
667
668 /**
669 * Retrieves the tax information.
670 *
671 * @return void
672 */
673 public function tax_get(): void {
674 $this->verify();
675 $this->response = array();
676 foreach ( $this->wc_taxes() as $tax ) {
677 $this->response['selectedTaxRates'][] = $tax['id'] . '^^' . get_option( 'cmbird_zoho_inventory_tax_rate_' . $tax['id'] );
678 }
679 $this->serve();
680 }
681
682 /**
683 * Retrieves an array of all tax rates from WooCommerce.
684 *
685 * @return array An array of all tax rates.
686 */
687 public function wc_taxes(): array {
688 $wc_tax_array = array();
689 $tax_classes = WC_Tax::get_tax_classes(); // Retrieve all tax classes.
690 if ( ! in_array( '', $tax_classes ) ) { // Make sure "Standard rate" (empty class name) is present.
691 array_unshift( $tax_classes, '' );
692 }
693
694 foreach ( $tax_classes as $tax_class ) { // For each tax class, get all rates.
695 $taxes = WC_Tax::get_rates_for_tax_class( $tax_class );
696
697 foreach ( $taxes as $key => $tax ) {
698 $taxarray = (array) $tax;
699 $taxarray['id'] = $key;
700 $wc_tax_array[] = $taxarray;
701 }
702 }
703
704 return $wc_tax_array;
705 }
706
707 /**
708 * Resets the tax settings.
709 *
710 * This function verifies the tax settings and then deletes the options
711 * related to the tax rates
712 *
713 * @return void
714 */
715 public function tax_reset(): void {
716 $this->verify();
717 foreach ( $this->wc_taxes() as $tax ) {
718 delete_option( 'cmbird_zoho_inventory_tax_rate_' . $tax['id'] );
719 }
720 $this->response = array( 'message' => 'Reset successfully!' );
721 $this->serve();
722 }
723
724 /**
725 * Saves tax rates to the database.
726 */
727 public function tax_set(): void {
728 $this->verify( self::FORMS['tax'] );
729 $rates = array_filter( $this->data['selectedTaxRates'] );
730 try {
731 foreach ( $rates as $value ) {
732 $valarray = explode( '^^', $value );
733 update_option( 'cmbird_zoho_inventory_tax_rate_' . $valarray[0], $valarray[1] );
734 }
735 $this->response = array( 'message' => 'Saved' );
736 } catch (Throwable $throwable) {
737 $this->errors = array( 'message' => $throwable->getMessage() );
738 }
739
740 $this->serve();
741 }
742
743 /**
744 * Sets the connection for the Zoho Inventory API.
745 */
746 public function connection_set(): void {
747 $this->verify(
748 array(
749 'account_domain',
750 'organization_id',
751 'client_id',
752 'client_secret',
753 'redirect_uri',
754 ),
755 );
756 try {
757 $inventory = sprintf( 'https://www.zohoapis.%s/', $this->data['account_domain'] );
758 update_option( 'cmbird_zoho_inventory_domain', $this->data['account_domain'] );
759 update_option( 'cmbird_zoho_inventory_oid', $this->data['organization_id'] );
760 update_option( 'cmbird_zoho_inventory_cid', $this->data['client_id'] );
761 update_option( 'cmbird_zoho_inventory_cs', $this->data['client_secret'] );
762 update_option( 'cmbird_zoho_inventory_url', $inventory );
763 update_option( 'cmbird_authorization_redirect_uri', $this->data['redirect_uri'] );
764 $redirect = esc_url_raw( 'https://accounts.zoho.' . $this->data['account_domain'] . '/oauth/v2/auth?response_type=code&client_id=' . $this->data['client_id'] . '&scope=ZohoInventory.FullAccess.all&redirect_uri=' . $this->data['redirect_uri'] . '&prompt=consent&access_type=offline&state=' . wp_create_nonce( Template::NAME ) );
765 $this->response = array(
766 'redirect' => $redirect,
767 'message' => 'We are redirecting you to zoho. please wait...',
768 );
769 } catch (Throwable $throwable) {
770 $this->errors = array( 'message' => $throwable->getMessage() );
771 }
772
773 $this->serve();
774 }
775
776 /**
777 * Resets the connection by deleting specific options from the database.
778 */
779 public function connection_reset(): void {
780 $this->verify();
781 try {
782 $options = array(
783 'cmbird_zoho_inventory_domain',
784 'cmbird_zoho_inventory_oid',
785 'cmbird_zoho_inventory_cid',
786 'cmbird_zoho_inventory_cs',
787 'cmbird_zoho_inventory_url',
788 'cmbird_zoho_inventory_access_token',
789 );
790 foreach ( $options as $zi_option ) {
791 delete_option( $zi_option );
792 }
793 $this->response = array( 'message' => 'Reset successfully!' );
794 } catch (Throwable $throwable) {
795 $this->errors = array( 'message' => $throwable->getMessage() );
796 }
797
798 $this->serve();
799 }
800
801 /**
802 * Retrieves the Zoho tax rates for the organization.
803 *
804 * This function verifies the request, retrieves the organization ID and URL
805 * from the options, constructs the URL for the API request, and executes
806 * the cURL call to fetch the tax rates. If the response contains the
807 * 'taxes' key, it sets the response to the tax rates. Otherwise, it sets
808 * the errors to a default message.
809 */
810 public function zoho_tax_rates_collect(): void {
811 $this->verify();
812 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_oid' );
813 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
814 $url = $zoho_inventory_url . 'inventory/v1/settings/taxes?organization_id=' . $zoho_inventory_oid;
815 try {
816 $execute_curl_call_handle = new CMBIRD_API_Handler_Zoho();
817 $json = (array) $execute_curl_call_handle->execute_curl_call_get( $url );
818 if ( array_key_exists( 'taxes', $json ) ) {
819 $this->response = $json['taxes'];
820 } else {
821 $this->errors = array( 'message' => 'Something went wrong!' );
822 }
823 } catch (Throwable $throwable) {
824 $this->errors = array( 'message' => $throwable->getMessage() );
825 }
826
827 $this->serve();
828 }
829
830 /**
831 * Executes the wc_tax_collect function.
832 *
833 * This function verifies the current state, retrieves the taxes using the
834 * wc_taxes method, and serves the response.
835 */
836 public function wc_tax_collect(): void {
837 $this->verify();
838 $this->response = $this->wc_taxes();
839 $this->serve();
840 }
841
842 /**
843 * Handles the oAuth code.
844 */
845 public function handle_code(): void {
846 $this->verify();
847 if ( array_key_exists( 'code', $this->request ) ) {
848 $class_functions = new CMBIRD_Auth_Zoho();
849 $code = $this->request['code'];
850 try {
851 $access_token = $class_functions->get_zoho_access_token( $code, 'zoho_inventory' );
852 if ( array_key_exists( 'access_token', $access_token ) ) {
853 update_option( 'cmbird_zoho_inventory_auth_code', $code );
854 update_option( 'cmbird_zoho_inventory_access_token', $access_token['access_token'] );
855 update_option( 'cmbird_zoho_inventory_refresh_token', $access_token['refresh_token'] );
856 update_option( 'cmbird_zoho_inventory_timestamp', strtotime( gmdate( 'Y-m-d H:i:s' ) ) + $access_token['expires_in'] );
857 $this->response = (array) $access_token;
858 } else {
859 $this->errors = (array) $access_token;
860 }
861 } catch (Throwable $throwable) {
862 $this->errors = array( 'message' => $throwable->getMessage() );
863 }
864 }
865 $this->serve();
866 }
867 }
868