webhook_processing_service = $webhook_processing_service; } /** * Configure REST API routes. */ public function register_routes() { register_rest_route( $this->namespace, '/' . $this->rest_base, [ 'methods' => WP_REST_Server::CREATABLE, 'callback' => [ $this, 'handle_webhook' ], 'permission_callback' => [ $this, 'check_permission' ], ] ); } /** * Retrieve transactions to respond with via API. * * @param WP_REST_Request $request Full data about the request. * * @return WP_REST_Response */ public function handle_webhook( $request ) { $body = $request->get_json_params(); try { $this->webhook_processing_service->process( $body ); } catch ( Invalid_Webhook_Data_Exception $e ) { Logger::error( $e ); return new WP_REST_Response( [ 'result' => self::RESULT_BAD_REQUEST ], 400 ); } catch ( Exception $e ) { Logger::error( $e ); return new WP_REST_Response( [ 'result' => self::RESULT_ERROR ], 500 ); } return new WP_REST_Response( [ 'result' => self::RESULT_SUCCESS ] ); } }