Client::get_refresh_token()   A
last analyzed

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-2022 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
use Pronamic\WordPress\Http\Facades\Http;
14
15
/**
16
 * Client.
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.3.4
20
 * @since   1.0.0
21
 */
22
class Client {
23
	/**
24
	 * URL OmniKassa API.
25
	 *
26
	 * @var string
27
	 */
28
	const URL_PRODUCTION = 'https://betalen.rabobank.nl/omnikassa-api/';
29
30
	/**
31
	 * URL OmniKassa sandbox API.
32
	 *
33
	 * @var string
34
	 */
35
	const URL_SANDBOX = 'https://betalen.rabobank.nl/omnikassa-api-sandbox/';
36
37
	/**
38
	 * Error
39
	 *
40
	 * @var \WP_Error
41
	 */
42
	private $error;
43
44
	/**
45
	 * The URL.
46
	 *
47
	 * @var string
48
	 */
49
	private $url;
50
51
	/**
52
	 * Refresh token.
53
	 *
54
	 * @var string
55
	 */
56
	private $refresh_token;
57
58
	/**
59
	 * Signing key.
60
	 *
61
	 * @var string
62
	 */
63
	private $signing_key;
64
65
	/**
66
	 * Error.
67
	 *
68
	 * @return \WP_Error
69
	 */
70
	public function get_error() {
71
		return $this->error;
72
	}
73
74
	/**
75
	 * Get the URL.
76
	 *
77
	 * @return string
78
	 */
79
	public function get_url() {
80
		return $this->url;
81
	}
82
83
	/**
84
	 * Set the action URL
85
	 *
86
	 * @param string $url URL.
87
	 * @return void
88
	 */
89
	public function set_url( $url ) {
90
		$this->url = $url;
91
	}
92
93
	/**
94
	 * Get refresh token.
95
	 *
96
	 * @return string
97
	 */
98
	public function get_refresh_token() {
99
		return $this->refresh_token;
100
	}
101
102
	/**
103
	 * Set refresh token.
104
	 *
105
	 * @param string $refresh_token Refresh token.
106
	 * @return void
107
	 */
108
	public function set_refresh_token( $refresh_token ) {
109
		$this->refresh_token = $refresh_token;
110
	}
111
112
	/**
113
	 * Get signing key.
114
	 *
115
	 * @return string
116
	 */
117
	public function get_signing_key() {
118
		return $this->signing_key;
119
	}
120
121
	/**
122
	 * Set signing key.
123
	 *
124
	 * @param string $signing_key Signing key.
125
	 * @return void
126
	 */
127
	public function set_signing_key( $signing_key ) {
128
		$this->signing_key = $signing_key;
129
	}
130
131
	/**
132
	 * Request URL with specified method, token.
133
	 *
134
	 * @param string      $method   HTTP request method.
135
	 * @param string      $endpoint URL endpoint to request.
136
	 * @param string      $token    Authorization token.
137
	 * @param object|null $object   Object.
138
	 * @return object
139
	 * @throws \Exception Throws exception when Rabobank OmniKassa 2.0 response is not what we expect.
140
	 */
141
	private function request( $method, $endpoint, $token, $object = null ) {
142
		// URL.
143
		$url = $this->get_url() . $endpoint;
144
145
		/*
146
		 * Arguments.
147
		 *
148
		 * The `timeout` argument is intentionally increased from the WordPress
149
		 * default `5` seconds to `30`. This is mainly important for AfterPay
150
		 * order announcements requests, but it can't hurt for other requests.
151
		 * It is probably better to wait a little longer for an answer than
152
		 * having timeout issues while starting a payment or requesting a
153
		 * payment status. The value can also be adjusted via the
154
		 * `pronamic_pay_omnikassa_2_request_args` filter.
155
		 */
156
		$args = array(
157
			'method'  => $method,
158
			'headers' => array(
159
				'Authorization' => 'Bearer ' . $token,
160
			),
161
			'timeout' => 30,
162
		);
163
164
		if ( null !== $object ) {
165
			$args['headers']['Content-Type'] = 'application/json';
166
167
			$args['body'] = \wp_json_encode( $object );
168
		}
169
170
		/**
171
		 * Filters the OmniKassa 2.0 remote request arguments.
172
		 *
173
		 * Developers who want to adjust the WordPress remote request arguments
174
		 * towards the OmniKassa servers can use this filter. For example, it
175
		 * can be used to increase the HTTP timeout to, for example, 3600. This
176
		 * is sometimes useful for testing AfterPay payments for example.
177
		 * Mainly because the OmniKassa servers sometimes respond slowly when
178
		 * starting AfterPay payments.
179
		 *
180
		 * @link https://github.com/wp-pay-gateways/omnikassa-2#pronamic_pay_omnikassa_2_request_args
181
		 * @param array $args WordPress remote request arguments.
182
		 */
183
		$args = \apply_filters( 'pronamic_pay_omnikassa_2_request_args', $args );
184
185
		/**
186
		 * Build cURL command for debug purposes.
187
		 *
188
		 * @link https://curl.haxx.se/
189
		 */
190
191
		// phpcs:disable SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
192
193
		$curl = '';
194
195
		$tab = "\t";
196
		$eol = ' \\' . \PHP_EOL;
197
198
		$curl .= \sprintf( 'curl --request %s %s', $method, \escapeshellarg( $url ) ) . $eol;
199
		$curl .= $tab . \sprintf( '--header %s', \escapeshellarg( 'Authorization: Bearer ' . $token ) ) . $eol;
200
		$curl .= $tab . \sprintf( '--header %s', \escapeshellarg( 'Content-Type: application/json' ) ) . $eol;
201
		$curl .= $tab . \sprintf( '--data %s', \escapeshellarg( \strval( \wp_json_encode( $object ) ) ) ) . $eol;
202
		$curl .= $tab . \sprintf(
203
			'--user-agent %s',
204
			\escapeshellarg( 'WordPress/' . \get_bloginfo( 'version' ) . '; ' . \get_bloginfo( 'url' ) )
205
		) . $eol;
206
		$curl .= $tab . '--verbose';
207
208
		// phpcs:enable
209
210
		// Request.
211
		$response = Http::request( $url, $args );
212
213
		$data = $response->json();
214
215
		// Object.
216
		if ( ! \is_object( $data ) ) {
217
			throw new \Exception(
218
				\sprintf(
219
					'Could not JSON decode OmniKassa 2.0 response to an object, HTTP response: "%s %s", HTTP body length: "%d".',
220
					$response->status(),
221
					$response->message(),
222
					\strlen( $response->body() )
223
				),
224
				\intval( $response->status() )
225
			);
226
		}
227
228
		// Error.
229
		if ( isset( $data->errorCode ) ) {
230
			$error = Error::from_object( $data );
231
232
			throw $error;
233
		}
234
235
		// Ok.
236
		return $data;
237
	}
238
239
	/**
240
	 * Get access token.
241
	 *
242
	 * @return object
243
	 */
244
	public function get_access_token_data() {
245
		return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() );
246
	}
247
248
	/**
249
	 * Order announce.
250
	 *
251
	 * @param Config $config Config.
252
	 * @param Order  $order  Order.
253
	 * @return OrderAnnounceResponse
254
	 */
255
	public function order_announce( $config, Order $order ) {
256
		$result = $this->request( 'POST', 'order/server/api/v2/order', $config->access_token, $order );
257
258
		return OrderAnnounceResponse::from_object( $result );
259
	}
260
261
	/**
262
	 * Get order results by the notification token.
263
	 *
264
	 * @param string $notification_token Notification token.
265
	 * @return OrderResults
266
	 */
267
	public function get_order_results( $notification_token ) {
268
		$result = $this->request(
269
			'GET',
270
			'order/server/api/events/results/merchant.order.status.changed',
271
			$notification_token
272
		);
273
274
		return OrderResults::from_object( $result );
275
	}
276
}
277