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