1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\PayNL; |
4
|
|
|
|
5
|
|
|
use Pronamic\WordPress\Pay\Core\XML\Security; |
6
|
|
|
use Pronamic\WordPress\Pay\Gateways\PayNL\Error as PayNL_Error; |
7
|
|
|
use stdClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Title: Pay.nl client |
11
|
|
|
* Description: |
12
|
|
|
* Copyright: 2005-2021 Pronamic |
13
|
|
|
* Company: Pronamic |
14
|
|
|
* |
15
|
|
|
* @author Remco Tolsma |
16
|
|
|
* @version 2.0.4 |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
class Client { |
20
|
|
|
/** |
21
|
|
|
* API URL |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const API_URL = 'https://rest-api.pay.nl/%s/%s/%s/%s/'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Token. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $token; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Service id. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $service_id; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct and initialize an Pay.nl client |
43
|
|
|
* |
44
|
|
|
* @param string $token Token. |
45
|
|
|
* @param string $service_id Service ID. |
46
|
|
|
*/ |
47
|
1 |
|
public function __construct( $token, $service_id ) { |
48
|
1 |
|
$this->token = $token; |
49
|
1 |
|
$this->service_id = $service_id; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get Pay.nl API URL. |
54
|
|
|
* |
55
|
|
|
* @param string $version Version. |
56
|
|
|
* @param string $namespace Namespace. |
57
|
|
|
* @param string $method Method. |
58
|
|
|
* @param string $output Output. |
59
|
|
|
* @param array $parameters Parameters. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
1 |
|
private function get_url( $version, $namespace, $method, $output, $parameters = array() ) { |
64
|
1 |
|
return add_query_arg( |
65
|
1 |
|
rawurlencode_deep( $parameters ), |
66
|
1 |
|
sprintf( |
67
|
1 |
|
self::API_URL, |
68
|
|
|
$version, |
69
|
|
|
$namespace, |
70
|
|
|
$method, |
71
|
|
|
$output |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Send request to the specified URL. |
78
|
|
|
* |
79
|
|
|
* @param string $version Version. |
80
|
|
|
* @param string $namespace Namespace. |
81
|
|
|
* @param string $method Method. |
82
|
|
|
* @param string $output Output. |
83
|
|
|
* @param array $parameters Parameters. |
84
|
|
|
* |
85
|
|
|
* @return null|array|stdClass Response object or null if request failed. |
86
|
|
|
*/ |
87
|
1 |
|
private function send_request( $version, $namespace, $method, $output, $parameters = array() ) { |
88
|
1 |
|
$url = $this->get_url( $version, $namespace, $method, $output, $parameters ); |
89
|
|
|
|
90
|
1 |
|
$response = wp_remote_get( $url ); |
91
|
|
|
|
92
|
1 |
|
if ( is_wp_error( $response ) ) { |
93
|
|
|
throw new \Exception( __( 'Unknown response from Pay.nl.', 'pronamic_ideal' ) ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Body. |
97
|
1 |
|
$body = wp_remote_retrieve_body( $response ); |
98
|
|
|
|
99
|
1 |
|
$result = json_decode( $body ); |
100
|
|
|
|
101
|
|
|
// Result is array. |
102
|
1 |
|
if ( is_array( $result ) ) { |
103
|
|
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Result is object |
107
|
|
|
// NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. |
108
|
1 |
|
if ( ! is_object( $result ) ) { |
109
|
|
|
throw new \Exception( __( 'Unknown response from Pay.nl error.', 'pronamic_ideal' ) ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// Error. |
113
|
1 |
|
if ( isset( $result->request->errorId, $result->request->errorMessage ) && ! empty( $result->request->errorId ) ) { |
114
|
1 |
|
$pay_nl_error = new PayNL_Error( $result->request->errorId, $result->request->errorMessage ); |
115
|
|
|
|
116
|
1 |
|
throw new \Exception( (string) $pay_nl_error ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Check result (v3). |
120
|
|
|
if ( isset( $result->status, $result->error ) && ! filter_var( $result->status, FILTER_VALIDATE_BOOLEAN ) && ! empty( $result->error ) ) { |
121
|
|
|
throw new \Exception( $result->error ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Check result (v4). |
125
|
|
|
if ( isset( $result->request, $result->request->result ) && '1' !== $result->request->result ) { |
126
|
|
|
throw new \Exception( __( 'Unknown Pay.nl error.', 'pronamic_ideal' ) ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Return result. |
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Transaction start |
135
|
|
|
* |
136
|
|
|
* @param int $amount Transaction amount. |
137
|
|
|
* @param string $ip_address IP address. |
138
|
|
|
* @param string $finish_url Finish URL. |
139
|
|
|
* @param array $request_param Request parameters. |
140
|
|
|
* |
141
|
|
|
* @return null|stdClass |
142
|
|
|
* |
143
|
|
|
* @link https://admin.pay.nl/docpanel/api/Transaction/start/4 |
144
|
|
|
*/ |
145
|
|
|
public function transaction_start( $amount, $ip_address, $finish_url, $request_param = array() ) { |
146
|
|
|
$parameters = array_merge( |
147
|
|
|
$request_param, |
148
|
|
|
array( |
149
|
|
|
'token' => $this->token, |
150
|
|
|
'serviceId' => $this->service_id, |
151
|
|
|
'amount' => $amount, |
152
|
|
|
'ipAddress' => $ip_address, |
153
|
|
|
'finishUrl' => $finish_url, |
154
|
|
|
) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
// Request. |
158
|
|
|
$result = $this->send_request( 'v4', 'Transaction', 'start', 'json', $parameters ); |
159
|
|
|
|
160
|
|
|
if ( is_array( $result ) ) { |
|
|
|
|
161
|
|
|
return null; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Return result. |
165
|
|
|
return $result; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Transaction info. |
170
|
|
|
* |
171
|
|
|
* @param string $transaction_id Transaction ID. |
172
|
|
|
* |
173
|
|
|
* @link https://admin.pay.nl/docpanel/api/Transaction/info/4 |
174
|
|
|
* |
175
|
|
|
* @return null|array|stdClass |
176
|
|
|
*/ |
177
|
|
|
public function transaction_info( $transaction_id ) { |
178
|
|
|
// Request. |
179
|
|
|
$result = $this->send_request( |
180
|
|
|
'v4', |
181
|
|
|
'Transaction', |
182
|
|
|
'info', |
183
|
|
|
'json', |
184
|
|
|
array( |
185
|
|
|
'token' => $this->token, |
186
|
|
|
'transactionId' => $transaction_id, |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
// Return result. |
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get issuers |
196
|
|
|
* |
197
|
|
|
* @return array|bool |
198
|
|
|
*/ |
199
|
1 |
|
public function get_issuers() { |
200
|
|
|
// Request. |
201
|
1 |
|
$result = $this->send_request( |
202
|
1 |
|
'v4', |
203
|
1 |
|
'Transaction', |
204
|
1 |
|
'getService', |
205
|
1 |
|
'json', |
206
|
|
|
array( |
207
|
1 |
|
'token' => $this->token, |
208
|
1 |
|
'serviceId' => $this->service_id, |
209
|
1 |
|
'paymentMethodId' => Methods::IDEAL, |
210
|
|
|
) |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
if ( ! is_object( $result ) ) { |
|
|
|
|
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// Country option list. |
218
|
|
|
if ( ! isset( $result->countryOptionList ) ) { |
219
|
|
|
throw new \Exception( __( 'Unknown Pay.nl error.', 'pronamic_ideal' ) ); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
// Ok. |
223
|
|
|
$issuers = array(); |
224
|
|
|
|
225
|
|
|
foreach ( $result->countryOptionList as $countries ) { |
226
|
|
|
foreach ( $countries->paymentOptionList as $payment_method ) { |
227
|
|
|
if ( Methods::IDEAL !== $payment_method->id ) { |
228
|
|
|
continue; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
foreach ( $payment_method->paymentOptionSubList as $issuer ) { |
232
|
|
|
$id = Security::filter( $issuer->id ); |
233
|
|
|
$name = Security::filter( $issuer->name ); |
234
|
|
|
|
235
|
|
|
$issuers[ $id ] = $name; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $issuers; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|