1 | <?php |
||
2 | /** |
||
3 | * Webhook controller |
||
4 | * |
||
5 | * @author Pronamic <[email protected]> |
||
6 | * @copyright 2005-2021 Pronamic |
||
7 | * @license GPL-3.0-or-later |
||
8 | * @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
||
9 | */ |
||
10 | |||
11 | namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
||
12 | |||
13 | use Pronamic\WordPress\Pay\GatewayPostType; |
||
14 | use Pronamic\WordPress\Pay\Plugin; |
||
15 | |||
16 | /** |
||
17 | * Webhook controller |
||
18 | * |
||
19 | * @author Remco Tolsma |
||
20 | * @version 2.3.3 |
||
21 | * @since 2.3.0 |
||
22 | */ |
||
23 | class WebhookController { |
||
24 | /** |
||
25 | * Setup. |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | public function setup() { |
||
30 | \add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
||
31 | |||
32 | \add_action( 'wp_loaded', array( $this, 'wp_loaded' ) ); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * REST API init. |
||
37 | * |
||
38 | * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ |
||
39 | * @link https://developer.wordpress.org/reference/hooks/rest_api_init/ |
||
40 | * @return void |
||
41 | */ |
||
42 | public function rest_api_init() { |
||
43 | \register_rest_route( |
||
44 | Integration::REST_ROUTE_NAMESPACE, |
||
45 | '/webhook', |
||
46 | array( |
||
47 | 'methods' => 'POST', |
||
48 | 'callback' => array( $this, 'rest_api_omnikassa_2_webhook' ), |
||
49 | 'permission_callback' => '__return_true', |
||
50 | ) |
||
51 | ); |
||
52 | |||
53 | \register_rest_route( |
||
54 | Integration::REST_ROUTE_NAMESPACE, |
||
55 | '/webhook/(?P<id>[\d]+)', |
||
56 | array( |
||
57 | 'args' => array( |
||
58 | 'id' => array( |
||
59 | 'description' => __( 'Unique identifier for the gateway configuration post.', 'pronamic_ideal' ), |
||
60 | 'type' => 'integer', |
||
61 | ), |
||
62 | ), |
||
63 | 'methods' => 'POST', |
||
64 | 'callback' => array( $this, 'rest_api_omnikassa_2_webhook_item' ), |
||
65 | 'permission_callback' => '__return_true', |
||
66 | ) |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * REST API OmniKassa 2.0 webhook handler. |
||
72 | * |
||
73 | * @param \WP_REST_Request $request Request. |
||
74 | * @return object |
||
75 | * @throws \Exception Throws exception when something unexpected happens ;-). |
||
76 | */ |
||
77 | public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) { |
||
78 | // Query. |
||
79 | $query = new \WP_Query( |
||
80 | array( |
||
81 | 'post_type' => GatewayPostType::POST_TYPE, |
||
82 | 'post_status' => 'publish', |
||
83 | 'nopaging' => true, |
||
84 | 'meta_query' => array( |
||
85 | array( |
||
86 | 'key' => '_pronamic_gateway_id', |
||
87 | 'value' => 'rabobank-omnikassa-2', |
||
88 | ), |
||
89 | ), |
||
90 | ) |
||
91 | ); |
||
92 | |||
93 | $results = array(); |
||
94 | |||
95 | foreach ( $query->posts as $post ) { |
||
96 | $id = \get_post_field( 'ID', $post ); |
||
97 | |||
98 | $request->set_param( 'id', $id ); |
||
99 | |||
100 | $results[] = $this->rest_api_omnikassa_2_webhook_item( $request ); |
||
101 | } |
||
102 | |||
103 | // Response. |
||
104 | $response = new \WP_REST_Response( array( |
||
105 | 'success' => true, |
||
106 | 'results' => $results, |
||
107 | ) ); |
||
108 | |||
109 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
110 | |||
111 | return $response; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * REST API OmniKassa 2.0 webhook handler. |
||
116 | * |
||
117 | * @param \WP_REST_Request $request Request. |
||
118 | * @return object |
||
119 | * @throws \Exception Throws exception when something unexpected happens ;-). |
||
120 | */ |
||
121 | public function rest_api_omnikassa_2_webhook_item( \WP_REST_Request $request ) { |
||
122 | // Input. |
||
123 | $json = $request->get_body(); |
||
124 | |||
125 | try { |
||
126 | $notification = Notification::from_json( $json ); |
||
127 | } catch ( \JsonSchema\Exception\ValidationException $e ) { |
||
128 | // Invalid input data. |
||
129 | return new \WP_Error( |
||
130 | 'rest_omnikassa_2_notification_invalid', |
||
131 | \__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ), |
||
132 | array( |
||
133 | 'status' => 400, |
||
134 | 'notification' => $json, |
||
135 | ) |
||
136 | ); |
||
137 | } |
||
138 | |||
139 | // Gateway configuration. |
||
140 | $id = $request->get_param( 'id' ); |
||
141 | |||
142 | if ( null === $id ) { |
||
143 | return new \WP_Error( |
||
144 | 'rest_omnikassa_2_gateway_no_id', |
||
145 | __( 'No gateway ID given in `id` parameter.', 'pronamic_ideal' ) |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | $gateway = Plugin::get_gateway( $id ); |
||
0 ignored issues
–
show
|
|||
150 | |||
151 | if ( ! $gateway instanceof Gateway ) { |
||
152 | // Invalid gateway. |
||
153 | return new \WP_Error( |
||
154 | 'rest_omnikassa_2_gateway_invalid', |
||
155 | \__( 'Invalid OmniKassa 2.0 gateway.', 'pronamic_ideal ' ), |
||
156 | array( |
||
157 | 'status' => 400, |
||
158 | 'id' => $id, |
||
159 | ) |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | try { |
||
164 | $gateway->handle_notification( $notification ); |
||
165 | } catch ( \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException $e ) { |
||
166 | /** |
||
167 | * We don't return an error for unknown order IDs, since OmniKassa |
||
168 | * otherwise assumes that the notification could not be processed. |
||
169 | */ |
||
170 | } catch ( \Exception $e ) { |
||
171 | return new \WP_Error( |
||
172 | 'rest_omnikassa_2_exception', |
||
173 | $e->getMessage(), |
||
174 | array( |
||
175 | 'status' => 400, |
||
176 | 'notification' => $json, |
||
177 | 'id' => $id, |
||
178 | ) |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | // Response. |
||
183 | $response = new \WP_REST_Response( $data ); |
||
184 | |||
185 | $response->add_link( 'self', \rest_url( $request->get_route() ) ); |
||
186 | |||
187 | return $response; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * WordPress loaded, check for deprecated webhook call. |
||
192 | * |
||
193 | * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309 |
||
194 | * @return void |
||
195 | */ |
||
196 | public function wp_loaded() { |
||
197 | if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) { |
||
198 | return; |
||
199 | } |
||
200 | |||
201 | \rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' ); |
||
202 | |||
203 | exit; |
||
204 | } |
||
205 | } |
||
206 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.