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 |