Failed Conditions
Push — develop ( 850abc...ab26db )
by Reüel
06:03
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
		if ( $expected_response_code != $response_code ) { // WPCS: loose comparison ok.
150
			$this->error = new WP_Error( 'nocks_error', 'Unexpected response code.' );
151
		}
152
153
		// Body.
154
		$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

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