| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Square; |
| 4 |
|
| 5 |
use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 |
use IvyForms\Common\Sanitizer\Sanitizer; |
| 7 |
use IvyForms\Controllers\Controller; |
| 8 |
use IvyForms\Services\Payment\SquareOAuthService; |
| 9 |
use IvyForms\Services\Translations\BackendStrings; |
| 10 |
use WP_REST_Request; |
| 11 |
use WP_REST_Response; |
| 12 |
|
| 13 |
/** |
| 14 |
* Initiates Square OAuth (same role as MailChimpConnectController). |
| 15 |
*/ |
| 16 |
class SquareAuthUrlController extends Controller |
| 17 |
{ |
| 18 |
private SquareOAuthService $squareOAuthService; |
| 19 |
|
| 20 |
public function __construct(SquareOAuthService $squareOAuthService) |
| 21 |
{ |
| 22 |
$this->squareOAuthService = $squareOAuthService; |
| 23 |
} |
| 24 |
|
| 25 |
protected function handle(WP_REST_Request $data): WP_REST_Response |
| 26 |
{ |
| 27 |
Sanitizer::verifyNonce($data->get_header('X-WP-Nonce')); |
| 28 |
|
| 29 |
$result = $this->squareOAuthService->getAuthorizationUrl(); |
| 30 |
|
| 31 |
if (!$result['success']) { |
| 32 |
throw new InvalidArgumentException( |
| 33 |
$result['message'] ?? BackendStrings::getPaymentStrings()['square_auth_url_failed'] |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
return new WP_REST_Response( |
| 38 |
[ |
| 39 |
'auth_url' => filter_var((string) $result['url'], FILTER_SANITIZE_URL), |
| 40 |
], |
| 41 |
200 |
| 42 |
); |
| 43 |
} |
| 44 |
} |
| 45 |
|