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 |