From f3325b6c2db6149f37d289d133a431481e0876c7 Mon Sep 17 00:00:00 2001 From: John Austin Date: Mon, 3 Feb 2020 12:33:36 -0800 Subject: [PATCH] Updated helper functions to return sandbox or production values based on an optional param whose default value requests sandbox values --- templates/php/sq_config.php | 94 ++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 21 deletions(-) diff --git a/templates/php/sq_config.php b/templates/php/sq_config.php index aa1f13cb2..26f818f9a 100644 --- a/templates/php/sq_config.php +++ b/templates/php/sq_config.php @@ -16,6 +16,15 @@ // {{{ constants +/** +* Your Square sandbox location ID +* Used to make test calls against the Square sandbox +* REPLACE_ME = a sandbox location ID from the application Locations tab +*/ +if (!defined('_SQ_SANDBOX_LOCATION_ID')) { + define('_SQ_SANDBOX_LOCATION_ID', "REPLACE_ME") ; +} + /** * Your Square sandbox token * Used to make test calls against the Square sandbox @@ -43,6 +52,23 @@ define('_SQ_SANDBOX_APP_SECRET', "REPLACE_ME") ; } +/** +* Square sandbox domain for REST API calls +*/ +if (!defined('_SQ_SANDBOX_DOMAIN')) { + define('_SQ_SANDBOX_DOMAIN', "connect.squareupsandbox.com") ; +} + + +/** +* Your Square production location ID +* Used to make test calls against the Square production environment +* REPLACE_ME = a location ID from the application Locations tab +*/ +if (!defined('_SQ_LOCATION_ID')) { + define('_SQ_LOCATION_ID', "REPLACE_ME") ; +} + /** * Your Square application ID * REPLACE_ME = an application ID from the application Credentials tab @@ -60,17 +86,19 @@ } /** -* Square domain for REST API calls +* Your Square production token +* Used to make test calls against the Square production environment +* REPLACE_ME = a production access token from the application Credentials tab */ -if (!defined('_SQ_DOMAIN')) { - define('_SQ_DOMAIN', "connect.squareup.com") ; +if (!defined('_SQ_TOKEN')) { + define('_SQ_TOKEN', "REPLACE_ME") ; } /** -* Square sandbox domain for REST API calls +* Square domain for REST API calls */ -if (!defined('_SQ_SANDBOX_DOMAIN')) { - define('_SQ_SANDBOX_DOMAIN', "connect.squareupsandbox.com") ; +if (!defined('_SQ_DOMAIN')) { + define('_SQ_DOMAIN', "connect.squareup.com") ; } @@ -88,13 +116,32 @@ * * @return string a valid access token */ -function getAccessToken() { - - $accessToken = _SQ_SANDBOX_TOKEN; +function getAccessToken(bool $requestSandboxToken = TRUE) { + if ($requestSandboxToken) { + return _SQ_SANDBOX_TOKEN; + } else { + return _SQ_TOKEN; + } +} - return $accessToken; +/** + * Returns an OAuth app secret for Square API calls + * + * By default, the function below returns sandbox credentials for testing and + * development. For production, return the application secret assigned in the + * OAuth page of your app registration with the dashboard set to Production Settings. + * + * @return string a valid app secret + */ +function getAppSecret(bool $requestSandboxSecret = TRUE) { + if ($requestSandboxSecret) { + return _SQ_SANDBOX_APP_SECRET; + } else { + return _SQ_APP_SECRET; + } } + /** * Returns an application Id for Square API calls * @@ -104,28 +151,33 @@ function getAccessToken() { * * @return string a valid application ID token */ -function getApplicationId() { - - $accessToken = _SQ_SANDBOX_APP_ID; - - return $accessToken; +function getApplicationId(bool $requestSandboxAppId = TRUE) { + if ($requestSandboxAppId) { + return _SQ_SANDBOX_APP_ID; + } else { + return _SQ_APP_ID; + } } /** * Returns a location ID for Square API calls * - * By default, the function below returns a hardcoded location ID from the + * By default, the function below returns a hardcoded sandbox location ID from the * Application Dashboard. For production, update the function implementation * to fetch a valid location ID programmtically. * * @return string a valid location ID */ -function getLocationId() { - - // Replace the string with a sandbox location ID from the Application Dashboard - return "REPLACE_ME" ; - +function getLocationId(bool $requestSandboxLocation = TRUE) { + + if ($requestSandboxLocation) { + // Replace the string with a sandbox location ID from the Application Dashboard + return _SQ_SANDBOX_LOCATION_ID ; + } else { + // Replace the string with a production location ID from the Application Dashboard + return _SQ_LOCATION_ID ; + } } // }}}