Client   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 70
c 4
b 0
f 0
dl 0
loc 215
ccs 0
cts 99
cp 0
rs 10
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
B send_request() 0 44 6
A get_issuers() 0 13 1
A start_transaction() 0 6 1
A set_merchant_profile() 0 2 1
A get_transaction() 0 4 1
A get_merchant_profile() 0 2 1
A set_access_token() 0 2 1
A get_access_token() 0 2 1
A get_transaction_quote() 0 16 1
A get_merchant_profiles() 0 14 5
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Nocks;
4
5
/**
6
 * Title: Nocks client
7
 * Description:
8
 * Copyright: 2005-2020 Pronamic
9
 * Company: Pronamic
10
 *
11
 * @author  Reüel van der Steege
12
 * @version 2.0.3
13
 * @since   1.0.0
14
 */
15
class Client {
16
	/**
17
	 * URL Nocks API.
18
	 *
19
	 * @link https://docs.nocks.com/
20
	 *
21
	 * @var string
22
	 */
23
	const API_URL = 'https://api.nocks.com/api/v2/';
24
25
	/**
26
	 * URL Nocks sandbox API.
27
	 *
28
	 * @var string
29
	 */
30
	const NOCKS_DOMAIN = 'https://www.nocks.com/';
31
32
	/**
33
	 * Access Token.
34
	 *
35
	 * @var string
36
	 */
37
	private $access_token;
38
39
	/**
40
	 * Merchant profile.
41
	 *
42
	 * @var string
43
	 */
44
	private $merchant_profile;
45
46
	/**
47
	 * Get access token.
48
	 */
49
	public function get_access_token() {
50
		return $this->access_token;
51
	}
52
53
	/**
54
	 * Set access token.
55
	 *
56
	 * @param string $access_token Access token.
57
	 */
58
	public function set_access_token( $access_token ) {
59
		$this->access_token = $access_token;
60
	}
61
62
	/**
63
	 * Get merchant profile.
64
	 */
65
	public function get_merchant_profile() {
66
		return $this->merchant_profile;
67
	}
68
69
	/**
70
	 * Set merchant profile.
71
	 *
72
	 * @param string $merchant_profile Merchant profile id.
73
	 */
74
	public function set_merchant_profile( $merchant_profile ) {
75
		$this->merchant_profile = $merchant_profile;
76
	}
77
78
	/**
79
	 * Get issuers.
80
	 */
81
	public function get_issuers() {
82
		return array(
83
			'ABNANL2A' => __( 'ABN Amro', 'pronamic_ideal' ),
84
			'RABONL2U' => __( 'Rabobank', 'pronamic_ideal' ),
85
			'INGBNL2A' => __( 'ING Bank', 'pronamic_ideal' ),
86
			'SNSBNL2A' => __( 'SNS Bank', 'pronamic_ideal' ),
87
			'ASNBNL21' => __( 'ASN Bank', 'pronamic_ideal' ),
88
			'RBRBNL21' => __( 'RegioBank', 'pronamic_ideal' ),
89
			'TRIONL2U' => __( 'Triodos Bank', 'pronamic_ideal' ),
90
			'FVLBNL22' => __( 'Van Lanschot', 'pronamic_ideal' ),
91
			'KNABNL2H' => __( 'Knab', 'pronamic_ideal' ),
92
			'BUNQNL2A' => __( 'Bunq', 'pronamic_ideal' ),
93
			'MOYONL21' => __( 'Moneyou', 'pronamic_ideal' ),
94
		);
95
	}
96
97
	/**
98
	 * Send request with the specified action and parameters
99
	 *
100
	 * @param string $end_point              Request end point.
101
	 * @param string $method                 HTTP method.
102
	 * @param array  $data                   Data.
103
	 * @param int    $expected_response_code Expected response code.
104
	 *
105
	 * @return bool|object
106
	 */
107
	private function send_request( $end_point, $method = 'GET', array $data = array(), $expected_response_code = 200 ) {
108
		// Request.
109
		$url = self::API_URL . $end_point;
110
111
		if ( is_array( $data ) && ! empty( $data ) ) {
112
			$data = wp_json_encode( $data );
113
		}
114
115
		$response = wp_remote_request(
116
			$url,
117
			array(
118
				'method'  => $method,
119
				'headers' => array(
120
					'Accept'        => 'application/json',
121
					'Content-Type'  => 'application/json',
122
					'Authorization' => 'Bearer ' . $this->get_access_token(),
123
				),
124
				'body'    => $data,
125
			)
126
		);
127
128
		// Response code.
129
		$response_code = wp_remote_retrieve_response_code( $response );
130
131
		// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
132
		if ( $expected_response_code != $response_code ) {
133
			throw new \Exception( 'Unexpected response code.' );
134
		}
135
136
		// Body.
137
		$body = wp_remote_retrieve_body( $response );
138
139
		$data = json_decode( $body );
140
141
		if ( ! is_object( $data ) ) {
142
			throw new \Exception( 'Could not parse response.' );
143
		}
144
145
		// Nocks error.
146
		if ( isset( $data->error, $data->error->message ) ) {
147
			throw new \Exception( $data->error->message );
148
		}
149
150
		return $data;
151
	}
152
153
	/**
154
	 * Get merchant profiles.
155
	 *
156
	 * @return array
157
	 */
158
	public function get_merchant_profiles() {
159
		$profiles = array();
160
161
		$merchants = $this->send_request( 'merchant', 'GET' );
162
163
		if ( \is_object( $merchants ) && isset( $merchants->data ) ) {
164
			foreach ( $merchants->data as $merchant ) {
165
				foreach ( $merchant->merchant_profiles->data as $profile ) {
166
					$profiles[ $profile->uuid ] = $merchant->name . ' - ' . $profile->name;
167
				}
168
			}
169
		}
170
171
		return $profiles;
172
	}
173
174
	/**
175
	 * Start transaction.
176
	 *
177
	 * @param Transaction $transaction Transaction object.
178
	 *
179
	 * @return bool|object
180
	 */
181
	public function start_transaction( Transaction $transaction ) {
182
		return $this->send_request(
183
			'transaction',
184
			'POST',
185
			$transaction->get_data(),
186
			201
187
		);
188
	}
189
190
	/**
191
	 * Get transaction.
192
	 *
193
	 * @param string $transaction_uuid Transaction UUID.
194
	 *
195
	 * @return bool|object
196
	 */
197
	public function get_transaction( $transaction_uuid ) {
198
		return $this->send_request(
199
			'transaction/' . $transaction_uuid,
200
			'GET'
201
		);
202
	}
203
204
	/**
205
	 * Get transaction quote.
206
	 *
207
	 * @param string $source_currency Source currency.
208
	 * @param string $target_currency Target currency.
209
	 * @param string $amount          Amount in given source currency.
210
	 * @param string $payment_method  Payment method.
211
	 *
212
	 * @return bool|object
213
	 */
214
	public function get_transaction_quote( $source_currency, $target_currency, $amount, $payment_method ) {
215
		$data = array(
216
			'source_currency'  => $source_currency,
217
			'target_currency'  => $target_currency,
218
			'merchant_profile' => $this->get_merchant_profile(),
219
			'amount'           => array(
220
				'amount'   => (string) $amount,
221
				'currency' => $source_currency,
222
			),
223
			'payment_method'   => array( 'method' => $payment_method ),
224
		);
225
226
		return $this->send_request(
227
			'transaction/quote',
228
			'POST',
229
			$data
230
		);
231
	}
232
}
233