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
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* REST API OmniKassa 2.0 webhook handler. |
56
|
|
|
* |
57
|
|
|
* @param \WP_REST_Request $request Request. |
58
|
|
|
* @return object |
59
|
|
|
* @throws \Exception Throws exception when something unexpected happens ;-). |
60
|
|
|
*/ |
61
|
|
|
public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) { |
62
|
|
|
// Input. |
63
|
|
|
$json = $request->get_body(); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$notification = Notification::from_json( $json ); |
67
|
|
|
} catch ( \JsonSchema\Exception\ValidationException $e ) { |
68
|
|
|
// Invalid input data. |
69
|
|
|
return new \WP_Error( |
70
|
|
|
'rest_omnikassa_2_notification_invalid', |
71
|
|
|
\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ), |
72
|
|
|
array( |
73
|
|
|
'status' => 400, |
74
|
|
|
'notification' => $json, |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Query. |
80
|
|
|
$query = new \WP_Query( |
81
|
|
|
array( |
82
|
|
|
'post_type' => GatewayPostType::POST_TYPE, |
83
|
|
|
'post_status' => 'publish', |
84
|
|
|
'nopaging' => true, |
85
|
|
|
'meta_query' => array( |
86
|
|
|
array( |
87
|
|
|
'key' => '_pronamic_gateway_id', |
88
|
|
|
'value' => 'rabobank-omnikassa-2', |
89
|
|
|
), |
90
|
|
|
), |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$exceptions = array(); |
95
|
|
|
|
96
|
|
|
foreach ( $query->posts as $post ) { |
97
|
|
|
$gateway = Plugin::get_gateway( \get_post_field( 'ID', $post ) ); |
|
|
|
|
98
|
|
|
|
99
|
|
|
if ( ! $gateway instanceof Gateway ) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Try to handle notification, if an exception occurs we |
105
|
|
|
* will keep trying the other gateways. |
106
|
|
|
*/ |
107
|
|
|
try { |
108
|
|
|
$gateway->handle_notification( $notification ); |
109
|
|
|
} catch ( \Exception $e ) { |
110
|
|
|
$exceptions[] = $e; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ( $exceptions as $e ) { |
115
|
|
|
throw $e; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Response. |
119
|
|
|
$response = new \WP_REST_Response( array( 'success' => true ) ); |
120
|
|
|
|
121
|
|
|
$response->add_link( 'self', \rest_url( $request->get_route() ) ); |
122
|
|
|
|
123
|
|
|
return $response; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* WordPress loaded, check for deprecated webhook call. |
128
|
|
|
* |
129
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309 |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
|
public function wp_loaded() { |
133
|
|
|
if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' ); |
138
|
|
|
|
139
|
|
|
exit; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
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.