Failed Conditions
Push — develop ( d387e0...4c18bc )
by Remco
03:45
created

src/Client.php (4 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 Exception;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\XML\Security;
16
use WP_Error;
17
18
/**
19
 * Adyen client
20
 *
21
 * @author  Remco Tolsma
22
 * @version 1.0.0
23
 * @since   1.0.0
24
 * @link    https://github.com/adyenpayments/php/blob/master/generatepaymentform.php
25
 */
26
class Client {
27
	/**
28
	 * Config.
29
	 *
30
	 * @var Config
31
	 */
32
	private $config;
33
34
	/**
35
	 * Constructs and initializes an Adyen client object.
36
	 *
37
	 * @param Config $config Adyen config.
38
	 */
39 4
	public function __construct( Config $config ) {
40 4
		$this->config = $config;
41 4
	}
42
43
	/**
44
	 * Send request with the specified action and parameters
45
	 *
46
	 * @param string $method Adyen API method.
47
	 * @param object $data   Request data.
48
	 * @return object
49
	 * @throws Exception Throws exception when error occurs.
50
	 */
51 4
	private function send_request( $method, $data ) {
52
		// Request.
53 4
		$url = $this->config->get_api_url( $method );
54
55 4
		$response = wp_remote_request(
56 4
			$url,
57
			array(
58 4
				'method'  => 'POST',
59
				'headers' => array(
60 4
					'X-API-key'    => $this->config->get_api_key(),
61 4
					'Content-Type' => 'application/json',
62
				),
63 4
				'body'    => wp_json_encode( $data ),
64
			)
65
		);
66
67 4
		if ( $response instanceof WP_Error ) {
68 1
			throw new Exception( $response->get_error_message() );
69
		}
70
71
		// Body.
72 3
		$body = wp_remote_retrieve_body( $response );
73
74 3
		$data = json_decode( $body );
75
76 3
		if ( ! is_object( $data ) ) {
77
			$code = wp_remote_retrieve_response_code( $response );
78
79
			throw new Exception(
80
				sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
81
				$code
0 ignored issues
show
It seems like $code can also be of type string; however, parameter $code of Exception::__construct() does only seem to accept integer, 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

81
				/** @scrutinizer ignore-type */ $code
Loading history...
82
			);
83
		}
84
85
		// Error.
86 3
		if ( isset( $data->error ) ) {
87 1
			$error = Error::from_object( $data->error );
88
89 1
			throw $error;
90
		}
91
92
		// Service Exception.
93 2
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
94 1
			$service_exception = ServiceException::from_object( $data );
95
96 1
			throw $service_exception;
97
		}
98
99 1
		return $data;
100
	}
101
102
	/**
103
	 * Create payment.
104
	 *
105
	 * @param PaymentRequest $request Payment request.
106
	 *
107
	 * @return bool|object
108
	 */
109
	public function create_payment( PaymentRequest $request ) {
110
		return $this->send_request( 'payments/', $request->get_json() );
111
	}
112
113
	/**
114
	 * Create payment session.
115
	 *
116
	 * @param PaymentSessionRequest $request Payment session request.
117
	 *
118
	 * @return bool|object
119
	 */
120
	public function create_payment_session( PaymentSessionRequest $request ) {
121
		return $this->send_request( 'paymentSession', $request->get_json() );
122
	}
123
124
	/**
125
	 * Get payment details.
126
	 *
127
	 * @param string $payload Payload to get payment details for.
128
	 *
129
	 * @return bool|object
130
	 */
131
	public function get_payment_details( $payload ) {
132
		if ( empty( $payload ) ) {
133
			return false;
134
		}
135
136
		$data = array(
137
			'details' => array(
138
				'payload' => $payload,
139
			),
140
		);
141
142
		return $this->send_request( 'payments/details', $data );
0 ignored issues
show
$data of type array<string,array<string,string>> is incompatible with the type object expected by parameter $data of Pronamic\WordPress\Pay\G...\Client::send_request(). ( Ignorable by Annotation )

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

142
		return $this->send_request( 'payments/details', /** @scrutinizer ignore-type */ $data );
Loading history...
143
	}
144
145
	/**
146
	 * Get payment result.
147
	 *
148
	 * @param string $payload Payload to get payment details for.
149
	 *
150
	 * @return bool|object
151
	 */
152
	public function get_payment_result( $payload ) {
153
		if ( empty( $payload ) ) {
154
			return false;
155
		}
156
157
		$data = array(
158
			'payload' => $payload,
159
		);
160
161
		return $this->send_request( 'payments/result', $data );
0 ignored issues
show
$data of type array<string,string> is incompatible with the type object expected by parameter $data of Pronamic\WordPress\Pay\G...\Client::send_request(). ( Ignorable by Annotation )

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

161
		return $this->send_request( 'payments/result', /** @scrutinizer ignore-type */ $data );
Loading history...
162
	}
163
164
	/**
165
	 * Get issuers.
166
	 *
167
	 * @param string $payment_method Payment method.
168
	 *
169
	 * @return array|bool
170
	 */
171
	public function get_issuers( $payment_method = null ) {
172
		// Check payment method.
173
		if ( empty( $payment_method ) ) {
174
			return false;
175
		}
176
177
		// Get issuers.
178
		$methods = $this->get_payment_methods();
179
180
		if ( false === $methods ) {
181
			return false;
182
		}
183
184
		$issuers = array();
185
186
		foreach ( $methods as $method_type => $method ) {
187
			if ( $payment_method !== $method_type ) {
188
				continue;
189
			}
190
191
			if ( ! isset( $method['details']['issuer'] ) ) {
192
				return false;
193
			}
194
195
			foreach ( $method['details']['issuer']->items as $issuer ) {
196
				$id   = Security::filter( $issuer->id );
197
				$name = Security::filter( $issuer->name );
198
199
				$issuers[ $id ] = $name;
200
			}
201
		}
202
203
		return $issuers;
204
	}
205
206
	/**
207
	 * Get payment methods.
208
	 *
209
	 * @return array|bool
210
	 */
211 4
	public function get_payment_methods() {
212
		$data = array(
213 4
			'merchantAccount'       => $this->config->get_merchant_account(),
214
			'allowedPaymentMethods' => array(),
215
		);
216
217 4
		$response = $this->send_request( 'paymentMethods', $data );
0 ignored issues
show
$data of type array<string,array|string> is incompatible with the type object expected by parameter $data of Pronamic\WordPress\Pay\G...\Client::send_request(). ( Ignorable by Annotation )

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

217
		$response = $this->send_request( 'paymentMethods', /** @scrutinizer ignore-type */ $data );
Loading history...
218
219 1
		if ( false === $response ) {
220
			return false;
221
		}
222
223 1
		$payment_methods = array();
224
225 1
		if ( isset( $response->paymentMethods ) ) {
226 1
			foreach ( $response->paymentMethods as $payment_method ) {
227 1
				$type = Security::filter( $payment_method->type );
228 1
				$name = Security::filter( $payment_method->name );
229
230
				$method = array(
231 1
					'name'    => $name,
232
					'details' => array(),
233
				);
234
235 1
				if ( isset( $payment_method->details ) ) {
236 1
					foreach ( $payment_method->details as $detail ) {
237 1
						$key = $detail->key;
238
239 1
						$method['details'][ $key ] = $detail;
240
241 1
						unset( $method['details'][ $key ]->key );
242
					}
243
				}
244
245 1
				$payment_methods[ $type ] = $method;
246
			}
247
		}
248
249 1
		return $payment_methods;
250
	}
251
}
252