Failed Conditions
Push — develop ( 86f40d...9dbbf7 )
by Remco
03:47
created

src/Client.php (1 issue)

Labels
Severity
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 6
	public function __construct( Config $config ) {
40 6
		$this->config = $config;
41 6
	}
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 6
	private function send_request( $method, $data ) {
52
		// Request.
53 6
		$url = $this->config->get_api_url( $method );
54
55 6
		$response = wp_remote_request(
56 6
			$url,
57
			array(
58 6
				'method'  => 'POST',
59
				'headers' => array(
60 6
					'X-API-key'    => $this->config->get_api_key(),
61 6
					'Content-Type' => 'application/json',
62
				),
63 6
				'body'    => wp_json_encode( $data ),
64
			)
65
		);
66
67 6
		if ( $response instanceof WP_Error ) {
68 1
			throw new Exception( $response->get_error_message() );
69
		}
70
71
		// Body.
72 5
		$body = wp_remote_retrieve_body( $response );
73
74 5
		$data = json_decode( $body );
75
76
		// JSON error.
77 5
		$json_error = json_last_error();
78
79 5
		if ( JSON_ERROR_NONE !== $json_error ) {
80
			throw new Exception(
81
				json_last_error_msg(),
82
				$json_error
83
			);
84
		}
85
86
		// Object.
87 5
		if ( ! is_object( $data ) ) {
88
			$code = wp_remote_retrieve_response_code( $response );
89
90
			throw new Exception(
91
				sprintf( 'Could not JSON decode Adyen response to an object (HTTP Status Code: %s).', $code ),
92
				intval( $code )
93
			);
94
		}
95
96
		// Error.
97 5
		if ( isset( $data->error ) ) {
98 1
			$error = Error::from_object( $data->error );
99
100 1
			throw $error;
101
		}
102
103
		// Service Exception.
104 4
		if ( isset( $data->status, $data->errorCode, $data->message, $data->errorType ) ) {
105 1
			$service_exception = ServiceException::from_object( $data );
106
107 1
			throw $service_exception;
108
		}
109
110 3
		return $data;
111
	}
112
113
	/**
114
	 * Create payment.
115
	 *
116
	 * @param PaymentRequest $request Payment request.
117
	 * @return PaymentResponse
118
	 */
119 1
	public function create_payment( PaymentRequest $request ) {
120 1
		$data = $this->send_request( 'payments', $request->get_json() );
121
122 1
		return PaymentResponse::from_object( $data );
123
	}
124
125
	/**
126
	 * Create payment session.
127
	 *
128
	 * @param PaymentSessionRequest $request Payment session request.
129
	 * @return PaymentSessionResponse
130
	 */
131 1
	public function create_payment_session( PaymentSessionRequest $request ) {
132 1
		$data = $this->send_request( 'paymentSession', $request->get_json() );
133
134 1
		return PaymentSessionResponse::from_object( $data );
135
	}
136
137
	/**
138
	 * Get payment result.
139
	 *
140
	 * @param PaymentResultRequest $request Payment result request.
141
	 * @return PaymentResultResponse
142
	 */
143
	public function get_payment_result( PaymentResultRequest $request ) {
144
		$data = $this->send_request( 'payments/result', $request->get_json() );
145
146
		return PaymentResultResponse::from_object( $data );
147
	}
148
149
	/**
150
	 * Get issuers.
151
	 *
152
	 * @param string $payment_method Payment method.
153
	 *
154
	 * @return array|bool
155
	 */
156
	public function get_issuers( $payment_method = null ) {
157
		// Check payment method.
158
		if ( empty( $payment_method ) ) {
159
			return false;
160
		}
161
162
		// Get issuers.
163
		$methods = $this->get_payment_methods();
164
165
		if ( false === $methods ) {
166
			return false;
167
		}
168
169
		$issuers = array();
170
171
		foreach ( $methods as $method_type => $method ) {
172
			if ( $payment_method !== $method_type ) {
173
				continue;
174
			}
175
176
			if ( ! isset( $method['details']['issuer'] ) ) {
177
				return false;
178
			}
179
180
			foreach ( $method['details']['issuer']->items as $issuer ) {
181
				$id   = Security::filter( $issuer->id );
182
				$name = Security::filter( $issuer->name );
183
184
				$issuers[ $id ] = $name;
185
			}
186
		}
187
188
		return $issuers;
189
	}
190
191
	/**
192
	 * Get payment methods.
193
	 *
194
	 * @return array|bool
195
	 */
196 4
	public function get_payment_methods() {
197
		$data = array(
198 4
			'merchantAccount'       => $this->config->get_merchant_account(),
199
			'allowedPaymentMethods' => array(),
200
		);
201
202 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

202
		$response = $this->send_request( 'paymentMethods', /** @scrutinizer ignore-type */ $data );
Loading history...
203
204 1
		if ( false === $response ) {
205
			return false;
206
		}
207
208 1
		$payment_methods = array();
209
210 1
		if ( isset( $response->paymentMethods ) ) {
211 1
			foreach ( $response->paymentMethods as $payment_method ) {
212 1
				$type = Security::filter( $payment_method->type );
213 1
				$name = Security::filter( $payment_method->name );
214
215
				$method = array(
216 1
					'name'    => $name,
217
					'details' => array(),
218
				);
219
220 1
				if ( isset( $payment_method->details ) ) {
221 1
					foreach ( $payment_method->details as $detail ) {
222 1
						$key = $detail->key;
223
224 1
						$method['details'][ $key ] = $detail;
225
226 1
						unset( $method['details'][ $key ]->key );
227
					}
228
				}
229
230 1
				$payment_methods[ $type ] = $method;
231
			}
232
		}
233
234 1
		return $payment_methods;
235
	}
236
}
237