From a239ef33ec489ce6d0f25ac2f256df0ac54a7e50 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Wed, 16 Sep 2026 00:07:34 +0000
Subject: [PATCH] =Tackling the integrations again. This time Helcim, with some minnor edits to GMB and Square

---
 inc/integrations/services/Square.php |   27 +++++++++++++++++----------
 1 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/inc/integrations/services/Square.php b/inc/integrations/services/Square.php
index 89d49c1..c642247 100644
--- a/inc/integrations/services/Square.php
+++ b/inc/integrations/services/Square.php
@@ -254,10 +254,15 @@
 
 	protected function refreshAccessToken(OAuthCredentials $credentials): array
 	{
-		if (!$this->getClientID() || !$this->getClientSecret())
+		if (!$this->getClientID() || !$this->getClientSecret() || empty($credentials->refresh_token)) {
+			$this->logError('refreshAccessToken', 'Missing client credentials or refresh token');
+			return $this->response(false, 'No client ID or client secret provided');
+		}
+
 		$response = wp_remote_post(
 			$this->oauth->getToken(),
 			[
+				'timeout'	=> 15,
 				'body'	=> [
 					'client_id'		=> $this->getClientID(),
 					'client_secret' => $this->getClientSecret(),
@@ -279,16 +284,18 @@
 		}
 
 		$data = json_decode(wp_remote_retrieve_body($response));
-		if (isset($data['access_token'])) {
-			$credentials = [
-				'access_token'	=>  $data['access_token'],
-				'expires_at'	=> time() + ($data['expires_in'] ?? 2592000) // 30 days default
-			];
-
-			$this->saveCredentials($credentials);
-			return $this->response(true, 'Successfully refreshed access token');
+		if (empty($data['access_token'])) {
+			$this->logError('refreshAccessToken', 'No access_token in refresh response', ['body' => $data]);
+			return $this->response(false, 'No Access Token in Response');
 		}
-		return $this->response(false, 'Something went wrong that wasn\'t caught - could not refresh access token');
+		$credentials = [
+			'access_token'  => $data['access_token'],
+			'expires_at'    => strtotime($data['expires_at'] ?? '') ?: (time() + 2592000),
+			'refresh_token' => $data['refresh_token'] ?? $credentials->refresh_token,
+		];
+		$this->saveCredentials($credentials);
+
+		return $this->response(true, 'Successfully refreshed access token');
 	}
 
 	protected function getRequestHeaders(): array

--
Gitblit v1.10.0