namespace, '/' . $this->rest_base, array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'process_webhook' ), 'permission_callback' => array( $this, 'validate_webhook' ), ) ); } /** * Validate the webhook. * * @param WP_REST_Request $request The request object. * @return bool True if the webhook is valid, false otherwise. */ public function validate_webhook( WP_REST_Request $request ) { try { if ( class_exists( 'Automattic\Jetpack\Connection\REST_Authentication' ) && method_exists( 'Automattic\Jetpack\Connection\REST_Authentication', 'is_signed_with_blog_token' ) ) { return \Automattic\Jetpack\Connection\REST_Authentication::is_signed_with_blog_token(); } return false; } catch ( \Throwable $e ) { WC_Gateway_Paypal::log( 'REST authentication method not available. Webhook data: ' . wc_print_r( $request->get_json_params(), true ), 'error' ); return false; } } /** * Process the webhook. * * @param WP_REST_Request $request The request object. * @return WP_REST_Response The response object. */ public function process_webhook( WP_REST_Request $request ) { include_once WC_ABSPATH . 'includes/gateways/paypal/includes/class-wc-gateway-paypal-webhook-handler.php'; $webhook_handler = new WC_Gateway_Paypal_Webhook_Handler(); try { $webhook_handler->process_webhook( $request ); return new WP_REST_Response( array( 'message' => 'Webhook processed successfully' ), 200 ); } catch ( Exception $e ) { return new WP_REST_Response( array( 'error' => $e->getMessage() ), 500 ); } } }