Failed Conditions
Push — develop ( 6cd57d...8c7357 )
by Remco
09:31
created

WebhookController::rest_api_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 2
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\Mollie
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
 * @link https://docs.mollie.com/guides/webhooks
20
 * @author  Remco Tolsma
21
 * @version 2.1.0
22
 * @since   2.1.0
23
 */
24
class WebhookController {
25
	/**
26
	 * Setup.
27
	 *
28
	 * @return void
29
	 */
30
	public function setup() {
31
		\add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
32
33
		\add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
34
	}
35
36
	/**
37
	 * REST API init.
38
	 *
39
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
40
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
41
	 * @return void
42
	 */
43
	public function rest_api_init() {
44
		\register_rest_route(
45
			Integration::REST_ROUTE_NAMESPACE,
46
			'/webhook',
47
			array(
48
				'methods'             => 'POST',
49
				'callback'            => array( $this, 'rest_api_omnikassa_2_webhook' ),
50
				'permission_callback' => '__return_true',
51
			)
52
		);
53
	}
54
55
	/**
56
	 * REST API OmniKassa 2.0 webhook handler.
57
	 *
58
	 * @param \WP_REST_Request $request Request.
59
	 * @return object
60
	 */
61
	public function rest_api_omnikassa_2_webhook( \WP_REST_Request $request ) {
62
		/**
63
		 * Result.
64
		 *
65
		 * @link https://developer.wordpress.org/reference/functions/wp_send_json_success/
66
		 */
67
		$response = \rest_ensure_response(
68
			array(
69
				'success' => true,
70
			)
71
		);
72
73
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
74
75
		// JSON.
76
		$json = $request->get_body();
77
78
		// Get notification from input data.
79
		try {
80
			$notification = Notification::from_json( $json );
81
		} catch ( \JsonSchema\Exception\ValidationException $e ) {
82
			// Invalid input data.
83
			return new \WP_Error(
84
				'rest_omnikassa_2_notification_invalid',
85
				\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ),
86
				array(
87
					'status'       => 400,
88
					'notification' => $json,
89
				)
90
			);
91
		}
92
93
		$query = new \WP_Query(
94
			array(
95
				'post_type'   => GatewayPostType::POST_TYPE,
96
				'post_status' => 'publish',
97
				'nopaging'    => true,
98
				'meta_query'  => array(
99
					array(
100
						'key'   => '_pronamic_gateway_id',
101
						'value' => 'rabobank-omnikassa-2',
102
					),
103
				),
104
			)
105
		);
106
107
		foreach ( $query->posts as $post ) {
108
			$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...
109
110
			if ( $gateway instanceof Gateway ) {
111
				try {
112
					$gateway->handle_notification( $notification );
113
				} catch ( \Exception $e ) {
114
					continue;
115
				}
116
			}
117
		}
118
119
		return $response;
120
	}
121
122
	/**
123
	 * WordPress loaded, check for deprecated webhook call.
124
	 *
125
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
126
	 * @return void
127
	 */
128
	public function wp_loaded() {
129
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
130
			return;
131
		}
132
133
		$json = \file_get_contents( 'php://input' );
134
135
		if ( empty( $json ) ) {
136
			return;
137
		}
138
139
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
140
141
		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...
142
	}
143
}
144