PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / includes / rest-api / Controllers / Version3 / class-wc-rest-paypal-webhooks-controller.php
class-wc-rest-paypal-webhooks-controller.php
97 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 *
4 * REST API PayPal webhooks controller
5 *
6 * Handles requests to the /paypal-webhooks endpoint.
7 *
8 * @package WooCommerce\RestApi
9 * @since 2.6.0
10 */
11
12 declare(strict_types=1);
13
14 defined( 'ABSPATH' ) || exit;
15
16 use Automattic\WooCommerce\Gateways\PayPal\WebhookHandler as PayPalWebhookHandler;
17
18 /**
19 * REST API PayPal webhook handler controller class.
20 *
21 * @package WooCommerce\RestApi
22 * @extends WC_REST_Controller
23 */
24 class WC_REST_Paypal_Webhooks_Controller extends WC_REST_Controller {
25
26 /**
27 * Endpoint namespace.
28 *
29 * @var string
30 */
31 protected $namespace = 'wc/v3';
32
33 /**
34 * Route base.
35 *
36 * @var string
37 */
38 protected $rest_base = 'paypal-webhooks';
39
40 /**
41 * Register the routes for the PayPal webhook handler.
42 *
43 * @return void
44 */
45 public function register_routes() {
46 // POST /v3/paypal-webhooks.
47 register_rest_route(
48 $this->namespace,
49 '/' . $this->rest_base,
50 array(
51 'methods' => WP_REST_Server::CREATABLE,
52 'callback' => array( $this, 'process_webhook' ),
53 'permission_callback' => array( $this, 'validate_webhook' ),
54 )
55 );
56 }
57
58 /**
59 * Validate the webhook.
60 *
61 * @param WP_REST_Request $request The request object.
62 * @return bool True if the webhook is valid, false otherwise.
63 */
64 public function validate_webhook( WP_REST_Request $request ) {
65 try {
66 if (
67 class_exists( 'Automattic\Jetpack\Connection\REST_Authentication' ) &&
68 method_exists( 'Automattic\Jetpack\Connection\REST_Authentication', 'is_signed_with_blog_token' )
69 ) {
70 return \Automattic\Jetpack\Connection\REST_Authentication::is_signed_with_blog_token();
71 }
72 return false;
73 } catch ( \Throwable $e ) {
74 WC_Gateway_Paypal::log( 'REST authentication method not available. Webhook data: ' . wc_print_r( $request->get_json_params(), true ), 'error' );
75 return false;
76 }
77 }
78
79 /**
80 * Process the webhook.
81 *
82 * @param WP_REST_Request $request The request object.
83 * @return WP_REST_Response The response object.
84 */
85 public function process_webhook( WP_REST_Request $request ) {
86 $webhook_handler = new PayPalWebhookHandler();
87
88 try {
89 $webhook_handler->process_webhook( $request );
90 return new WP_REST_Response( array( 'message' => 'Webhook processed successfully' ), 200 );
91 } catch ( \Throwable $e ) {
92 WC_Gateway_Paypal::log( 'Error processing webhook: ' . $e->getMessage() );
93 return new WP_REST_Response( array( 'error' => __( 'Webhook processing failed.', 'woocommerce' ) ), 500 );
94 }
95 }
96 }
97