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 ) { $webhook_handler = new PayPalWebhookHandler(); try { $webhook_handler->process_webhook( $request ); return new WP_REST_Response( array( 'message' => 'Webhook processed successfully' ), 200 ); } catch ( \Throwable $e ) { WC_Gateway_Paypal::log( 'Error processing webhook: ' . $e->getMessage() ); return new WP_REST_Response( array( 'error' => __( 'Webhook processing failed.', 'woocommerce' ) ), 500 ); } } }