← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-gateway-webhook-controller.php
+6
-48
4.4.0
→
4.4.10
View file →
| @@ -52,15 +52,15 @@ | ||
| 52 | 52 | * @param WP_REST_Request $request |
| 53 | 53 | * |
| 54 | 54 | * @return Response |
| 55 | 55 | * @since 4.3.7 |
| 56 | - * @version 1.0.0 | |
| 56 | + * @version 1.0.2 | |
| 57 | 57 | */ |
| 58 | 58 | public function listen_subscription_webhook( WP_REST_Request $request ): Response { |
| 59 | - $response = new Response(); | |
| 59 | + $response = new Response(); | |
| 60 | + $gateway_id = sanitize_key( (string) $request->get_param( 'gateway' ) ); | |
| 60 | 61 | |
| 61 | 62 | try { |
| 62 | - $gateway_id = sanitize_key( (string) $request->get_param( 'gateway' ) ); | |
| 63 | 63 | if ( empty( $gateway_id ) ) { |
| 64 | 64 | throw new Exception( __( 'Gateway is required.', 'learnpress' ), 400 ); |
| 65 | 65 | } |
| 66 | 66 | |
| @@ -72,63 +72,21 @@ | ||
| 72 | 72 | if ( ! $gateway->is_enabled() ) { |
| 73 | 73 | throw new Exception( __( 'Gateway is not enable.', 'learnpress' ), 404 ); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - LP_Debug::log_to_comment( 'Webhook payload: ' . json_encode( $request->get_body(), JSON_UNESCAPED_UNICODE ) ); | |
| 76 | + //LP_Debug::log_to_comment( 'Webhook payload: ' . json_encode( $request->get_body(), JSON_UNESCAPED_UNICODE ) ); | |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @var LP_Gateway_Paypal|LP_Gateway_Stripe $gateway |
| 80 | 80 | */ |
| 81 | 81 | $gateway->capture_subscription_webhook( $request ); |
| 82 | + $response->status = Response::STATUS_SUCCESS; | |
| 82 | 83 | } catch ( Throwable $e ) { |
| 84 | + LP_Debug::log_to_comment( 'Webhook error: ' . $gateway_id . ' - ' . $e->getMessage() ); | |
| 83 | 85 | LP_Debug::error_log( $e ); |
| 84 | 86 | $response->message = $e->getMessage(); |
| 85 | 87 | } |
| 86 | 88 | |
| 87 | 89 | 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 | 90 | } |
| 133 | 91 | } |
| 134 | 92 | } |