|
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\OmniKassa2 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
|
12
|
|
|
|
|
13
|
|
|
use Pronamic\WordPress\Pay\Facades\Http; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Client. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Remco Tolsma |
|
19
|
|
|
* @version 2.2.4 |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Client { |
|
23
|
|
|
/** |
|
24
|
|
|
* URL OmniKassa API. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
const URL_PRODUCTION = 'https://betalen.rabobank.nl/omnikassa-api/'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* URL OmniKassa sandbox API. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
const URL_SANDBOX = 'https://betalen.rabobank.nl/omnikassa-api-sandbox/'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Error |
|
39
|
|
|
* |
|
40
|
|
|
* @var \WP_Error |
|
41
|
|
|
*/ |
|
42
|
|
|
private $error; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The URL. |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
private $url; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Refresh token. |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
private $refresh_token; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Signing key. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $signing_key; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Error. |
|
67
|
|
|
* |
|
68
|
|
|
* @return \WP_Error |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get_error() { |
|
71
|
|
|
return $this->error; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the URL. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function get_url() { |
|
80
|
|
|
return $this->url; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the action URL |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $url URL. |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function set_url( $url ) { |
|
90
|
|
|
$this->url = $url; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get refresh token. |
|
95
|
|
|
* |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function get_refresh_token() { |
|
99
|
|
|
return $this->refresh_token; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Set refresh token. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $refresh_token Refresh token. |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function set_refresh_token( $refresh_token ) { |
|
109
|
|
|
$this->refresh_token = $refresh_token; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get signing key. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function get_signing_key() { |
|
118
|
|
|
return $this->signing_key; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set signing key. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $signing_key Signing key. |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public function set_signing_key( $signing_key ) { |
|
128
|
|
|
$this->signing_key = $signing_key; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Request URL with specified method, token. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $method HTTP request method. |
|
135
|
|
|
* @param string $endpoint URL endpoint to request. |
|
136
|
|
|
* @param string $token Authorization token. |
|
137
|
|
|
* @param object|null $object Object. |
|
138
|
|
|
* @return object |
|
139
|
|
|
* @throws \Exception Throws exception when Rabobank OmniKassa 2.0 response is not what we expect. |
|
140
|
|
|
*/ |
|
141
|
|
|
private function request( $method, $endpoint, $token, $object = null ) { |
|
142
|
|
|
// URL. |
|
143
|
|
|
$url = $this->get_url() . $endpoint; |
|
144
|
|
|
|
|
145
|
|
|
/* |
|
146
|
|
|
* Arguments. |
|
147
|
|
|
* |
|
148
|
|
|
* The `timeout` argument is intentionally increased from the WordPress |
|
149
|
|
|
* default `5` seconds to `30`. This is mainly important for AfterPay |
|
150
|
|
|
* order announcements requests, but it can't hurt for other requests. |
|
151
|
|
|
* It is probably better to wait a little longer for an answer than |
|
152
|
|
|
* having timeout issues while starting a payment or requesting a |
|
153
|
|
|
* payment status. The value can also be adjusted via the |
|
154
|
|
|
* `pronamic_pay_omnikassa_2_request_args` filter. |
|
155
|
|
|
*/ |
|
156
|
|
|
$args = array( |
|
157
|
|
|
'method' => $method, |
|
158
|
|
|
'headers' => array( |
|
159
|
|
|
'Authorization' => 'Bearer ' . $token, |
|
160
|
|
|
), |
|
161
|
|
|
'timeout' => 30, |
|
162
|
|
|
); |
|
163
|
|
|
|
|
164
|
|
|
if ( null !== $object ) { |
|
165
|
|
|
$args['headers']['Content-Type'] = 'application/json'; |
|
166
|
|
|
|
|
167
|
|
|
$args['body'] = \wp_json_encode( $object ); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$args = \apply_filters( 'pronamic_pay_omnikassa_2_request_args', $args ); |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Build cURL command for debug purposes. |
|
174
|
|
|
* |
|
175
|
|
|
* @link https://curl.haxx.se/ |
|
176
|
|
|
*/ |
|
177
|
|
|
|
|
178
|
|
|
// phpcs:disable SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
179
|
|
|
|
|
180
|
|
|
$curl = ''; |
|
181
|
|
|
|
|
182
|
|
|
$tab = "\t"; |
|
183
|
|
|
$eol = ' \\' . \PHP_EOL; |
|
184
|
|
|
|
|
185
|
|
|
$curl .= \sprintf( 'curl --request %s %s', $method, \escapeshellarg( $url ) ) . $eol; |
|
186
|
|
|
$curl .= $tab . \sprintf( '--header %s', \escapeshellarg( 'Authorization: Bearer ' . $token ) ) . $eol; |
|
187
|
|
|
$curl .= $tab . \sprintf( '--header %s', \escapeshellarg( 'Content-Type: application/json' ) ) . $eol; |
|
188
|
|
|
$curl .= $tab . \sprintf( '--data %s', \escapeshellarg( \strval( \wp_json_encode( $object ) ) ) ) . $eol; |
|
189
|
|
|
$curl .= $tab . \sprintf( |
|
190
|
|
|
'--user-agent %s', |
|
191
|
|
|
\escapeshellarg( 'WordPress/' . \get_bloginfo( 'version' ) . '; ' . \get_bloginfo( 'url' ) ) |
|
192
|
|
|
) . $eol; |
|
193
|
|
|
$curl .= $tab . '--verbose'; |
|
194
|
|
|
|
|
195
|
|
|
// phpcs:enable |
|
196
|
|
|
|
|
197
|
|
|
// Request. |
|
198
|
|
|
$response = Http::request( $url, $args ); |
|
199
|
|
|
|
|
200
|
|
|
$data = $response->json(); |
|
201
|
|
|
|
|
202
|
|
|
// Object. |
|
203
|
|
|
if ( ! \is_object( $data ) ) { |
|
204
|
|
|
throw new \Exception( |
|
205
|
|
|
\sprintf( |
|
206
|
|
|
'Could not JSON decode OmniKassa 2.0 response to an object, HTTP response: "%s %s", HTTP body length: "%d".', |
|
207
|
|
|
$response->status(), |
|
208
|
|
|
$response->message(), |
|
|
|
|
|
|
209
|
|
|
\strlen( $response->body() ) |
|
210
|
|
|
), |
|
211
|
|
|
\intval( $response->status() ) |
|
212
|
|
|
); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
// Error. |
|
216
|
|
|
if ( isset( $data->errorCode ) ) { |
|
217
|
|
|
$error = Error::from_object( $data ); |
|
218
|
|
|
|
|
219
|
|
|
throw $error; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
// Ok. |
|
223
|
|
|
return $data; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get access token. |
|
228
|
|
|
* |
|
229
|
|
|
* @return object |
|
230
|
|
|
*/ |
|
231
|
|
|
public function get_access_token_data() { |
|
232
|
|
|
return $this->request( 'GET', 'gatekeeper/refresh', $this->get_refresh_token() ); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Order announce. |
|
237
|
|
|
* |
|
238
|
|
|
* @param Config $config Config. |
|
239
|
|
|
* @param Order $order Order. |
|
240
|
|
|
* @return OrderAnnounceResponse |
|
241
|
|
|
*/ |
|
242
|
|
|
public function order_announce( $config, Order $order ) { |
|
243
|
|
|
$result = $this->request( 'POST', 'order/server/api/v2/order', $config->access_token, $order ); |
|
244
|
|
|
|
|
245
|
|
|
return OrderAnnounceResponse::from_object( $result ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get order results by the notification token. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $notification_token Notification token. |
|
252
|
|
|
* @return OrderResults |
|
253
|
|
|
*/ |
|
254
|
|
|
public function get_order_results( $notification_token ) { |
|
255
|
|
|
$result = $this->request( |
|
256
|
|
|
'GET', |
|
257
|
|
|
'order/server/api/events/results/merchant.order.status.changed', |
|
258
|
|
|
$notification_token |
|
259
|
|
|
); |
|
260
|
|
|
|
|
261
|
|
|
return OrderResults::from_object( $result ); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.