Failed Conditions
Push — develop ( 04266c...d387e0 )
by Remco
03:08
created

src/Client.php (6 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 3
	public function __construct( Config $config ) {
40 3
		$this->config = $config;
41 3
	}
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 3
	private function send_request( $method, $data ) {
52
		// Request.
53 3
		$url = $this->config->get_api_url( $method );
54
55 3
		$response = wp_remote_request(
56 3
			$url,
57
			array(
58 3
				'method'  => 'POST',
59
				'headers' => array(
60 3
					'X-API-key'    => $this->config->get_api_key(),
61 3
					'Content-Type' => 'application/json',
62
				),
63 3
				'body'    => wp_json_encode( $data ),
64
			)
65
		);
66
67 3
		if ( $response instanceof WP_Error ) {
68 1
			throw new Exception( $response->get_error_message() );
69
		}
70
71
		// Body.
72 2
		$body = wp_remote_retrieve_body( $response );
73
74 2
		$data = json_decode( $body );
75
76 2
		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 2
		if ( isset( $data->error ) ) {
87 1
			$error = Error::from_object( $data->error );
88
89 1
			throw $error;
90
		}
91
92
		// Adyen error.
93 1
		if ( isset( $data->errorCode, $data->message ) ) {
94
			$message = sprintf(
95
				'%1$s %2$s - %3$s',
96
				$data->status,
97
				$data->errorCode,
98
				$data->message
99
			);
100
101
			$this->error = new WP_Error( 'adyen_error', $message, $data->errorCode );
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
102
103
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type object.
Loading history...
104
		}
105
106 1
		return $data;
107
	}
108
109
	/**
110
	 * Create payment.
111
	 *
112
	 * @param PaymentRequest $request Payment request.
113
	 *
114
	 * @return bool|object
115
	 */
116
	public function create_payment( PaymentRequest $request ) {
117
		return $this->send_request( 'payments/', $request->get_json() );
118
	}
119
120
	/**
121
	 * Create payment session.
122
	 *
123
	 * @param PaymentSessionRequest $request Payment session request.
124
	 *
125
	 * @return bool|object
126
	 */
127
	public function create_payment_session( PaymentSessionRequest $request ) {
128
		return $this->send_request( 'paymentSession', $request->get_json() );
129
	}
130
131
	/**
132
	 * Get payment details.
133
	 *
134
	 * @param string $payload Payload to get payment details for.
135
	 *
136
	 * @return bool|object
137
	 */
138
	public function get_payment_details( $payload ) {
139
		if ( empty( $payload ) ) {
140
			return false;
141
		}
142
143
		$data = array(
144
			'details' => array(
145
				'payload' => $payload,
146
			),
147
		);
148
149
		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

149
		return $this->send_request( 'payments/details', /** @scrutinizer ignore-type */ $data );
Loading history...
150
	}
151
152
	/**
153
	 * Get payment result.
154
	 *
155
	 * @param string $payload Payload to get payment details for.
156
	 *
157
	 * @return bool|object
158
	 */
159
	public function get_payment_result( $payload ) {
160
		if ( empty( $payload ) ) {
161
			return false;
162
		}
163
164
		$data = array(
165
			'payload' => $payload,
166
		);
167
168
		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

168
		return $this->send_request( 'payments/result', /** @scrutinizer ignore-type */ $data );
Loading history...
169
	}
170
171
	/**
172
	 * Get issuers.
173
	 *
174
	 * @param string $payment_method Payment method.
175
	 *
176
	 * @return array|bool
177
	 */
178
	public function get_issuers( $payment_method = null ) {
179
		// Check payment method.
180
		if ( empty( $payment_method ) ) {
181
			return false;
182
		}
183
184
		// Get issuers.
185
		$methods = $this->get_payment_methods();
186
187
		if ( false === $methods ) {
188
			return false;
189
		}
190
191
		$issuers = array();
192
193
		foreach ( $methods as $method_type => $method ) {
194
			if ( $payment_method !== $method_type ) {
195
				continue;
196
			}
197
198
			if ( ! isset( $method['details']['issuer'] ) ) {
199
				return false;
200
			}
201
202
			foreach ( $method['details']['issuer']->items as $issuer ) {
203
				$id   = Security::filter( $issuer->id );
204
				$name = Security::filter( $issuer->name );
205
206
				$issuers[ $id ] = $name;
207
			}
208
		}
209
210
		return $issuers;
211
	}
212
213
	/**
214
	 * Get payment methods.
215
	 *
216
	 * @return array|bool
217
	 */
218 3
	public function get_payment_methods() {
219
		$data = array(
220 3
			'merchantAccount'       => $this->config->get_merchant_account(),
221
			'allowedPaymentMethods' => array(),
222
		);
223
224 3
		$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

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