Test Failed
Push — develop ( 1c5760...a27985 )
by Remco
16:56 queued 12:18
created

src/Client.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Nocks;
4
5
use WP_Error;
6
7
/**
8
 * Title: Nocks client
9
 * Description:
10
 * Copyright: 2005-2019 Pronamic
11
 * Company: Pronamic
12
 *
13
 * @author  Reüel van der Steege
14
 * @version 2.0.1
15
 * @since   1.0.0
16
 */
17
class Client {
18
	/**
19
	 * URL Nocks API.
20
	 *
21
	 * @link https://docs.nocks.com/
22
	 *
23
	 * @var string
24
	 */
25
	const API_URL = 'https://api.nocks.com/api/v2/';
26
27
	/**
28
	 * URL Nocks sandbox API.
29
	 *
30
	 * @var string
31
	 */
32
	const NOCKS_DOMAIN = 'https://www.nocks.com/';
33
34
	/**
35
	 * Access Token.
36
	 *
37
	 * @var string
38
	 */
39
	private $access_token;
40
41
	/**
42
	 * Merchant profile.
43
	 *
44
	 * @var string
45
	 */
46
	private $merchant_profile;
47
48
	/**
49
	 * Error
50
	 *
51
	 * @var WP_Error
52
	 */
53
	private $error;
54
55
	/**
56
	 * Error
57
	 *
58
	 * @return WP_Error
59
	 */
60
	public function get_error() {
61
		return $this->error;
62
	}
63
64
	/**
65
	 * Get access token.
66
	 */
67
	public function get_access_token() {
68
		return $this->access_token;
69
	}
70
71
	/**
72
	 * Set access token.
73
	 *
74
	 * @param string $access_token Access token.
75
	 */
76
	public function set_access_token( $access_token ) {
77
		$this->access_token = $access_token;
78
	}
79
80
	/**
81
	 * Get merchant profile.
82
	 */
83
	public function get_merchant_profile() {
84
		return $this->merchant_profile;
85
	}
86
87
	/**
88
	 * Set merchant profile.
89
	 *
90
	 * @param string $merchant_profile Merchant profile id.
91
	 */
92
	public function set_merchant_profile( $merchant_profile ) {
93
		$this->merchant_profile = $merchant_profile;
94
	}
95
96
	/**
97
	 * Get issuers.
98
	 */
99
	public function get_issuers() {
100
		return array(
101
			'ABNANL2A' => __( 'ABN Amro', 'pronamic_ideal' ),
102
			'RABONL2U' => __( 'Rabobank', 'pronamic_ideal' ),
103
			'INGBNL2A' => __( 'ING Bank', 'pronamic_ideal' ),
104
			'SNSBNL2A' => __( 'SNS Bank', 'pronamic_ideal' ),
105
			'ASNBNL21' => __( 'ASN Bank', 'pronamic_ideal' ),
106
			'RBRBNL21' => __( 'RegioBank', 'pronamic_ideal' ),
107
			'TRIONL2U' => __( 'Triodos Bank', 'pronamic_ideal' ),
108
			'FVLBNL22' => __( 'Van Lanschot', 'pronamic_ideal' ),
109
			'KNABNL2H' => __( 'Knab', 'pronamic_ideal' ),
110
			'BUNQNL2A' => __( 'Bunq', 'pronamic_ideal' ),
111
			'MOYONL21' => __( 'Moneyou', 'pronamic_ideal' ),
112
		);
113
	}
114
115
	/**
116
	 * Send request with the specified action and parameters
117
	 *
118
	 * @param string $end_point              Request end point.
119
	 * @param string $method                 HTTP method.
120
	 * @param array  $data                   Data.
121
	 * @param int    $expected_response_code Expected response code.
122
	 *
123
	 * @return bool|object
124
	 */
125
	private function send_request( $end_point, $method = 'GET', array $data = array(), $expected_response_code = 200 ) {
126
		// Request.
127
		$url = self::API_URL . $end_point;
128
129
		if ( is_array( $data ) && ! empty( $data ) ) {
130
			$data = wp_json_encode( $data );
131
		}
132
133
		$response = wp_remote_request(
134
			$url,
135
			array(
136
				'method'  => $method,
137
				'headers' => array(
138
					'Accept'        => 'application/json',
139
					'Content-Type'  => 'application/json',
140
					'Authorization' => 'Bearer ' . $this->get_access_token(),
141
				),
142
				'body'    => $data,
143
			)
144
		);
145
146
		// Response code.
147
		$response_code = wp_remote_retrieve_response_code( $response );
0 ignored issues
show
It seems like $response can also be of type WP_Error; however, parameter $response of wp_remote_retrieve_response_code() 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

147
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
148
149
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
150
		if ( $expected_response_code != $response_code ) {
151
			$this->error = new WP_Error( 'nocks_error', 'Unexpected response code.' );
152
		}
153
154
		// Body.
155
		$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

155
		$body = wp_remote_retrieve_body( /** @scrutinizer ignore-type */ $response );
Loading history...
156
157
		$data = json_decode( $body );
158
159
		if ( ! is_object( $data ) ) {
160
			$this->error = new WP_Error( 'nocks_error', 'Could not parse response.' );
161
162
			return false;
163
		}
164
165
		// Nocks error.
166
		if ( isset( $data->error, $data->error->message ) ) {
167
			$this->error = new WP_Error( 'nocks_error', $data->error->message, $data->error );
168
169
			return false;
170
		}
171
172
		return $data;
173
	}
174
175
	/**
176
	 * Get merchant profiles.
177
	 *
178
	 * @return array
179
	 */
180
	public function get_merchant_profiles() {
181
		$profiles = array();
182
183
		$merchants = $this->send_request( 'merchant', 'GET' );
184
185
		if ( $merchants ) {
186
			foreach ( $merchants->data as $merchant ) {
187
				foreach ( $merchant->merchant_profiles->data as $profile ) {
188
					$profiles[ $profile->uuid ] = $merchant->name . ' - ' . $profile->name;
189
				}
190
			}
191
		}
192
193
		return $profiles;
194
	}
195
196
	/**
197
	 * Start transaction.
198
	 *
199
	 * @param Transaction $transaction Transaction object.
200
	 *
201
	 * @return array|bool|mixed|object
202
	 */
203
	public function start_transaction( Transaction $transaction ) {
204
		return $this->send_request(
205
			'transaction',
206
			'POST',
207
			$transaction->get_data(),
208
			201
209
		);
210
	}
211
212
	/**
213
	 * Get transaction.
214
	 *
215
	 * @param string $transaction_uuid Transaction UUID.
216
	 *
217
	 * @return array|bool|mixed|object
218
	 */
219
	public function get_transaction( $transaction_uuid ) {
220
		return $this->send_request(
221
			'transaction/' . $transaction_uuid,
222
			'GET'
223
		);
224
	}
225
226
	/**
227
	 * Get transaction quote.
228
	 *
229
	 * @param string $source_currency Source currency.
230
	 * @param string $target_currency Target currency.
231
	 * @param string $amount          Amount in given source currency.
232
	 * @param string $payment_method  Payment method.
233
	 *
234
	 * @return array|bool|mixed|object
235
	 */
236
	public function get_transaction_quote( $source_currency, $target_currency, $amount, $payment_method ) {
237
		$data = array(
238
			'source_currency'  => $source_currency,
239
			'target_currency'  => $target_currency,
240
			'merchant_profile' => $this->get_merchant_profile(),
241
			'amount'           => array(
242
				'amount'   => (string) $amount,
243
				'currency' => $source_currency,
244
			),
245
			'payment_method'   => array( 'method' => $payment_method ),
246
		);
247
248
		return $this->send_request(
249
			'transaction/quote',
250
			'POST',
251
			$data
252
		);
253
	}
254
}
255