PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 All 141 releases
learnpress / inc / rest-api / v1 / frontend / class-lp-rest-gateway-webhook-controller.php

class-lp-rest-gateway-webhook-controller.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.9.1, at inc/rest-api/v1/frontend/class-lp-rest-gateway-webhook-controller.php

93 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.2
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 $response->status = Response::STATUS_SUCCESS;
83 } catch ( Throwable $e ) {
84 LP_Debug::log_to_comment( 'Webhook error: ' . $gateway_id . ' - ' . $e->getMessage() );
85 LP_Debug::error_log( $e );
86 $response->message = $e->getMessage();
87 }
88
89 return $response;
90 }
91 }
92 }
93