Jake Vanderwerf
2026-08-28 5b50afdcafd150186ea5c7b0e1cad98065590b35
inc/integrations/services/Square.php
@@ -4,7 +4,7 @@
use Exception;
use JVBase\integrations\Admin;
use JVBase\integrations\APIEndpoint;
use JVBase\integrations\Instance;
use JVBase\Instance;
use JVBase\integrations\Integrations;
use JVBase\integrations\OAuth;
use JVBase\integrations\OAuthCredentials;
@@ -14,10 +14,13 @@
use JVBase\integrations\SyncFrom;
use JVBase\integrations\SyncTo;
use JVBase\integrations\Webhooks;
use JVBase\managers\Dashboard\Dashboard;
use JVBase\managers\queue\executors\IntegrationExecutor;
use JVBase\managers\queue\Queue;
use JVBase\managers\queue\TypeConfig;
use JVBase\meta\Sanitizer;
use JVBase\registrar\Registrar;
use JVBase\ui\CRUDSkeleton;
use WP_Error;
if (!defined('ABSPATH')) {
@@ -36,18 +39,16 @@
      Webhooks;
   protected string $orderPostType = '_square_order';
   protected string $apiVersion = '2026-07-15';
   public bool $hasBatchCreate = true;
   public bool $hasBatchUpdate = true;
   public bool $hasBatchDelete = true;
   public array $apiEndpoints = [
   ];
   protected function __construct()
   {
      $this->service_name = 'square';
      $this->title = 'Square';
      $this->icon = 'square-logo';
      $this->apiVersion = '2026-07-15';
      $this->supportsWebp = false;
      $this->canSync = [
@@ -55,6 +56,9 @@
         'update' => true,
         'delete' => true,
      ];
      $this->hasBatchCreate = true;
      $this->hasBatchUpdate = true;
      $this->hasBatchDelete = true;
      $this->defineFields();
      $this->defineInstructions();
@@ -89,8 +93,8 @@
         $locs->setHidden();
         $locs->setOptions($this->getLocationOptions());
      $loc = $this->addField('location_id', 'Default Location', 'select', 'manage_options');
         $loc->setOptions($this->loadCredentials()['locations']);
      $loc = $this->addField('location_id', 'Set Location', 'select', 'manage_options');
         $loc->setOptions($this->loadCredentials()['locations']??[]);
         $loc->setHint('Select default Square location for transactions');
   }
@@ -162,6 +166,37 @@
      add_action(BASE.'dashboard_page_orders', [$this, 'renderDashPage']);
   }
      public function addDashboardPages():void
      {
         $page = Dashboard::getInstance()->addPage('Your Orders', 'orders', 'receipt');
         $page->setScripts(['jvb-crud']);
      }
   public function renderDashPage():void
   {
      $this->determineUser();
      $crud = new CRUDSkeleton();
      $crud->icon('receipt');
      $crud->title('Your Orders','Here you can see your past orders, and reorder from there if you\'d like');
      $crud->content($this->orderPostType, 'Order', 'Orders');
      $crud->addSearch();
      $crud->addCapabilities(['view']);
      $crud->setEmptyState(sprintf(
         '<div class="empty-state">
            <h3>%sNothing here%s</h3>
            <p>It doesn\'t look like you have any orders yet.</p>
            <p>Head on over to <a href="%s">our menu</a> and make your first order!</p>
         </div>',
         jvbDashIcon($crud->getIcon()),
         jvbDashIcon($crud->getIcon()),
         get_post_type_archive_link(BASE.'menu_item')
      ));
      $crud->render();
   }
   protected function initialize(): void
   {
      $this->defineEndpoints();
@@ -265,21 +300,18 @@
      ];
   }
   protected function handleResponse(\WP_Error|array $response, string $method, string $endpoint): array
   protected function handleResponse(WP_Error|array $response, string $method, string $endpoint): array
   {
      if (is_wp_error($response) && $response->get_error_message() === 'rate_limit_exceeded') {
         return $this->response(false, 'Rate limit exceeded');
      }
      $endpoint = str_replace($this->getBaseURL(), '', $endpoint);
      switch ($endpoint) {
         case '/v2/customers/search':
            return $this->handleEmailSearchResponse($response, $method);
         case '/v2/customers':
            return $this->handleCreateUserResponse($response, $method);
         case '/v2/locations':
            return $this->handleGetLocationResponse($response, $method);
      }
      return $this->response(false, 'Unhandled Response for '.$endpoint.' endpoint, with method: '.$method);
      return match ($endpoint) {
         '/v2/customers/search' => $this->handleEmailSearchResponse($response, $method),
         '/v2/customers' => $this->handleCreateUserResponse($response, $method),
         '/v2/locations' => $this->handleGetLocationResponse($response, $method),
         default => $this->response(false, 'Unhandled Response for ' . $endpoint . ' endpoint, with method: ' . $method),
      };
   }
   protected function setContentTypes(): void
@@ -720,7 +752,7 @@
   ********************************************************************/
   protected function registerAdditionalQueueTypes(IntegrationExecutor $executor): void
   {
      $queue = JVB()->queue();
      $queue = Queue::getInstance();
      $queue->registry()->register(
         self::$syncCustomer,
         new TypeConfig(
@@ -865,7 +897,7 @@
   {
      return $this->loadCredentials()['location_id']??false;
   }
   protected function getLocations():array|false
   public function getLocations():array|false
   {
      if (!$this->getAccessToken()) {
         return false;
@@ -914,10 +946,7 @@
         ? 'https://connect.squareup.com'
         : 'https://connect.squareupsandbox.com';
   }
   protected function getAccessToken():string
   {
      return $this->loadCredentials()['access_token']??'';
   }
   /***************************************************************
    * ACTIONS
   ***************************************************************/
@@ -959,7 +988,17 @@
      return $this->contentTypes[$content_type];
   }
   public function handleImportFromSquare():array
   {
      return $this->response(true, 'Did a thing');
   }
   public function handleSyncToSquare():array
   {
      return $this->response(true, 'Did a thing');
   }
}