Failed Conditions
Push — develop ( fc7b68...4eeca8 )
by Reüel
05:13
created

src/Client.php (1 issue)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Buckaroo;
4
5
/**
6
 * Title: Buckaroo client
7
 * Description:
8
 * Copyright: 2005-2020 Pronamic
9
 * Company: Pronamic
10
 *
11
 * @author Remco Tolsma
12
 * @version 2.0.4
13
 * @since 1.0.0
14
 */
15
class Client {
16
	/**
17
	 * Gateway URL
18
	 *
19
	 * @var string
20
	 */
21
	const GATEWAY_URL = 'https://checkout.buckaroo.nl/html/';
22
23
	/**
24
	 * Gateway test URL
25
	 *
26
	 * @var string
27
	 */
28
	const GATEWAY_TEST_URL = 'https://testcheckout.buckaroo.nl/html/';
29
30
	/**
31
	 * Gateway Name-Value-Pair URL
32
	 *
33
	 * @var string
34
	 */
35
	const GATEWAY_NVP_URL = 'https://checkout.buckaroo.nl/nvp/';
36
37
	/**
38
	 * Gateway Name-Value-Pair test URL
39
	 *
40
	 * @var string
41
	 */
42
	const GATEWAY_NVP_TEST_URL = 'https://testcheckout.buckaroo.nl/nvp/';
43
44
	/**
45
	 * Indicator for the iDEAL payment method
46
	 *
47
	 * @var string
48
	 */
49
	const PAYMENT_METHOD_IDEAL = 'ideal';
50
51
	/**
52
	 * The payment server URL
53
	 *
54
	 * @var string
55
	 */
56
	private $payment_server_url;
57
58
	/**
59
	 * The amount
60
	 *
61
	 * @var int
62
	 */
63
	private $amount;
64
65
	/**
66
	 * The website key
67
	 *
68
	 * @var string
69
	 */
70
	private $website_key;
71
72
	/**
73
	 * The secret key
74
	 *
75
	 * @var string
76
	 */
77
	private $secret_key;
78
79
	/**
80
	 * The payment method
81
	 *
82
	 * @var string
83
	 */
84
	private $payment_method;
85
86
	/**
87
	 * The iDEAL issuer
88
	 *
89
	 * @since 1.2.4
90
	 * @var string
91
	 */
92
	private $ideal_issuer;
93
94
	/**
95
	 * The country code (culture)
96
	 *
97
	 * @var string
98
	 */
99
	private $culture;
100
101
	/**
102
	 * The currency
103
	 *
104
	 * @var string
105
	 */
106
	private $currency;
107
108
	/**
109
	 * The invoice number
110
	 *
111
	 * @var string
112
	 */
113
	private $invoice_number;
114
115
	/**
116
	 * The description
117
	 *
118
	 * @var string
119
	 */
120
	private $description;
121
122
	/**
123
	 * The return url
124
	 *
125
	 * @var string
126
	 */
127
	private $return_url;
128
129
	/**
130
	 * The return reject url
131
	 *
132
	 * @var string
133
	 */
134
	private $return_reject_url;
135
136
	/**
137
	 * The return error url
138
	 *
139
	 * @var string
140
	 */
141
	private $return_error_url;
142
143
	/**
144
	 * The return cancel url
145
	 *
146
	 * @var string
147
	 */
148
	private $return_cancel_url;
149
150
	/**
151
	 * Push URL
152
	 *
153
	 * @var string
154
	 */
155
	private $push_url;
156
157
	/**
158
	 * Requested services
159
	 *
160
	 * @var array
161
	 */
162
	private $requested_services;
163
164
	/**
165
	 * Excluded services
166
	 *
167
	 * @var array
168
	 */
169
	private $excluded_services;
170
171
	/**
172
	 * Pronamic payment ID
173
	 *
174
	 * @var array
175
	 */
176
	private $payment_id;
177
178
	/**
179
	 * Constructs and initialize a iDEAL kassa object
180
	 */
181 1
	public function __construct() {
182 1
		$this->set_payment_server_url( self::GATEWAY_URL );
183
184 1
		$this->requested_services = array();
185 1
	}
186
187
	/**
188
	 * Get the payment server URL
189
	 *
190
	 * @return string the payment server URL
191
	 */
192
	public function get_payment_server_url() {
193
		return $this->payment_server_url;
194
	}
195
196
	/**
197
	 * Set the payment server URL
198
	 *
199
	 * @param string $url an URL
200
	 */
201 1
	public function set_payment_server_url( $url ) {
202 1
		$this->payment_server_url = $url;
203 1
	}
204
205 1
	public function get_website_key() {
206 1
		return $this->website_key;
207
	}
208
209 1
	public function set_website_key( $website_key ) {
210 1
		$this->website_key = $website_key;
211 1
	}
212
213 1
	public function get_secret_key() {
214 1
		return $this->secret_key;
215
	}
216
217 1
	public function set_secret_key( $secret_key ) {
218 1
		$this->secret_key = $secret_key;
219 1
	}
220
221
	public function get_payment_method() {
222
		return $this->payment_method;
223
	}
224
225
	public function set_payment_method( $payment_method ) {
226
		$this->payment_method = $payment_method;
227
	}
228
229
	/**
230
	 * Get iDEAL issuer.
231
	 *
232
	 * @since 1.2.4
233
	 * @return string
234
	 */
235
	public function get_ideal_issuer() {
236
		return $this->ideal_issuer;
237
	}
238
239
	/**
240
	 * Set iDEAL issuer.
241
	 *
242
	 * @since 1.2.4
243
	 *
244
	 * @param string $issuer
245
	 */
246
	public function set_ideal_issuer( $issuer ) {
247
		$this->ideal_issuer = $issuer;
248
	}
249
250
	public function get_requested_services() {
251
		return $this->requested_services;
252
	}
253
254
	public function add_requested_service( $service ) {
255
		$this->requested_services[] = $service;
256
	}
257
258
	public function get_excluded_services() {
259
		return $this->excluded_services;
260
	}
261
262
	public function set_excluded_services( $service ) {
263
		$this->excluded_services = $service;
264
	}
265
266
	public function get_culture() {
267
		return $this->culture;
268
	}
269
270
	public function set_culture( $culture ) {
271
		$this->culture = $culture;
272
	}
273
274
	public function get_currency() {
275
		return $this->currency;
276
	}
277
278
	public function set_currency( $currency ) {
279
		$this->currency = $currency;
280
	}
281
282
	public function get_invoice_number() {
283
		return $this->invoice_number;
284
	}
285
286
	public function set_invoice_number( $invoice_number ) {
287
		$this->invoice_number = $invoice_number;
288
	}
289
290
	public function get_description() {
291
		return $this->description;
292
	}
293
294
	public function set_description( $description ) {
295
		$this->description = $description;
296
	}
297
298
	/**
299
	 * Get amount.
300
	 *
301
	 * @return int
302
	 */
303
	public function get_amount() {
304
		return $this->amount;
305
	}
306
307
	/**
308
	 * Set amount.
309
	 *
310
	 * @param int $amount Amount.
311
	 */
312
	public function set_amount( $amount ) {
313
		$this->amount = $amount;
314
	}
315
316
	/**
317
	 * Get return URL
318
	 *
319
	 * @return string
320
	 */
321
	public function get_return_url() {
322
		return $this->return_url;
323
	}
324
325
	/**
326
	 * Set return URL
327
	 *
328
	 * @param string $url Return URL.
329
	 */
330
	public function set_return_url( $url ) {
331
		$this->return_url = $url;
332
	}
333
334
	/**
335
	 * Get return reject URL
336
	 *
337
	 * @return string
338
	 */
339
	public function get_return_reject_url() {
340
		return $this->return_reject_url;
341
	}
342
343
	/**
344
	 * Set return reject URL
345
	 *
346
	 * @param string $url Return reject URL.
347
	 */
348
	public function set_return_reject_url( $url ) {
349
		$this->return_reject_url = $url;
350
	}
351
352
	/**
353
	 * Get return error URL
354
	 *
355
	 * @return string
356
	 */
357
	public function get_return_error_url() {
358
		return $this->return_error_url;
359
	}
360
361
	/**
362
	 * Set return error URL
363
	 *
364
	 * @param string $url Return error URL.
365
	 */
366
	public function set_return_error_url( $url ) {
367
		$this->return_error_url = $url;
368
	}
369
370
	/**
371
	 * Get return cancel URL
372
	 *
373
	 * @return string
374
	 */
375
	public function get_return_cancel_url() {
376
		return $this->return_cancel_url;
377
	}
378
379
	/**
380
	 * Set return cancel URL
381
	 *
382
	 * @param string $url Return cancel URL.
383
	 */
384
	public function set_return_cancel_url( $url ) {
385
		$this->return_cancel_url = $url;
386
	}
387
388
	/**
389
	 * Get push URL
390
	 *
391
	 * @return string
392
	 */
393
	public function get_push_url() {
394
		return $this->push_url;
395
	}
396
397
	/**
398
	 * Set push URL
399
	 *
400
	 * @param string $url Push URL.
401
	 */
402
	public function set_push_url( $url ) {
403
		$this->push_url = $url;
404
	}
405
406
	/**
407
	 * Get Pronamic payment ID
408
	 *
409
	 * @return string
410
	 */
411
	public function get_payment_id() {
412
		return $this->payment_id;
413
	}
414
415
	/**
416
	 * Set Pronamic payment ID
417
	 *
418
	 * @param string $payment_id Payment ID.
419
	 */
420
	public function set_payment_id( $payment_id ) {
421
		$this->payment_id = $payment_id;
422
	}
423
424
	/**
425
	 * Get issuers
426
	 *
427
	 * @since 1.2.4
428
	 * @link http://support.buckaroo.nl/index.php/Service_iDEAL#iDEAL_banken_lijst_opvragen
429
	 * @return array
430
	 */
431 1
	public function get_issuers() {
432 1
		$issuers = array();
433
434 1
		$url = add_query_arg( 'op', 'TransactionRequestSpecification', self::GATEWAY_NVP_TEST_URL );
435
436
		$data = array(
437 1
			'brq_websitekey'        => $this->get_website_key(),
438 1
			'brq_services'          => 'ideal',
439 1
			'brq_latestversiononly' => 'True',
440
		);
441
442 1
		$signature = Security::create_signature( $data, $this->get_secret_key() );
443
444 1
		$data[ Parameters::SIGNATURE ] = $signature;
445
446 1
		$result = wp_remote_post(
447 1
			$url,
448
			array(
449 1
				'body' => http_build_query( $data ),
450
			)
451
		);
452
453 1
		$body = wp_remote_retrieve_body( $result );
454
455 1
		wp_parse_str( $body, $data );
456
457 1
		$data = Util::transform_flat_response( $data );
458
459 1
		$error_msg = __( 'Unable to retrieve issuers from Buckaroo.', 'pronamic_ideal' );
460
461 1
		if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
462
			throw new \Exception( $error_msg );
463
		}
464
465 1
		if ( isset( $data['BRQ_APIRESULT'] ) && 'Fail' === $data['BRQ_APIRESULT'] ) {
466
			if ( isset( $data['BRQ_APIERRORMESSAGE'] ) ) {
467
				$error_msg = sprintf( '%s %s', $error_msg, $data['BRQ_APIERRORMESSAGE'] );
468
			}
469
470
			throw new \Exception( $error_msg );
471
		}
472
473 1
		if ( ! isset( $data['BRQ_SERVICES'] ) ) {
474
			return $issuers;
475
		}
476
477 1
		foreach ( $data['BRQ_SERVICES'] as $service ) {
478 1
			if ( ! isset( $service['NAME'], $service['VERSION'], $service['ACTIONDESCRIPTION'] ) ) {
479
				return $issuers;
480
			}
481
482 1
			if ( PaymentMethods::IDEAL !== $service['NAME'] ) {
483
				continue;
484
			}
485
486 1
			foreach ( $service['ACTIONDESCRIPTION'] as $action ) {
487 1
				if ( ! isset( $action['NAME'], $action['REQUESTPARAMETERS'] ) ) {
488
					return $issuers;
489
				}
490
491 1
				if ( 'Pay' !== $action['NAME'] ) {
492 1
					continue;
493
				}
494
495 1
				foreach ( $action['REQUESTPARAMETERS'] as $parameter ) {
496
497 1
					if ( ! isset( $parameter['NAME'], $parameter['LISTITEMDESCRIPTION'] ) ) {
498
						return $issuers;
499
					}
500
501 1
					if ( 'issuer' !== $parameter['NAME'] ) {
502
						continue;
503
					}
504
505 1
					foreach ( $parameter['LISTITEMDESCRIPTION'] as $issuer ) {
506 1
						$issuers[ $issuer['VALUE'] ] = $issuer['DESCRIPTION'];
507
					}
508
509 1
					break;
510
				}
511
			}
512
		}
513
514 1
		return $issuers;
515
	}
