Jake Vanderwerf
2026-02-04 2127b1bdd73ecd2423e443992da4b442f5a3c1a3
inc/integrations/Square.php
@@ -1,8 +1,10 @@
<?php
namespace JVBase\integrations;
use JVBase\meta\MetaManager;
use JVBase\meta\Form;
use JVBase\meta\Meta;
use Exception;
use JVBase\registry\PostTypeRegistrar;
use WP_Error;
if (!defined('ABSPATH')) {
@@ -71,6 +73,8 @@
      $this->title = 'Square';
      $this->icon = 'square-logo';
      $this->refresh_interval = 7 * DAY_IN_SECONDS;
      // Define credential fields
      $this->fields = [
         'environment'  => [
@@ -171,6 +175,8 @@
            'sync_to_square' => 'Sync Site to Square',
         ]
      );
      add_action('init', [$this, 'registerSquarePostTypes']);
   }
   /**
@@ -207,6 +213,134 @@
   }
   public function getSquarePostConfig(string $post = 'all'):array
   {
      $posts = [
         '_sq_orders' => [
            'singular'  => 'Square Order',
            'plural' => 'Square Orders',
            'public' => false,
            'fields' => [
               'post_title' => [
                  'type' => 'text',
                  'label' => 'Order Number'
               ],
               'square_order_id' => [
                  'type' => 'text',
                  'label' => 'Square Order ID',
                  'readonly' => true
               ],
               'square_payment_id' => [
                  'type' => 'text',
                  'label' => 'Square Payment ID',
                  'readonly' => true
               ],
               'square_customer_id' => [
                  'type' => 'text',
                  'label' => 'Square Customer ID',
                  'readonly' => true
               ],
               'amount' => [
                  'type' => 'number',
                  'label' => 'Total Amount (cents)',
                  'readonly' => true
               ],
               'status' => [
                  'type' => 'select',
                  'label' => 'Order Status',
                  'options' => [
                     'PROPOSED' => 'Proposed',
                     'RESERVED' => 'Reserved',
                     'PREPARED' => 'Prepared (Ready for Pickup)',
                     'COMPLETED' => 'Completed',
                     'CANCELED' => 'Canceled'
                  ],
                  'readonly' => true
               ],
               'fulfillment_status' => [
                  'type' => 'select',
                  'label' => 'Fulfillment Status',
                  'options' => [
                     'PROPOSED' => 'Proposed',
                     'RESERVED' => 'Reserved',
                     'PREPARED' => 'Prepared',
                     'COMPLETED' => 'Completed',
                     'CANCELED' => 'Canceled',
                     'FAILED' => 'Failed'
                  ],
                  'readonly' => true
               ],
               'pickup_time' => [
                  'type' => 'datetime',
                  'label' => 'Pickup Time'
               ],
               'customer_email' => [
                  'type' => 'email',
                  'label' => 'Customer Email',
                  'readonly' => true
               ],
               'customer_name' => [
                  'type' => 'text',
                  'label' => 'Customer Name',
                  'readonly' => true
               ],
               'customer_phone' => [
                  'type' => 'tel',
                  'label' => 'Customer Phone',
                  'readonly' => true
               ],
               'special_instructions' => [
                  'type' => 'textarea',
                  'label' => 'Special Instructions',
                  'readonly' => true
               ],
               'items' => [
                  'type' => 'repeater',
                  'label' => 'Order Items',
                  'readonly' => true,
                  'fields' => [
                     'name' => ['type' => 'text', 'label' => 'Item Name'],
                     'quantity' => ['type' => 'number', 'label' => 'Quantity'],
                     'price' => ['type' => 'number', 'label' => 'Price'],
                     'note' => ['type' => 'text', 'label' => 'Note']
                  ]
               ],
               'receipt_url' => [
                  'type' => 'url',
                  'label' => 'Receipt URL',
                  'readonly' => true
               ],
               'created_at' => [
                  'type' => 'datetime',
                  'label' => 'Created At',
                  'readonly' => true
               ],
               'updated_at' => [
                  'type' => 'datetime',
                  'label' => 'Last Updated',
                  'readonly' => true
               ]
            ]
         ]
      ];
      if ($post === 'all'){
         return $posts;
      }elseif(array_key_exists($post, $posts)) {
         return $posts[$post];
      }
      return [];
   }
   public function registerSquarePostTypes():void
   {
      $squarePostTypes = $this->getSquarePostConfig();
      foreach ($squarePostTypes as $slug => $config) {
         $registrar = new PostTypeRegistrar($slug, $config);
         $registrar->register();
      }
   }
   /**
    * Get request headers for API calls
    */
