| 1 |
<?php |
| 2 |
/** |
| 3 |
* Store Integration REST Controller |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Controllers/Rest |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Controllers\Rest; |
| 10 |
|
| 11 |
use SeQura\Core\BusinessLogic\ConfigurationWebhookAPI\ConfigurationWebhookAPI; |
| 12 |
use SeQura\Core\BusinessLogic\ConfigurationWebhookAPI\Responses\TopicMissingErrorResponse; |
| 13 |
use SeQura\WC\Services\Log\Interface_Logger_Service; |
| 14 |
use SeQura\Core\Infrastructure\Utility\RegexProvider; |
| 15 |
use SeQura\WC\Core\Implementation\BusinessLogic\Domain\Integration\StoreIntegration\Interface_Store_Integration_Service; |
| 16 |
use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext; |
| 17 |
use WP_Error; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_REST_Response; |
| 20 |
|
| 21 |
/** |
| 22 |
* Store Integration REST Controller |
| 23 |
*/ |
| 24 |
class Store_Integration_REST_Controller extends REST_Controller { |
| 25 |
|
| 26 |
protected const PARAM_SIGNATURE = 'signature'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Store integration service |
| 30 |
* |
| 31 |
* @var Interface_Store_Integration_Service |
| 32 |
*/ |
| 33 |
private $store_integration_service; |
| 34 |
|
| 35 |
/** |
| 36 |
* Store context |
| 37 |
* |
| 38 |
* @var StoreContext |
| 39 |
*/ |
| 40 |
private $store_context; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* @param string $rest_namespace The namespace. |
| 46 |
* @param Interface_Logger_Service $logger The logger service. |
| 47 |
* @param RegexProvider $regex The regex provider. |
| 48 |
* @param Interface_Store_Integration_Service $store_integration_service The store integration service. |
| 49 |
* @param StoreContext $store_context The store context. |
| 50 |
*/ |
| 51 |
public function __construct( |
| 52 |
$rest_namespace, |
| 53 |
Interface_Logger_Service $logger, |
| 54 |
RegexProvider $regex, |
| 55 |
Interface_Store_Integration_Service $store_integration_service, |
| 56 |
StoreContext $store_context |
| 57 |
) { |
| 58 |
parent::__construct( $logger, $regex ); |
| 59 |
$this->store_integration_service = $store_integration_service; |
| 60 |
$this->namespace = $rest_namespace; |
| 61 |
$this->rest_base = $this->store_integration_service->get_rest_base(); |
| 62 |
$this->store_context = $store_context; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Register the API endpoints. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
public function register_routes() { |
| 71 |
$this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); |
| 72 |
$args = array( |
| 73 |
self::PARAM_STORE_ID => $this->get_arg_string(), |
| 74 |
self::PARAM_SIGNATURE => $this->get_arg_string(), |
| 75 |
); |
| 76 |
$this->register_post( $this->store_integration_service->get_endpoint(), 'handle_post', $args, 'check_permissions' ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Check the request data to see if it has permission to proceed. |
| 81 |
*/ |
| 82 |
public function check_permissions(): bool { |
| 83 |
// The signature is checked in the Integration Core, no need to check it here. |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Handle POST request. |
| 89 |
* |
| 90 |
* @throws \Exception |
| 91 |
* @param WP_REST_Request $request The request. |
| 92 |
* |
| 93 |
* @return WP_REST_Response|WP_Error |
| 94 |
*/ |
| 95 |
public function handle_post( WP_REST_Request $request ) { |
| 96 |
$signature = $request->get_param( 'signature' ); |
| 97 |
$payload = $request->get_json_params(); |
| 98 |
if ( ! \is_array( $payload ) ) { |
| 99 |
$payload = array(); |
| 100 |
} |
| 101 |
$response = ConfigurationWebhookAPI::configurationHandler( $this->store_context->getStoreId() ) |
| 102 |
->handleRequest( $signature, $payload ); |
| 103 |
$wp_response = $this->build_response( $response ); |
| 104 |
|
| 105 |
if ( $wp_response instanceof WP_Error && $response instanceof TopicMissingErrorResponse ) { |
| 106 |
$wp_response->add_data( array( 'status' => 400 ) ); |
| 107 |
} |
| 108 |
|
| 109 |
return $wp_response; |
| 110 |
} |
| 111 |
} |
| 112 |
|