← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-gateway-webhook-controller.php
+135
-134
4.3.8
→
4.4.9
View file →
| @@ -1,134 +1,135 @@ | ||
| 1 | -<?php | |
| 2 | -use LearnPress\Helpers\Response; | |
| 3 | - | |
| 4 | -defined( 'ABSPATH' ) || exit(); | |
| 5 | - | |
| 6 | -if ( ! class_exists( 'LP_REST_Gateway_Webhook_Controller' ) ) { | |
| 7 | - /** | |
| 8 | - * REST API endpoint for gateway subscription webhooks. | |
| 9 | - * | |
| 10 | - * @since 4.3.7 | |
| 11 | - * @version 1.0.0 | |
| 12 | - */ | |
| 13 | - class LP_REST_Gateway_Webhook_Controller extends LP_Abstract_REST_Controller { | |
| 14 | - /** | |
| 15 | - * Configure REST namespace/base for gateway webhook routes. | |
| 16 | - * | |
| 17 | - * @return void | |
| 18 | - */ | |
| 19 | - public function __construct() { | |
| 20 | - $this->namespace = 'lp/v1'; | |
| 21 | - $this->rest_base = 'gateways'; | |
| 22 | - | |
| 23 | - parent::__construct(); | |
| 24 | - } | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Register public subscription webhook route. | |
| 28 | - * | |
| 29 | - * Path pattern: /lp/v1/gateways/{gateway}/subscription-webhook | |
| 30 | - * | |
| 31 | - * @return void | |
| 32 | - */ | |
| 33 | - public function register_routes() { | |
| 34 | - $this->routes = array( | |
| 35 | - '(?P<gateway>[a-zA-Z0-9_-]+)/subscription-webhook' => array( | |
| 36 | - array( | |
| 37 | - 'methods' => WP_REST_Server::ALLMETHODS, | |
| 38 | - 'callback' => array( $this, 'listen_subscription_webhook' ), | |
| 39 | - 'permission_callback' => '__return_true', | |
| 40 | - ), | |
| 41 | - ), | |
| 42 | - ); | |
| 43 | - | |
| 44 | - parent::register_routes(); | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Dispatch provider subscription webhook to gateway implementation. | |
| 49 | - * | |
| 50 | - * This is the centralized entrypoint used by Stripe/PayPal webhook calls. | |
| 51 | - * | |
| 52 | - * @param WP_REST_Request $request | |
| 53 | - * | |
| 54 | - * @return Response | |
| 55 | - * @since 4.3.7 | |
| 56 | - * @version 1.0.0 | |
| 57 | - */ | |
| 58 | - public function listen_subscription_webhook( WP_REST_Request $request ): Response { | |
| 59 | - $response = new Response(); | |
| 60 | - | |
| 61 | - try { | |
| 62 | - $gateway_id = sanitize_key( (string) $request->get_param( 'gateway' ) ); | |
| 63 | - if ( empty( $gateway_id ) ) { | |
| 64 | - throw new Exception( __( 'Gateway is required.', 'learnpress' ), 400 ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - $gateway = LP_Gateways::instance()->get_gateway( $gateway_id ); | |
| 68 | - if ( ! $gateway instanceof LP_Gateway_Abstract ) { | |
| 69 | - throw new Exception( __( 'Gateway not found.', 'learnpress' ), 404 ); | |
| 70 | - } | |
| 71 | - | |
| 72 | - if ( ! $gateway->is_enabled() ) { | |
| 73 | - throw new Exception( __( 'Gateway is not enable.', 'learnpress' ), 404 ); | |
| 74 | - } | |
| 75 | - | |
| 76 | - LP_Debug::log_to_comment( 'Webhook payload: ' . json_encode( $request->get_body(), JSON_UNESCAPED_UNICODE ) ); | |
| 77 | - | |
| 78 | - /** | |
| 79 | - * @var LP_Gateway_Paypal|LP_Gateway_Stripe $gateway | |
| 80 | - */ | |
| 81 | - $gateway->capture_subscription_webhook( $request ); | |
| 82 | - } catch ( Throwable $e ) { | |
| 83 | - LP_Debug::error_log( $e ); | |
| 84 | - $response->message = $e->getMessage(); | |
| 85 | - } | |
| 86 | - | |
| 87 | - return $response; | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Build sanitized REST error response for public webhook endpoint. | |
| 92 | - * | |
| 93 | - * Internal provider error details are logged server-side, while API | |
| 94 | - * response returns a generic/safe message by status class. | |
| 95 | - * | |
| 96 | - * @param Throwable $error | |
| 97 | - * | |
| 98 | - * @return WP_REST_Response | |
| 99 | - */ | |
| 100 | - protected function build_error_response( Throwable $error ): WP_REST_Response { | |
| 101 | - $status = absint( $error->getCode() ); | |
| 102 | - if ( $status < 100 || $status > 599 ) { | |
| 103 | - $status = 400; | |
| 104 | - } | |
| 105 | - | |
| 106 | - $error_code = 'lp_subscription_webhook_error'; | |
| 107 | - $private_message = (string) $error->getMessage(); | |
| 108 | - $public_message = __( 'Invalid webhook request.', 'learnpress' ); | |
| 109 | - | |
| 110 | - if ( 429 === $status ) { | |
| 111 | - $public_message = __( 'Too many webhook requests.', 'learnpress' ); | |
| 112 | - } elseif ( 413 === $status ) { | |
| 113 | - $public_message = __( 'Webhook payload too large.', 'learnpress' ); | |
| 114 | - } | |
| 115 | - | |
| 116 | - error_log( | |
| 117 | - sprintf( | |
| 118 | - 'LP subscription webhook error [%s]: %s', | |
| 119 | - $error_code, | |
| 120 | - $private_message | |
| 121 | - ) | |
| 122 | - ); | |
| 123 | - | |
| 124 | - return new WP_REST_Response( | |
| 125 | - array( | |
| 126 | - 'status' => 'error', | |
| 127 | - 'code' => $error_code, | |
| 128 | - 'message' => $public_message, | |
| 129 | - ), | |
| 130 | - $status | |
| 131 | - ); | |
| 132 | - } | |
| 133 | - } | |
| 134 | -} | |
| 1 | +<?php | |
| 2 | +use LearnPress\Helpers\Response; | |
| 3 | + | |
| 4 | +defined( 'ABSPATH' ) || exit(); | |
| 5 | + | |
| 6 | +if ( ! class_exists( 'LP_REST_Gateway_Webhook_Controller' ) ) { | |
| 7 | + /** | |
| 8 | + * REST API endpoint for gateway subscription webhooks. | |
| 9 | + * | |
| 10 | + * @since 4.3.7 | |
| 11 | + * @version 1.0.0 | |
| 12 | + */ | |
| 13 | + class LP_REST_Gateway_Webhook_Controller extends LP_Abstract_REST_Controller { | |
| 14 | + /** | |
| 15 | + * Configure REST namespace/base for gateway webhook routes. | |
| 16 | + * | |
| 17 | + * @return void | |
| 18 | + */ | |
| 19 | + public function __construct() { | |
| 20 | + $this->namespace = 'lp/v1'; | |
| 21 | + $this->rest_base = 'gateways'; | |
| 22 | + | |
| 23 | + parent::__construct(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Register public subscription webhook route. | |
| 28 | + * | |
| 29 | + * Path pattern: /lp/v1/gateways/{gateway}/subscription-webhook | |
| 30 | + * | |
| 31 | + * @return void | |
| 32 | + */ | |
| 33 | + public function register_routes() { | |
| 34 | + $this->routes = array( | |
| 35 | + '(?P<gateway>[a-zA-Z0-9_-]+)/subscription-webhook' => array( | |
| 36 | + array( | |
| 37 | + 'methods' => WP_REST_Server::ALLMETHODS, | |
| 38 | + 'callback' => array( $this, 'listen_subscription_webhook' ), | |
| 39 | + 'permission_callback' => '__return_true', | |
| 40 | + ), | |
| 41 | + ), | |
| 42 | + ); | |
| 43 | + | |
| 44 | + parent::register_routes(); | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Dispatch provider subscription webhook to gateway implementation. | |
| 49 | + * | |
| 50 | + * This is the centralized entrypoint used by Stripe/PayPal webhook calls. | |
| 51 | + * | |
| 52 | + * @param WP_REST_Request $request | |
| 53 | + * | |
| 54 | + * @return Response | |
| 55 | + * @since 4.3.7 | |
| 56 | + * @version 1.0.1 | |
| 57 | + */ | |
| 58 | + public function listen_subscription_webhook( WP_REST_Request $request ): Response { | |
| 59 | + $response = new Response(); | |
| 60 | + $gateway_id = sanitize_key( (string) $request->get_param( 'gateway' ) ); | |
| 61 | + | |
| 62 | + try { | |
| 63 | + if ( empty( $gateway_id ) ) { | |
| 64 | + throw new Exception( __( 'Gateway is required.', 'learnpress' ), 400 ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + $gateway = LP_Gateways::instance()->get_gateway( $gateway_id ); | |
| 68 | + if ( ! $gateway instanceof LP_Gateway_Abstract ) { | |
| 69 | + throw new Exception( __( 'Gateway not found.', 'learnpress' ), 404 ); | |
| 70 | + } | |
| 71 | + | |
| 72 | + if ( ! $gateway->is_enabled() ) { | |
| 73 | + throw new Exception( __( 'Gateway is not enable.', 'learnpress' ), 404 ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + //LP_Debug::log_to_comment( 'Webhook payload: ' . json_encode( $request->get_body(), JSON_UNESCAPED_UNICODE ) ); | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * @var LP_Gateway_Paypal|LP_Gateway_Stripe $gateway | |
| 80 | + */ | |
| 81 | + $gateway->capture_subscription_webhook( $request ); | |
| 82 | + } catch ( Throwable $e ) { | |
| 83 | + LP_Debug::log_to_comment( 'Webhook error: ' . $gateway_id . ' - ' . $e->getMessage() ); | |
| 84 | + LP_Debug::error_log( $e ); | |
| 85 | + $response->message = $e->getMessage(); | |
| 86 | + } | |
| 87 | + | |
| 88 | + return $response; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * Build sanitized REST error response for public webhook endpoint. | |
| 93 | + * | |
| 94 | + * Internal provider error details are logged server-side, while API | |
| 95 | + * response returns a generic/safe message by status class. | |
| 96 | + * | |
| 97 | + * @param Throwable $error | |
| 98 | + * | |
| 99 | + * @return WP_REST_Response | |
| 100 | + */ | |
| 101 | + protected function build_error_response( Throwable $error ): WP_REST_Response { | |
| 102 | + $status = absint( $error->getCode() ); | |
| 103 | + if ( $status < 100 || $status > 599 ) { | |
| 104 | + $status = 400; | |
| 105 | + } | |
| 106 | + | |
| 107 | + $error_code = 'lp_subscription_webhook_error'; | |
| 108 | + $private_message = (string) $error->getMessage(); | |
| 109 | + $public_message = __( 'Invalid webhook request.', 'learnpress' ); | |
| 110 | + | |
| 111 | + if ( 429 === $status ) { | |
| 112 | + $public_message = __( 'Too many webhook requests.', 'learnpress' ); | |
| 113 | + } elseif ( 413 === $status ) { | |
| 114 | + $public_message = __( 'Webhook payload too large.', 'learnpress' ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + error_log( | |
| 118 | + sprintf( | |
| 119 | + 'LP subscription webhook error [%s]: %s', | |
| 120 | + $error_code, | |
| 121 | + $private_message | |
| 122 | + ) | |
| 123 | + ); | |
| 124 | + | |
| 125 | + return new WP_REST_Response( | |
| 126 | + array( | |
| 127 | + 'status' => 'error', | |
| 128 | + 'code' => $error_code, | |
| 129 | + 'message' => $public_message, | |
| 130 | + ), | |
| 131 | + $status | |
| 132 | + ); | |
| 133 | + } | |
| 134 | + } | |
| 135 | +} | |