Jake Vanderwerf
2025-09-30 6a627f09d33967ca7fa5b6d939a9347d8223a1e6
inc/integrations/Square.php
@@ -1195,7 +1195,7 @@
         $price = floatval($meta->getValue('price') ?: 0);
         $catalog_object['item_data']['variations'][] = [
            'type' => 'ITEM_VARIATION',
            'id' => $existing_square_id ? null : '#neb_menu_item_' . $postID . '_var_default',
            'id' => $existing_square_id ? null : '#'.BASE.'menu_item_' . $postID . '_var_default',
            'item_variation_data' => [
               'name' => 'Regular',
               'ordinal' => 0,
@@ -1213,7 +1213,7 @@
            $existing_var_id = get_post_meta($postID, BASE . '_square_variation_' . $index . '_id', true);
            $catalog_object['item_data']['variations'][] = [
               'type' => 'ITEM_VARIATION',
               'id' => $existing_var_id ?: '#neb_menu_item_' . $postID . '_var_' . $index,
               'id' => $existing_var_id ?: '#'.BASE.'menu_item_' . $postID . '_var_' . $index,
               'item_variation_data' => [
                  'name' => $variation['name'] ?? 'Variation ' . ($index + 1),
                  'ordinal' => $index,
@@ -1755,172 +1755,6 @@
   }
   /******************************************************************
    * ORDER PROCESSING
    ******************************************************************/
   /**
    * Process checkout order
    */
   public function processOrder($data):WP_Error|array
   {
      check_ajax_referer('square_checkout_nonce', 'nonce');
      $cart_items = json_decode(stripslashes($data['cart'] ?? '[]'), true);
      $customer_info = [
         'name' => sanitize_text_field($data['name'] ?? ''),
         'email' => sanitize_email($data['email'] ?? ''),
         'phone' => sanitize_text_field($data['phone'] ?? ''),
      ];
      $payment_token = sanitize_text_field($data['payment_token'] ?? '');
      if (empty($cart_items) || empty($payment_token)) {
         return new WP_Error('error', 'Invalid order data');
      }
      // Calculate order total
      $order_total = $this->calculateOrderTotal($cart_items);
      // Create Square order
      $order_response = $this->createSquareOrder($cart_items, $customer_info, $order_total);
      if (is_wp_error($order_response)) {
         return new WP_Error('error', $order_response->get_error_message());
      }
      // Process payment
      $payment_response = $this->processSquarePayment($payment_token, $order_response['order']['id'], $order_total);
      if (is_wp_error($payment_response)) {
         return new WP_Error('error', $order_response->get_error_message());
      }
      // Save order to user if logged in
      if (is_user_logged_in()) {
         $this->saveOrderToUser(get_current_user_id(), $order_response['order']['id']);
      }
      return [
         'success'   => true,
         'order_id' => $order_response['order']['id'],
         'receipt_url' => $payment_response['payment']['receipt_url'] ?? '',
         'message' => 'Order placed successfully!'
      ];
   }
   /**
    * Calculate order total from cart items
    */
   private function calculateOrderTotal(array $cart_items): int
   {
      $total = 0;
      foreach ($cart_items as $item) {
         $post_id = intval($item['id'] ?? 0);
         if (!$post_id) continue;
         $meta = new MetaManager($post_id, 'post');
         $price = floatval($meta->getValue('price'));
         $quantity = intval($item['quantity'] ?? 1);
         $total += ($price * $quantity * 100); // Convert to cents
      }
      // Add tax (simplified - you'd want more complex tax calculation)
      $tax_rate = floatval(get_option(BASE . 'square_tax_rate', 0.05));
      $tax = intval($total * $tax_rate);
      return $total + $tax;
   }
   /**
    * Create Square order
    */
   private function createSquareOrder(array $cart_items, array $customer_info, int $total): array|WP_Error
   {
      $line_items = [];
      foreach ($cart_items as $item) {
         $post_id = intval($item['id'] ?? 0);
         $variation_index = $item['variation'] ?? null;
         if (!$post_id) continue;
         $post = get_post($post_id);
         $square_catalog_id = get_post_meta($post_id, BASE . '_square_catalog_id', true);
         $line_item = [
            'quantity' => strval($item['quantity'] ?? 1),
            'name' => $post->post_title
         ];
         // If variation specified, get variation ID
         if ($variation_index !== null && $square_catalog_id) {
            $variation_id = get_post_meta($post_id, BASE . '_square_variation_' . $variation_index . '_id', true);
            if ($variation_id) {
               $line_item['catalog_object_id'] = $variation_id;
               // Add variation name to line item
               $meta = new MetaManager($post_id, 'post');
               $variations = $meta->getValue('product_variations');
               if (!empty($variations[$variation_index]['name'])) {
                  $line_item['name'] .= ' - ' . $variations[$variation_index]['name'];
               }
            }
         } elseif ($square_catalog_id) {
            // Use default variation if no specific variation
            $default_variation_id = get_post_meta($post_id, BASE . '_square_variation_0_id', true);
            if ($default_variation_id) {
               $line_item['catalog_object_id'] = $default_variation_id;
            }
         }
         // If no catalog ID, create ad-hoc line item
         if (empty($line_item['catalog_object_id'])) {
            $meta = new MetaManager($post_id, 'post');
            $variations = $meta->getValue('product_variations');
            if ($variation_index !== null && !empty($variations[$variation_index]['price'])) {
               $price = floatval($variations[$variation_index]['price']);
            } else {
               $price = floatval($meta->getValue('price'));
            }
            $line_item['base_price_money'] = [
               'amount' => intval($price * 100),
               'currency' => 'CAD'
            ];
         }
         $line_items[] = $line_item;
      }
      return $this->postRequest('orders', [
         'order' => [
            'location_id' => $this->locationId,
            'line_items' => $line_items,
            'customer_id' => $this->getOrCreateSquareCustomer($customer_info)
         ]
      ]);
   }
   /**
    * Process Square payment
    */
   private function processSquarePayment(string $payment_token, string $order_id, int $amount): array|WP_Error
   {
      return $this->postRequest('payments', [
         'source_id' => $payment_token,
         'idempotency_key' => wp_generate_uuid4(),
         'amount_money' => [
            'amount' => $amount,
            'currency' => 'CAD'
         ],
         'order_id' => $order_id,
         'location_id' => $this->locationId
      ]);
   }
   /******************************************************************
    * WEBHOOK HANDLING
    ******************************************************************/
@@ -2121,7 +1955,12 @@
         'squareConfig',
         [
            'isOpen'    => jvbIsOpen(),
//          'currency' => get_option('jvb_currency', 'CAD')
            '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')
         ]
      );
   }
