Failed Conditions
Push — develop ( b48a0e...170d92 )
by Reüel
15:46
created

src/Client.php (2 issues)

1
<?php
2
/**
3
 * 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\ING\KassaCompleet
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet;
12
13
use Pronamic\WordPress\Pay\Core\XML\Security;
14
15
/**
16
 * Title: ING Kassa Compleet client
17
 * Description:
18
 * Copyright: 2005-2019 Pronamic
19
 * Company: Pronamic
20
 *
21
 * @author  Reüel van der Steege
22
 * @version 2.0.0
23
 * @since   1.0.0
24
 */
25
class Client {
26
	/**
27
	 * ING Kasse Compleet API endpoint URL
28
	 *
29
	 * @var string url
30
	 */
31
	const API_URL = 'https://api.kassacompleet.nl/v1/';
32
33
	/**
34
	 * API Key
35
	 *
36
	 * @var string
37
	 */
38
	private $api_key;
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
	 * Send request with the specified action and parameters
51
	 *
52
	 * @param string $endpoint API endpoint.
53
	 * @param string $method   HTTP method to use for request.
54
	 * @param array  $data     Data to send.
55
	 *
56
	 * @return array
57
	 */
58
	private function send_request( $endpoint, $method = 'POST', array $data = array() ) {
59
		$url = self::API_URL . $endpoint;
60
61
		$headers = array(
62
			// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
63
			'Authorization' => 'Basic ' . base64_encode( $this->api_key . ':' ),
64
		);
65
66
		if ( is_array( $data ) && ! empty( $data ) ) {
67
			$data = wp_json_encode( $data );
68
69
			$headers['Content-Type'] = 'application/json';
70
		}
71
72
		$return = wp_remote_request(
73
			$url,
74
			array(
75
				'method'  => $method,
76
				'headers' => $headers,
77
				'body'    => $data,
78
			)
79
		);
80
81
		if ( is_wp_error( $return ) ) {
82
			throw new \Pronamic\WordPress\Pay\GatewayException( 'ing_kassa_compleet', $return->get_error_message() );
0 ignored issues
show
The type Pronamic\WordPress\Pay\GatewayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
		}
84
85
		return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return also could return the type WP_Error which is incompatible with the documented return type array.
Loading history...
86
	}
87
88
	/**
89
	 * Create order.
90
	 *
91
	 * @param OrderRequest $request Order request.
92
	 *
93
	 * @return array|mixed|object|null
94
	 */
95
	public function create_order( OrderRequest $request ) {
96
		$result = null;
97
98
		$data = $request->get_array();
99
100
		$response = $this->send_request( 'orders/', 'POST', $data );
101
102
		$response_code = wp_remote_retrieve_response_code( $response );
103
104
		$body = wp_remote_retrieve_body( $response );
105
106
		// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
107
		$ing_result = json_decode( $body );
108
109
		if ( 201 === $response_code ) {
110
			if ( $ing_result && 'error' === $ing_result->status ) {
111
				$error_msg = $ing_result->transactions[0]->reason;
112
				$error     = $ing_result->transactions[0];
113
			} else {
114
				$result = $ing_result;
115
			}
116
		} else {
117
			$error_msg = '';
118
			$error     = '';
119
120
			if ( $ing_result ) {
121
				$error_msg = $ing_result->error->value;
122
				$error     = $ing_result->error;
123
			}
124
125
			if ( 401 === $response_code ) {
126
				// The default error message for an unauthorized API call does not mention the API key in any way.
127
				$error_msg .= ' Please check the API key.';
128
			}
129
		}
130
131
		if ( isset( $error_msg, $error ) ) {
132
			throw new \Pronamic\WordPress\Pay\GatewayException( 'ing_kassa_compleet', $error_msg, $error );
133
		}
134
135
		return $result;
136
	}
137
138
	/**
139
	 * Get order.
140
	 *
141
	 * @param string $order_id Order ID.
142
	 *
143
	 * @return array|mixed|object|null
144
	 */
145
	public function get_order( $order_id ) {
146
		$result = null;
147
148
		$response = $this->send_request( 'orders/' . $order_id . '/', 'GET' );
149
150
		$response_code = wp_remote_retrieve_response_code( $response );
151
152
		if ( 200 === $response_code ) {
153
			$body = wp_remote_retrieve_body( $response );
154
155
			// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
156
			$result = json_decode( $body );
157
		}
158
159
		return $result;
160
	}
161
162
	/**
163
	 * Get issuers.
164
	 *
165
	 * @return array|bool
166
	 */
167
	public function get_issuers() {
168
		$issuers = false;
169
170
		$response = $this->send_request( 'ideal/issuers/', 'GET' );
171
172
		$response_code = wp_remote_retrieve_response_code( $response );
173
174
		$body = wp_remote_retrieve_body( $response );
175
176
		// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
177
		$result = json_decode( $body );
178
179
		if ( 200 !== $response_code ) {
180
			$error_msg = $result->error->value;
181
182
			if ( 401 === $response_code ) {
183
				// An unauthorized API call has nothing to do with the browser of the user in our case, remove to prevent confusion.
184
				$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 );
185
186
				// The default error message for an unauthorized API call does not mention the API key in any way.
187
				$error_msg .= ' Please check the API key.';
188
			}
189
190
			throw new \Pronamic\WordPress\Pay\GatewayException( 'ing_kassa_compleet', $error_msg, $result->error );
191
		}
192
193
		if ( null !== $result ) {
194
			$issuers = array();
195
196
			foreach ( $result as $issuer ) {
197
				$id   = Security::filter( $issuer->id );
198
				$name = Security::filter( $issuer->name );
199
200
				$issuers[ $id ] = $name;
201
			}
202
		}
203
204
		return $issuers;
205
	}
206
}
207