PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.13
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.13
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 11 months ago index.php 1 year ago
CommerceBird.php
206 lines
1 <?php
2
3 namespace CommerceBird\Admin\Connectors;
4
5 use CommerceBird\Admin\Actions\Ajax\ExactOnlineAjax;
6 use CommerceBird\Admin\Traits\LogWriter;
7 use WP_Error;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 final class CommerceBird {
14 use LogWriter;
15
16 const COST_CENTERS = 'customs/exact/cost-centers';
17 const COST_UNITS = 'customs/exact/cost-units';
18 const GL_ACCOUNTS = 'customs/exact/gl-accounts';
19 const ITEM = 'customs/exact/bulk-items';
20 const CUSTOMER = 'customs/exact/bulk-customers';
21 const ORDER = 'customs/exact/bulk-orders';
22 const PAYMENT_STATUS = 'customs/exact/invoice-payment-status';
23 const WEBHOOKS = 'customs/exact/webhooks';
24 const WOO_WEBHOOKS = 'customs/woo/webhooks';
25 const CONNECT_EXACT = 'webapp/exact/auth-url/plugin';
26 const EXACT_DIVISIONS = 'webapp/exact/divisions/plugin';
27 const API = 'https://api.commercebird.com';
28 // const API = 'http://localhost:3000';
29
30
31 /**
32 * Connect to exact online and get authorization URL.
33 *
34 * @param array $data array ( callback_url, topic )
35 * @return array|WP_Error array ( webhook_id, topic )
36 */
37 public function create_woo_webbhook() {
38 return $this->request( self::WOO_WEBHOOKS, 'POST' );
39 }
40
41 /**
42 * Connect to exact online and get authorization URL.
43 *
44 * @param array $data array ( callback_url, topic )
45 * @return array|WP_Error array ( webhook_id, topic )
46 */
47 public function connect_exact( $domain_suffix ) {
48 return $this->request( self::CONNECT_EXACT, 'GET', array(), array( 'domain' => $domain_suffix ) );
49 }
50
51 /**
52 * Get Exact Online divisions
53 *
54 * @return array|WP_Error array ( divisions )
55 */
56 public function get_exact_divisions() {
57 return $this->request( self::EXACT_DIVISIONS );
58 }
59
60 public function cost_centers() {
61 return $this->request( self::COST_CENTERS );
62 }
63
64 public function gl_accounts() {
65 return $this->request( self::GL_ACCOUNTS );
66 }
67
68 /**
69 * Suscribe to Exact Online Webhooks. Pass the webhook URL to Exact Online and Topic
70 *
71 * @param array $data array ( callback_url, topic )
72 * @return array|WP_Error array ( webhook_id, topic )
73 */
74 public function subscribe_exact_webhooks( array $data ) {
75 $response = $this->request( self::WEBHOOKS, 'POST', $data );
76 return $response['code'] === 200 ? $response['data'] : $response['message'];
77 }
78
79 /**
80 * Collect customer or account id
81 *
82 * @param array $customer array ( customer_email, company_name )
83 *
84 * @return array|WP_Error array ( account_id, company_id )
85 * @throws WP_Error Invalid customer if empty
86 */
87 public function customer() {
88 $response = $this->request( self::CUSTOMER );
89 return $response['code'] === 200 ? $response['data'] : $response['message'];
90 }
91 /**
92 * Collect customer or account id
93 *
94 * @param array $customer array ( customer_email, company_name )
95 *
96 * @return array|WP_Error array ( account_id, company_id )
97 * @throws WP_Error Invalid customer if empty
98 */
99 public function order( array $range ) {
100
101 $response = $this->request(
102 self::ORDER,
103 'GET',
104 array(),
105 $range
106 );
107 return $response['code'] === 200 ? $response['data'] : $response['message'];
108 }
109
110 /**
111 * Send Order Ids to Exact Online using the bulk endpoint
112 *
113 * @param array $data array ( order_ids )
114 * @return array|WP_Error array ( order_ids )
115 * @throws WP_Error Invalid order if empty
116 */
117 public function send_orders( array $data ) {
118 $response = $this->request( self::ORDER, 'POST', $data );
119 return $response['code'] === 200 ? $response['data'] : $response['message'];
120 }
121
122 /**
123 * Get item ID by product ID
124 *
125 * @param int $id of the item
126 *
127 * @return array|WP_Error The ID of the item or an error object.
128 */
129 public function products() {
130 $response = $this->request( self::ITEM );
131 return $response['code'] === 200 ? $response['data'] : $response['message'];
132 }
133
134 public function cost_units() {
135 return $this->request( self::COST_UNITS );
136 }
137
138 /**
139 * Get payment status
140 *
141 * @param array
142 *
143 * @return array|WP_Error array ( payment_status )
144 */
145 public function payment_status( array $data ) {
146 $response = $this->request( self::PAYMENT_STATUS, 'POST', $data, array() );
147 return $response['code'] === 200 ? $response['data'] : $response['message'];
148 }
149
150 /**
151 * Generate request URL
152 * @param string $endpoint
153 * @param string $method
154 * @param array $data
155 * @param array $params
156 * @return array|WP_Error
157 */
158 public function request( string $endpoint, string $method = 'GET', array $data = array(), array $params = array() ) {
159 $token = ExactOnlineAjax::instance()->get_token();
160 $token = ! empty( $token ) ? $token : '';
161 $url = sprintf( '%s/%s?token=%s', self::API, $endpoint, $token );
162 if ( ! empty( $params ) ) {
163 $url .= '&' . http_build_query( $params );
164 }
165
166 // if current site contains localhost, use https://dev.commercebird.com
167 $current_site_url = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
168 $site_url = empty( $current_site_url ) || str_contains( $current_site_url, 'localhost' ) ? 'https://dev.commercebird.com' : site_url();
169
170 if ( 'POST' === $method ) {
171 $response = wp_remote_post(
172 $url,
173 array(
174 'headers' => array(
175 'Accept' => 'application/json',
176 'Content-Type' => 'application/json',
177 'zohowooagent' => $site_url,
178 ),
179 'timeout' => 60,
180 'sslverify' => false,
181 'body' => wp_json_encode( $data ),
182 )
183 );
184 } else {
185 $response = wp_remote_get(
186 $url,
187 array(
188 'headers' => array(
189 'Accept' => 'application/json',
190 'zohowooagent' => $site_url,
191 'x-woozo-module' => $data['module'] ?? '',
192 ),
193 'timeout' => 60,
194 'sslverify' => false,
195 )
196 );
197 }
198 if ( is_wp_error( $response ) ) {
199 $this->write_log( $response->get_error_message(), 'commercebird-connector' );
200 return new WP_Error( 'request_failed', $response->get_error_message() );
201 }
202 $response = wp_remote_retrieve_body( $response );
203 return json_decode( $response, true );
204 }
205 }
206