Test Failed
Push — develop ( 6bf0ad...eec332 )
by Reüel
04:35
created

src/Client.php (1 issue)

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