Failed Conditions
Push — develop ( 475ba3...fad11a )
by Remco
04:36
created

Client::send_request()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 94
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.9449

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 7
eloc 46
c 6
b 0
f 0
nc 7
nop 2
dl 0
loc 94
ccs 25
cts 44
cp 0.5682
crap 10.9449
rs 8.2448

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
 * Adyen client
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
/**
14
 * Adyen client
15
 *
16
 * @link https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
17
 *
18
 * @author  Remco Tolsma
19
 * @version 1.0.5
20
 * @since   1.0.0
21
 */
22
class Client {
23
	/**
24
	 * Config.
25
	 *
26
	 * @var Config
27
	 */
28
	private $config;
29
30
	/**
31
	 * Constructs and initializes an Adyen client object.
32
	 *
33
	 * @param Config $config Adyen config.
34
	 */
35 13
	public function __construct( Config $config ) {
36 13
		$this->config = $config;
37 13
	}
38
39
	/**
40
	 * Send request with the specified action and parameters
41
	 *
42
	 * @param string  $method  Adyen API method.
43
	 * @param Request $request Request object.
44
	 * @return object
45
	 * @throws \Exception Throws exception when error occurs.
46
	 */
47 11
	private function send_request( $method, $request ) {
48
		// Request.
49 11
		$url = $this->config->get_api_url( $method );
50
51 10
		$response = \wp_remote_request(
52 10
			$url,
53
			array(
54 10
				'method'  => 'POST',
55
				'headers' => array(
56 10
					'X-API-key'    => $this->config->get_api_key(),
57 10
					'Content-Type' => 'application/json',
58
				),
59 10
				'body'    => \wp_json_encode( $request->get_json() ),
60
			)
61
		);
62
63 10
		if ( $response instanceof \WP_Error ) {
64 1
			throw new \Exception( $response->get_error_message() );
65
		}
66
67
		// Body.
68 9
		$body = \wp_remote_retrieve_body( $response );
69
70
		// Response.
71 9
		$response_code    = \wp_remote_retrieve_response_code( $response );
72 9
		$response_message = \wp_remote_retrieve_response_message( $response );
73
74
		/**
75
		 * On PHP 7 or higher the `json_decode` function will return `null` and
76
		 * `json_last_error` will return `4` (Syntax error). On PHP 5.6 or lower
77
		 * the `json_decode` will also return `null`, but json_last_error` will
78
		 * return `0` (No error). Therefor we check if the HTTP response body
79
		 * is an empty string.
80
		 *
81
		 * @link https://3v4l.org/
82
		 */
83 9
		if ( '' === $body ) {
84
			throw new \Exception(
85
				\sprintf(
86
					'Adyen response is empty, HTTP response: "%s %s".',
87
					$response_code,
88
					$response_message
89
				)
90
			);
91
		}
92
93
		// JSON.
94 9
		$data = json_decode( $body );
95
96
		// JSON error.
97 9
		$json_error = json_last_error();
98
99 9
		if ( \JSON_ERROR_NONE !== $json_error ) {
100
			throw new \Exception(
101
				\sprintf(
102
					'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".',
103
					$response_code,
104
					$response_message,
105
					\strlen( $body ),
106
					\json_last_error_msg()
107
				),
108
				$json_error
109
			);
110
		}
111
112
		// Object.
113 9
		if ( ! \is_object( $data ) ) {
114
			throw new \Exception(
115
				\sprintf(
116
					'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".',
117
					$response_code,
118
					$response_message,
119
					$body
120
				),
121
				\intval( $response_code )
122
			);
123
		}
124
125
		// Error.
126 9
		if ( isset( $data->error ) ) {
127 1
			$error = Error::from_object( $data->error );
128
129 1
			throw $error;
130
		}
131
132
		// Service Exception.
133
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
134 8
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
135 4
			$service_exception = ServiceException::from_object( $data );
136
137 4
			throw $service_exception;
138
		}
139
140 4
		return $data;
141
	}
142
143
	/**
144
	 * Create payment.
145
	 *
146
	 * @param PaymentRequest $request Payment request.
147
	 *
148
	 * @return PaymentResponse
149
	 *
150
	 * @throws \Exception Throws error if request fails.
151
	 */
152 1
	public function create_payment( PaymentRequest $request ) {
153 1
		$data = $this->send_request( 'v51/payments', $request );
154
155 1
		return PaymentResponse::from_object( $data );
156
	}
157
158
	/**
159
	 * Submit additional payment details.
160
	 *
161
	 * @param PaymentDetailsRequest $request Payment request.
162
	 *
163
	 * @return PaymentResponse
164
	 *
165
	 * @throws \Exception Throws error if request fails.
166
	 */
167
	public function request_payment_details( PaymentDetailsRequest $request ) {
168
		$data = $this->send_request( 'v51/payments/details', $request );
169
170
		return PaymentResponse::from_object( $data );
171
	}
172
173
	/**
174
	 * Create payment session.
175
	 *
176
	 * @param PaymentSessionRequest $request Payment session request.
177
	 *
178
	 * @return PaymentSessionResponse
179
	 *
180
	 * @throws \Exception Throws error if request fails.
181
	 */
182 1
	public function create_payment_session( PaymentSessionRequest $request ) {
183 1
		$data = $this->send_request( 'v41/paymentSession', $request );
184
185 1
		return PaymentSessionResponse::from_object( $data );
186
	}
187
188
	/**
189
	 * Get payment result.
190
	 *
191
	 * @param PaymentResultRequest $request Payment result request.
192
	 *
193
	 * @return PaymentResultResponse
194
	 *
195
	 * @throws \Exception Throws error if request fails.
196
	 */
197 2
	public function get_payment_result( PaymentResultRequest $request ) {
198 2
		$data = $this->send_request( 'v41/payments/result', $request );
199
200 1
		return PaymentResultResponse::from_object( $data );
201
	}
202
203
	/**
204
	 * Get payment methods.
205
	 *
206
	 * @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v51/paymentMethods
207
	 * @link https://docs.adyen.com/checkout/drop-in-web#step-1-get-available-payment-methods
208
	 *
209
	 * @param PaymentMethodsRequest $request Payment methods request.
210
	 *
211
	 * @return PaymentMethodsResponse
212
	 * @throws \Exception Throws error if request fails.
213
	 */
214 7
	public function get_payment_methods( PaymentMethodsRequest $request ) {
215 7
		$data = $this->send_request( 'v51/paymentMethods', $request );
216
217 1
		return PaymentMethodsResponse::from_object( $data );
218
	}
219
}
220