CommerceBird.php
332 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 | 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 INVOICE_LAYOUTS = 'customs/exact/invoice-layouts'; |
| 20 | const JOURNAL_LIST = 'customs/exact/journal'; |
| 21 | const EXACT_WAREHOUSES = 'customs/exact/warehouses'; |
| 22 | const EXACT_GENERAL_SETTINGS = 'customs/exact/gen-settings'; |
| 23 | const ITEM = 'customs/exact/bulk-items'; |
| 24 | const CUSTOMER = 'customs/exact/bulk-customers'; |
| 25 | const ORDER = 'customs/exact/bulk-orders'; |
| 26 | const PAYMENT_STATUS = 'customs/exact/invoice-payment-status'; |
| 27 | const WEBHOOKS = 'customs/exact/webhooks'; |
| 28 | const WOO_WEBHOOKS = 'customs/woo/webhooks'; |
| 29 | const CONNECT_EXACT = 'webapp/exact/auth-url/plugin'; |
| 30 | const EXACT_DIVISIONS = 'webapp/exact/divisions/plugin'; |
| 31 | const ACCOUNT_SETTINGS = 'customs/settings'; |
| 32 | const SUBSCRIPTION = 'customs/subscriptions'; |
| 33 | const EXACT_TAXMAPPING = 'customs/exact/settings/taxes'; |
| 34 | const EXACT_VATCODES = 'customs/exact/settings/vat-codes'; |
| 35 | const EXACT_PAYMENT_CONDITIONS = 'customs/exact/settings/payment-conditions'; |
| 36 | const EXACT_PAYMENT_STATUS_MAPPING = 'customs/exact/settings/payment-status'; |
| 37 | const API = 'https://api.commercebird.com'; |
| 38 | // const API = 'http://localhost:3000'; |
| 39 | |
| 40 | /** |
| 41 | * Get Subscription data from CommerceBird API. |
| 42 | * @param integer $subscription_id |
| 43 | * @param string $store_token |
| 44 | * @return array|WP_Error |
| 45 | */ |
| 46 | public function get_subscription( $data ) { |
| 47 | $response = $this->request( |
| 48 | self::SUBSCRIPTION, |
| 49 | 'POST', |
| 50 | $data |
| 51 | ); |
| 52 | if ( is_wp_error( $response ) ) { |
| 53 | $this->write_log( $response->get_error_message(), 'commercebird-connector' ); |
| 54 | return new WP_Error( 'request_failed', $response->get_error_message() ); |
| 55 | } |
| 56 | if ( isset( $response['code'] ) && $response['code'] !== 200 ) { |
| 57 | $this->write_log( $response['message'], 'commercebird-connector' ); |
| 58 | return new WP_Error( 'request_failed', $response['message'] ); |
| 59 | } |
| 60 | return $response['data']; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Connect to exact online and get authorization URL. |
| 65 | * |
| 66 | * @param array $data array ( callback_url, topic ) |
| 67 | * @return array|WP_Error array ( webhook_id, topic ) |
| 68 | */ |
| 69 | public function create_woo_webbhook() { |
| 70 | return $this->request( self::WOO_WEBHOOKS, 'POST' ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Connect to exact online and get authorization URL. |
| 75 | * |
| 76 | * @param array $data array ( callback_url, topic ) |
| 77 | * @return array|WP_Error array ( webhook_id, topic ) |
| 78 | */ |
| 79 | public function connect_exact( $domain_suffix ) { |
| 80 | return $this->request( self::CONNECT_EXACT, 'GET', array(), array( 'domain' => $domain_suffix ) ); |
| 81 | } |
| 82 | |
| 83 | public function get_account_settings() { |
| 84 | return $this->request( self::ACCOUNT_SETTINGS ); |
| 85 | } |
| 86 | public function save_account_settings( array $data ) { |
| 87 | return $this->request( self::ACCOUNT_SETTINGS, 'POST', $data ); |
| 88 | } |
| 89 | |
| 90 | public function get_exact_divisions() { |
| 91 | return $this->request( self::EXACT_DIVISIONS ); |
| 92 | } |
| 93 | |
| 94 | public function cost_centers() { |
| 95 | return $this->request( self::COST_CENTERS ); |
| 96 | } |
| 97 | |
| 98 | public function gl_accounts() { |
| 99 | return $this->request( self::GL_ACCOUNTS ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the list of journals from Exact Online. |
| 104 | * @return array|WP_Error |
| 105 | */ |
| 106 | public function journals() { |
| 107 | return $this->request( self::JOURNAL_LIST ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the list of invoice layouts from Exact Online. |
| 112 | * @return array|WP_Error |
| 113 | */ |
| 114 | public function invoice_layouts() { |
| 115 | return $this->request( self::INVOICE_LAYOUTS ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the list warehouses from Exact Online. |
| 120 | * @return array|WP_Error |
| 121 | */ |
| 122 | public function exact_warehouses() { |
| 123 | return $this->request( self::EXACT_WAREHOUSES ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get mapped general settings for Exact Online. |
| 128 | * @return array|WP_Error |
| 129 | */ |
| 130 | public function exact_general_settings() { |
| 131 | return $this->request( self::EXACT_GENERAL_SETTINGS ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Save general settings for Exact Online. |
| 136 | * @param array $data mapped general settings data |
| 137 | * @return array|WP_Error |
| 138 | */ |
| 139 | public function save_exact_general_settings( array $data ) { |
| 140 | return $this->request( self::EXACT_GENERAL_SETTINGS, 'POST', $data ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get exact vat codes. |
| 145 | * @return array|WP_Error |
| 146 | */ |
| 147 | public function get_exact_vat_codes() { |
| 148 | return $this->request( self::EXACT_VATCODES ); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get mapped taxes for Exact Online. |
| 153 | * @return array|WP_Error |
| 154 | */ |
| 155 | public function get_exact_tax_mapping() { |
| 156 | return $this->request( self::EXACT_TAXMAPPING ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Save mapped taxes for Exact Online. |
| 161 | * @param array $data mapped taxes data |
| 162 | * @return array|WP_Error |
| 163 | */ |
| 164 | public function save_exact_tax_mapping( array $data ) { |
| 165 | return $this->request( self::EXACT_TAXMAPPING, 'POST', $data ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get mapped payment conditions for Exact Online. |
| 170 | * @return array|WP_Error |
| 171 | */ |
| 172 | public function get_exact_payment_conditions() { |
| 173 | return $this->request( self::EXACT_PAYMENT_CONDITIONS ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get mapped payment staus for Exact Online. |
| 178 | * @return array|WP_Error |
| 179 | */ |
| 180 | public function get_exact_payment_status_mapping() { |
| 181 | return $this->request( self::EXACT_PAYMENT_STATUS_MAPPING ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Save mapped payment status for Exact Online. |
| 186 | * @param array $data mapped taxes data |
| 187 | * @return array|WP_Error |
| 188 | */ |
| 189 | public function save_exact_payment_status_mapping( array $data ) { |
| 190 | return $this->request( self::EXACT_PAYMENT_STATUS_MAPPING, 'POST', $data ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Suscribe to Exact Online Webhooks. Pass the webhook URL to Exact Online and Topic |
| 195 | * |
| 196 | * @param array $data array ( callback_url, topic ) |
| 197 | * @return array|WP_Error array ( webhook_id, topic ) |
| 198 | */ |
| 199 | public function subscribe_exact_webhooks( array $data ) { |
| 200 | $response = $this->request( self::WEBHOOKS, 'POST', $data ); |
| 201 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Collect customer or account id |
| 206 | * |
| 207 | * @param array $customer array ( customer_email, company_name ) |
| 208 | * |
| 209 | * @return array|WP_Error array ( account_id, company_id ) |
| 210 | * @throws WP_Error Invalid customer if empty |
| 211 | */ |
| 212 | public function customer() { |
| 213 | $response = $this->request( self::CUSTOMER ); |
| 214 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 215 | } |
| 216 | /** |
| 217 | * Collect customer or account id |
| 218 | * |
| 219 | * @param array $customer array ( customer_email, company_name ) |
| 220 | * |
| 221 | * @return array|WP_Error array ( account_id, company_id ) |
| 222 | * @throws WP_Error Invalid customer if empty |
| 223 | */ |
| 224 | public function order( array $range ) { |
| 225 | |
| 226 | $response = $this->request( |
| 227 | self::ORDER, |
| 228 | 'GET', |
| 229 | array(), |
| 230 | $range |
| 231 | ); |
| 232 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Send Order Ids to Exact Online using the bulk endpoint |
| 237 | * |
| 238 | * @param array $data array ( order_ids ) |
| 239 | * @return array|WP_Error array ( order_ids ) |
| 240 | * @throws WP_Error Invalid order if empty |
| 241 | */ |
| 242 | public function send_orders( array $data ) { |
| 243 | $response = $this->request( self::ORDER, 'POST', $data ); |
| 244 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Get item ID by product ID |
| 249 | * |
| 250 | * @param int $id of the item |
| 251 | * |
| 252 | * @return array|WP_Error The ID of the item or an error object. |
| 253 | */ |
| 254 | public function products() { |
| 255 | $response = $this->request( self::ITEM ); |
| 256 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 257 | } |
| 258 | |
| 259 | public function cost_units() { |
| 260 | return $this->request( self::COST_UNITS ); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Get payment status |
| 265 | * |
| 266 | * @param array |
| 267 | * |
| 268 | * @return array|WP_Error array ( payment_status ) |
| 269 | */ |
| 270 | public function payment_status( array $data ) { |
| 271 | $response = $this->request( self::PAYMENT_STATUS, 'POST', $data, array() ); |
| 272 | return $response['code'] === 200 ? $response['data'] : $response['message']; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Generate request URL |
| 277 | * @param string $endpoint |
| 278 | * @param string $method |
| 279 | * @param array $data |
| 280 | * @param array $params |
| 281 | * @return array|WP_Error |
| 282 | */ |
| 283 | public function request( string $endpoint, string $method = 'GET', array $data = array(), array $params = array() ) { |
| 284 | // $token = ExactOnlineAjax::instance()->get_token(); |
| 285 | $token = SettingsAjax::instance()->get_token(); |
| 286 | |
| 287 | $token = ! empty( $token ) ? $token : ''; |
| 288 | $url = sprintf( '%s/%s?token=%s', self::API, $endpoint, $token ); |
| 289 | if ( ! empty( $params ) ) { |
| 290 | $url .= '&' . http_build_query( $params ); |
| 291 | } |
| 292 | |
| 293 | // if current site contains localhost, use https://dev.commercebird.com |
| 294 | $current_site_url = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_url( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; |
| 295 | $site_url = empty( $current_site_url ) || str_contains( $current_site_url, 'localhost' ) ? 'https://dev.commercebird.com' : site_url(); |
| 296 | if ( 'POST' === $method ) { |
| 297 | $response = wp_remote_post( |
| 298 | $url, |
| 299 | array( |
| 300 | 'headers' => array( |
| 301 | 'Accept' => 'application/json', |
| 302 | 'Content-Type' => 'application/json', |
| 303 | 'zohowooagent' => $site_url, |
| 304 | ), |
| 305 | 'timeout' => 60, |
| 306 | 'sslverify' => false, |
| 307 | 'body' => wp_json_encode( $data ), |
| 308 | ) |
| 309 | ); |
| 310 | } else { |
| 311 | $response = wp_remote_get( |
| 312 | $url, |
| 313 | array( |
| 314 | 'headers' => array( |
| 315 | 'Accept' => 'application/json', |
| 316 | 'zohowooagent' => $site_url, |
| 317 | 'x-woozo-module' => $data['module'] ?? '', |
| 318 | ), |
| 319 | 'timeout' => 60, |
| 320 | 'sslverify' => false, |
| 321 | ) |
| 322 | ); |
| 323 | } |
| 324 | if ( is_wp_error( $response ) ) { |
| 325 | $this->write_log( $response->get_error_message(), 'commercebird-connector' ); |
| 326 | return new WP_Error( 'request_failed', $response->get_error_message() ); |
| 327 | } |
| 328 | $response = wp_remote_retrieve_body( $response ); |
| 329 | return json_decode( $response, true ); |
| 330 | } |
| 331 | } |
| 332 |