516
517
	/**
518
	 * Get HTML fields
519
	 *
520
	 * @since 1.1.1
521
	 * @return string
522
	 */
523
	public function get_fields() {
524
		$data = array(
525
			Parameters::ADD_PRONAMIC_PAYMENT_ID => $this->get_payment_id(),
526
			Parameters::WEBSITE_KEY             => $this->get_website_key(),
527
			Parameters::INVOICE_NUMBER          => $this->get_invoice_number(),
528
			Parameters::AMOUNT                  => number_format( $this->get_amount(), 2, '.', '' ),
529
			Parameters::CURRENCY                => $this->get_currency(),
530
			Parameters::CULTURE                 => $this->get_culture(),
531
			Parameters::DESCRIPTION             => $this->get_description(),
532
			Parameters::PAYMENT_METHOD          => $this->get_payment_method(),
533
			Parameters::RETURN_URL              => $this->get_return_url(),
534
			Parameters::RETURN_REJECT_URL       => $this->get_return_reject_url(),
535
			Parameters::RETURN_ERROR_URL        => $this->get_return_error_url(),
536
			Parameters::RETURN_CANCEL_URL       => $this->get_return_cancel_url(),
537
			Parameters::PUSH_URL                => $this->get_push_url(),
538
			Parameters::PUSH_FAILURE_URL        => $this->get_push_url(),
539
			Parameters::REQUESTED_SERVICES      => implode( ',', $this->get_requested_services() ),
540
			Parameters::EXCLUDED_SERVICES       => $this->get_excluded_services(),
541
			Parameters::IDEAL_ISSUER            => $this->get_ideal_issuer(),
542
		);
543
544
		$signature = Security::create_signature( $data, $this->get_secret_key() );
545
546
		$data[ Parameters::SIGNATURE ] = $signature;
547
548
		return $data;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $data returns the type array<string,array|string> which is incompatible with the documented return type string.
Loading history...
549
	}
