Failed Conditions
Push — develop ( 22f236...c0dbff )
by Remco
03:29
created

src/Client.php (2 issues)

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
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\XML\Security;
15
use WP_Error;
16
17
/**
18
 * Adyen client
19
 *
20
 * @author  Remco Tolsma
21
 * @version 1.0.0
22
 * @since   1.0.0
23
 * @link    https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
24
 */
25
class Client {
26
	/**
27
	 * Config.
28
	 *
29
	 * @var Config
30
	 */
31
	private $config;
32
33
	/**
34
	 * Error
35
	 *
36
	 * @var WP_Error
37
	 */
38
	private $error;
39
40
	/**
41
	 * Constructs and initializes an Adyen client object.
42
	 *
43
	 * @param Config $config Adyen config.
44
	 */
45
	public function __construct( Config $config ) {
46
		$this->config = $config;
47
	}
48
49
	/**
50
	 * Error
51
	 *
52
	 * @return WP_Error
53
	 */
54
	public function get_error() {
55
		return $this->error;
56
	}
57
58
	/**
59
	 * Send request with the specified action and parameters
60
	 *
61
	 * @param string       $end_point              Requested endpoint.
62
	 * @param string       $method                 HTTP method to use.
63
	 * @param array|object $data                   Request data.
64
	 * @param int          $expected_response_code Expected response code.
65
	 *
66
	 * @return bool|object
67
	 */
68
	private function send_request( $end_point, $method = 'GET', $data = null, $expected_response_code = 200 ) {
69
		// Request.
70
		$url = $this->config->get_api_url() . $end_point;
71
72
		$response = wp_remote_request(
73
			$url,
74
			array(
75
				'method'  => $method,
76
				'headers' => array(
77
					'X-API-key'    => $this->config->get_api_key(),
78
					'Content-Type' => 'application/json',
79
				),
80
				'body'    => wp_json_encode( $data ),
81
			)
82
		);
83
84
		// Response code.
85
		$response_code = wp_remote_retrieve_response_code( $response );
0 ignored issues
show
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_response_code() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
86
87
		if ( $expected_response_code != $response_code ) { // WPCS: loose comparison ok.
88
			$this->error = new WP_Error( 'adyen_error', 'Unexpected response code.' );
89
		}
90
91
		// Body.
92
		$body = wp_remote_retrieve_body( $response );
0 ignored issues
show
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_body() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
93
94
		$data = json_decode( $body );
95
96
		if ( ! is_object( $data ) ) {
97
			$this->error = new WP_Error( 'adyen_error', 'Could not parse response.' );
98
99
			return false;
100
		}
101
102
		// Adyen error.
103
		if ( isset( $data->errorCode, $data->message ) ) {
104
			$message = sprintf(
105
				'%1$s %2$s - %3$s',
106
				$data->status,
107
				$data->errorCode,
108
				$data->message
109
			);
110
111
			$this->error = new WP_Error( 'adyen_error', $message, $data->errorCode );
112
113
			return false;
114
		}
115
116
		return $data;
117
	}
118
119
	/**
120
	 * Create payment.
121
	 *
122
	 * @param PaymentRequest $request Payment request.
123
	 *
124
	 * @return bool|object
125
	 */
126
	public function create_payment( PaymentRequest $request ) {
127
		return $this->send_request( 'payments/', 'POST', $request->get_json(), 200 );
128
	}
129
130
	/**
131
	 * Create payment session.
132
	 *
133
	 * @param PaymentSessionRequest $request Payment session request.
134
	 *
135
	 * @return bool|object
136
	 */
137
	public function create_payment_session( PaymentSessionRequest $request ) {
138
		return $this->send_request( 'paymentSession', 'POST', $request->get_json(), 200 );
139
	}
140
141
	/**
142
	 * Get payments.
143
	 *
144
	 * @return bool|object
145
	 */
146
	public function get_payments() {
147
		return $this->send_request( 'payments/', 'GET' );
148
	}
149
150
	/**
151
	 * Get payment details.
152
	 *
153
	 * @param string $payload Payload to get payment details for.
154
	 *
155
	 * @return bool|object
156
	 */
157
	public function get_payment_details( $payload ) {
158
		if ( empty( $payload ) ) {
159
			return false;
160
		}
161
162
		$data = array(
163
			'details' => array(
164
				'payload' => $payload,
165
			),
166
		);
167
168
		return $this->send_request( 'payments/details', 'POST', $data );
169
	}
170
171
	/**
172
	 * Get payment result.
173
	 *
174
	 * @param string $payload Payload to get payment details for.
175
	 *
176
	 * @return bool|object
177
	 */
178
	public function get_payment_result( $payload ) {
179
		if ( empty( $payload ) ) {
180
			return false;
181
		}
182
183
		$data = array(
184
			'payload' => $payload,
185
		);
186
187
		return $this->send_request( 'payments/result', 'POST', $data );
188
	}
189
190
	/**
191
	 * Get issuers.
192
	 *
193
	 * @param string $payment_method Payment method.
194
	 *
195
	 * @return array|bool
196
	 */
197
	public function get_issuers( $payment_method = null ) {
198
		// Check payment method.
199
		if ( empty( $payment_method ) ) {
200
			return false;
201
		}
202
203
		// Get issuers.
204
		$methods = $this->get_payment_methods();
205
206
		if ( false === $methods ) {
207
			return false;
208
		}
209
210
		$issuers = array();
211
212
		foreach ( $methods as $method_type => $method ) {
213
			if ( $payment_method !== $method_type ) {
214
				continue;
215
			}
216
217
			if ( ! isset( $method['details']['issuer'] ) ) {
218
				return false;
219
			}
220
221
			foreach ( $method['details']['issuer']->items as $issuer ) {
222
				$id   = Security::filter( $issuer->id );
223
				$name = Security::filter( $issuer->name );
224
225
				$issuers[ $id ] = $name;
226
			}
227
		}
228
229
		return $issuers;
230
	}
231
232
	/**
233
	 * Get payment methods.
234
	 *
235
	 * @return array|bool
236
	 */
237
	public function get_payment_methods() {
238
		$data = array(
239
			'merchantAccount'       => $this->config->get_merchant_account(),
240
			'allowedPaymentMethods' => array(),
241
		);
242
243
		$response = $this->send_request( 'paymentMethods/', 'POST', $data );
244
245
		if ( false === $response ) {
246
			return false;
247
		}
248
249
		$payment_methods = array();
250
251
		if ( isset( $response->paymentMethods ) ) {
252
			foreach ( $response->paymentMethods as $payment_method ) {
253
				$type = Security::filter( $payment_method->type );
254
				$name = Security::filter( $payment_method->name );
255
256
				$method = array(
257
					'name'    => $name,
258
					'details' => array(),
259
				);
260
261
				if ( isset( $payment_method->details ) ) {
262
					foreach ( $payment_method->details as $detail ) {
263
						$key = $detail->key;
264
265
						$method['details'][ $key ] = $detail;
266
267
						unset( $method['details'][ $key ]->key );
268
					}
269
				}
270
271
				$payment_methods[ $type ] = $method;
272
			}
273
		}
274
275
		return $payment_methods;
276
	}
277
}
278