|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Client. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2021 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet; |
|
12
|
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Core\XML\Security; |
|
14
|
|
|
use WP_Error; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Title: ING Kassa Compleet client |
|
18
|
|
|
* Description: |
|
19
|
|
|
* Copyright: 2005-2021 Pronamic |
|
20
|
|
|
* Company: Pronamic |
|
21
|
|
|
* |
|
22
|
|
|
* @author Reüel van der Steege |
|
23
|
|
|
* @version 2.0.0 |
|
24
|
|
|
* @since 1.0.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class Client { |
|
27
|
|
|
/** |
|
28
|
|
|
* ING Kasse Compleet API endpoint URL |
|
29
|
|
|
* |
|
30
|
|
|
* @var string url |
|
31
|
|
|
*/ |
|
32
|
|
|
const API_URL = 'https://api.kassacompleet.nl/v1/'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* API Key |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private $api_key; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Error |
|
43
|
|
|
* |
|
44
|
|
|
* @var WP_Error |
|
45
|
|
|
*/ |
|
46
|
|
|
private $error; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Construct and initialize an ING Kassa Compleet client object |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $api_key API key. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct( $api_key ) { |
|
54
|
|
|
$this->api_key = $api_key; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Error |
|
59
|
|
|
* |
|
60
|
|
|
* @return WP_Error |
|
61
|
|
|
*/ |
|
62
|
|
|
public function get_error() { |
|
63
|
|
|
return $this->error; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Send request with the specified action and parameters |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $endpoint API endpoint. |
|
70
|
|
|
* @param string $method HTTP method to use for request. |
|
71
|
|
|
* @param array $data Data to send. |
|
72
|
|
|
* |
|
73
|
|
|
* @return array|WP_Error |
|
74
|
|
|
*/ |
|
75
|
|
|
private function send_request( $endpoint, $method = 'POST', array $data = array() ) { |
|
76
|
|
|
$url = self::API_URL . $endpoint; |
|
77
|
|
|
|
|
78
|
|
|
$headers = array( |
|
79
|
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
|
80
|
|
|
'Authorization' => 'Basic ' . base64_encode( $this->api_key . ':' ), |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
if ( is_array( $data ) && ! empty( $data ) ) { |
|
84
|
|
|
$data = wp_json_encode( $data ); |
|
85
|
|
|
|
|
86
|
|
|
$headers['Content-Type'] = 'application/json'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$return = wp_remote_request( |
|
90
|
|
|
$url, |
|
91
|
|
|
array( |
|
92
|
|
|
'method' => $method, |
|
93
|
|
|
'headers' => $headers, |
|
94
|
|
|
'body' => $data, |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
return $return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Create order. |
|
103
|
|
|
* |
|
104
|
|
|
* @param OrderRequest $request Order request. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array|mixed|object|null |
|
107
|
|
|
*/ |
|
108
|
|
|
public function create_order( OrderRequest $request ) { |
|
109
|
|
|
$result = null; |
|
110
|
|
|
|
|
111
|
|
|
$data = $request->get_array(); |
|
112
|
|
|
|
|
113
|
|
|
$response = $this->send_request( 'orders/', 'POST', $data ); |
|
114
|
|
|
|
|
115
|
|
|
if ( $response instanceof WP_Error ) { |
|
116
|
|
|
$this->error = $response; |
|
117
|
|
|
|
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
122
|
|
|
|
|
123
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
124
|
|
|
|
|
125
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
|
126
|
|
|
$ing_result = json_decode( $body ); |
|
127
|
|
|
|
|
128
|
|
|
if ( 201 === $response_code ) { |
|
129
|
|
|
if ( $ing_result && 'error' === $ing_result->status ) { |
|
130
|
|
|
$error_msg = $ing_result->transactions[0]->reason; |
|
131
|
|
|
$error = $ing_result->transactions[0]; |
|
132
|
|
|
} else { |
|
133
|
|
|
$result = $ing_result; |
|
134
|
|
|
} |
|
135
|
|
|
} else { |
|
136
|
|
|
$error_msg = ''; |
|
137
|
|
|
$error = ''; |
|
138
|
|
|
|
|
139
|
|
|
if ( $ing_result ) { |
|
140
|
|
|
$error_msg = $ing_result->error->value; |
|
141
|
|
|
$error = $ing_result->error; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ( 401 === $response_code ) { |
|
145
|
|
|
// The default error message for an unauthorized API call does not mention the API key in any way. |
|
146
|
|
|
$error_msg .= ' Please check the API key.'; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if ( isset( $error_msg, $error ) ) { |
|
151
|
|
|
$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $error ); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Get order. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $order_id Order ID. |
|
161
|
|
|
* |
|
162
|
|
|
* @return array|mixed|object|null |
|
163
|
|
|
*/ |
|
164
|
|
|
public function get_order( $order_id ) { |
|
165
|
|
|
$result = null; |
|
166
|
|
|
|
|
167
|
|
|
$response = $this->send_request( 'orders/' . $order_id . '/', 'GET' ); |
|
168
|
|
|
|
|
169
|
|
|
if ( $response instanceof WP_Error ) { |
|
170
|
|
|
$this->error = $response; |
|
171
|
|
|
|
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
176
|
|
|
|
|
177
|
|
|
if ( 200 === $response_code ) { |
|
178
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
179
|
|
|
|
|
180
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
|
181
|
|
|
$result = json_decode( $body ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $result; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get issuers. |
|
189
|
|
|
* |
|
190
|
|
|
* @return array|bool |
|
191
|
|
|
*/ |
|
192
|
|
|
public function get_issuers() { |
|
193
|
|
|
$issuers = false; |
|
194
|
|
|
|
|
195
|
|
|
$response = $this->send_request( 'ideal/issuers/', 'GET' ); |
|
196
|
|
|
|
|
197
|
|
|
if ( $response instanceof WP_Error ) { |
|
198
|
|
|
$this->error = $response; |
|
199
|
|
|
|
|
200
|
|
|
return $issuers; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
204
|
|
|
|
|
205
|
|
|
if ( 200 === $response_code ) { |
|
206
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
207
|
|
|
|
|
208
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
|
209
|
|
|
$result = json_decode( $body ); |
|
210
|
|
|
|
|
211
|
|
|
if ( null !== $result ) { |
|
212
|
|
|
$issuers = array(); |
|
213
|
|
|
|
|
214
|
|
|
foreach ( $result as $issuer ) { |
|
215
|
|
|
$id = Security::filter( $issuer->id ); |
|
216
|
|
|
$name = Security::filter( $issuer->name ); |
|
217
|
|
|
|
|
218
|
|
|
$issuers[ $id ] = $name; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} else { |
|
222
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
223
|
|
|
|
|
224
|
|
|
$ing_result = json_decode( $body ); |
|
225
|
|
|
|
|
226
|
|
|
$error_msg = $ing_result->error->value; |
|
227
|
|
|
|
|
228
|
|
|
if ( 401 === $response_code ) { |
|
229
|
|
|
// An unauthorized API call has nothing to do with the browser of the user in our case, remove to prevent confusion. |
|
230
|
|
|
$error_msg = str_replace( "You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.", '', $error_msg ); |
|
231
|
|
|
|
|
232
|
|
|
// The default error message for an unauthorized API call does not mention the API key in any way. |
|
233
|
|
|
$error_msg .= ' Please check the API key.'; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $ing_result->error ); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $issuers; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|