Test Failed
Push — develop ( ee5b51...397f1c )
by Reüel
05:20
created

PaymentsController::rest_api_adyen_payments()   B

Complexity

Conditions 10
Paths 12

Size

Total Lines 98
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 54
nc 12
nop 1
dl 0
loc 98
rs 7.1369
c 1
b 0
f 0

How to fix   Long Method    Complexity   

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
 * 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
	public function setup() {
33
		add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
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
	 *
42
	 * @return void
43
	 */
44
	public function rest_api_init() {
45
		register_rest_route(
46
			Integration::REST_ROUTE_NAMESPACE,
47
			'/payments/(?P<payment_id>\d+)',
48
			array(
49
				'methods'  => 'POST',
50
				'callback' => array( $this, 'rest_api_adyen_payments' ),
51
				'args'     => array(
52
					'payment_id'  => array(
53
						'description' => __( 'Payment ID.', 'pronamic_ideal' ),
54
						'type'        => 'integer',
55
					),
56
					'data'    => array(
57
						'description' => __( 'State data.', 'pronamic_ideal' ),
58
						'type'        => 'object',
59
					),
60
				),
61
			)
62
		);
63
	}
64
65
	/**
66
	 * REST API Adyen payments handler.
67
	 *
68
	 * @param WP_REST_Request $request Request.
69
	 * @return object
70
	 */
71
	public function rest_api_adyen_payments( WP_REST_Request $request ) {
72
		$payment_id = $request->get_param( 'payment_id' );
73
74
		// Payment ID.
75
		if ( null === $payment_id ) {
76
			return new \WP_Error(
77
				'pronamic-pay-adyen-no-payment-id',
78
				__( 'No payment ID given in `payment_id` parameter.', 'pronamic_ideal' )
79
			);
80
		}
81
82
		$payment = \get_pronamic_payment( $payment_id );
83
84
		if ( null === $payment ) {
85
			return new \WP_Error(
86
				'pronamic-pay-adyen-payment-not-found',
87
				sprintf(
88
					/* translators: %s: payment ID */
89
					__( 'Could not find payment with ID `%s`.', 'pronamic_ideal' ),
90
					$payment_id
91
				),
92
				$payment_id
93
			);
94
		}
95
96
		// State data.
97
		$data = \json_decode( \file_get_contents( 'php://input' ) );
98
99
		if ( null === $data ) {
100
			return new \WP_Error(
101
				'pronamic-pay-adyen-no-data',
102
				__( 'No state data given in request body.', 'pronamic_ideal' )
103
			);
104
		}
105
106
		// Gateway.
107
		$config_id = $payment->get_config_id();
108
109
		$gateway   = Plugin::get_gateway( $config_id );
110
111
		if ( empty( $gateway ) ) {
112
			return new \WP_Error(
113
				'pronamic-pay-adyen-gateway-not-found',
114
				sprintf(
115
					/* translators: %s: Gateway configuration ID */
116
					__( 'Could not find gateway with ID `%s`.', 'pronamic_ideal' ),
117
					$config_id
118
				),
119
				$config_id
120
			);
121
		}
122
123
		if ( ! isset( $gateway->client ) ) {
124
			return new \WP_Error(
125
				'pronamic-pay-adyen-client-not-found',
126
				sprintf(
127
					/* translators: %s: Gateway configuration ID */
128
					__( 'Could not find client in gateway with ID `%s`.', 'pronamic_ideal' ),
129
					$config_id
130
				),
131
				$config_id
132
			);
133
		}
134
135
		// Create payment.
136
		if ( ! isset( $data->paymentMethod->type ) ) {
137
			return new \WP_Error(
138
				'pronamic-pay-adyen-no-payment-method',
139
				__( 'No payment method given.', 'pronamic_ideal' )
140
			);
141
		}
142
143
		switch ( $data->paymentMethod->type ) {
144
			case PaymentMethodType::DIRECT_DEBIT:
145
				$payment_method = new PaymentMethodSepaDirectDebit( $data->paymentMethod->type, $data->paymentMethod->{'sepa.ibanNumber'}, $data->paymentMethod->{'sepa.ownerName'} );
146
147
				break;
148
			case PaymentMethodType::IDEAL:
149
				$payment_method = new PaymentMethodIDeal( $data->paymentMethod->type, $data->paymentMethod->issuer );
150
151
				break;
152
			default:
153
				$payment_method = PaymentMethod::from_object( $data->paymentMethod );
154
		}
155
156
		$response = $gateway->create_payment( $payment, $payment_method );
157
158
		// Return action if available.
159
		$action = $response->get_action();
160
161
		if ( null !== $action ) {
162
			return (object) array(
163
				'action' => $action->get_json(),
164
			);
165
		}
166
167
		return (object) array(
168
			'resultCode' => $response->get_result_code(),
169
		);
170
	}
171
}
172