1
|
|
|
<?php |
2
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet; |
3
|
|
|
|
4
|
|
|
use Pronamic\WordPress\Pay\Core\XML\Security; |
5
|
|
|
use Pronamic\WordPress\Pay\Gateways\ING\KassaCompleet\OrderRequest; |
6
|
|
|
use WP_Error; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Title: ING Kassa Compleet client |
10
|
|
|
* Description: |
11
|
|
|
* Copyright: 2005-2019 Pronamic |
12
|
|
|
* Company: Pronamic |
13
|
|
|
* |
14
|
|
|
* @author Reüel van der Steege |
15
|
|
|
* @version 2.0.0 |
16
|
|
|
* @since 1.0.0 |
17
|
|
|
*/ |
18
|
|
|
class Client { |
19
|
|
|
/** |
20
|
|
|
* ING Kasse Compleet API endpoint URL |
21
|
|
|
* |
22
|
|
|
* @var string url |
23
|
|
|
*/ |
24
|
|
|
const API_URL = 'https://api.kassacompleet.nl/v1/'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* API Key |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $api_key; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Error |
35
|
|
|
* |
36
|
|
|
* @var WP_Error |
37
|
|
|
*/ |
38
|
|
|
private $error; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructs and initalize an ING Kassa Compleet client object |
42
|
|
|
*/ |
43
|
|
|
public function __construct( $api_key ) { |
44
|
|
|
$this->api_key = $api_key; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Error |
49
|
|
|
* |
50
|
|
|
* @return WP_Error |
51
|
|
|
*/ |
52
|
|
|
public function get_error() { |
53
|
|
|
return $this->error; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Send request with the specified action and parameters |
58
|
|
|
* |
59
|
|
|
* @param string $endpoint |
60
|
|
|
* @param string $method |
61
|
|
|
* @param array $data |
62
|
|
|
*/ |
63
|
|
|
private function send_request( $endpoint, $method = 'POST', array $data = array() ) { |
64
|
|
|
$url = self::API_URL . $endpoint; |
65
|
|
|
|
66
|
|
|
$headers = array( |
67
|
|
|
'Authorization' => 'Basic ' . base64_encode( $this->api_key . ':' ), |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if ( is_array( $data ) && ! empty( $data ) ) { |
71
|
|
|
$data = wp_json_encode( $data ); |
72
|
|
|
|
73
|
|
|
$headers['Content-Type'] = 'application/json'; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$return = wp_remote_request( $url, array( |
77
|
|
|
'method' => $method, |
78
|
|
|
'headers' => $headers, |
79
|
|
|
'body' => $data, |
80
|
|
|
) ); |
81
|
|
|
|
82
|
|
|
return $return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function create_order( OrderRequest $request ) { |
86
|
|
|
$result = null; |
87
|
|
|
|
88
|
|
|
$data = $request->get_array(); |
89
|
|
|
|
90
|
|
|
$response = $this->send_request( 'orders/', 'POST', $data ); |
91
|
|
|
|
92
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
97
|
|
|
$ing_result = json_decode( $body ); |
98
|
|
|
|
99
|
|
|
if ( 201 === $response_code ) { |
100
|
|
|
if ( $ing_result && 'error' === $ing_result->status ) { |
101
|
|
|
$error_msg = $ing_result->transactions[0]->reason; |
102
|
|
|
$error = $ing_result->transactions[0]; |
103
|
|
|
} else { |
104
|
|
|
$result = $ing_result; |
105
|
|
|
} |
106
|
|
|
} else { |
107
|
|
|
$error_msg = ''; |
108
|
|
|
$error = ''; |
109
|
|
|
|
110
|
|
|
if ( $ing_result ) { |
111
|
|
|
$error_msg = $ing_result->error->value; |
112
|
|
|
$error = $ing_result->error; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( 401 === $response_code ) { |
116
|
|
|
// The default error message for an unauthorized API call does not mention the API key in any way. |
117
|
|
|
$error_msg .= ' Please check the API key.'; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ( isset( $error_msg, $error ) ) { |
122
|
|
|
$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $error ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function get_order( $order_id ) { |
129
|
|
|
$result = null; |
130
|
|
|
|
131
|
|
|
$response = $this->send_request( 'orders/' . $order_id . '/', 'GET' ); |
132
|
|
|
|
133
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
|
|
|
134
|
|
|
|
135
|
|
|
if ( 200 === $response_code ) { |
136
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
|
|
|
137
|
|
|
|
138
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
139
|
|
|
$result = json_decode( $body ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get issuers |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function get_issuers() { |
151
|
|
|
$issuers = false; |
152
|
|
|
|
153
|
|
|
$response = $this->send_request( 'ideal/issuers/', 'GET' ); |
154
|
|
|
|
155
|
|
|
$response_code = wp_remote_retrieve_response_code( $response ); |
|
|
|
|
156
|
|
|
|
157
|
|
|
if ( 200 === $response_code ) { |
158
|
|
|
$body = wp_remote_retrieve_body( $response ); |
|
|
|
|
159
|
|
|
|
160
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
161
|
|
|
$result = json_decode( $body ); |
162
|
|
|
|
163
|
|
|
if ( null !== $result ) { |
164
|
|
|
$issuers = array(); |
165
|
|
|
|
166
|
|
|
foreach ( $result as $issuer ) { |
167
|
|
|
$id = Security::filter( $issuer->id ); |
168
|
|
|
$name = Security::filter( $issuer->name ); |
169
|
|
|
|
170
|
|
|
$issuers[ $id ] = $name; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} else { |
174
|
|
|
$body = wp_remote_retrieve_body( $response ); |
175
|
|
|
|
176
|
|
|
$ing_result = json_decode( $body ); |
177
|
|
|
|
178
|
|
|
$error_msg = $ing_result->error->value; |
179
|
|
|
|
180
|
|
|
if ( 401 === $response_code ) { |
181
|
|
|
// An unauthorized API call has nothing to do with the browser of the user in our case, remove to prevent confusion. |
182
|
|
|
$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 ); |
183
|
|
|
|
184
|
|
|
// The default error message for an unauthorized API call does not mention the API key in any way. |
185
|
|
|
$error_msg .= ' Please check the API key.'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$this->error = new WP_Error( 'ing_kassa_compleet_error', $error_msg, $ing_result->error ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $issuers; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|