Failed Conditions
Push — develop ( 7e43f4...28b9ae )
by Remco
05:54
created

src/WebhookController.php (1 issue)

Labels
Severity
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(
67
			array(
68
				'success' => true,
69
			)
70
		);
71
72
		$response->add_link( 'self', \rest_url( $request->get_route() ) );
73
74
		// JSON.
75
		$json = $request->get_body();
76
77
		// Get notification from input data.
78
		try {
79
			$notification = Notification::from_json( $json );
80
		} catch ( \JsonSchema\Exception\ValidationException $e ) {
81
			// Invalid input data.
82
			return new \WP_Error(
83
				'rest_omnikassa_2_notification_invalid',
84
				\__( 'Invalid OmniKassa 2.0 notification.', 'pronamic_ideal ' ),
85
				array(
86
					'status'       => 400,
87
					'notification' => $json,
88
				)
89
			);
90
		}
91
92
		$query = new \WP_Query(
93
			array(
94
				'post_type'   => GatewayPostType::POST_TYPE,
95
				'post_status' => 'publish',
96
				'nopaging'    => true,
97
				'meta_query'  => array(
98
					array(
99
						'key'   => '_pronamic_gateway_id',
100
						'value' => 'rabobank-omnikassa-2',
101
					),
102
				),
103
			)
104
		);
105
106
		foreach ( $query->posts as $post ) {
107
			$gateway = Plugin::get_gateway( $post->ID );
0 ignored issues
show
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...
108
109
			if ( $gateway instanceof Gateway ) {
110
				try {
111
					$gateway->handle_notification( $notification );
112
				} catch ( \Exception $e ) {
113
					continue;
114
				}
115
			}
116
		}
117
118
		return $response;
119
	}
120
121
	/**
122
	 * WordPress loaded, check for deprecated webhook call.
123
	 *
124
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
125
	 * @return void
126
	 */
127
	public function wp_loaded() {
128
		if ( ! \filter_has_var( \INPUT_GET, 'omnikassa2_webhook' ) ) {
129
			return;
130
		}
131
132
		\rest_get_server()->serve_request( '/pronamic-pay/omnikassa-2/v1/webhook' );
133
134
		exit;
135
	}
136
}
137