Failed Conditions
Push — develop ( cede1f...576ba7 )
by Remco
03:30
created

Client::get_issuers()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 33
ccs 0
cts 23
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 7
nop 1
crap 56
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 $method Adyen API method.
62
	 * @param object $data   Request data.
63
	 * @return object
64
	 */
65
	private function send_request( $method, $data ) {
66
		// Request.
67
		$url = $this->config->get_api_url( $method );
68
69
		$response = wp_remote_request(
70
			$url,
71
			array(
72
				'method'  => 'POST',
73
				'headers' => array(
74
					'X-API-key'    => $this->config->get_api_key(),
75
					'Content-Type' => 'application/json',
76
				),
77
				'body'    => wp_json_encode( $data ),
78
			)
79
		);
80
81
		// Body.
82
		$body = wp_remote_retrieve_body( $response );
0 ignored issues
show
Bug introduced by
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

82
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
83
84
		$data = json_decode( $body );
85
86
		if ( ! is_object( $data ) ) {
87
			$this->error = new WP_Error( 'adyen_error', 'Could not parse response.' );
88
89
			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...
90
		}
91
92
		// Adyen error.
93
		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 );
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
		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
Bug introduced by
$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
Bug introduced by
$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
	public function get_payment_methods() {
219
		$data = array(
220
			'merchantAccount'       => $this->config->get_merchant_account(),
221
			'allowedPaymentMethods' => array(),
222
		);
223
224
		$response = $this->send_request( 'paymentMethods/', $data );
0 ignored issues
show
Bug introduced by
$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
		if ( false === $response ) {
0 ignored issues
show
introduced by
The condition false === $response is always false.
Loading history...
227
			return false;
228
		}
229
230
		$payment_methods = array();
231
232
		if ( isset( $response->paymentMethods ) ) {
233
			foreach ( $response->paymentMethods as $payment_method ) {
234
				$type = Security::filter( $payment_method->type );
235
				$name = Security::filter( $payment_method->name );
236
237
				$method = array(
238
					'name'    => $name,
239
					'details' => array(),
240
				);
241
242
				if ( isset( $payment_method->details ) ) {
243
					foreach ( $payment_method->details as $detail ) {
244
						$key = $detail->key;
245
246
						$method['details'][ $key ] = $detail;
247
248
						unset( $method['details'][ $key ]->key );
249
					}
250
				}
251
252
				$payment_methods[ $type ] = $method;
253
			}
254
		}
255
256
		return $payment_methods;
257
	}
258
}
259