Failed Conditions
Push — develop ( c20c13...163106 )
by Reüel
11:13 queued 04:02
created

src/Client.php (2 issues)

1
<?php
2
/**
3
 * Client.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 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 WP_Error;
14
15
/**
16
 * Client.
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.0.2
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
	 */
88
	public function set_url( $url ) {
89
		$this->url = $url;
90
	}
91
92
	/**
93
	 * Get refresh token.
94
	 *
95
	 * @return string
96
	 */
97
	public function get_refresh_token() {
98
		return $this->refresh_token;
99
	}
100
101
	/**
102
	 * Set refresh token.
103
	 *
104
	 * @param string $refresh_token Refresh token.
105
	 */
106
	public function set_refresh_token( $refresh_token ) {
107
		$this->refresh_token = $refresh_token;
108
	}
109
110
	/**
111
	 * Get signing key.
112
	 *
113
	 * @return string
114
	 */
115
	public function get_signing_key() {
116
		return $this->signing_key;
117
	}
118
119
	/**
120
	 * Set signing key.
121
	 *
122
	 * @param string $signing_key Signing key.
123
	 */
124
	public function set_signing_key( $signing_key ) {
125
		$this->signing_key = $signing_key;
126
	}
127
128
	/**
129
	 * Request URL with specified method, token.
130
	 *
131
	 * @param string      $method   HTTP request method.
132
	 * @param string      $endpoint URL endpoint to request.
133
	 * @param string      $token    Authorization token.
134
	 * @param object|null $object   Object.
135
	 */
136
	private function request( $method, $endpoint, $token, $object = null ) {
137
		// URL.
138
		$url = $this->get_url() . $endpoint;
139
140
		// Arguments.
141
		$args = array(
142
			'method'  => $method,
143
			'headers' => array(
144
				'Authorization' => 'Bearer ' . $token,
145
			),
146
		);
147
148
		if ( null !== $object ) {
149
			$args['headers']['Content-Type'] = 'application/json';
150
151
			$args['body'] = wp_json_encode( $object );
152
		}
153
154
		// Request.
155
		$response = wp_remote_request( $url, $args );
156
157
		if ( is_wp_error( $response ) ) {
158
			$this->error = $response;
0 ignored issues
show
Documentation Bug introduced by
It seems like $response can also be of type array. However, the property $error is declared as type WP_Error. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
159
160
			$this->error->add( 'omnikassa_2_error', 'HTTP Request Failed' );
161
162
			return false;
163
		}
164
165
		// Body.
166
		$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

166
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
167
168
		$data = json_decode( $body );
169
170
		if ( ! is_object( $data ) ) {
171
			$this->error = new \WP_Error( 'omnikassa_2_error', 'Could not parse response.', $data );
172
173
			return false;
174
		}
175
176
		// Error.
177
		if ( isset( $data->errorCode ) ) {
178
			$message = 'Unknown error.';
179
180
			if ( isset( $data->consumerMessage ) ) {
181
				$message = $data->consumerMessage;
182
			} elseif ( isset( $data->errorMessage ) ) {
183
				$message = $data->errorMessage;
184
			}
185
186
			$this->error = new \WP_Error( 'omnikassa_2_error', $message, $data );
187
188
			return false;
189
		}
190
191
		// Ok.
192
		return $data;
193
	}
194
195
	/**
196
	 * Get access token.
197
	 *
198
	 * @return object|false
199
	 */
200
	public function get_access_token_data() {
201
		return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() );
202
	}
203
204
	/**
205
	 * Order announce.
206
	 *
207
	 * @param Config $config Config.
208
	 * @param Order  $order  Order.
209
	 * @return object|bool
210
	 */
211
	public function order_announce( $config, Order $order ) {
212
		$object = $order->get_json();
213
214
		$object->signature = Security::get_signature( $order, $config->signing_key );
215
216
		return $this->request( 'POST', 'order/server/api/order', $config->access_token, $object );
217
	}
218
219
	/**
220
	 * Get order results by the notification token.
221
	 *
222
	 * @param string $notification_token Notification token.
223
	 *
224
	 * @return OrderResults|false
225
	 */
226
	public function get_order_results( $notification_token ) {
227
		$result = $this->request( 'GET', 'order/server/api/events/results/merchant.order.status.changed', $notification_token );
228
229
		if ( ! is_object( $result ) ) {
230
			return false;
231
		}
232
233
		return OrderResults::from_object( $result );
234
	}
235
}
236