@@ -245,8 +379,6 @@
    */
   protected function exchangeOAuthCode(string $code): ?array
   {
      error_log('Exchanging tokens with credentials: '.print_r($this->credentials, true));
      $this->ensureInitialized();
      // Prepare the request body as an array
@@ -272,7 +404,6 @@
      }
      $data = json_decode(wp_remote_retrieve_body($response), true);
      error_log('OAuth Response: '.print_r($data, true));
      if (isset($data['access_token'])) {
         return [
            'access_token' => $data['access_token'],
@@ -334,7 +465,6 @@
      $data = json_decode(wp_remote_retrieve_body($response), true);
      error_log('RefreshAccessToken Response: '.print_r($data, true));
      if (isset($data['access_token'])) {
         $this->credentials['access_token'] = $data['access_token'];
         $this->credentials['expires_at'] = time() + ($data['expires_in'] ?? 2592000); // 30 days
@@ -357,7 +487,6 @@
   {
      // Skip if we don't have credentials yet (during OAuth flow)
      if (empty($this->credentials['access_token'])) {
         error_log('[Square] Skipping loadLocations - no access token yet');
         return;
      }
      try {
@@ -717,10 +846,10 @@
      // User login tracking for security
      add_action('wp_login', [$this, 'trackUserLogin'], 10, 2);
      add_action('jvbAdditionalActions', [$this, 'outputCheckout']);
      // Enqueue checkout scripts
      add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
      add_filter('jvbAdditionalActions', [$this, 'outputCheckout']);
   }
@@ -728,8 +857,7 @@
      if (is_singular(BASE.'dash') || is_post_type_archive(BASE.'dash')) {
         return $actions;
      }
      $form = '<aside id="cart" class="right">
      $form = '<aside id="cart" class="right main">
         <form id="checkout" data-form-id="checkout" data-save="checkout">';
            $tabs = [
@@ -745,12 +873,37 @@
         'description' => 'Securely checkout with your name, email, and payments processed by Square.',
         'content'   => '<div class="checkout-section">
                        <h3>Customer Information</h3>
                        <input type="text" name="name" placeholder="Full Name" required autocomplete="name">
                        <input type="email" name="email" placeholder="Email" required autocomplete="email">
                        <input type="tel" name="phone" placeholder="Phone" required autocomplete="tel"">
                        <h3>Pickup Details</h3>
                        <input type="time" name="pickup_time" min="11:00" max="20:00" required>
                        '.Form::render('cart_name', null, [
                           'type'      => 'text',
                           'label'     => 'Your Name',
                           'required'  => true,
                           'autocomplete' => 'name'
                        ]).
                        Form::render('cart_email', null, [
                           'type'      => 'email',
                           'label'     => 'Your Email',
                           'required'  => true,
                           'autocomplete'=> 'email',
                        ]).
                        Form::render('cart_phone', null, [
                           'type'      => 'tel',
                           'label'     => 'Your Phone',
                           'required'  => true,
                           'autocomplete'=> 'phone'
                        ]).'
                        <h3>Pickup Details</h3>'.
                        Form::render('pickup_time', null, [
                           'type'      => 'datetime',
                           'label'     => 'Pickup Type',
                           'min'    => '11:00',
                           'max'    => '20:00',
                           'required'  => true,
                        ]).
                        Form::render('special_instructions', null, [
                           'type'      => 'textarea',
                           'label'     => 'Special Instructions',
                           'quill'     => true,
                        ]).'
                        <textarea name="special_instructions" placeholder="Special instructions or dietary notes"></textarea>
                     </div>
@@ -768,7 +921,7 @@
         'content'   => $this->renderOrderStatus()
      ]
            ];
      $form .= jvbRenderTabs($tabs);
      $form .= jvbRenderTabs($tabs, true);
      $form .= '<div class="cart-total row end"><p class="tax">Tax: <span></span></p><p class="total">GRAND TOTAL: <span></span></p></div>
      </form>
@@ -790,11 +943,11 @@
               <label for="quantity"></label>
               <div class="quantity field" data-min="0" data-max="50" data-step="1" data-price="17" data-id="">
                  <button type="button" class="decrease"aria-label="Decrease Add to Order">'.jvbIcon('minus').'</button>
                  <button type="button" class="decrease"aria-label="Decrease Add to Order">'.jvbIcon('minus-square').'</button>
                  <input type="number" id="quantity" name="quantity" value="0" min="0" max="50" step="1" class="quantity-input">
                  <button type="button" class="increase" aria-label="Increase Add to Order">'.jvbIcon('add').'</button>
                  <button type="button" class="increase" aria-label="Increase Add to Order">'.jvbIcon('plus-square').'</button>
               </div>
            </td>
            <td class="price">
@@ -817,8 +970,8 @@
      $actions[] = [
         'button' =>    '<button type="button" class="toggle-cart row" title="Your Cart" data-action="toggle-cart" aria-label="Open Cart" aria-controls="checkout" aria-expanded="false" hidden>
               '.jvbIcon('cart').'<span class="abs"></span><span class="abs count"></span>
         'button' =>    '<button type="button" class="toggle-cart row" title="Your Cart" data-action="toggle-cart" aria-label="Open Cart" aria-controls="checkout" aria-expanded="false">
               '.jvbIcon('shopping-cart').'<span class="abs"></span><span class="abs count"></span>
            </button>',
         'content' =>   $form
      ];
@@ -924,7 +1077,6 @@
    */
   protected function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void
   {
      error_log('Queuing Sync to Square');
      // Queue the sync operation
      $this->queueOperation('sync_to_square', [
         'items' => [$postID],
@@ -1166,7 +1318,7 @@
         return new WP_Error('post_not_found', "Post $postID not found");
      }
      $meta = new MetaManager($postID, 'post');
      $meta = Meta::forPost($postID);
      $post_type = get_post_type($postID);
      // Get existing Square catalog ID if it exists
@@ -1191,10 +1343,10 @@
      }
      // Add variations
      $variations = $meta->getValue('product_variations');
      $variations = $meta->get('product_variations');
      if (empty($variations)) {
         // Create default variation if none exist
         $price = floatval($meta->getValue('price') ?: 0);
         $price = floatval($meta->get('price') ?: 0);
         $catalog_object['item_data']['variations'][] = [
            'type' => 'ITEM_VARIATION',
            'id' => $existing_square_id ? null : '#'.BASE.'menu_item_' . $postID . '_var_default',
@@ -1247,7 +1399,7 @@
      }
      // Add modifiers if they exist
      $modifiers = $meta->getValue('modifiers');
      $modifiers = $meta->get('modifiers');
      if (!empty($modifiers)) {
         $modifier_ids = [];
         foreach ($modifiers as $modifier) {
@@ -1263,7 +1415,7 @@
      }
      // Add tax settings
      $tax_ids = $meta->getValue('tax_ids');
      $tax_ids = $meta->get('tax_ids');
      if (!empty($tax_ids)) {
         $catalog_object['item_data']['tax_ids'] = $tax_ids;
      }
@@ -1690,20 +1842,22 @@
      $message = sprintf(
         "Welcome to %s!\n\n" .
         "Your account has been created. Please click the link below to set your password:\n\n" .
         "Your account has been created. Please click the button below to set your password:\n\n" .
         "%s\n\n" .
         "Once you've set your password, you can log in to:\n" .
         "Or, copy and paste the link below:\n\n".
         "%s\n\n" .
         "Once you've set your password, you can:\n" .
         "- View your order history\n" .
         "- Save your favorite items\n" .
         "- Speed up checkout with saved payment methods\n\n" .
         "If you didn't create this account, please ignore this email.\n\n" .
         "Thanks,\n%s",
         "Thanks,\n",
         $site_name,
         $reset_url,
         $site_name
         JVB()->email()->button('Reset Password', $reset_url),
         JVB()->email()->link($reset_url),
      );
      jvbMail(
      JVB()->email()->sendEmail(
         $user->user_email,
         sprintf('[%s] Welcome! Set Your Password', $site_name),
         $message
@@ -1747,11 +1901,10 @@
      // Send notification
      $user = get_user_by('ID', $user_id);
      if ($user) {
         wp_mail(
         JVB()->email()->sendEmail(
            $user->user_email,
            '['.get_bloginfo('name').'] Security Code',
            'For your security, enter this code to continue accessing your account and saved payment methods.',
            ['Content-Type: text/html; charset=UTF-8']
         );
      }
   }
@@ -1836,16 +1989,49 @@
   /**
    * Handle order status webhook
    */
   /**
    * Handle order status webhook - NOW UPDATES POST TYPE
    */
   private function handleOrderWebhook(array $data): bool
   {
      $order_id = $data['object']['order']['id'] ?? '';
      $state = $data['object']['order']['state'] ?? '';
      $fulfillments = $data['object']['order']['fulfillments'] ?? [];
      if (!$order_id) {
         return false;
      }
      // Update cached order status
      // Find the WP post for this order
      $wp_order_id = get_option(BASE . 'square_order_map_' . $order_id);
      if ($wp_order_id) {
         // Update the post meta
         $meta = Meta::forPost($wp_order_id);
         $updates = [
            'status' => $state,
            'updated_at' => current_time('mysql')
         ];
         // Extract fulfillment status and pickup time
         if (!empty($fulfillments[0])) {
            $fulfillment = $fulfillments[0];
            $updates['fulfillment_status'] = $fulfillment['state'] ?? $state;
            if (!empty($fulfillment['pickup_details']['pickup_at'])) {
               $updates['pickup_time'] = $fulfillment['pickup_details']['pickup_at'];
            }
         }
         $meta->setAll($updates);
         // Trigger notification to customer if order is ready
         if ($state === 'PREPARED') {
            do_action(BASE . 'square_order_ready', $wp_order_id, $order_id);
         }
      }
      // Also update transient cache for quick status checks
      set_transient(BASE . 'square_order_' . $order_id, $state, HOUR_IN_SECONDS);
      // Trigger action for other integrations
@@ -1940,7 +2126,6 @@
            'jvb-a11y',
            'jvb-cache',
            'jvb-tabs',
            'jvb-modal',
            'jvb-popup'
         ],
         '1.0.0',
@@ -1957,13 +2142,15 @@
         'jvb-square-checkout',
         'squareConfig',
         [
            'isOpen'    => jvbIsOpen(),
            'isOpen' => jvbIsOpen(),
            'application_id' => $this->credentials['client_id'] ?? '',
            'location_id' => $this->locationId,
            'environment' => $this->environment,
            'api_url' => rest_url('jvb/v1/square/'),
            'nonce' => wp_create_nonce('wp_rest'),
            'currency' => get_option(BASE . 'currency', 'CAD')
            'currency' => get_option(BASE . 'currency', 'CAD'),
            'is_logged_in' => is_user_logged_in(),
            'user_email' => is_user_logged_in() ? wp_get_current_user()->user_email : '' // NEW
         ]
      );
   }
@@ -2248,7 +2435,7 @@
    */
   private function mapSquareFieldsToWordPress(int $post_id, array $item): void
   {
      $meta = new MetaManager($post_id, 'post');
      $meta = Meta::forPost($post_id);
      $field_map = $this->getFieldMapping(get_post_type($post_id));
      $values_to_save = [];
@@ -2384,7 +2571,7 @@
         update_user_meta($user->ID, BASE . '_square_customer_updated', current_time('mysql'));
         // Clear cached customer data
         $this->cache->delete('square_customer_' . $user->ID);
         $this->cache->forget('square_customer_' . $user->ID);
      }
      return true;
@@ -2615,7 +2802,6 @@
         // Validate environment setting
         if (isset($credentials['environment'])) {
            error_log('Environment: '.print_r($credentials['environment'], true));
            $validEnvironments = ['sandbox', 'production'];
            if (!in_array($credentials['environment'], $validEnvironments)) {
               $this->logError('Invalid environment setting', [
@@ -2950,7 +3136,8 @@
               'name' => $image_title ?: 'Image',
               'caption' => $alt_text ?: ''
            ]
         ]
         ],
         'object_id' => $supported_image_id
      ];
      $body = $this->buildMultipartBody($file_path, $request_json, $boundary);
@@ -3279,4 +3466,221 @@
         $this->deleteCredentials();
      }
   }
   private function createSquareOrder(array $items, ?string $customer_id, array $data): array|WP_Error
   {
      // Build line items for Square
      $line_items = [];
      foreach ($items as $item) {
         $line_item = [
            'quantity' => (string)$item['quantity'], // MUST be string!
         ];
         // Use catalog_object_id if available (recommended)
         if (!empty($item['catalog_object_id'])) {
            $line_item['catalog_object_id'] = $item['catalog_object_id'];
            $line_item['catalog_version'] = $item['catalog_version'] ?? null;
         } else {
            // Ad-hoc line item (not recommended - no tax/inventory automation)
            $line_item['name'] = $item['name'];
            $line_item['base_price_money'] = [
               'amount' => (int)$item['price'],
               'currency' => $this->getCurrency()
            ];
         }
         if (!empty($item['note'])) {
            $line_item['note'] = $item['note'];
         }
         $line_items[] = $line_item;
      }
      $order_data = [
         'idempotency_key' => wp_generate_uuid4(), // Different from payment idempotency key
         'order' => [
            'location_id' => $this->locationId,
            'line_items' => $line_items,
            'state' => 'OPEN'
         ]
      ];
      // Add customer if available
      if ($customer_id) {
         $order_data['order']['customer_id'] = $customer_id;
      }
      // Add metadata
      if (!empty($data['note'])) {
         $order_data['order']['metadata'] = [
            'special_instructions' => $data['note']
         ];
      }
      if (!empty($data['pickup_time'])) {
         $order_data['order']['metadata']['pickup_time'] = $data['pickup_time'];
      }
      return $this->postRequest('orders', $order_data);
   }
   private function createSquarePayment(
      string $source_id,
      string $idempotency_key,
      int $amount_cents,
      string $order_id,
      ?string $customer_id
   ): array|WP_Error
   {
      $payment_data = [
         'idempotency_key' => $idempotency_key,
         'source_id' => $source_id,
         'amount_money' => [
            'amount' => $amount_cents, // Already in cents!
            'currency' => $this->getCurrency()
         ],
         'order_id' => $order_id,
         'location_id' => $this->locationId,
         'autocomplete' => true, // Capture immediately
      ];
      // Add customer if available
      if ($customer_id) {
         $payment_data['customer_id'] = $customer_id;
      }
      // Add reference ID for tracking
      $payment_data['reference_id'] = 'WP_' . time();
      return $this->postRequest('payments', $payment_data);
   }
   private function saveOrderToWordPress(array $order_data): int
   {
      // Extract customer info
      $customer_email = $order_data['customer']['email'] ?? '';
      $customer_name = $order_data['customer']['name'] ?? '';
      // Find or create WP user for logged-in association
      $user_id = 0;
      if ($customer_email) {
         $user = get_user_by('email', $customer_email);
         if ($user) {
            $user_id = $user->ID;
            // Store Square customer ID on user
            if (!empty($order_data['square_customer_id'])) {
               update_user_meta($user_id, BASE . '_square_customer_id', $order_data['square_customer_id']);
            }
         }
      }
      // Create order post
      $order_post_id = wp_insert_post([
         'post_type' => BASE . '_sq_orders',
         'post_title' => 'Order #' . $order_data['square_order_id'],
         'post_status' => 'publish',
         'post_author' => $user_id // Associate with user if logged in
      ]);
      if (!$order_post_id || is_wp_error($order_post_id)) {
         $this->logError('Failed to create order post', ['order_data' => $order_data]);
         return 0;
      }
      // Save all order meta
      $meta = Meta::forPost($order_post_id);
      $fields = $this->getSquarePostConfig('_sq_orders')['fields'];
      unset($fields['post_title']);
      $meta->setAll([
         'square_order_id' => $order_data['square_order_id'],
         'square_payment_id' => $order_data['square_payment_id'] ?? '',
         'square_customer_id' => $order_data['square_customer_id'] ?? '',
         'amount' => $order_data['amount'],
         'status' => $order_data['status'],
         'fulfillment_status' => $order_data['fulfillment_status'] ?? 'PROPOSED',
         'pickup_time' => $order_data['pickup_time'] ?? '',
         'customer_email' => $customer_email,
         'customer_name' => $customer_name,
         'customer_phone' => $order_data['customer']['phone'] ?? '',
         'special_instructions' => $order_data['note'] ?? '',
         'items' => $order_data['items'],
         'receipt_url' => $order_data['receipt_url'] ?? '',
         'created_at' => current_time('mysql'),
         'updated_at' => current_time('mysql')
      ]);
      // Index by Square order ID for quick webhook lookups
      update_option(BASE . 'square_order_map_' . $order_data['square_order_id'], $order_post_id);
      return $order_post_id;
   }
   /**
    * Get currency code
    */
   private function getCurrency(): string
   {
      return get_option(BASE . 'currency', 'CAD');
   }
   /**
    * Get customer with saved cards (2025-compliant)
    */
   public function getUserCards(string $customer_id): array
   {
      $response = $this->getRequest('cards?customer_id=' . $customer_id);
      return $response['cards'] ?? [];
   }
   public function getUserOrders(string $customer_email): array
   {
      // First get Square customer ID
      $customer_response = $this->postRequest('customers/search', [
         'filter' => [
            'email_address' => ['exact' => $customer_email]
         ]
      ]);
      $customer_id = $customer_response['customers'][0]['id'] ?? null;
      if (!$customer_id) {
         return [];
      }
      // Get their orders
      $orders_response = $this->postRequest('orders/search', [
         'filter' => [
            'customer_filter' => [
               'customer_ids' => [$customer_id]
            ]
         ],
         'sort' => [
            'sort_field' => 'CREATED_AT',
            'sort_order' => 'DESC'
         ],
         'limit' => 50
      ]);
      return $orders_response['orders'] ?? [];
   }
   public function checkOrderStatus(string $order_id): ?string
   {
      // Check transient cache first
      $cached = get_transient(BASE . 'square_order_' . $order_id);
      if ($cached) {
         return $cached;
      }
      // Fetch from Square
      $response = $this->getRequest('orders/' . $order_id);
      if (!is_wp_error($response)) {
         $state = $response['order']['state'] ?? null;
         set_transient(BASE . 'square_order_' . $order_id, $state, HOUR_IN_SECONDS);
         return $state;
      }
      return null;
   }
}