From c204185ae86a98994f80010abf35a190c9406739 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 12 Jul 2026 18:08:19 +0000
Subject: [PATCH] =Refactor of Integrations.php. Separated different functionality into traits that classes can use to add that functionality. Hopefully will make maintaining it a little easier. Still have to finish up, as well as refactoring the individual classes to utilize the new system.

---
 inc/rest/routes/IntegrationsSquareRoutes.php |   67 +++++++++++----------------------
 1 files changed, 23 insertions(+), 44 deletions(-)

diff --git a/inc/rest/routes/IntegrationsSquareRoutes.php b/inc/rest/routes/IntegrationsSquareRoutes.php
index 596be1e..79cbd0d 100644
--- a/inc/rest/routes/IntegrationsSquareRoutes.php
+++ b/inc/rest/routes/IntegrationsSquareRoutes.php
@@ -2,9 +2,10 @@
 namespace JVBase\rest\routes;
 
 
+use Exception;
+use JVBase\integrations\services\Square;
 use JVBase\meta\Meta;
 use JVBase\rest\Rest;
-use Exception;
 use JVBase\rest\Route;
 use WP_REST_Request;
 use WP_REST_Response;
@@ -20,22 +21,26 @@
 		Route::for('square/process-payment')
 			->post([$this, 'handlePaymentProcessing'])
 			->auth('public')
-			->rateLimit(2);
+			->rateLimit(2)
+			->register();
 
 		Route::for('square/saved-cards')
 			->post([$this, 'getSavedCards'])
 			->auth('user')
-			->rateLimit(5);
+			->rateLimit(5)
+			->register();
 
 		Route::for('square/order-history')
 			->get([$this, 'getOrderHistory'])
 			->auth('user')
-			->rateLimit(5);
+			->rateLimit(5)
+			->register();
 
 		Route::for(Route::pattern('square/order-status/{order_id}'))
 			->get([$this, 'getOrderStatus'])
 			->auth('public')
-			->rateLimit(20);
+			->rateLimit(20)
+			->register();
 	}
 
 	//TODO: Are we processing this through our server at all? Or is it in the javascript going straight to square?
@@ -43,6 +48,7 @@
 	{
 		$data = $request->get_json_params();
 
+
 		// Generate idempotency key from cart_id + timestamp
 		// This ensures retries use SAME key
 		$cart_id = $data['cart_id'] ?? '';
@@ -73,6 +79,7 @@
 		}
 
 		try {
+			/** @var Square $square */
 			$square = JVB()->connect('square');
 			// Step 1: Get or create Square customer
 			$customer_id = $square->getOrCreateSquareCustomer($data['customer']);
@@ -92,7 +99,7 @@
 			// Step 3: Create Payment in Square
 			$payment_response = $square->createSquarePayment(
 				$data['source_id'],
-				$data['idempotency_key'],
+				$idempotency_key,
 				$data['amount'],
 				$order_id,
 				$customer_id
@@ -142,12 +149,12 @@
 	public function getSavedCards(WP_REST_Request $request):WP_REST_Response
 	{
 		$data = $request->get_params();
-		$user_id = absint($data['user']??0);
-		if ($user_id === 0) {
-			return $this->validationError(['message' => 'Not logged in']);
+		error_log('Getting Saved Cards: '.print_r($data, true));
+		$user_id = get_current_user_id();
+		if (!$user_id) {
+			return $this->success(['cards' => []]);
 		}
 
-		$square = JVB()->connect('square');
 
 		// Get Square customer ID for this user
 		$square_customer_id = get_user_meta($user_id, BASE . '_square_customer_id', true);
@@ -156,58 +163,30 @@
 			return $this->success(['cards' => []]);
 		}
 
-		// Fetch cards from Square (2025-compliant - separate endpoint)
-		$cards_response = $square->getRequest('cards?customer_id=' . $square_customer_id);
-
-		if (is_wp_error($cards_response)) {
-			return $this->error('Failed to fetch cards');
-		}
-
-		return $this->success(['cards' => $cards_response['cards']??[]]);
+		return $this->success(['cards' => Square::getInstance()->getUserCards( $square_customer_id)]);
 	}
 
 	public function getOrderHistory(WP_REST_Request $request):WP_REST_Response
 	{
-		$data = $request->get_params();
-		$user_id = absint($data['user']??0);
-		if ($user_id === 0) {
+		$user_id = get_current_user_id();
+		if (!$user_id) {
 			return $this->validationError(['message' => 'Not logged in']);
 		}
 
-		// Get orders from custom post type
-		$orders = get_posts([
-			'post_type' => BASE . '_sq_orders',
-			'author' => $user_id,
-			'posts_per_page' => 50,
-			'orderby' => 'date',
-			'order' => 'DESC'
-		]);
-
-		$order_data = [];
-		foreach ($orders as $order) {
-			$meta = Meta::forPost($order->ID);
-			$fields = $meta->getAll(['square_order_id', 'status', 'amount', 'items', 'created_at', 'pickup_time']);
-			$order_data[] = array_merge([
-				'wp_order_id' => $order->ID,
-			], $fields);
-		}
-
-		return $this->success(['orders' => $order_data]);
+		return $this->success(['orders' => Square::getInstance()->getOrderHistory($user_id)]);
 	}
 
 	public function getOrderStatus(WP_REST_Request $request):WP_REST_Response
 	{
 		$order_id = $request->get_param('order_id');
-
-		// Find WP post by Square order ID
-		$wp_order_id = get_option(BASE . 'square_order_map_' . $order_id);
+		$wp_order_id = Square::getInstance()->getOrderPost($order_id);
 
 		if (!$wp_order_id) {
 			return $this->error('Order not found');
 		}
 
 		$meta = Meta::forPost($wp_order_id);
-		$fields = $meta->getAll(['status', 'fulfillment_status', 'pickup_time', 'items']);
+		$fields = $meta->getAll(['square_payment_status', 'fulfillment_status', 'pickup_time', 'items']);
 
 		return $this->success(['order' => $fields]);
 	}

--
Gitblit v1.10.0