Failed Conditions
Push — master ( e6b844...0f20c1 )
by Reüel
09:09 queued 12s
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-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.0
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
	 */
60
	public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) {
61
		// Input.
62
		$json = $request->get_body();
63
64
		try {
65
			$notification = Notification::from_json( $json );
66
		} catch ( \JsonSchema\Exception\ValidationException $e ) {
67
			// Invalid input data.
68
			return new \WP_Error(
69
				'rest_omnikassa_2_notification_invalid',
70
				\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ),
71
				array(
72
					'status'       => 400,
73
					'notification' => $json,
74
				)
75
			);
76
		}
77
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
		foreach ( $query->posts as $post ) {
94
			$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...
95
96
			if ( $gateway instanceof Gateway ) {
97
				try {
98
					$gateway->handle_notification( $notification );
99
				} catch ( \Exception $e ) {
100
					continue;
101
				}
102
			}
103
		}
104
105
		// Response.
106
		$response = new \WP_REST_Response( array( 'success' => true ) );
107
108
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
109
110
		return $response;
111
	}
112
113
	/**
114
	 * WordPress loaded, check for deprecated webhook call.
115
	 *
116
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
117
	 * @return void
118
	 */
119
	public function wp_loaded() {
120
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
121
			return;
122
		}
123
124
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
125
126
		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...
127
	}
128
}
129