PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.3.8
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.3.8
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 index.php 1 year ago
CommerceBird.php
161 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 API = 'https://api.commercebird.com';
25
26 public function cost_centers() {
27 return $this->request( self::COST_CENTERS );
28 }
29
30 public function gl_accounts() {
31 return $this->request( self::GL_ACCOUNTS );
32 }
33
34 /**
35 * Suscribe to Exact Online Webhooks. Pass the webhook URL to Exact Online and Topic
36 *
37 * @param array $data array ( callback_url, topic )
38 * @return array|WP_Error array ( webhook_id, topic )
39 */
40 public function subscribe_exact_webhooks( array $data ) {
41 $response = $this->request( self::WEBHOOKS, 'POST', $data );
42 return $response['code'] === 200 ? $response['data'] : $response['message'];
43 }
44
45 /**
46 * Collect customer or account id
47 *
48 * @param array $customer array ( customer_email, company_name )
49 *
50 * @return array|WP_Error array ( account_id, company_id )
51 * @throws WP_Error Invalid customer if empty
52 */
53 public function customer() {
54 $response = $this->request( self::CUSTOMER );
55 return $response['code'] === 200 ? $response['data'] : $response['message'];
56 }
57 /**
58 * Collect customer or account id
59 *
60 * @param array $customer array ( customer_email, company_name )
61 *
62 * @return array|WP_Error array ( account_id, company_id )
63 * @throws WP_Error Invalid customer if empty
64 */
65 public function order( array $range ) {
66
67 $response = $this->request(
68 self::ORDER,
69 'GET',
70 array(),
71 $range
72 );
73 return $response['code'] === 200 ? $response['data'] : $response['message'];
74 }
75
76 /**
77 * Get item ID by product ID
78 *
79 * @param int $id of the item
80 *
81 * @return array|WP_Error The ID of the item or an error object.
82 */
83 public function products() {
84 $response = $this->request( self::ITEM );
85 return $response['code'] === 200 ? $response['data'] : $response['message'];
86 }
87
88 public function cost_units() {
89 return $this->request( self::COST_UNITS );
90 }
91
92 /**
93 * Get payment status
94 *
95 * @param array
96 *
97 * @return array|WP_Error array ( payment_status )
98 */
99 public function payment_status( array $data ) {
100 $response = $this->request( self::PAYMENT_STATUS, 'POST', $data, array() );
101 return $response['code'] === 200 ? $response['data'] : $response['message'];
102 }
103
104 /**
105 * Generate request URL
106 * @param string $endpoint
107 * @param string $method
108 * @param array $data
109 * @param array $params
110 * @return array|WP_Error
111 */
112 public function request( string $endpoint, string $method = 'GET', array $data = array(), array $params = array() ) {
113 $token = ExactOnlineAjax::instance()->get_token();
114 $token = ! empty( $token ) ? $token : '';
115 $url = sprintf( '%s/%s?token=%s', self::API, $endpoint, $token );
116 if ( ! empty( $params ) ) {
117 $url .= '&' . http_build_query( $params );
118 }
119
120 // if current site contains localhost, use https://dev.commercebird.com
121 $current_site_url = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
122 $site_url = str_contains( $current_site_url, 'localhost' ) ? 'https://dev.commercebird.com' : site_url();
123
124 if ( 'POST' === $method ) {
125 $response = wp_remote_post(
126 $url,
127 array(
128 'headers' => array(
129 'Accept' => 'application/json',
130 'Content-Type' => 'application/json',
131 'zohowooagent' => $site_url,
132 ),
133 'timeout' => 60,
134 'sslverify' => false,
135 'body' => wp_json_encode( $data ),
136 )
137 );
138 } else {
139 $response = wp_remote_get(
140 $url,
141 array(
142 'headers' => array(
143 'Accept' => 'application/json',
144 'zohowooagent' => $site_url,
145 'x-woozo-module' => $data['module'] ?? '',
146 ),
147 'timeout' => 60,
148 'sslverify' => false,
149 )
150 );
151 }
152
153 if ( is_wp_error( $response ) ) {
154 $this->write_log( $response->get_error_message(), 'commercebird-connector' );
155 return new WP_Error( 'request_failed', $response->get_error_message() );
156 }
157 $response = wp_remote_retrieve_body( $response );
158 return json_decode( $response, true );
159 }
160 }
161