1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Mollie client. |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2021 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Mollie; |
12
|
|
|
|
13
|
|
|
use Pronamic\WordPress\DateTime\DateTime; |
14
|
|
|
use Pronamic\WordPress\Http\Facades\Http; |
15
|
|
|
use Pronamic\WordPress\Pay\Banks\BankAccountDetails; |
16
|
|
|
use Pronamic\WordPress\Pay\Core\XML\Security; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Title: Mollie |
20
|
|
|
* Description: |
21
|
|
|
* Copyright: 2005-2021 Pronamic |
22
|
|
|
* Company: Pronamic |
23
|
|
|
* |
24
|
|
|
* @author Remco Tolsma |
25
|
|
|
* @version 2.1.4 |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Client { |
29
|
|
|
/** |
30
|
|
|
* Mollie API endpoint URL |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
const API_URL = 'https://api.mollie.com/v2/'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Mollie API Key ID |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $api_key; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructs and initializes an Mollie client object |
45
|
|
|
* |
46
|
|
|
* @param string $api_key Mollie API key. |
47
|
|
|
*/ |
48
|
39 |
|
public function __construct( $api_key ) { |
49
|
39 |
|
$this->api_key = $api_key; |
50
|
39 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Send request with the specified action and parameters |
54
|
|
|
* |
55
|
|
|
* @param string $url URL. |
56
|
|
|
* @param string $method HTTP method to use. |
57
|
|
|
* @param array<string, string|object|null> $data Request data. |
58
|
|
|
* @return object |
59
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
60
|
|
|
* @throws \Exception Throws exception when error occurs. |
61
|
|
|
*/ |
62
|
9 |
|
public function send_request( $url, $method = 'GET', array $data = array() ) { |
63
|
|
|
// Request. |
64
|
9 |
|
$response = Http::request( |
65
|
9 |
|
$url, |
66
|
|
|
array( |
67
|
9 |
|
'method' => $method, |
68
|
|
|
'headers' => array( |
69
|
9 |
|
'Authorization' => 'Bearer ' . $this->api_key, |
70
|
|
|
), |
71
|
9 |
|
'body' => $data, |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
9 |
|
$data = $response->json(); |
76
|
|
|
|
77
|
|
|
// Object. |
78
|
9 |
|
if ( ! \is_object( $data ) ) { |
79
|
|
|
$code = $response->status(); |
80
|
|
|
|
81
|
|
|
throw new \Exception( |
82
|
|
|
\sprintf( 'Could not JSON decode Mollie response to an object (HTTP Status Code: %s).', $code ), |
83
|
|
|
\intval( $code ) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Mollie error from JSON response. |
88
|
9 |
|
if ( isset( $data->status, $data->title, $data->detail ) ) { |
89
|
3 |
|
throw new Error( |
90
|
3 |
|
$data->status, |
91
|
3 |
|
$data->title, |
92
|
3 |
|
$data->detail |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
6 |
|
return $data; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get URL. |
101
|
|
|
* |
102
|
|
|
* @param string $endpoint URL endpoint. |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
9 |
|
public function get_url( $endpoint ) { |
106
|
9 |
|
$url = self::API_URL . $endpoint; |
107
|
|
|
|
108
|
9 |
|
return $url; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Send request to endpoint. |
113
|
|
|
* |
114
|
|
|
* @param string $endpoint Endpoint. |
115
|
|
|
* @param string $method HTTP method to use. |
116
|
|
|
* @param array<string, string|object|null> $data Request data. |
117
|
|
|
* @return object |
118
|
|
|
*/ |
119
|
9 |
|
public function send_request_to_endpoint( $endpoint, $method = 'GET', array $data = array() ) { |
120
|
9 |
|
return $this->send_request( $this->get_url( $endpoint ), $method, $data ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get profile. |
125
|
|
|
* |
126
|
|
|
* @param string $profile Mollie profile ID. |
127
|
|
|
* @return object |
128
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
129
|
|
|
*/ |
130
|
|
|
public function get_profile( $profile ) { |
131
|
|
|
return $this->send_request_to_endpoint( 'profiles/' . $profile, 'GET' ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get current profile. |
136
|
|
|
* |
137
|
|
|
* @return object |
138
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
139
|
|
|
*/ |
140
|
|
|
public function get_current_profile() { |
141
|
|
|
return $this->get_profile( 'me' ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Create payment. |
146
|
|
|
* |
147
|
|
|
* @param PaymentRequest $request Payment request. |
148
|
|
|
* @return object |
149
|
|
|
*/ |
150
|
|
|
public function create_payment( PaymentRequest $request ) { |
151
|
|
|
return $this->send_request_to_endpoint( 'payments', 'POST', $request->get_array() ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get payments. |
156
|
|
|
* |
157
|
|
|
* @return bool|object |
158
|
|
|
*/ |
159
|
|
|
public function get_payments() { |
160
|
|
|
return $this->send_request_to_endpoint( 'payments', 'GET' ); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get payment. |
165
|
|
|
* |
166
|
|
|
* @param string $payment_id Mollie payment ID. |
167
|
|
|
* @param array<string, mixed> $parameters Parameters. |
168
|
|
|
* @return Payment |
169
|
|
|
* @throws \InvalidArgumentException Throws exception on empty payment ID argument. |
170
|
|
|
*/ |
171
|
|
|
public function get_payment( $payment_id, $parameters = array() ) { |
172
|
|
|
if ( empty( $payment_id ) ) { |
173
|
|
|
throw new \InvalidArgumentException( 'Mollie payment ID can not be empty string.' ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$object = $this->send_request_to_endpoint( 'payments/' . $payment_id, 'GET', $parameters ); |
177
|
|
|
|
178
|
|
|
$payment = Payment::from_json( $object ); |
179
|
|
|
|
180
|
|
|
return $payment; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get issuers |
185
|
|
|
* |
186
|
|
|
* @return array<string> |
187
|
|
|
*/ |
188
|
3 |
|
public function get_issuers() { |
189
|
3 |
|
$response = $this->send_request_to_endpoint( 'methods/ideal?include=issuers', 'GET' ); |
190
|
|
|
|
191
|
|
|
$issuers = array(); |
192
|
|
|
|
193
|
|
|
if ( isset( $response->issuers ) ) { |
194
|
|
|
foreach ( $response->issuers as $issuer ) { |
195
|
|
|
$id = Security::filter( $issuer->id ); |
196
|
|
|
$name = Security::filter( $issuer->name ); |
197
|
|
|
|
198
|
|
|
if ( null === $id || null === $name ) { |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$issuers[ $id ] = $name; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $issuers; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get payment methods |
211
|
|
|
* |
212
|
|
|
* @param string $sequence_type Sequence type. |
213
|
|
|
* |
214
|
|
|
* @return array<string> |
215
|
|
|
* @throws \Exception Throws exception for methods on failed request or invalid response. |
216
|
|
|
*/ |
217
|
2 |
|
public function get_payment_methods( $sequence_type = '' ) { |
218
|
|
|
$data = array( |
219
|
2 |
|
'includeWallets' => Methods::APPLE_PAY, |
220
|
|
|
); |
221
|
|
|
|
222
|
2 |
|
if ( '' !== $sequence_type ) { |
223
|
2 |
|
$data['sequenceType'] = $sequence_type; |
224
|
|
|
} |
225
|
|
|
|
226
|
2 |
|
$response = $this->send_request_to_endpoint( 'methods', 'GET', $data ); |
227
|
|
|
|
228
|
2 |
|
$payment_methods = array(); |
229
|
|
|
|
230
|
2 |
|
if ( ! isset( $response->_embedded ) ) { |
231
|
|
|
throw new \Exception( 'No embedded data in Mollie response.' ); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
if ( isset( $response->_embedded->methods ) ) { |
235
|
2 |
|
foreach ( $response->_embedded->methods as $payment_method ) { |
236
|
2 |
|
$id = Security::filter( $payment_method->id ); |
237
|
2 |
|
$name = Security::filter( $payment_method->description ); |
238
|
|
|
|
239
|
2 |
|
if ( null === $id || null === $name ) { |
240
|
|
|
continue; |
241
|
|
|
} |
242
|
|
|
|
243
|
2 |
|
$payment_methods[ $id ] = $name; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
2 |
|
return $payment_methods; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Create customer. |
252
|
|
|
* |
253
|
|
|
* @param Customer $customer Customer. |
254
|
|
|
* @return Customer |
255
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
256
|
|
|
* @since 1.1.6 |
257
|
|
|
*/ |
258
|
|
|
public function create_customer( Customer $customer ) { |
259
|
|
|
$response = $this->send_request_to_endpoint( |
260
|
|
|
'customers', |
261
|
|
|
'POST', |
262
|
|
|
$customer->get_array() |
263
|
|
|
); |
264
|
|
|
|
265
|
|
|
if ( \property_exists( $response, 'id' ) ) { |
266
|
|
|
$customer->set_id( $response->id ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $customer; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get customer. |
274
|
|
|
* |
275
|
|
|
* @param string $customer_id Mollie customer ID. |
276
|
|
|
* |
277
|
|
|
* @return null|object |
278
|
|
|
* @throws \InvalidArgumentException Throws exception on empty customer ID argument. |
279
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
280
|
|
|
*/ |
281
|
4 |
|
public function get_customer( $customer_id ) { |
282
|
4 |
|
if ( empty( $customer_id ) ) { |
283
|
|
|
throw new \InvalidArgumentException( 'Mollie customer ID can not be empty string.' ); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
try { |
287
|
4 |
|
return $this->send_request_to_endpoint( 'customers/' . $customer_id, 'GET' ); |
288
|
|
|
} catch ( Error $error ) { |
289
|
|
|
if ( 404 === $error->get_status() ) { |
290
|
|
|
return null; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
throw $error; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Create mandate. |
299
|
|
|
* |
300
|
|
|
* @param string $customer_id Customer ID. |
301
|
|
|
* @param BankAccountDetails $consumer_bank_details Consumer bank details. |
302
|
|
|
* @return object |
303
|
|
|
* @throws Error Throws Error when Mollie error occurs. |
304
|
|
|
* @since unreleased |
305
|
|
|
*/ |
306
|
|
|
public function create_mandate( $customer_id, BankAccountDetails $consumer_bank_details ) { |
307
|
|
|
$response = $this->send_request_to_endpoint( |
308
|
|
|
'customers/' . $customer_id . '/mandates', |
309
|
|
|
'POST', |
310
|
|
|
array( |
311
|
|
|
'method' => Methods::DIRECT_DEBIT, |
312
|
|
|
'consumerName' => $consumer_bank_details->get_name(), |
313
|
|
|
'consumerAccount' => $consumer_bank_details->get_iban(), |
314
|
|
|
) |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
return $response; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get mandate. |
322
|
|
|
* |
323
|
|
|
* @param string $mandate_id Mollie mandate ID. |
324
|
|
|
* @param string $customer_id Mollie customer ID. |
325
|
|
|
* @return object |
326
|
|
|
* @throws \InvalidArgumentException Throws exception on empty mandate ID argument. |
327
|
|
|
*/ |
328
|
|
|
public function get_mandate( $mandate_id, $customer_id ) { |
329
|
|
|
if ( '' === $mandate_id ) { |
330
|
|
|
throw new \InvalidArgumentException( 'Mollie mandate ID can not be empty string.' ); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
if ( '' === $customer_id ) { |
334
|
|
|
throw new \InvalidArgumentException( 'Mollie customer ID can not be empty string.' ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $this->send_request_to_endpoint( 'customers/' . $customer_id . '/mandates/' . $mandate_id, 'GET' ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Get mandates for customer. |
342
|
|
|
* |
343
|
|
|
* @param string $customer_id Mollie customer ID. |
344
|
|
|
* @return object |
345
|
|
|
* @throws \InvalidArgumentException Throws exception on empty customer ID argument. |
346
|
|
|
*/ |
347
|
|
|
public function get_mandates( $customer_id ) { |
348
|
|
|
if ( '' === $customer_id ) { |
349
|
|
|
throw new \InvalidArgumentException( 'Mollie customer ID can not be empty string.' ); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return $this->send_request_to_endpoint( 'customers/' . $customer_id . '/mandates?limit=250', 'GET' ); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Is there a valid mandate for customer? |
357
|
|
|
* |
358
|
|
|
* @param string $customer_id Mollie customer ID. |
359
|
|
|
* @param string|null $payment_method Payment method to find mandates for. |
360
|
|
|
* @param string|null $search Search. |
361
|
|
|
* |
362
|
|
|
* @return string|bool |
363
|
|
|
* @throws \Exception Throws exception for mandates on failed request or invalid response. |
364
|
|
|
*/ |
365
|
|
|
public function has_valid_mandate( $customer_id, $payment_method = null, $search = null ) { |
366
|
|
|
$mandates = $this->get_mandates( $customer_id ); |
367
|
|
|
|
368
|
|
|
$mollie_method = Methods::transform( $payment_method ); |
369
|
|
|
|
370
|
|
|
if ( ! isset( $mandates->_embedded ) ) { |
371
|
|
|
throw new \Exception( 'No embedded data in Mollie response.' ); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
foreach ( $mandates->_embedded->mandates as $mandate ) { |
375
|
|
|
if ( null !== $mollie_method && $mollie_method !== $mandate->method ) { |
376
|
|
|
continue; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
// Search consumer account or card number. |
380
|
|
|
if ( null !== $search ) { |
381
|
|
|
switch ( $mollie_method ) { |
382
|
|
|
case Methods::DIRECT_DEBIT: |
383
|
|
|
case Methods::PAYPAL: |
384
|
|
|
if ( $search !== $mandate->details->consumerAccount ) { |
385
|
|
|
continue 2; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
break; |
389
|
|
|
case Methods::CREDITCARD: |
390
|
|
|
if ( $search !== $mandate->details->cardNumber ) { |
391
|
|
|
continue 2; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
break; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
if ( 'valid' === $mandate->status ) { |
399
|
|
|
return $mandate->id; |
400
|
|
|
} |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
return false; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get formatted date and time of first valid mandate. |
408
|
|
|
* |
409
|
|
|
* @param string $customer_id Mollie customer ID. |
410
|
|
|
* @param string $payment_method Payment method. |
411
|
|
|
* |
412
|
|
|
* @return null|DateTime |
413
|
|
|
* @throws \Exception Throws exception for mandates on failed request or invalid response. |
414
|
|
|
*/ |
415
|
|
|
public function get_first_valid_mandate_datetime( $customer_id, $payment_method = null ) { |
416
|
|
|
$mandates = $this->get_mandates( $customer_id ); |
417
|
|
|
|
418
|
|
|
$mollie_method = Methods::transform( $payment_method ); |
419
|
|
|
|
420
|
|
|
if ( ! isset( $mandates->_embedded ) ) { |
421
|
|
|
throw new \Exception( 'No embedded data in Mollie response.' ); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
foreach ( $mandates->_embedded->mandates as $mandate ) { |
425
|
|
|
if ( $mollie_method !== $mandate->method ) { |
426
|
|
|
continue; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
if ( 'valid' !== $mandate->status ) { |
430
|
|
|
continue; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
if ( ! isset( $valid_mandates ) ) { |
434
|
|
|
$valid_mandates = array(); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
// @codingStandardsIgnoreStart |
438
|
|
|
$valid_mandates[ $mandate->createdAt ] = $mandate; |
439
|
|
|
// @codingStandardsIgnoreEnd |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if ( isset( $valid_mandates ) ) { |
443
|
|
|
ksort( $valid_mandates ); |
444
|
|
|
|
445
|
|
|
$mandate = array_shift( $valid_mandates ); |
446
|
|
|
|
447
|
|
|
// @codingStandardsIgnoreStart |
448
|
|
|
$create_date = new DateTime( $mandate->createdAt ); |
449
|
|
|
// @codingStandardsIgnoreEnd |
450
|
|
|
|
451
|
|
|
return $create_date; |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
return null; |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Create refund. |
459
|
|
|
* |
460
|
|
|
* @param string $payment_id Mollie payment ID. |
461
|
|
|
* @param RefundRequest $refund_request Refund request. |
462
|
|
|
* @return Refund |
463
|
|
|
*/ |
464
|
|
|
public function create_refund( $payment_id, RefundRequest $refund_request ) { |
465
|
|
|
$response = $this->send_request_to_endpoint( 'payments/' . $payment_id . '/refunds', 'POST', $refund_request->get_array() ); |
466
|
|
|
|
467
|
|
|
return Refund::from_json( $response ); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Get payment chargebacks. |
472
|
|
|
* |
473
|
|
|
* @param string $payment_id Mollie payment ID. |
474
|
|
|
* @param array<string, mixed> $parameters Parameters. |
475
|
|
|
* @return array<Chargeback> |
476
|
|
|
*/ |
477
|
|
|
public function get_payment_chargebacks( $payment_id, $parameters ) { |
478
|
|
|
$object = $this->send_request_to_endpoint( 'payments/' . $payment_id . '/chargebacks', 'GET', $parameters ); |
479
|
|
|
|
480
|
|
|
$chargebacks = array(); |
481
|
|
|
|
482
|
|
|
if ( \property_exists( $object, '_embedded' ) && \property_exists( $object->_embedded, 'chargebacks' ) ) { |
483
|
|
|
foreach ( $object->_embedded->chargebacks as $chargeback_object ) { |
484
|
|
|
$chargebacks[] = Chargeback::from_json( $chargeback_object ); |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
return $chargebacks; |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|