Passed
Push — master ( d4ad7c...cc3467 )
by Reüel
07:39 queued 12s
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Test Coverage

Coverage 98.33%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
eloc 58
c 11
b 0
f 0
dl 0
loc 179
ccs 59
cts 60
cp 0.9833
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
B send_request() 0 94 7
A get_payment_result() 0 4 1
A create_payment() 0 4 1
A get_payment_methods() 0 6 1
A create_payment_session() 0 4 1
1
<?php
2
/**
3
 * Adyen client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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 12
	public function __construct( Config $config ) {
36 12
		$this->config = $config;
37 12
	}
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
			throw new \Exception( $response->get_error_message() );
65
		}
66
67
		// Body.
68 10
		$body = \wp_remote_retrieve_body( $response );
69
70
		// Response.
71 10
		$response_code    = \wp_remote_retrieve_response_code( $response );
72 10
		$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 10
		if ( '' === $body ) {
84 1
			throw new \Exception(
85 1
				\sprintf(
86 1
					'Adyen response is empty, HTTP response: "%s %s".',
87 1
					$response_code,
88 1
					$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 1
			throw new \Exception(
101 1
				\sprintf(
102 1
					'Could not JSON decode Adyen response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".',
103 1
					$response_code,
104 1
					$response_message,
105 1
					\strlen( $body ),
106 1
					\json_last_error_msg()
107
				),
108
				$json_error
109
			);
110
		}
111
112
		// Object.
113 8
		if ( ! \is_object( $data ) ) {
114 1
			throw new \Exception(
115 1
				\sprintf(
116 1
					'Could not JSON decode Adyen response to an object, HTTP response: "%s %s", HTTP body: "%s".',
117 1
					$response_code,
118 1
					$response_message,
119 1
					$body
120
				),
121 1
				\intval( $response_code )
122
			);
123
		}
124
125
		// Error.
126 7
		if ( isset( $data->error ) ) {
127 2
			$error = Error::from_object( $data->error );
128
129 2
			throw $error;
130
		}
131
132
		// Service Exception.
133
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Adyen JSON object.
134 5
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
135 1
			$service_exception = ServiceException::from_object( $data );
136
137 1
			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( 'payments', $request );
154
155 1
		return PaymentResponse::from_object( $data );
156
	}
157
158
	/**
159
	 * Create payment session.
160
	 *
161
	 * @param PaymentSessionRequest $request Payment session request.
162
	 *
163
	 * @return PaymentSessionResponse
164
	 *
165
	 * @throws \Exception Throws error if request fails.
166
	 */
167 1
	public function create_payment_session( PaymentSessionRequest $request ) {
168 1
		$data = $this->send_request( 'paymentSession', $request );
169
170 1
		return PaymentSessionResponse::from_object( $data );
171
	}
172
173
	/**
174
	 * Get payment result.
175
	 *
176
	 * @param PaymentResultRequest $request Payment result request.
177
	 *
178
	 * @return PaymentResultResponse
179
	 *
180
	 * @throws \Exception Throws error if request fails.
181
	 */
182 2
	public function get_payment_result( PaymentResultRequest $request ) {
183 2
		$data = $this->send_request( 'payments/result', $request );
184
185 1
		return PaymentResultResponse::from_object( $data );
186
	}
187
188
	/**
189
	 * Get payment methods.
190
	 *
191
	 * @return PaymentMethodsResponse
192
	 *
193
	 * @throws \Exception Throws error if request fails.
194
	 */
195 7
	public function get_payment_methods() {
196 7
		$request = new PaymentMethodsRequest( $this->config->get_merchant_account() );
197
198 7
		$data = $this->send_request( 'paymentMethods', $request );
199
200 1
		return PaymentMethodsResponse::from_object( $data );
201
	}
202
}
203