Test Failed
Push — develop ( f1e77e...745105 )
by Remco
05:28
created

rest_api_mollie_webhook_payment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 56
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 19
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 56
ccs 0
cts 9
cp 0
crap 12
rs 9.6333

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use Pronamic\WordPress\Pay\Plugin;
14
use WP_Error;
15
use WP_REST_Request;
16
use WP_REST_Response;
17
18
/**
19
 * Webhook controller
20
 *
21
 * @link https://docs.mollie.com/guides/webhooks
22
 *
23
 * @author  Remco Tolsma
24
 * @version 2.1.0
25
 * @since   2.1.0
26
 */
27
class WebhookController {
28
	/**
29
	 * Setup.
30
	 *
31
	 * @return void
32
	 */
33
	public function setup() {
34
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
35
36
		add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
37
	}
38
39
	/**
40
	 * REST API init.
41
	 *
42
	 * @link https://docs.mollie.com/overview/webhooks
43
	 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
44
	 * @link https://developer.wordpress.org/reference/hooks/rest_api_init/
45
	 *
46
	 * @return void
47
	 */
48
	public function rest_api_init() {
49
		\register_rest_route(
50
			Integration::REST_ROUTE_NAMESPACE,
51
			'/webhook',
52
			array(
53
				'methods'             => 'POST',
54
				'callback'            => array( $this, 'rest_api_mollie_webhook' ),
55
				'args'                => array(
56
					'id' => array(
57
						'description' => \__( 'Mollie transaction ID.', 'pronamic_ideal' ),
58
						'type'        => 'string',
59
						'required'    => true,
60
					),
61
				),
62
				'permission_callback' => '__return_true',
63
			)
64
		);
65
66
		\register_rest_route(
67
			Integration::REST_ROUTE_NAMESPACE,
68
			'/webhook/(?P<payment_id>\d+)',
69
			array(
70
				'methods'             => 'POST',
71
				'callback'            => array( $this, 'rest_api_mollie_webhook_payment' ),
72
				'args'                => array(
73
					'payment_id' => array(
74
						'description' => \__( 'Payment ID.', 'pronamic_ideal' ),
75
						'type'        => 'string',
76
						'required'    => true,
77
					),
78
					'id'         => array(
79
						'description' => \__( 'Mollie transaction ID.', 'pronamic_ideal' ),
80
						'type'        => 'string',
81
						'required'    => true,
82
					),
83
				),
84
				'permission_callback' => '__return_true',
85
			)
86
		);
87
	}
88
89
	/**
90
	 * REST API Mollie webhook handler.
91
	 *
92
	 * @param WP_REST_Request $request Request.
93
	 * @return object
94
	 */
95
	public function rest_api_mollie_webhook( WP_REST_Request $request ) {
96
		$id = $request->get_param( 'id' );
97
98
		if ( empty( $id ) ) {
99
			return $this->rest_api_mollie_webhook_payment( $request );
100
		}
101
102
		$payment = \get_pronamic_payment_by_transaction_id( $id );
103
104
		if ( null !== $payment ) {
105
			$request->set_param( 'payment_id', $payment->get_id() );
106
		}
107
108
		return $this->rest_api_mollie_webhook_payment( $request );
109
	}
110
111
	/**
112
	 * REST API Mollie webhook handler.
113
	 *
114
	 * @param WP_REST_Request $request Request.
115
	 * @return object
116
	 */
117
	public function rest_api_mollie_webhook_payment( WP_REST_Request $request ) {
118
		$id = $request->get_param( 'id' );
119
120
		/**
121
		 * Result.
122
		 *
123
		 * @link https://developer.wordpress.org/reference/functions/wp_send_json_success/
124
		 */
125
		$response = new WP_REST_Response(
126
			array(
127
				'success' => true,
128
				'id'      => $id,
129
			)
130
		);
131
132
		$response->add_link( 'self', rest_url( $request->get_route() ) );
133
134
		/**
135
		 * Payment.
136
		 */
137
		$payment_id = $request->get_param( 'payment_id' );
138
139
		if ( empty( $payment_id ) ) {
140
			return $response;
141
		}
142
143
		$payment = \get_pronamic_payment( $payment_id );
144
145
		if ( null === $payment ) {
146
			/**
147
			 * How to handle unknown IDs?
148
			 *
149
			 * To not leak any information to malicious third parties, it is recommended
150
			 * to return a 200 OK response even if the ID is not known to your system.
151
			 *
152
			 * @link https://docs.mollie.com/guides/webhooks#how-to-handle-unknown-ids
153
			 */
154
			return $response;
155
		}
156
157
		// Add note.
158
		$note = \sprintf(
159
			/* translators: %s: payment provider name */
160
			\__( 'Webhook requested by %s.', 'pronamic_ideal' ),
161
			\__( 'Mollie', 'pronamic_ideal' )
162
		);
163
164
		$payment->add_note( $note );
165
166
		// Log webhook request.
167
		\do_action( 'pronamic_pay_webhook_log_payment', $payment );
168
169
		// Update payment.
170
		Plugin::update_payment( $payment, false );
171
172
		return $response;
173
	}
174
175
	/**
176
	 * WordPress loaded, check for deprecated webhook call.
177
	 *
178
	 * @link https://github.com/WordPress/WordPress/blob/5.3/wp-includes/rest-api.php#L277-L309
179
	 * @return void
180
	 */
181
	public function wp_loaded() {
182
		if ( ! filter_has_var( INPUT_GET, 'mollie_webhook' ) ) {
183
			return;
184
		}
185
186
		if ( ! filter_has_var( INPUT_POST, 'id' ) ) {
187
			return;
188
		}
189
190
		\rest_get_server()->serve_request( '/pronamic-pay/mollie/v1/webhook' );
191
192
		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...
193
	}
194
}
195