@@ -2167,6 +2006,43 @@
   }
   /**
    * Save order reference for status tracking
    */
   public function saveOrderReference($data): array
   {
      $order_id = sanitize_text_field($data['order_id'] ?? '');
      $payment_id = sanitize_text_field($data['payment_id'] ?? '');
      if (!$order_id) {
         return ['success' => false, 'message' => 'Invalid order data'];
      }
      // Save to user if logged in
      if (is_user_logged_in()) {
         $user_id = get_current_user_id();
         $orders = get_user_meta($user_id, BASE . '_square_orders', true) ?: [];
         $orders[] = [
            'order_id' => $order_id,
            'payment_id' => $payment_id,
            'date' => current_time('mysql'),
            'customer' => $data['customer'] ?? []
         ];
         // Keep last 50 orders
         if (count($orders) > 50) {
            $orders = array_slice($orders, -50);
         }
         update_user_meta($user_id, BASE . '_square_orders', $orders);
      }
      return [
         'success' => true,
         'order_id' => $order_id,
         'message' => 'Order saved'
      ];
   }
   /**
    * Save order to user meta
    */
   private function saveOrderToUser(int $user_id, string $order_id): void
@@ -2186,9 +2062,9 @@
   }
   /**
    * Get order status
    * Get order status (for customer feedback)
    */
   public function getOrderStatus($data):WP_Error|array
   public function getOrderStatus($data): WP_Error|array
   {
      $order_id = sanitize_text_field($data['order_id'] ?? '');
@@ -2196,26 +2072,24 @@
         return new WP_Error('error', 'Order ID required');
      }
      // Check cache first
      $cached_status = get_transient(BASE . 'square_order_' . $order_id);
      if ($cached_status !== false) {
         return $cached_status;
      }
      // Fetch from Square
      $response = $this->getRequest('orders/' . $order_id);
      $response = $this->getRequest('v2/orders/' . $order_id);
      if (is_wp_error($response)) {
         return new WP_Error('error', 'Could not fetch order status');
      }
      $status = $response['order']['state'] ?? 'UNKNOWN';
      set_transient(BASE . 'square_order_' . $order_id, $status, 5 * MINUTE_IN_SECONDS);
      $order = $response['order'] ?? [];
      $status_data = [
         'state' => $order['state'] ?? 'UNKNOWN',
         'fulfillment_eta' => $order['fulfillments'][0]['pickup_details']['pickup_at'] ?? null
      ];
      return array_merge([
         'success'   => true,
      ], $status);
      return [
         'success' => true,
         'status' => $status_data['state'],
         'eta' => $status_data['fulfillment_eta']
      ];
   }
   /**
@@ -3026,137 +2900,60 @@
   IMAGE PROCESSING
    *********************************************************/
   /**
    * Upload featured image to Square catalog
    *
    * @param int $postID WordPress post ID
    * @return array|WP_Error Result of the upload operation
    */
   public function uploadImageToSquare(int $imgID): array|WP_Error
   {
      if ($imgID === 0) {
         return new WP_Error('no_image', 'No image found for post');
      }
      try {
         // Get the supported image (converts WebP if needed)
         $supported_image_id = $this->getSupportedImage($imgID);
         // Check if we've already uploaded this image
         $existing_square_image_id = $this->getSquareImageId($supported_image_id);
         if ($existing_square_image_id) {
            return [
               'success' => true,
               'image_id' => $existing_square_image_id,
               'message' => 'Image already exists in Square catalog'
            ];
         }
         // Step 1: Create image object in catalog
         $image_object = $this->createSquareImageObject($supported_image_id);
         if (is_wp_error($image_object)) {
            return $image_object;
         }
         // Step 2: Upload the actual image file
         $upload_result = $this->uploadImageFileToSquare(
            $supported_image_id,
            $image_object['id']
         );
         if (is_wp_error($upload_result)) {
            return $upload_result;
         }
         // Store the Square image ID for future reference
         $this->setSquareImageId($supported_image_id, $image_object['id']);
         return [
            'success' => true,
            'image_id' => $image_object['id'],
            'message' => 'Image successfully uploaded to Square'
         ];
      } catch (\Exception $e) {
         $this->logError('Failed to upload image to Square', [
            'method' => 'uploadImageToSquare',
            'error' => $e->getMessage()
         ]);
         return new WP_Error('upload_failed', $e->getMessage());
      }
   }
   /**
    * Create image object in Square catalog
    *
    * @param int $attachment_id WordPress attachment ID
    * @return array|WP_Error Square image object or error
    */
   protected function createSquareImageObject(int $attachment_id): array|WP_Error
   {
      $image_title = get_the_title($attachment_id);
      $alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
      $image_object = [
         'type' => 'IMAGE',
         'id' => '#IMAGE_' . $attachment_id . '_' . time(),
         'image_data' => [
            'name' => $image_title ?: 'Image',
            'caption' => $alt_text ?: ''
         ]
      ];
      $response = $this->postRequest('catalog/batch-upsert', [
         'idempotency_key' => wp_generate_uuid4(),
         'batches' => [[
            'objects' => [$image_object]
         ]]
      ]);
      if (is_wp_error($response)) {
         return $response;
      }
      if (!empty($response['objects'][0])) {
         return $response['objects'][0];
      }
      return new WP_Error('creation_failed', 'Failed to create image object in Square');
   }
   /**
    * Upload image file to Square
    *
    * @param int $attachment_id WordPress attachment ID
    * @param string $square_image_id Square catalog image ID
    * @param int $imgID WordPress attachment ID
    * @return array|WP_Error Upload result or error
    */
   protected function uploadImageFileToSquare(int $attachment_id, string $square_image_id): array|WP_Error
   protected function uploadImageToSquare(int $imgID): array|WP_Error
   {
      $file_path = get_attached_file($attachment_id);
      $supported_image_id = $this->getSupportedImage($imgID);
      // Check if already uploaded
      $existing_square_image_id = $this->getSquareImageId($supported_image_id);
      if ($existing_square_image_id) {
         return [
            'success' => true,
            'image_id' => $existing_square_image_id
         ];
      }
      $file_path = get_attached_file($supported_image_id);
      if (!file_exists($file_path)) {
         return new WP_Error('file_not_found', 'Image file not found');
      }
      // Verify file type
      $mime_type = get_post_mime_type($attachment_id);
      $allowed_types = ['image/jpeg', 'image/png', 'image/gif'];
      if (!in_array($mime_type, $allowed_types)) {
      $mime_type = get_post_mime_type($supported_image_id);
      if (!in_array($mime_type, ['image/jpeg', 'image/png', 'image/gif'])) {
         return new WP_Error('invalid_type', 'Square only supports JPEG, PNG, and GIF images');
      }
      // Prepare the multipart form data
      $image_title = get_the_title($supported_image_id);
      $alt_text = get_post_meta($supported_image_id, '_wp_attachment_image_alt', true);
      // Build multipart request - SINGLE STEP
      $boundary = wp_generate_password(24);
      $headers = $this->getRequestHeaders();
      $headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary;
      $body = $this->buildMultipartBody($file_path, $square_image_id, $boundary);
      // Request JSON part
      $request_json = [
         'idempotency_key' => wp_generate_uuid4(),
         'image' => [
            'type' => 'IMAGE',
            'id' => '#IMAGE_' . $supported_image_id . '_' . time(),
            'image_data' => [
               'name' => $image_title ?: 'Image',
               'caption' => $alt_text ?: ''
            ]
         ]
      ];
      $body = $this->buildMultipartBody($file_path, $request_json, $boundary);
      $response = wp_remote_post(
         $this->getApiUrl('catalog/images'),
         $this->getApiUrl('v2/catalog/images'),
         [
            'headers' => $headers,
            'body' => $body,
@@ -3168,56 +2965,43 @@
         return $response;
      }
      $body = wp_remote_retrieve_body($response);
      $data = json_decode($body, true);
      $data = json_decode(wp_remote_retrieve_body($response), true);
      if (!empty($data['errors'])) {
         $error_message = $data['errors'][0]['detail'] ?? 'Unknown error';
         return new WP_Error('upload_error', $error_message);
         return new WP_Error('upload_error', $data['errors'][0]['detail'] ?? 'Unknown error');
      }
      if (!empty($data['image'])) {
         return $data;
      if (!empty($data['image']['id'])) {
         $this->setSquareImageId($supported_image_id, $data['image']['id']);
         return [
            'success' => true,
            'image_id' => $data['image']['id']
         ];
      }
      return new WP_Error('upload_failed', 'Failed to upload image to Square');
      return new WP_Error('upload_failed', 'Failed to upload image');
   }
   /**
    * Build multipart form data for image upload
    *
    * @param string $file_path Path to image file
    * @param string $square_image_id Square catalog image ID
    * @param string $boundary Multipart boundary
    * @return string Multipart body
    */
   protected function buildMultipartBody(string $file_path, string $square_image_id, string $boundary): string
   protected function buildMultipartBody(string $file_path, array $request_json, string $boundary): string
   {
      $eol = "\r\n";
      $body = '';
      // Add request JSON
      $request_data = [
         'idempotency_key' => wp_generate_uuid4(),
         'object_id' => $square_image_id
      ];
      // Add request JSON part
      $body .= '--' . $boundary . $eol;
      $body .= 'Content-Disposition: form-data; name="request"' . $eol;
      $body .= 'Content-Type: application/json' . $eol . $eol;
      $body .= json_encode($request_data) . $eol;
      $body .= json_encode($request_json) . $eol;
      // Add image file
      // Add image file part
      $filename = basename($file_path);
      $file_contents = file_get_contents($file_path);
      $mime_type = mime_content_type($file_path);
      $body .= '--' . $boundary . $eol;
      $body .= 'Content-Disposition: form-data; name="image"; filename="' . $filename . '"' . $eol;
      $body .= 'Content-Disposition: form-data; name="file"; filename="' . $filename . '"' . $eol;
      $body .= 'Content-Type: ' . $mime_type . $eol . $eol;
      $body .= $file_contents . $eol;
      // End boundary
      $body .= '--' . $boundary . '--' . $eol;
      return $body;