Failed Conditions
Push — develop ( eec332...395ba8 )
by Remco
10:23 queued 15s
created

Client::get_error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
/**
14
 * Client.
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.1.8
18
 * @since   1.0.0
19
 */
20
class Client {
21
	/**
22
	 * URL OmniKassa API.
23
	 *
24
	 * @var string
25
	 */
26
	const URL_PRODUCTION = 'https://betalen.rabobank.nl/omnikassa-api/';
27
28
	/**
29
	 * URL OmniKassa sandbox API.
30
	 *
31
	 * @var string
32
	 */
33
	const URL_SANDBOX = 'https://betalen.rabobank.nl/omnikassa-api-sandbox/';
34
35
	/**
36
	 * Error
37
	 *
38
	 * @var \WP_Error
39
	 */
40
	private $error;
41
42
	/**
43
	 * The URL.
44
	 *
45
	 * @var string
46
	 */
47
	private $url;
48
49
	/**
50
	 * Refresh token.
51
	 *
52
	 * @var string
53
	 */
54
	private $refresh_token;
55
56
	/**
57
	 * Signing key.
58
	 *
59
	 * @var string
60
	 */
61
	private $signing_key;
62
63
	/**
64
	 * Error.
65
	 *
66
	 * @return \WP_Error
67
	 */
68
	public function get_error() {
69
		return $this->error;
70
	}
71
72
	/**
73
	 * Get the URL.
74
	 *
75
	 * @return string
76
	 */
77
	public function get_url() {
78
		return $this->url;
79
	}
80
81
	/**
82
	 * Set the action URL
83
	 *
84
	 * @param string $url URL.
85
	 */
86
	public function set_url( $url ) {
87
		$this->url = $url;
88
	}
89
90
	/**
91
	 * Get refresh token.
92
	 *
93
	 * @return string
94
	 */
95
	public function get_refresh_token() {
96
		return $this->refresh_token;
97
	}
98
99
	/**
100
	 * Set refresh token.
101
	 *
102
	 * @param string $refresh_token Refresh token.
103
	 */
104
	public function set_refresh_token( $refresh_token ) {
105
		$this->refresh_token = $refresh_token;
106
	}
107
108
	/**
109
	 * Get signing key.
110
	 *
111
	 * @return string
112
	 */
113
	public function get_signing_key() {
114
		return $this->signing_key;
115
	}
116
117
	/**
118
	 * Set signing key.
119
	 *
120
	 * @param string $signing_key Signing key.
121
	 */
122
	public function set_signing_key( $signing_key ) {
123
		$this->signing_key = $signing_key;
124
	}
125
126
	/**
127
	 * Request URL with specified method, token.
128
	 *
129
	 * @param string      $method   HTTP request method.
130
	 * @param string      $endpoint URL endpoint to request.
131
	 * @param string      $token    Authorization token.
132
	 * @param object|null $object   Object.
133
	 * @return bool|object
134
	 */
135
	private function request( $method, $endpoint, $token, $object = null ) {
136
		// URL.
137
		$url = $this->get_url() . $endpoint;
138
139
		/*
140
		 * Arguments.
141
		 *
142
		 * The `timeout` argument is intentionally increased from the WordPress
143
		 * default `5` seconds to `30`. This is mainly important for AfterPay
144
		 * order announcements requests, but it can't hurt for other requests.
145
		 * It is probably better to wait a little longer for an answer than
146
		 * having timeout issues while starting a payment or requesting a
147
		 * payment status. The value can also be adjusted via the
148
		 * `pronamic_pay_omnikassa_2_request_args` filter.
149
		 */
150
		$args = array(
151
			'method'  => $method,
152
			'headers' => array(
153
				'Authorization' => 'Bearer ' . $token,
154
			),
155
			'timeout' => 30,
156
		);
157
158
		if ( null !== $object ) {
159
			$args['headers']['Content-Type'] = 'application/json';
160
161
			$args['body'] = \wp_json_encode( $object );
162
		}
163
164
		$args = \apply_filters( 'pronamic_pay_omnikassa_2_request_args', $args );
165
166
		// Request.
167
		$response = \wp_remote_request( $url, $args );
168
169
		if ( $response instanceof \WP_Error ) {
170
			$this->error = $response;
171
172
			$this->error->add( 'omnikassa_2_error', 'HTTP Request Failed' );
173
174
			return false;
175
		}
176
177
		// Body.
178
		$body = \wp_remote_retrieve_body( $response );
179
180
		$data = \json_decode( $body );
181
182
		if ( ! \is_object( $data ) ) {
183
			$message = \implode(
184
				"\r\n",
185
				array(
186
					'Could not parse response.',
187
					\sprintf(
188
						'HTTP response status code: %s %s',
189
						\wp_remote_retrieve_response_code( $response ),
190
						\wp_remote_retrieve_response_message( $response )
191
					),
192
				)
193
			);
194
195
			$this->error = new \WP_Error( 'omnikassa_2_error', $message, $data );
196
197
			return false;
198
		}
199
200
		// Error.
201
		// @codingStandardsIgnoreStart
202
		if ( isset( $data->errorCode ) ) {
203
			// @codingStandardsIgnoreEnd
204
			$message = 'Unknown error.';
205
206
			// @codingStandardsIgnoreStart
207
			if ( isset( $data->consumerMessage ) ) {
208
				$message = $data->consumerMessage;
209
			} elseif ( isset( $data->errorMessage ) ) {
210
				$message = $data->errorMessage;
211
			}
212
			// @codingStandardsIgnoreEnd
213
214
			$this->error = new \WP_Error( 'omnikassa_2_error', $message, $data );
215
216
			return false;
217
		}
218
219
		// Ok.
220
		return $data;
221
	}
222
223
	/**
224
	 * Get access token.
225
	 *
226
	 * @return bool|object
227
	 */
228
	public function get_access_token_data() {
229
		return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() );
230
	}
231
232
	/**
233
	 * Order announce.
234
	 *
235
	 * @param Config $config Config.
236
	 * @param Order  $order  Order.
237
	 * @return OrderAnnounceResponse|false
238
	 */
239
	public function order_announce( $config, Order $order ) {
240
		$order->sign( $config->signing_key );
241
242
		$object = $order->get_json();
243
244
		$result = $this->request( 'POST', 'order/server/api/order', $config->access_token, $object );
245
246
		if ( ! \is_object( $result ) ) {
247
			return false;
248
		}
249
250
		return OrderAnnounceResponse::from_object( $result );
251
	}
252
253
	/**
254
	 * Get order results by the notification token.
255
	 *
256
	 * @param string $notification_token Notification token.
257
	 * @return OrderResults|false
258
	 */
259
	public function get_order_results( $notification_token ) {
260
		$result = $this->request( 'GET', 'order/server/api/events/results/merchant.order.status.changed', $notification_token );
261
262
		if ( ! \is_object( $result ) ) {
263
			return false;
264
		}
265
266
		return OrderResults::from_object( $result );
267
	}
268
}
269