| 1 |
<?php |
| 2 |
|
| 3 |
namespace IvyForms\Controllers\Square; |
| 4 |
|
| 5 |
use IvyForms\Services\Integrations\MiddlewareSignatureService; |
| 6 |
use IvyForms\Services\Payment\SquareOAuthService; |
| 7 |
use IvyForms\Services\Translations\BackendStrings; |
| 8 |
use WP_REST_Request; |
| 9 |
use WP_REST_Response; |
| 10 |
|
| 11 |
/** |
| 12 |
* Server-to-server OAuth token delivery from ivyforms-middleware (POST only). |
| 13 |
*/ |
| 14 |
class SquareAuthTokenController |
| 15 |
{ |
| 16 |
private SquareOAuthService $squareOAuthService; |
| 17 |
private MiddlewareSignatureService $middlewareSignatureService; |
| 18 |
|
| 19 |
public function __construct( |
| 20 |
SquareOAuthService $squareOAuthService, |
| 21 |
MiddlewareSignatureService $middlewareSignatureService |
| 22 |
) { |
| 23 |
$this->squareOAuthService = $squareOAuthService; |
| 24 |
$this->middlewareSignatureService = $middlewareSignatureService; |
| 25 |
} |
| 26 |
|
| 27 |
public function __invoke(WP_REST_Request $data): WP_REST_Response |
| 28 |
{ |
| 29 |
return $this->handle($data); |
| 30 |
} |
| 31 |
|
| 32 |
public function handle(WP_REST_Request $data): WP_REST_Response |
| 33 |
{ |
| 34 |
$params = $data->get_json_params(); |
| 35 |
if (!is_array($params)) { |
| 36 |
$params = $data->get_params(); |
| 37 |
} |
| 38 |
|
| 39 |
if (!$this->middlewareSignatureService->verifyTokenDeliverySignature($params)) { |
| 40 |
return new WP_REST_Response( |
| 41 |
[ |
| 42 |
'message' => BackendStrings::getCommonStrings()['invalid_request_data'], |
| 43 |
'data' => ['success' => false], |
| 44 |
], |
| 45 |
403 |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
$result = $this->squareOAuthService->handleAuthCallback($params); |
| 50 |
|
| 51 |
if (!$result['success']) { |
| 52 |
return new WP_REST_Response( |
| 53 |
[ |
| 54 |
'message' => $result['message'] ?? BackendStrings::getPaymentStrings()['square_connect_failed'], |
| 55 |
'data' => ['success' => false], |
| 56 |
], |
| 57 |
400 |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
return new WP_REST_Response( |
| 62 |
[ |
| 63 |
'message' => BackendStrings::getCommonStrings()['ok'], |
| 64 |
'data' => ['success' => true], |
| 65 |
], |
| 66 |
200 |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|