| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Plugin\Plugin; |
| 6 |
|
| 7 |
/** |
| 8 |
* Resolves Square sandbox vs production endpoints from IvyForms constants or middleware config. |
| 9 |
*/ |
| 10 |
class SquareEnvironment |
| 11 |
{ |
| 12 |
public static function isSandbox(): bool |
| 13 |
{ |
| 14 |
if (defined('IVYFORMS_SQUARE_AUTH_URL') && IVYFORMS_SQUARE_AUTH_URL !== '') { |
| 15 |
return str_contains((string) IVYFORMS_SQUARE_AUTH_URL, 'sandbox'); |
| 16 |
} |
| 17 |
|
| 18 |
if (defined('IVYFORMS_SQUARE_APP_ID') && IVYFORMS_SQUARE_APP_ID !== '') { |
| 19 |
return str_starts_with((string) IVYFORMS_SQUARE_APP_ID, 'sandbox-sq'); |
| 20 |
} |
| 21 |
|
| 22 |
$plugin = Plugin::getInstance(); |
| 23 |
if ($plugin->container !== null) { |
| 24 |
try { |
| 25 |
$squareOAuthService = $plugin->container->get(SquareOAuthService::class); |
| 26 |
|
| 27 |
return $squareOAuthService->isMiddlewareSandbox(); |
| 28 |
} catch (\Throwable $e) { |
| 29 |
// Fall through to default. |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
public static function getApiBaseUrl(): string |
| 37 |
{ |
| 38 |
return self::isSandbox() |
| 39 |
? 'https://connect.squareupsandbox.com/v2' |
| 40 |
: 'https://connect.squareup.com/v2'; |
| 41 |
} |
| 42 |
|
| 43 |
public static function getWebPaymentsSdkUrl(): string |
| 44 |
{ |
| 45 |
return self::isSandbox() |
| 46 |
? 'https://sandbox.web.squarecdn.com/v1/square.js' |
| 47 |
: 'https://web.squarecdn.com/v1/square.js'; |
| 48 |
} |
| 49 |
} |
| 50 |
|