| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Services\Payment; |
| 4 |
|
| 5 |
use IvyForms\Services\Integrations\MiddlewareUrl; |
| 6 |
|
| 7 |
/** |
| 8 |
* HTTP client for ivyforms-middleware Square endpoints. |
| 9 |
*/ |
| 10 |
class SquareMiddlewareClient |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @return array{applicationId: string, sandbox: bool} |
| 14 |
*/ |
| 15 |
public function fetchApplicationConfig(): array |
| 16 |
{ |
| 17 |
$empty = ['applicationId' => '', 'sandbox' => true]; |
| 18 |
$middlewareUrl = MiddlewareUrl::getBaseUrl(); |
| 19 |
if ($middlewareUrl === '') { |
| 20 |
return $empty; |
| 21 |
} |
| 22 |
|
| 23 |
$response = wp_remote_get( |
| 24 |
$middlewareUrl . '/square/application-config', |
| 25 |
['timeout' => 15] |
| 26 |
); |
| 27 |
|
| 28 |
if (is_wp_error($response) || (int) wp_remote_retrieve_response_code($response) !== 200) { |
| 29 |
return $empty; |
| 30 |
} |
| 31 |
|
| 32 |
$result = $this->parseResultPayload($response); |
| 33 |
if ($result === null) { |
| 34 |
return $empty; |
| 35 |
} |
| 36 |
|
| 37 |
return [ |
| 38 |
'applicationId' => trim((string) ($result['applicationId'] ?? '')), |
| 39 |
'sandbox' => !empty($result['sandbox']), |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
public function fetchAuthorizationUrl(string $callbackUrl): ?string |
| 44 |
{ |
| 45 |
$middlewareUrl = MiddlewareUrl::getBaseUrl(); |
| 46 |
if ($middlewareUrl === '') { |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
$response = wp_remote_get( |
| 51 |
$middlewareUrl . '/square/authorization/url?' . http_build_query( |
| 52 |
['callback_url' => $callbackUrl], |
| 53 |
'', |
| 54 |
'&', |
| 55 |
PHP_QUERY_RFC3986 |
| 56 |
), |
| 57 |
['timeout' => 15] |
| 58 |
); |
| 59 |
|
| 60 |
if (is_wp_error($response) || (int) wp_remote_retrieve_response_code($response) !== 200) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
$result = $this->parseResultPayload($response); |
| 65 |
if ($result === null) { |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
$authUrl = trim((string) ($result['authorizationUrl'] ?? '')); |
| 70 |
|
| 71 |
if ($authUrl === '' || ! $this->isValidAuthorizationUrl($authUrl)) { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
return $authUrl; |
| 76 |
} |
| 77 |
|
| 78 |
public function isValidAuthorizationUrl(string $url): bool |
| 79 |
{ |
| 80 |
if (filter_var($url, FILTER_VALIDATE_URL) === false) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$host = (string) wp_parse_url($url, PHP_URL_HOST); |
| 85 |
$query = []; |
| 86 |
parse_str((string) wp_parse_url($url, PHP_URL_QUERY), $query); |
| 87 |
$clientId = trim((string) ($query['client_id'] ?? '')); |
| 88 |
|
| 89 |
if ($clientId === '') { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
return $host !== '' && ( |
| 94 |
str_contains($host, 'squareup.com') |
| 95 |
|| str_contains($host, 'squareupsandbox.com') |
| 96 |
); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param array<string, mixed>|\WP_Error $response |
| 101 |
* @return array<string, mixed>|null |
| 102 |
*/ |
| 103 |
private function parseResultPayload($response): ?array |
| 104 |
{ |
| 105 |
$decoded = json_decode((string) wp_remote_retrieve_body($response), true); |
| 106 |
$result = is_array($decoded) ? ($decoded['result'] ?? null) : null; |
| 107 |
|
| 108 |
return is_array($result) ? $result : null; |
| 109 |
} |
| 110 |
} |
| 111 |
|