550
551
	/**
552
	 * Verify request Buckaroo
553
	 */
554
	public function verify_request( $data ) {
555
		$result = false;
556
557
		$signature = Security::get_signature( $data );
558
559
		$signature_check = Security::create_signature( $data, $this->get_secret_key() );
560
561
		if ( 0 === strcasecmp( $signature, $signature_check ) ) {
562
			$data = array_change_key_case( $data, CASE_LOWER );
563
564
			$result = filter_var_array(
565
				$data,
566
				array(
567
					Parameters::ADD_PRONAMIC_PAYMENT_ID    => FILTER_SANITIZE_STRING,
568
					Parameters::PAYMENT                    => FILTER_SANITIZE_STRING,
569
					Parameters::PAYMENT_METHOD             => FILTER_SANITIZE_STRING,
570
					Parameters::STATUS_CODE                => FILTER_VALIDATE_INT,
571
					Parameters::STATUS_CODE_DETAIL         => FILTER_SANITIZE_STRING,
572
					Parameters::STATUS_MESSAGE             => FILTER_SANITIZE_STRING,
573
					Parameters::INVOICE_NUMBER             => FILTER_SANITIZE_STRING,
574
					Parameters::AMOUNT                     => FILTER_VALIDATE_FLOAT,
575
					Parameters::CURRENCY                   => FILTER_SANITIZE_STRING,
576
					Parameters::TIMESTAMP                  => FILTER_SANITIZE_STRING,
577
					Parameters::SERVICE_IDEAL_CONSUMER_ISSUER => FILTER_SANITIZE_STRING,
578
					Parameters::SERVICE_IDEAL_CONSUMER_NAME => FILTER_SANITIZE_STRING,
579
					Parameters::SERVICE_IDEAL_CONSUMER_IBAN => FILTER_SANITIZE_STRING,
580
					Parameters::SERVICE_IDEAL_CONSUMER_BIC => FILTER_SANITIZE_STRING,
581
					Parameters::TRANSACTIONS               => FILTER_SANITIZE_STRING,
582
				)
583
			);
584
		}
585
586
		return $result;
587
	}
588
}
589