Failed Conditions
Push — develop ( 9e6419...850abc )
by Remco
04:19
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( $url, array(
134
			'method'  => $method,
135
			'headers' => array(
136
				'Accept'        => 'application/json',
137
				'Content-Type'  => 'application/json',
138
				'Authorization' => 'Bearer ' . $this->get_access_token(),
139
			),
140
			'body'    => $data,
141
		) );
142
143
		// Response code.
144
		$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

144
		$response_code = wp_remote_retrieve_response_code( /** @scrutinizer ignore-type */ $response );
Loading history...
145
146
		if ( $expected_response_code != $response_code ) { // WPCS: loose comparison ok.
147
			$this->error = new WP_Error( 'nocks_error', 'Unexpected response code.' );
148
		}
149
150
		// Body.
151
		$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

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