Failed Conditions
Push — develop ( 6395e5...567ea4 )
by Reüel
03:57
created

src/Client.php (6 issues)

Labels
Severity
1
<?php
2
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet;
3
4
use Pronamic\WordPress\Pay\Core\XML\Security;
5
use Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet\OrderRequest;
6
use WP_Error;
7
8
/**
9
 * Title: ING Kassa Compleet client
10
 * Description:
11
 * Copyright: 2005-2019 Pronamic
12
 * Company: Pronamic
13
 *
14
 * @author  Reüel van der Steege
15
 * @version 2.0.0
16
 * @since   1.0.0
17
 */
18
class Client {
19
	/**
20
	 * ING Kasse Compleet API endpoint URL
21
	 *
22
	 * @var string url
23
	 */
24
	const API_URL = 'https://api.kassacompleet.nl/v1/';
25
26
	/**
27
	 * API Key
28
	 *
29
	 * @var string
30
	 */
31
	private $api_key;
32
33
	/**
34
	 * Error
35
	 *
36
	 * @var WP_Error
37
	 */
38
	private $error;
39
40
	/**
41
	 * Constructs and initalize an ING Kassa Compleet client object
42
	 *
43
	 * @param string $api_key API key.
44
	 */
45
	public function __construct( $api_key ) {
46
		$this->api_key = $api_key;
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 $endpoint API endpoint.
62
	 * @param string $method   HTTP method to use for request.
63
	 * @param array  $data     Data to send.
64
	 */
65
	private function send_request( $endpoint, $method = 'POST', array $data = array() ) {
66
		$url = self::API_URL . $endpoint;
67
68
		$headers = array(
69
			'Authorization' => 'Basic ' . base64_encode( $this->api_key . ':' ),
70
		);
71
72
		if ( is_array( $data ) && ! empty( $data ) ) {
73
			$data = wp_json_encode( $data );
74
75
			$headers['Content-Type'] = 'application/json';
76
		}
77
78
		$return = wp_remote_request(
79
			$url,
80
			array(
81
				'method'  => $method,
82
				'headers' => $headers,
83
				'body'    => $data,
84
			)
85
		);
86
87
		return $return;
88
	}
89
90
	/**
91
	 * Create order.
92
	 *
93
	 * @param OrderRequest $request Order request.
94
	 *
95
	 * @return array|mixed|object|null
96
	 */
97
	public function create_order( OrderRequest $request ) {
98
		$result = null;
99
100
		$data = $request->get_array();
101
102
		$response = $this->send_request( 'orders/', 'POST', $data );
103
104
		$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

104
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
105
106
		$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

106
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
107
108
		// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
109
		$ing_result = json_decode( $body );
110
111
		if ( 201 === $response_code ) {
112
			if ( $ing_result && 'error' === $ing_result->status ) {
113
				$error_msg = $ing_result->transactions[0]->reason;
114
				$error     = $ing_result->transactions[0];
115
			} else {
116
				$result = $ing_result;
117
			}
118
		} else {
119
			$error_msg = '';
120
			$error     = '';
121
122
			if ( $ing_result ) {
123
				$error_msg = $ing_result->error->value;
124
				$error     = $ing_result->error;
125
			}
126
127
			if ( 401 === $response_code ) {
128
				// The default error message for an unauthorized API call does not mention the API key in any way.
129
				$error_msg .= ' Please check the API key.';
130
			}
131
		}
132
133
		if ( isset( $error_msg, $error ) ) {
134
			$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $error );
135
		}
136
137
		return $result;
138
	}
139
140
	/**
141
	 * Get order.
142
	 *
143
	 * @param string $order_id Order ID.
144
	 *
145
	 * @return array|mixed|object|null
146
	 */
147
	public function get_order( $order_id ) {
148
		$result = null;
149
150
		$response = $this->send_request( 'orders/' . $order_id . '/', 'GET' );
151
152
		$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

152
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
153
154
		if ( 200 === $response_code ) {
155
			$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

155
			$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
156
157
			// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
158
			$result = json_decode( $body );
159
		}
160
161
		return $result;
162
	}
163
164
	/**
165
	 * Get issuers
166
	 *
167
	 * @return array
168
	 */
169
	public function get_issuers() {
170
		$issuers = false;
171
172
		$response = $this->send_request( 'ideal/issuers/', 'GET' );
173
174
		$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

174
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
175
176
		if ( 200 === $response_code ) {
177
			$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

177
			$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
178
179
			// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
180
			$result = json_decode( $body );
181
182
			if ( null !== $result ) {
183
				$issuers = array();
184
185
				foreach ( $result as $issuer ) {
186
					$id   = Security::filter( $issuer->id );
187
					$name = Security::filter( $issuer->name );
188
189
					$issuers[ $id ] = $name;
190
				}
191
			}
192
		} else {
193
			$body = wp_remote_retrieve_body( $response );
194
195
			$ing_result = json_decode( $body );
196
197
			$error_msg = $ing_result->error->value;
198
199
			if ( 401 === $response_code ) {
200
				// An unauthorized API call has nothing to do with the browser of the user in our case, remove to prevent confusion.
201
				$error_msg = str_replace( "You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.", '', $error_msg );
202
203
				// The default error message for an unauthorized API call does not mention the API key in any way.
204
				$error_msg .= ' Please check the API key.';
205
			}
206
207
			$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $ing_result->error );
208
		}
209
210
		return $issuers;
211
	}
212
}
213