API.php
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Square |
| 4 | * |
| 5 | * This source file is subject to the GNU General Public License v3.0 |
| 6 | * that is bundled with this package in the file license.txt. |
| 7 | * It is also available through the world-wide-web at this URL: |
| 8 | * http://www.gnu.org/licenses/gpl-3.0.html |
| 9 | * If you did not receive a copy of the license and are unable to |
| 10 | * obtain it through the world-wide-web, please send an email |
| 11 | * to license@woocommerce.com so we can send you a copy immediately. |
| 12 | * |
| 13 | * DISCLAIMER |
| 14 | * |
| 15 | * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer |
| 16 | * versions in the future. If you wish to customize WooCommerce Square for your |
| 17 | * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/ |
| 18 | * |
| 19 | * @author WooCommerce |
| 20 | * @copyright Copyright: (c) 2019, Automattic, Inc. |
| 21 | * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 |
| 22 | */ |
| 23 | |
| 24 | namespace WooCommerce\Square; |
| 25 | |
| 26 | use WooCommerce\Square\Framework\Api\Base; |
| 27 | use WooCommerce\Square\API\Requests; |
| 28 | use WooCommerce\Square\API\Responses; |
| 29 | use Square\SquareClient; |
| 30 | use Square\Environment; |
| 31 | |
| 32 | defined( 'ABSPATH' ) || exit; |
| 33 | |
| 34 | /** |
| 35 | * WooCommerce Square API class |
| 36 | * |
| 37 | * @since 2.0.0 |
| 38 | */ |
| 39 | class API extends Base { |
| 40 | |
| 41 | |
| 42 | /** catalog request type */ |
| 43 | const REQUEST_TYPE_CATALOG = 'catalog'; |
| 44 | |
| 45 | /** inventory request type */ |
| 46 | const REQUEST_TYPE_INVENTORY = 'inventory'; |
| 47 | |
| 48 | |
| 49 | /** @var \Square\SquareClient Square API client instance */ |
| 50 | protected $client; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Constructs the main Square API wrapper class. |
| 55 | * |
| 56 | * @since 2.0.0 |
| 57 | * |
| 58 | * @param string $access_token Square API access token |
| 59 | * @param bool $is_sandbox If sandbox access is desired |
| 60 | */ |
| 61 | public function __construct( $access_token, $is_sandbox = null ) { |
| 62 | $this->client = new SquareClient( [ |
| 63 | 'accessToken' => $access_token, |
| 64 | 'environment' => $is_sandbox ? Environment::SANDBOX : Environment::PRODUCTION, |
| 65 | ] ); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /** Catalog API Methods *******************************************************************************************/ |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Batch-deletes an array of catalog objects. |
| 74 | * |
| 75 | * @since 2.0.0 |
| 76 | * |
| 77 | * @param string[] $object_ids array of square catalog object IDs |
| 78 | * @return Responses\Catalog |
| 79 | * @throws \Exception |
| 80 | */ |
| 81 | public function batch_delete_catalog_objects( array $object_ids ) { |
| 82 | |
| 83 | $request = $this->get_catalog_request(); |
| 84 | $request->set_batch_delete_catalog_objects_data( $object_ids ); |
| 85 | |
| 86 | return $this->perform_request( $request ); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Batch-retrieves an array of catalog objects. |
| 92 | * |
| 93 | * @since 2.0.0 |
| 94 | * |
| 95 | * @param string[] $object_ids array of square catalog object IDs |
| 96 | * @param bool $include_related_objects whether or not to include related objects in the response |
| 97 | * @return Responses\Catalog |
| 98 | * @throws \Exception |
| 99 | */ |
| 100 | public function batch_retrieve_catalog_objects( array $object_ids, $include_related_objects = false ) { |
| 101 | |
| 102 | $request = $this->get_catalog_request(); |
| 103 | $request->set_batch_retrieve_catalog_objects_data( $object_ids, (bool) $include_related_objects ); |
| 104 | |
| 105 | return $this->perform_request( $request ); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * Batch-upserts an array of catalog objects. |
| 111 | * |
| 112 | * @since 2.0.0 |
| 113 | * |
| 114 | * @param string $idempotency_key a UUID for this request |
| 115 | * @param array $batches an array of batches to upsert |
| 116 | * @return Responses\Catalog |
| 117 | * @throws \Exception |
| 118 | */ |
| 119 | public function batch_upsert_catalog_objects( $idempotency_key, array $batches ) { |
| 120 | |
| 121 | $request = $this->get_catalog_request(); |
| 122 | $request->set_batch_upsert_catalog_objects_data( $idempotency_key, $batches ); |
| 123 | |
| 124 | return $this->perform_request( $request ); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Returns info about the Catalog API, including helpful info like request size limits. |
| 130 | * |
| 131 | * @since 2.0.0 |
| 132 | * @return Responses\Catalog |
| 133 | * @throws \Exception |
| 134 | */ |
| 135 | public function catalog_info() { |
| 136 | |
| 137 | $request = $this->get_catalog_request(); |
| 138 | $request->set_catalog_info_data(); |
| 139 | |
| 140 | return $this->perform_request( $request ); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /** |
| 145 | * Deletes an object from the Square catalog. |
| 146 | * |
| 147 | * @since 2.0.0 |
| 148 | * |
| 149 | * @param string $object_id Square catalog object ID |
| 150 | * @return Responses\Catalog |
| 151 | * @throws \Exception |
| 152 | */ |
| 153 | public function delete_catalog_object( $object_id ) { |
| 154 | |
| 155 | $request = $this->get_catalog_request(); |
| 156 | $request->set_delete_catalog_object_data( $object_id ); |
| 157 | |
| 158 | return $this->perform_request( $request ); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /** |
| 163 | * Returns a list of Square catalog items. |
| 164 | * |
| 165 | * @since 2.0.0 |
| 166 | * |
| 167 | * @param string $cursor the cursor to list from |
| 168 | * @param string[] $types the item types to filter by |
| 169 | * @return Responses\Catalog |
| 170 | * @throws \Exception |
| 171 | */ |
| 172 | public function list_catalog( $cursor = '', $types = array() ) { |
| 173 | |
| 174 | $request = $this->get_catalog_request(); |
| 175 | $request->set_list_catalog_data( $cursor, $types ); |
| 176 | |
| 177 | return $this->perform_request( $request ); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Retrieves a single catalog object. |
| 183 | * |
| 184 | * @since 2.0.0 |
| 185 | * |
| 186 | * @param string $object_id the Square catalog object ID |
| 187 | * @param bool $include_related_objects whether or not to include related objects (such as categories) |
| 188 | * @return Responses\Catalog |
| 189 | * @throws \Exception |
| 190 | */ |
| 191 | public function retrieve_catalog_object( $object_id, $include_related_objects = false ) { |
| 192 | |
| 193 | $request = $this->get_catalog_request(); |
| 194 | $request->set_retrieve_catalog_object_data( $object_id, $include_related_objects ); |
| 195 | |
| 196 | return $this->perform_request( $request ); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Searches the catalog for objects. |
| 202 | * |
| 203 | * @since 2.0.0 |
| 204 | * |
| 205 | * @param array $args see Catalog::set_search_catalog_objects_data() for list of args |
| 206 | * @return Responses\Catalog |
| 207 | * @throws \Exception |
| 208 | */ |
| 209 | public function search_catalog_objects( $args = array() ) { |
| 210 | |
| 211 | $request = $this->get_catalog_request(); |
| 212 | $request->set_search_catalog_objects_data( $args ); |
| 213 | |
| 214 | return $this->perform_request( $request ); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Updates the modifier lists that apply to given items. |
| 220 | * |
| 221 | * @since 2.0.0 |
| 222 | * |
| 223 | * @param string[] $item_ids array of Square catalog item IDs |
| 224 | * @param string[] $modifier_lists_to_enable (optional) modifier list IDs to enable |
| 225 | * @param string[] $modifier_lists_to_disable (optional) modifier list IDs to disable |
| 226 | * @return Responses\Catalog |
| 227 | * @throws \Exception |
| 228 | */ |
| 229 | public function update_item_modifier_lists( array $item_ids, array $modifier_lists_to_enable = array(), array $modifier_lists_to_disable = array() ) { |
| 230 | |
| 231 | $request = $this->get_catalog_request(); |
| 232 | $request->set_update_item_modifier_lists_data( $item_ids, $modifier_lists_to_enable, $modifier_lists_to_disable ); |
| 233 | |
| 234 | return $this->perform_request( $request ); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /** |
| 239 | * Updates an item's applied taxes. |
| 240 | * |
| 241 | * @since 2.0.0 |
| 242 | * |
| 243 | * @param string[] $item_ids array of Square catalog item IDs |
| 244 | * @param string[] $taxes_to_enable (optional) tax IDs to enable |
| 245 | * @param string[] $taxes_to_disable (optional) tax IDs to disable |
| 246 | * @return Responses\Catalog |
| 247 | * @throws \Exception |
| 248 | */ |
| 249 | public function update_item_taxes( array $item_ids, array $taxes_to_enable = array(), array $taxes_to_disable = array() ) { |
| 250 | |
| 251 | $request = $this->get_catalog_request(); |
| 252 | $request->set_update_item_taxes_data( $item_ids, $taxes_to_enable, $taxes_to_disable ); |
| 253 | |
| 254 | return $this->perform_request( $request ); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * Upserts an object into the catalog. |
| 260 | * |
| 261 | * @since 2.0.0 |
| 262 | * |
| 263 | * @param string $idempotency_key UUID for this request |
| 264 | * @param \Square\Models\CatalogObject $object the object to upsert |
| 265 | * @return Responses\Catalog |
| 266 | * @throws \Exception |
| 267 | */ |
| 268 | public function upsert_catalog_object( $idempotency_key, $object ) { |
| 269 | |
| 270 | $request = $this->get_catalog_request(); |
| 271 | $request->set_upsert_catalog_object_data( $idempotency_key, $object ); |
| 272 | |
| 273 | return $this->perform_request( $request ); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * Creates an image in Square. |
| 279 | * |
| 280 | * Note that this method uses a custom request, since the Square SDK does not yet provide a method for image creation. |
| 281 | * |
| 282 | * @since 2.0.0 |
| 283 | * |
| 284 | * @param $image_path |
| 285 | * @param string $square_item_id |
| 286 | * @param string $caption optional image caption |
| 287 | * @return string |
| 288 | * @throws \Exception |
| 289 | */ |
| 290 | public function create_image( $image_path, $square_item_id = '', $caption = '' ) { |
| 291 | |
| 292 | if ( ! is_readable( $image_path ) ) { |
| 293 | throw new \Exception( 'Image file is not readable' ); |
| 294 | } |
| 295 | |
| 296 | $image = file_get_contents( $image_path ); |
| 297 | |
| 298 | $headers = array( |
| 299 | 'accept' => 'application/json', |
| 300 | 'content-type' => 'multipart/form-data; boundary="boundary"', |
| 301 | 'Square-Version' => '2019-05-08', |
| 302 | 'Authorization' => 'Bearer ' . wc_square()->get_settings_handler()->get_access_token(), |
| 303 | ); |
| 304 | |
| 305 | $body = '--boundary' . "\r\n"; |
| 306 | $body .= 'Content-Disposition: form-data; name="request"' . "\r\n"; |
| 307 | $body .= 'Content-Type: application/json' . "\r\n\r\n"; |
| 308 | |
| 309 | $request = array( |
| 310 | 'idempotency_key' => wc_square()->get_idempotency_key(), |
| 311 | 'image' => array( |
| 312 | 'type' => 'IMAGE', |
| 313 | 'id' => '#TEMP_ID', |
| 314 | 'image_data' => array( |
| 315 | 'caption' => esc_attr( $caption ), |
| 316 | ), |
| 317 | ), |
| 318 | ); |
| 319 | |
| 320 | if ( $square_item_id ) { |
| 321 | $request['object_id'] = $square_item_id; |
| 322 | } |
| 323 | |
| 324 | $body .= json_encode( $request ); |
| 325 | |
| 326 | $body .= "\r\n"; |
| 327 | |
| 328 | $body .= '--boundary' . "\r\n"; |
| 329 | $body .= 'Content-Disposition: form-data; name="file"; filename="' . esc_attr( basename( $image_path ) ) . '"' . "\r\n"; |
| 330 | $body .= 'Content-Type: image/jpeg' . "\r\n\r\n"; |
| 331 | $body .= $image . "\r\n"; |
| 332 | $body .= '--boundary--'; |
| 333 | |
| 334 | $url = $this->client->getBaseUri() . '/v2/catalog/images'; |
| 335 | |
| 336 | $response = wp_remote_post( |
| 337 | $url, |
| 338 | array( |
| 339 | 'headers' => $headers, |
| 340 | 'body' => $body, |
| 341 | ) |
| 342 | ); |
| 343 | |
| 344 | if ( is_wp_error( $response ) ) { |
| 345 | throw new \Exception( $response->get_error_message() ); |
| 346 | } |
| 347 | |
| 348 | $body = wp_remote_retrieve_body( $response ); |
| 349 | $body = json_decode( $body, true ); |
| 350 | |
| 351 | if ( ! is_array( $body ) ) { |
| 352 | throw new \Exception( 'Response was malformed' ); |
| 353 | } |
| 354 | |
| 355 | if ( ! empty( $body['errors'] ) || empty( $body['image']['id'] ) ) { |
| 356 | |
| 357 | if ( ! empty( $body['errors'][0]['detail'] ) ) { |
| 358 | $message = $body['errors'][0]['detail']; |
| 359 | } else { |
| 360 | $message = 'Unknown error'; |
| 361 | } |
| 362 | |
| 363 | throw new \Exception( $message ); |
| 364 | } |
| 365 | |
| 366 | return $body['image']['id']; |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /** Inventory API Methods *****************************************************************************************/ |
| 371 | |
| 372 | |
| 373 | /** |
| 374 | * Adds a count of inventory as "in-stock" to the given Square item variation ID as a result of a refund. |
| 375 | * |
| 376 | * @since 2.0.0 |
| 377 | * |
| 378 | * @param string $square_id Square object ID |
| 379 | * @param int $amount amount of inventory to add |
| 380 | * @return Responses\Inventory |
| 381 | * @throws \Exception |
| 382 | */ |
| 383 | public function add_inventory_from_refund( $square_id, $amount ) { |
| 384 | |
| 385 | return $this->add_inventory( $square_id, $amount, 'NONE' ); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /** |
| 390 | * Adds a count of inventory as "in-stock" to the given Square item variation ID. |
| 391 | * |
| 392 | * @since 2.0.0 |
| 393 | * |
| 394 | * @param string $square_id Square object ID |
| 395 | * @param int $amount amount of inventory to add |
| 396 | * @param string $from_state the API state the inventory is coming from |
| 397 | * @return Responses\Inventory |
| 398 | * @throws \Exception |
| 399 | */ |
| 400 | public function add_inventory( $square_id, $amount, $from_state = 'NONE' ) { |
| 401 | |
| 402 | return $this->adjust_inventory( $square_id, $amount, $from_state, 'IN_STOCK' ); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /** |
| 407 | * Removes a count of inventory as "in-stock" to the given Square item variation ID. |
| 408 | * |
| 409 | * @since 2.0.0 |
| 410 | * |
| 411 | * @param string $square_id Square object ID |
| 412 | * @param int $amount amount of inventory to remove |
| 413 | * @return Responses\Inventory |
| 414 | * @throws \Exception |
| 415 | */ |
| 416 | public function remove_inventory( $square_id, $amount ) { |
| 417 | |
| 418 | return $this->adjust_inventory( $square_id, $amount, 'IN_STOCK', 'SOLD' ); |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /** |
| 423 | * Performs an inventory adjustment. |
| 424 | * |
| 425 | * @since 2.0.0 |
| 426 | * |
| 427 | * @param string $square_id Square object ID |
| 428 | * @param int $amount amount of inventory to add |
| 429 | * @param string $from_state the API state the inventory is coming from |
| 430 | * @param string $to_state the API state the inventory is changing to |
| 431 | * @return Responses\Inventory |
| 432 | * @throws \Exception |
| 433 | */ |
| 434 | protected function adjust_inventory( $square_id, $amount, $from_state, $to_state ) { |
| 435 | |
| 436 | $date = new \DateTime(); |
| 437 | |
| 438 | $change = new \Square\Models\InventoryChange(); |
| 439 | $change->setType( 'ADJUSTMENT' ); |
| 440 | |
| 441 | $inventory_adjustment = new \Square\Models\InventoryAdjustment(); |
| 442 | $inventory_adjustment->setCatalogObjectId( $square_id ); |
| 443 | $inventory_adjustment->setLocationId( $this->get_plugin()->get_settings_handler()->get_location_id() ); |
| 444 | $inventory_adjustment->setQuantity( (string) absint( $amount ) ); |
| 445 | $inventory_adjustment->setFromState( $from_state ); |
| 446 | $inventory_adjustment->setToState( $to_state ); |
| 447 | $inventory_adjustment->setOccurredAt( $date->format( DATE_ATOM ) ); |
| 448 | |
| 449 | $change->setAdjustment( $inventory_adjustment ); |
| 450 | |
| 451 | return $this->batch_change_inventory( |
| 452 | uniqid( '', false ), |
| 453 | array( |
| 454 | $change, |
| 455 | ) |
| 456 | ); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /** |
| 461 | * Performs a Batch Change Inventory request. |
| 462 | * |
| 463 | * @since 2.0.0 |
| 464 | * |
| 465 | * @param string $idempotency_key UUID for this request |
| 466 | * @param \Square\Models\InventoryChange[] $changes array of Inventory Changes |
| 467 | * @param bool $ignore_unchanged_counts whether the current physical count should be ignored if the quantity is unchanged since the last physical count |
| 468 | * @return Responses\Inventory |
| 469 | * @throws \Exception |
| 470 | */ |
| 471 | public function batch_change_inventory( $idempotency_key, $changes, $ignore_unchanged_counts = true ) { |
| 472 | |
| 473 | $request = $this->get_inventory_request(); |
| 474 | $request->set_batch_change_inventory_data( $idempotency_key, $changes, $ignore_unchanged_counts ); |
| 475 | |
| 476 | return $this->perform_request( $request ); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /** |
| 481 | * Performs a Batch Retrieve Inventory Changes request. |
| 482 | * |
| 483 | * @since 2.0.0 |
| 484 | * |
| 485 | * @param array $args see Requests\Inventory::set_batch_retrieve_inventory_changes_data() for accepted arguments |
| 486 | * |
| 487 | * @return Responses\Inventory |
| 488 | * @throws \Exception |
| 489 | */ |
| 490 | public function batch_retrieve_inventory_changes( array $args = array() ) { |
| 491 | |
| 492 | $request = $this->get_inventory_request(); |
| 493 | $request->set_batch_retrieve_inventory_changes_data( $args ); |
| 494 | |
| 495 | return $this->perform_request( $request ); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * Performs a Batch Retrieve Inventory Counts request. |
| 501 | * |
| 502 | * @since 2.0.0 |
| 503 | * |
| 504 | * @param array $args see Requests\Inventory::set_batch_retrieve_inventory_counts_data() for accepted arguments |
| 505 | * |
| 506 | * @return Responses\Inventory |
| 507 | * @throws \Exception |
| 508 | */ |
| 509 | public function batch_retrieve_inventory_counts( array $args = array() ) { |
| 510 | |
| 511 | $request = $this->get_inventory_request(); |
| 512 | $request->set_batch_retrieve_inventory_counts_data( $args ); |
| 513 | |
| 514 | return $this->perform_request( $request ); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | /** |
| 519 | * Performs a Retrieve Inventory Adjustment request. |
| 520 | * |
| 521 | * @since 2.0.0 |
| 522 | * |
| 523 | * @param string $adjustment_id the InventoryAdjustment ID to retrieve |
| 524 | * |
| 525 | * @return Responses\Inventory |
| 526 | * @throws \Exception |
| 527 | */ |
| 528 | public function retrieve_inventory_adjustment( $adjustment_id ) { |
| 529 | |
| 530 | $request = $this->get_inventory_request(); |
| 531 | $request->set_retrieve_inventory_adjustment_data( $adjustment_id ); |
| 532 | |
| 533 | return $this->perform_request( $request ); |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /** |
| 538 | * Performs a Retrieve Inventory Changes request. |
| 539 | * |
| 540 | * @since 2.0.0 |
| 541 | * |
| 542 | * @param string $catalog_object_id the CatalogObject ID to retrieve |
| 543 | * |
| 544 | * @return Responses\Inventory |
| 545 | * @throws \Exception |
| 546 | */ |
| 547 | public function retrieve_inventory_changes( $catalog_object_id ) { |
| 548 | |
| 549 | $request = $this->get_inventory_request(); |
| 550 | $request->set_retrieve_inventory_changes_data( $catalog_object_id ); |
| 551 | |
| 552 | return $this->perform_request( $request ); |
| 553 | } |
| 554 | |
| 555 | |
| 556 | /** |
| 557 | * Performs a Retrieve Inventory Count request. |
| 558 | * |
| 559 | * @since 2.0.0 |
| 560 | * |
| 561 | * @param string $catalog_object_id the CatalogObject ID to retrieve |
| 562 | * @return Responses\Inventory |
| 563 | * @throws \Exception |
| 564 | */ |
| 565 | public function retrieve_inventory_count( $catalog_object_id ) { |
| 566 | |
| 567 | $request = $this->get_inventory_request(); |
| 568 | $request->set_retrieve_inventory_count_data( $catalog_object_id, $this->get_plugin()->get_settings_handler()->get_location_id() ); |
| 569 | |
| 570 | return $this->perform_request( $request ); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /** |
| 575 | * Performs a Retrieve Inventory Physical Count request. |
| 576 | * |
| 577 | * @since 2.0.0 |
| 578 | * |
| 579 | * @param string $physical_count_id the InventoryPhysicalCount ID to retrieve |
| 580 | * |
| 581 | * @return Responses\Inventory |
| 582 | * @throws \Exception |
| 583 | */ |
| 584 | public function retrieve_inventory_physical_count( $physical_count_id ) { |
| 585 | |
| 586 | $request = $this->get_inventory_request(); |
| 587 | $request->set_retrieve_inventory_physical_count_data( $physical_count_id ); |
| 588 | |
| 589 | return $this->perform_request( $request ); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /** Locations methods *********************************************************************************************/ |
| 594 | |
| 595 | |
| 596 | /** |
| 597 | * Gets the available locations. |
| 598 | * |
| 599 | * @since 2.0.0 |
| 600 | * |
| 601 | * @return \Square\Models\Location[] |
| 602 | * @throws \Exception |
| 603 | */ |
| 604 | public function get_locations() { |
| 605 | |
| 606 | $request = new API\Requests\Locations( $this->client ); |
| 607 | |
| 608 | $request->set_list_locations_data(); |
| 609 | |
| 610 | $this->set_response_handler( API\Responses\Locations::class ); |
| 611 | |
| 612 | /* @type API\Responses\Locations $response */ |
| 613 | $response = $this->perform_request( $request ); |
| 614 | |
| 615 | return $response->get_locations(); |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /** Customer methods **********************************************************************************************/ |
| 620 | |
| 621 | |
| 622 | /** |
| 623 | * Gets all customers. |
| 624 | * |
| 625 | * @since 2.0.0 |
| 626 | * |
| 627 | * @param string $cursor pagination cursor |
| 628 | * @return API\Response |
| 629 | * @throws \Exception |
| 630 | */ |
| 631 | public function get_customers( $cursor = '' ) { |
| 632 | |
| 633 | $request = new API\Requests\Customers( $this->client ); |
| 634 | |
| 635 | $request->set_get_customers_data( $cursor ); |
| 636 | |
| 637 | $this->set_response_handler( API\Response::class ); |
| 638 | |
| 639 | return $this->perform_request( $request ); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | /** Request Helper Methods ****************************************************************************************/ |
| 644 | |
| 645 | |
| 646 | /** |
| 647 | * Gets a new Catalog API request. |
| 648 | * |
| 649 | * @since 2.0.0 |
| 650 | * |
| 651 | * @return Requests\Catalog |
| 652 | * @throws \Exception |
| 653 | */ |
| 654 | protected function get_catalog_request() { |
| 655 | |
| 656 | return $this->get_new_request( self::REQUEST_TYPE_CATALOG ); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | /** |
| 661 | * Gets a new Inventory API request. |
| 662 | * |
| 663 | * @since 2.0.0 |
| 664 | * |
| 665 | * @return Requests\Inventory |
| 666 | * @throws \Exception |
| 667 | */ |
| 668 | protected function get_inventory_request() { |
| 669 | |
| 670 | return $this->get_new_request( self::REQUEST_TYPE_INVENTORY ); |
| 671 | } |
| 672 | |
| 673 | |
| 674 | /** |
| 675 | * Gets a new request object. |
| 676 | * |
| 677 | * @since 2.0.0 |
| 678 | * |
| 679 | * @param string $type desired request type |
| 680 | * @return Requests\Catalog|Requests\Inventory |
| 681 | * @throws \Exception |
| 682 | */ |
| 683 | protected function get_new_request( $type = '' ) { |
| 684 | |
| 685 | switch ( $type ) { |
| 686 | |
| 687 | case self::REQUEST_TYPE_CATALOG: |
| 688 | $request = new Requests\Catalog( $this->client ); |
| 689 | $response_handler = Responses\Catalog::class; |
| 690 | break; |
| 691 | |
| 692 | case self::REQUEST_TYPE_INVENTORY: |
| 693 | $request = new Requests\Inventory( $this->client ); |
| 694 | $response_handler = Responses\Inventory::class; |
| 695 | break; |
| 696 | |
| 697 | default: |
| 698 | throw new \Exception( 'Invalid request type.' ); |
| 699 | } |
| 700 | |
| 701 | $this->set_response_handler( $response_handler ); |
| 702 | |
| 703 | return $request; |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /** |
| 708 | * Performs an API request. |
| 709 | * |
| 710 | * @see Base::perform_request() |
| 711 | * |
| 712 | * @since 2.0.0 |
| 713 | * |
| 714 | * @param API\Request $request request object |
| 715 | * @return API_Response |
| 716 | * @throws \Exception |
| 717 | */ |
| 718 | protected function perform_request( $request ) { |
| 719 | |
| 720 | // ensure API is in its default state |
| 721 | $this->reset_response(); |
| 722 | |
| 723 | // save the request object |
| 724 | $this->request = $request; |
| 725 | |
| 726 | $start_time = microtime( true ); |
| 727 | |
| 728 | try { |
| 729 | |
| 730 | // set the request URI to the Square SDK method for better logging |
| 731 | $this->request_uri = $this->get_request()->get_square_api_method(); |
| 732 | $this->request_method = ''; |
| 733 | |
| 734 | // add any query args to the logged request URI for easier debugging |
| 735 | foreach ( $this->get_request()->get_square_api_args() as $arg ) { |
| 736 | |
| 737 | if ( is_string( $arg ) ) { |
| 738 | $this->request_uri .= "/{$arg}"; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | // perform the request |
| 743 | $response = $this->do_square_request( $this->get_request()->get_square_api(), $this->get_request()->get_square_api_method(), $this->get_request()->get_square_api_args() ); |
| 744 | |
| 745 | // calculate request duration |
| 746 | $this->request_duration = round( microtime( true ) - $start_time, 5 ); |
| 747 | |
| 748 | // parse & validate response |
| 749 | $response = $this->handle_response( $response ); |
| 750 | |
| 751 | } catch ( \Exception $e ) { |
| 752 | |
| 753 | // alert other actors that a request has been made |
| 754 | $this->broadcast_request(); |
| 755 | |
| 756 | throw $e; |
| 757 | } |
| 758 | |
| 759 | return $response; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | /** |
| 764 | * Handles and parses the response. |
| 765 | * |
| 766 | * @since 2.0.0 |
| 767 | * |
| 768 | * @param array|\WP_Error $response response data |
| 769 | * @throws \Exception |
| 770 | * @return API_Response|object request class instance that implements API_Request |
| 771 | */ |
| 772 | protected function handle_response( $response ) { |
| 773 | // parse the response body and tie it to the request |
| 774 | $this->response = $this->get_parsed_response( $this->raw_response_body ); |
| 775 | |
| 776 | // allow child classes to validate response after parsing -- this is useful |
| 777 | // for checking error codes/messages included in a parsed response |
| 778 | $this->do_post_parse_response_validation(); |
| 779 | |
| 780 | // fire do_action() so other actors can act on request/response data, |
| 781 | // primarily used for logging |
| 782 | $this->broadcast_request(); |
| 783 | |
| 784 | return $this->response; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /** |
| 789 | * Validates the response data after it's been parsed. |
| 790 | * |
| 791 | * @since 2.0.0 |
| 792 | * |
| 793 | * @return bool |
| 794 | * @throws \Exception |
| 795 | */ |
| 796 | protected function do_post_parse_response_validation() { |
| 797 | |
| 798 | if ( ! $this->get_response()->has_errors() ) { |
| 799 | return true; |
| 800 | } |
| 801 | |
| 802 | $errors = array(); |
| 803 | |
| 804 | foreach ( $this->get_response()->get_errors() as $error ) { |
| 805 | if ( empty( $error->code ) ) { |
| 806 | continue; |
| 807 | } |
| 808 | |
| 809 | $errors[] = trim( "[{$error->code}] {$error->detail}" ); |
| 810 | |
| 811 | // Last attempt to refresh access token. |
| 812 | if ( 'ACCESS_TOKEN_EXPIRED' == $error->code ) { |
| 813 | $this->get_plugin()->log( 'Access Token Expired, attempting a refresh.' ); |
| 814 | $this->get_plugin()->get_connection_handler()->refresh_connection(); |
| 815 | |
| 816 | $failure_value = get_option( 'wc_square_refresh_failed', 'yes' ); |
| 817 | |
| 818 | if ( empty( $failure_value ) ) { |
| 819 | // Successfully refreshed on the last attempt |
| 820 | $this->get_plugin()->log( 'Connection successfully refreshed.' ); |
| 821 | return true; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // if the error indicates that access token is bad, disconnect the plugin to prevent further attempts |
| 826 | if ( in_array( $error->code, array( 'ACCESS_TOKEN_EXPIRED', 'ACCESS_TOKEN_REVOKED' ), true ) ) { |
| 827 | $this->get_plugin()->get_connection_handler()->disconnect(); |
| 828 | $this->get_plugin()->log( 'Disconnected due to invalid authorization. Please try connecting again.' ); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // At this point we could not validate the response and assume a failed attempt. |
| 833 | throw new \Exception( implode( ' | ', $errors ) ); |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * Performs a remote request with the Square API class. |
| 838 | * |
| 839 | * @since 2.0.0 |
| 840 | * |
| 841 | * @param Object $square_api the square API class instance |
| 842 | * @param string $method the class method to call |
| 843 | * @param array $args the args to send with the method call |
| 844 | * @throws \Exception |
| 845 | */ |
| 846 | protected function do_square_request( $square_api, $method, $args ) { |
| 847 | |
| 848 | if ( ! is_callable( array( $square_api, $method ) ) ) { |
| 849 | throw new \Exception( 'Invalid API method' ); |
| 850 | } |
| 851 | |
| 852 | // perform the request |
| 853 | $response = call_user_func_array( array( $square_api, $method ), $args ); |
| 854 | |
| 855 | if ( $response instanceof \Square\Http\ApiResponse ) { |
| 856 | $this->response_code = $response->getStatusCode(); |
| 857 | $this->response_headers = $response->getHeaders(); |
| 858 | |
| 859 | if ( $response->isSuccess() ) { |
| 860 | $this->raw_response_body = $response->getResult(); |
| 861 | } else { |
| 862 | $this->raw_response_body = $response->getErrors(); |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | |
| 868 | /** |
| 869 | * Gets the main plugin instance. |
| 870 | * |
| 871 | * @since 2.0.0 |
| 872 | * |
| 873 | * @return \WooCommerce\Square\Plugin |
| 874 | */ |
| 875 | public function get_plugin() { |
| 876 | |
| 877 | return wc_square(); |
| 878 | } |
| 879 | |
| 880 | |
| 881 | } |
| 882 |