Failed Conditions
Push — develop ( 28b9ae...74ae5d )
by Remco
06:48
created

WebhookController::wp_loaded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Webhook controller
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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.2.5
21
 * @since   2.2.5
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
	 */
60
	public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) {
61
		/**
62
		 * Result.
63
		 *
64
		 * @link https://developer.wordpress.org/reference/functions/wp_send_json_success/
65
		 */
66
		$response = \rest_ensure_response( array( 'success' => true ) );
67
68
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
69
70
		// JSON.
71
		$json = $request->get_body();
72
73
		// Get notification from input data.
74
		try {
75
			$notification = Notification::from_json( $json );
76
		} catch ( \JsonSchema\Exception\ValidationException $e ) {
77
			// Invalid input data.
78
			return new \WP_Error(
79
				'rest_omnikassa_2_notification_invalid',
80
				\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ),
81
				array(
82
					'status'       => 400,
83
					'notification' => $json,
84
				)
85
			);
86
		}
87
88
		$query = new \WP_Query(
89
			array(
90
				'post_type'   => GatewayPostType::POST_TYPE,
91
				'post_status' => 'publish',
92
				'nopaging'    => true,
93
				'meta_query'  => array(
94
					array(
95
						'key'   => '_pronamic_gateway_id',
96
						'value' => 'rabobank-omnikassa-2',
97
					),
98
				),
99
			)
100
		);
101
102
		foreach ( $query->posts as $post ) {
103
			$gateway = Plugin::get_gateway( $post->ID );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...:get_gateway($post->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...
104
105
			if ( $gateway instanceof Gateway ) {
106
				try {
107
					$gateway->handle_notification( $notification );
108
				} catch ( \Exception $e ) {
109
					continue;
110
				}
111
			}
112
		}
113
114
		return $response;
115
	}
116
117
	/**
118
	 * WordPress loaded, check for deprecated webhook call.
119
	 *
120
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
121
	 * @return void
122
	 */
123
	public function wp_loaded() {
124
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
125
			return;
126
		}
127
128
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
129
130
		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...
131
	}
132
}
133