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 / Connectors / CommerceBird.php
commercebird / admin / includes / Connectors Last commit date
CommerceBird.php 1 year ago
CommerceBird.php
172 lines
1 <?php
2
3 namespace CommerceBird\Admin\Connectors;
4
5 use CommerceBird\Admin\Actions\Ajax\ExactOnlineAjax;
6 use CommerceBird\Admin\Actions\Ajax\ZohoCRMAjax;
7 use CommerceBird\Admin\Traits\LogWriter;
8 use WP_Error;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 final class CommerceBird {
15 use LogWriter;
16
17 const COST_CENTERS = 'customs/exact/cost-centers';
18 const COST_UNITS = 'customs/exact/cost-units';
19 const GL_ACCOUNTS = 'customs/exact/gl-accounts';
20 const ITEM = 'customs/exact/bulk-items';
21 const CUSTOMER = 'customs/exact/bulk-customers';
22 const ORDER = 'customs/exact/bulk-orders';
23 const PAYMENT_STATUS = 'customs/exact/invoice-payment-status';
24 const WEBHOOKS = 'customs/exact/webhooks';
25 const API = 'https://api.commercebird.com';
26 const ZCRMFIELDS = 'customs/zoho/fields';
27
28 public function cost_centers() {
29 return $this->request( self::COST_CENTERS );
30 }
31
32 public function gl_accounts() {
33 return $this->request( self::GL_ACCOUNTS );
34 }
35
36 /**
37 * Suscribe to Exact Online Webhooks. Pass the webhook URL to Exact Online and Topic
38 *
39 * @param array $data array ( callback_url, topic )
40 * @return array|WP_Error array ( webhook_id, topic )
41 */
42 public function subscribe_exact_webhooks( array $data ) {
43 $response = $this->request( self::WEBHOOKS, 'POST', $data );
44 return $response['code'] === 200 ? $response['data'] : $response['message'];
45 }
46
47 /**
48 * Get all Zoho CRM Custom fields
49 *
50 * @param
51 *
52 * @return array|WP_Error array ( account_id, company_id )
53 * @throws WP_Error Invalid customer if empty
54 */
55 public function get_zcrm_fields( $module ) {
56 $response = $this->request( self::ZCRMFIELDS, 'GET', array( 'module' => $module ) );
57 return $response['code'] === 200 ? $response['data'] : $response['message'];
58 }
59
60 /**
61 * Collect customer or account id
62 *
63 * @param array $customer array ( customer_email, company_name )
64 *
65 * @return array|WP_Error array ( account_id, company_id )
66 * @throws WP_Error Invalid customer if empty
67 */
68 public function customer() {
69 $response = $this->request( self::CUSTOMER );
70 return $response['code'] === 200 ? $response['data'] : $response['message'];
71 }
72 /**
73 * Collect customer or account id
74 *
75 * @param array $customer array ( customer_email, company_name )
76 *
77 * @return array|WP_Error array ( account_id, company_id )
78 * @throws WP_Error Invalid customer if empty
79 */
80 public function order( array $range ) {
81
82 $response = $this->request(
83 self::ORDER,
84 'GET',
85 array(),
86 $range
87 );
88 return $response['code'] === 200 ? $response['data'] : $response['message'];
89 }
90
91 /**
92 * Get item ID by product ID
93 *
94 * @param int $id of the item
95 *
96 * @return array|WP_Error The ID of the item or an error object.
97 */
98 public function products() {
99
100 $response = $this->request( self::ITEM );
101
102 return $response['code'] === 200 ? $response['data'] : $response['message'];
103 }
104
105 public function cost_units() {
106 return $this->request( self::COST_UNITS );
107 }
108
109 /**
110 * Get payment status
111 *
112 * @param array
113 *
114 * @return array|WP_Error array ( payment_status )
115 */
116 public function payment_status( array $data ) {
117 $response = $this->request( self::PAYMENT_STATUS, 'POST', $data, array() );
118 return $response['code'] === 200 ? $response['data'] : $response['message'];
119 }
120
121 /**
122 * Generate request URL
123 */
124 private function request( string $endpoint, string $method = 'GET', array $data = array(), array $params = array() ) {
125 $token = ! empty( ExactOnlineAjax::instance()->get_token() ) ? ExactOnlineAjax::instance()->get_token() : ZohoCRMAjax::instance()->get_token();
126 $url = sprintf( '%s/%s?token=%s', self::API, $endpoint, $token );
127 if ( ! empty( $params ) ) {
128 $url .= '&' . http_build_query( $params );
129 }
130
131 // if current site contains localhost, use https://dev.commercebird.com
132 $current_site_url = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
133 $site_url = str_contains( $current_site_url, 'localhost' ) ? 'https://dev.commercebird.com' : site_url();
134
135 if ( 'POST' === $method ) {
136 $response = wp_remote_post(
137 $url,
138 array(
139 'headers' => array(
140 'Accept' => 'application/json',
141 'Content-Type' => 'application/json',
142 'zohowooagent' => $site_url,
143 ),
144 'timeout' => 60,
145 'sslverify' => false,
146 'body' => wp_json_encode( $data ),
147 )
148 );
149 } else {
150 $response = wp_remote_get(
151 $url,
152 array(
153 'headers' => array(
154 'Accept' => 'application/json',
155 'zohowooagent' => $site_url,
156 'x-woozo-module' => $data['module'] ?? '',
157 ),
158 'timeout' => 60,
159 'sslverify' => false,
160 )
161 );
162 }
163
164 if ( is_wp_error( $response ) ) {
165 $this->write_log( $response->get_error_message(), 'commercebird-connector' );
166 return;
167 }
168 $response = wp_remote_retrieve_body( $response );
169 return json_decode( $response, true );
170 }
171 }
172