Failed Conditions
Push — develop ( 0cd19e...00933d )
by Remco
07:53
created

WebhookController::rest_api_omnikassa_2_webhook()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 5
b 0
f 0
nc 2
nop 1
dl 0
loc 35
ccs 0
cts 25
cp 0
crap 6
rs 9.6333
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
Bug introduced by
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\Plugin::get_gateway($id) targeting Pronamic\WordPress\Pay\Plugin::get_gateway() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

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.

Loading history...
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 ( \Exception $e ) {
166
			return new \WP_Error(
167
				'rest_omnikassa_2_exception',
168
				 $e->getMessage(),
169
				array(
170
					'status'       => 400,
171
					'notification' => $json,
172
					'id'           => $id,
173
				)
174
			);
175
		}
176
		
177
		// Response.
178
		$response = new \WP_REST_Response( $data );
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
179
180
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
181
182
		return $response;
183
	}
184
185
	/**
186
	 * WordPress loaded, check for deprecated webhook call.
187
	 *
188
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
189
	 * @return void
190
	 */
191
	public function wp_loaded() {
192
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
193
			return;
194
		}
195
196
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
197
198
		exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
199
	}
200
}
201