Failed Conditions
Push — master ( cc3467...84f509 )
by Reüel
09:31 queued 01:25
created

PaymentsController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Test Coverage

Coverage 14.72%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 150
c 5
b 0
f 0
dl 0
loc 316
ccs 19
cts 129
cp 0.1472
rs 10
wmc 28

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 2 1
A rest_api_init() 0 28 1
C rest_api_adyen_payment_details() 0 131 14
C rest_api_adyen_payments() 0 119 12
1
<?php
2
/**
3
 * Payments 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\Adyen
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Adyen;
12
13
use JsonSchema\Exception\ValidationException;
14
use Pronamic\WordPress\Pay\Plugin;
15
use WP_REST_Request;
16
17
/**
18
 * Payments result controller
19
 *
20
 * @link https://docs.adyen.com/developers/checkout/web-sdk/customization/logic#beforecomplete
21
 *
22
 * @author  Reüel van der Steege
23
 * @version 1.1.0
24
 * @since   1.1.0
25
 */
26
class PaymentsController {
27
	/**
28
	 * Setup.
29
	 *
30
	 * @return void
31
	 */
32 5
	public function setup() {
33 5
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
34 5
	}
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
	 *
42
	 * @return void
43
	 */
44 10
	public function rest_api_init() {
45
		// Register REST route `/payments//{payment_id}`.
46 10
		register_rest_route(
47 10
			Integration::REST_ROUTE_NAMESPACE,
48 10
			'/payments/(?P<payment_id>\d+)',
49
			array(
50 10
				'methods'  => 'POST',
51 10
				'callback' => array( $this, 'rest_api_adyen_payments' ),
52
				'args'     => array(
53
					'payment_id' => array(
54 10
						'description' => __( 'Payment ID.', 'pronamic_ideal' ),
55 10
						'type'        => 'integer',
56
					),
57
				),
58
			)
59
		);
60
61
		// Register REST route `/payments/details/{payment_id}`.
62 10
		register_rest_route(
63 10
			Integration::REST_ROUTE_NAMESPACE,
64 10
			'/payments/details/(?P<payment_id>\d+)',
65
			array(
66 10
				'methods'  => 'POST',
67 10
				'callback' => array( $this, 'rest_api_adyen_payment_details' ),
68
				'args'     => array(
69
					'payment_id' => array(
70 10
						'description' => __( 'Payment ID.', 'pronamic_ideal' ),
71 10
						'type'        => 'integer',
72
					),
73
				),
74
			)
75
		);
76 10
	}
77
78
	/**
79
	 * REST API Adyen payments handler.
80
	 *
81
	 * @param WP_REST_Request $request Request.
82
	 * @return object
83
	 */
84
	public function rest_api_adyen_payments( WP_REST_Request $request ) {
85
		$payment_id = $request->get_param( 'payment_id' );
86
87
		// Payment ID.
88
		if ( null === $payment_id ) {
89
			return new \WP_Error(
90
				'pronamic-pay-adyen-no-payment-id',
91
				__( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' )
92
			);
93
		}
94
95
		$payment = \get_pronamic_payment( $payment_id );
96
97
		if ( null === $payment ) {
98
			return new \WP_Error(
99
				'pronamic-pay-adyen-payment-not-found',
100
				sprintf(
101
					/* translators: %s: payment ID */
102
					__( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ),
103
					$payment_id
104
				),
105
				$payment_id
106
			);
107
		}
108
109
		// State data.
110
		$data = \json_decode( $request->get_body() );
111
112
		if ( null === $data ) {
113
			return new \WP_Error(
114
				'pronamic-pay-adyen-no-data',
115
				__( 'No data given in request body.', 'pronamic_ideal' )
116
			);
117
		}
118
119
		// Gateway.
120
		$config_id = $payment->get_config_id();
121
122
		if ( null === $config_id ) {
123
			return new \WP_Error(
124
				'pronamic-pay-adyen-no-config',
125
				__( 'No gateway configuration ID given in payment.', 'pronamic_ideal' )
126
			);
127
		}
128
129
		$gateway = Plugin::get_gateway( $config_id );
130
131
		if ( empty( $gateway ) ) {
132
			return new \WP_Error(
133
				'pronamic-pay-adyen-gateway-not-found',
134
				sprintf(
135
					/* translators: %s: Gateway configuration ID */
136
					__( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ),
137
					$config_id
138
				),
139
				$config_id
140
			);
141
		}
142
143
		if ( ! isset( $gateway->client ) ) {
144
			return new \WP_Error(
145
				'pronamic-pay-adyen-client-not-found',
146
				sprintf(
147
					/* translators: %s: Gateway configuration ID */
148
					__( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ),
149
					$config_id
150
				),
151
				$config_id
152
			);
153
		}
154
155
		// Create payment.
156
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
157
		if ( ! isset( $data->paymentMethod->type ) ) {
158
			return new \WP_Error(
159
				'pronamic-pay-adyen-no-payment-method',
160
				__( 'No payment method given.', 'pronamic_ideal' )
161
			);
162
		}
163
164
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
165
		$payment_method = PaymentMethod::from_object( $data->paymentMethod );
166
167
		try {
168
			if ( ! \is_callable( array( $gateway, 'create_payment' ) ) ) {
169
				return (object) array(
170
					'error' => __( 'Gateway does not support method to create payment.', 'pronamic_ideal' ),
171
				);
172
			}
173
174
			$response = $gateway->create_payment( $payment, $payment_method );
175
		} catch ( \Exception $e ) {
176
			return (object) array(
177
				'error' => $e->getMessage(),
178
			);
179
		}
180
181
		// Update payment status based on response.
182
		PaymentResponseHelper::update_payment( $payment, $response );
183
184
		$result = array(
185
			'resultCode' => $response->get_result_code(),
186
		);
187
188
		// Return action if available.
189
		$action = $response->get_action();
190
191
		if ( null !== $action ) {
192
			$result['action'] = $action->get_json();
193
		}
194
195
		// Return refusal reason if available.
196
		$refusal_reason = $response->get_refusal_reason();
197
198
		if ( null !== $refusal_reason ) {
199
			$result['refusalReason'] = $refusal_reason;
200
		}
201
202
		return (object) $result;
203
	}
204
205
	/**
206
	 * REST API Adyen payment details handler.
207
	 *
208
	 * @param WP_REST_Request $request Request.
209
	 * @return object
210
	 */
211
	public function rest_api_adyen_payment_details( WP_REST_Request $request ) {
212
		$payment_id = $request->get_param( 'payment_id' );
213
214
		// Payment ID.
215
		if ( null === $payment_id ) {
216
			return new \WP_Error(
217
				'pronamic-pay-adyen-no-payment-id',
218
				__( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' )
219
			);
220
		}
221
222
		$payment = \get_pronamic_payment( $payment_id );
223
224
		if ( null === $payment ) {
225
			return new \WP_Error(
226
				'pronamic-pay-adyen-payment-not-found',
227
				sprintf(
228
					/* translators: %s: payment ID */
229
					__( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ),
230
					$payment_id
231
				),
232
				$payment_id
233
			);
234
		}
235
236
		// State data.
237
		$data = \json_decode( $request->get_body() );
238
239
		if ( null === $data ) {
240
			return new \WP_Error(
241
				'pronamic-pay-adyen-no-data',
242
				__( 'No data given in request body.', 'pronamic_ideal' )
243
			);
244
		}
245
246
		// Gateway.
247
		$config_id = $payment->get_config_id();
248
249
		if ( null === $config_id ) {
250
			return new \WP_Error(
251
				'pronamic-pay-adyen-no-config',
252
				__( 'No gateway configuration ID given in payment.', 'pronamic_ideal' )
253
			);
254
		}
255
256
		$gateway = Plugin::get_gateway( $config_id );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $gateway is correct as Pronamic\WordPress\Pay\P...get_gateway($config_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...
257
258
		if ( empty( $gateway ) ) {
259
			return new \WP_Error(
260
				'pronamic-pay-adyen-gateway-not-found',
261
				sprintf(
262
					/* translators: %s: Gateway configuration ID */
263
					__( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ),
264
					$config_id
265
				),
266
				$config_id
267
			);
268
		}
269
270
		if ( ! isset( $gateway->client ) ) {
271
			return new \WP_Error(
272
				'pronamic-pay-adyen-client-not-found',
273
				sprintf(
274
					/* translators: %s: Gateway configuration ID */
275
					__( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ),
276
					$config_id
277
				),
278
				$config_id
279
			);
280
		}
281
282
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
283
		if ( ! isset( $data->paymentMethod->type ) ) {
284
			return new \WP_Error(
285
				'pronamic-pay-adyen-no-payment-method',
286
				__( 'No payment method given.', 'pronamic_ideal' )
287
			);
288
		}
289
290
		// Send additional payment details.
291
		$payment_details_request = new PaymentDetailsRequest();
292
293
		// Set payment data from original payment response.
294
		$payment_response = $payment->get_meta( 'adyen_payment_response' );
295
296
		if ( is_string( $payment_response ) && '' !== $payment_response ) {
297
			$payment_response = \json_decode( $payment_response );
298
299
			$payment_response = PaymentResponse::from_object( $payment_response );
300
301
			$payment_data = $payment_response->get_payment_data();
302
303
			$payment_details_request->set_payment_data( $payment_data );
304
		}
305
306
		try {
307
			if ( ! \is_callable( array( $gateway, 'send_payment_details' ) ) ) {
308
				return (object) array(
309
					'error' => __( 'Gateway does not support sending additional payment details.', 'pronamic_ideal' ),
310
				);
311
			}
312
313
			$response = $gateway->send_payment_details( $payment_details_request );
314
315
			// Update payment status based on response.
316
			PaymentResponseHelper::update_payment( $payment, $response );
317
		} catch ( \Exception $e ) {
318
			return (object) array(
319
				'error' => $e->getMessage(),
320
			);
321
		}
322
323
		$result = array(
324
			'resultCode' => $response->get_result_code(),
325
		);
326
327
		// Return action if available.
328
		$action = $response->get_action();
329
330
		if ( null !== $action ) {
331
			$result['action'] = $action->get_json();
332
		}
333
334
		// Return refusal reason if available.
335
		$refusal_reason = $response->get_refusal_reason();
336
337
		if ( null !== $refusal_reason ) {
338
			$result['refusalReason'] = $refusal_reason;
339
		}
340
341
		return (object) $result;
342
	}
343
}
344