Failed Conditions
Push — develop ( b16f61...f4b027 )
by Reüel
04:41
created

src/Client.php (7 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\EMS\ECommerce;
4
5
use Pronamic\WordPress\DateTime\DateTime;
6
use Pronamic\WordPress\DateTime\DateTimeZone;
7
8
/**
9
 * Title: EMS e-Commerce client
10
 * Description:
11
 * Copyright: 2005-2021 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
	 * Action URL to start a payment request in the test environment,
21
	 * the POST data is sent to.
22
	 *
23
	 * @see page 14 - http://pronamic.nl/wp-content/uploads/2013/10/integratiehandleiding_rabo_omnikassa_en_versie_5_0_juni_2013_10_29451215.pdf
24
	 * @var string
25
	 */
26
	const ACTION_URL_TEST = 'https://test.ipg-online.com/connect/gateway/processing';
27
28
	/**
29
	 * Action URL For a payment request in the production environment,
30
	 * the POST data is sent to
31
	 *
32
	 * @see page 14 - http://pronamic.nl/wp-content/uploads/2013/10/integratiehandleiding_rabo_omnikassa_en_versie_5_0_juni_2013_10_29451215.pdf
33
	 * @var string
34
	 */
35
	const ACTION_URL_PRODUCTION = 'https://www.ipg-online.com/connect/gateway/processing';
36
37
	/**
38
	 * Hash algorithm SHA256 indicator
39
	 *
40
	 * @var string
41
	 */
42
	const HASH_ALGORITHM_SHA256 = 'sha256';
43
44
	/**
45
	 * The action URL
46
	 *
47
	 * @var string
48
	 */
49
	private $action_url;
50
51
	/**
52
	 * Currency code in ISO 4217-Numeric codification
53
	 *
54
	 * @link https://en.wikipedia.org/wiki/ISO_4217
55
	 * @link http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm
56
	 *
57
	 * @var string N3
58
	 */
59
	private $currency_numeric_code;
60
61
	/**
62
	 * Storename
63
	 *
64
	 * @var string N15 @todo DOC - Storename format requirement
65
	 */
66
	private $storename;
67
68
	/**
69
	 * Normal return URL
70
	 *
71
	 * @var string ANS512 url
72
	 */
73
	private $return_url;
74
75
	/**
76
	 * Amount
77
	 *
78
	 * @var string N12
79
	 */
80
	private $amount;
81
82
	/**
83
	 * Notification URL
84
	 *
85
	 * @var string ANS512 url
86
	 */
87
	private $notification_url;
88
89
	/**
90
	 * Language in ISO 639‐1 Alpha2
91
	 *
92
	 * @link https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
93
	 * @var string A2
94
	 */
95
	private $language;
96
97
	/**
98
	 * Payment method
99
	 *
100
	 * @var array
101
	 */
102
	private $payment_method;
103
104
	/**
105
	 * Order ID
106
	 *
107
	 * @var string AN32
108
	 */
109
	private $order_id;
110
111
	/**
112
	 * Payment ID
113
	 *
114
	 * @var string AN32
115
	 */
116
	private $payment_id;
117
118
	/**
119
	 * Shared secret
120
	 *
121
	 * @var string
122
	 */
123
	private $secret;
124
125
	/**
126
	 * Issuer ID.
127
	 *
128
	 * @var string
129
	 */
130
	private $issuer_id;
131
132
	/**
133
	 * Transaction datetime.
134
	 *
135
	 * @var string
136
	 */
137
	private $transaction_datetime;
138
139
	/**
140
	 * Constructs and initalize an EMS e-Commerce object
141
	 */
142
	public function __construct() {
143
	}
144
145
	/**
146
	 * Get the action URL
147
	 *
148
	 * @return string action URL
149
	 */
150
	public function get_action_url() {
151
		return $this->action_url;
152
	}
153
154
	/**
155
	 * Set the action URL
156
	 *
157
	 * @param string $url Action URL.
158
	 */
159
	public function set_action_url( $url ) {
160
		$this->action_url = $url;
161
	}
162
163
	/**
164
	 * Get the currency numeric code
165
	 *
166
	 * @return string currency numeric code
167
	 */
168
	public function get_currency_numeric_code() {
169
		return $this->currency_numeric_code;
170
	}
171
172
	/**
173
	 * Set the currency code
174
	 *
175
	 * @param string $code Currency numeric code.
176
	 */
177
	public function set_currency_numeric_code( $code ) {
178
		$this->currency_numeric_code = $code;
179
	}
180
181
	/**
182
	 * Get storename
183
	 *
184
	 * @return string
185
	 */
186
	public function get_storename() {
187
		return $this->storename;
188
	}
189
190
	/**
191
	 * Set the storename
192
	 *
193
	 * @param string $storename Storename.
194
	 */
195
	public function set_storename( $storename ) {
196
		$this->storename = $storename;
197
	}
198
199
	/**
200
	 * Get normal return URL
201
	 *
202
	 * @return string
203
	 */
204
	public function get_return_url() {
205
		return $this->return_url;
206
	}
207
208
	/**
209
	 * Set the normal return URL
210
	 *
211
	 * LET OP! De URL mag geen parameters bevatten.
212
	 *
213
	 * @param string $return_url Return URL.
214
	 */
215
	public function set_return_url( $return_url ) {
216
		$this->return_url = $return_url;
217
	}
218
219
	/**
220
	 * Get amount
221
	 *
222
	 * @return float
223
	 */
224
	public function get_amount() {
225
		return $this->amount;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->amount returns the type string which is incompatible with the documented return type double.
Loading history...
226
	}
227
228
	/**
229
	 * Set amount
230
	 *
231
	 * @param float $amount Amount.
232
	 */
233
	public function set_amount( $amount ) {
234
		$this->amount = $amount;
235
	}
236
237
	/**
238
	 * Get notification URL
239
	 *
240
	 * @return string
241
	 */
242
	public function get_notification_url() {
243
		return $this->notification_url;
244
	}
245
246
	/**
247
	 * Set notification URL
248
	 *
249
	 * @param string $notification_url Notification URL.
250
	 */
251
	public function set_notification_url( $notification_url ) {
252
		$this->notification_url = $notification_url;
253
	}
254
255
	/**
256
	 * Get language.
257
	 *
258
	 * @return string
259
	 */
260
	public function get_language() {
261
		return $this->language;
262
	}
263
264
	/**
265
	 * Set language.
266
	 *
267
	 * @param string $language Language.
268
	 */
269
	public function set_language( $language ) {
270
		$this->language = $language;
271
	}
272
273
	/**
274
	 * Set the payment method.
275
	 *
276
	 * @param string $payment_method Payment method.
277
	 */
278
	public function set_payment_method( $payment_method ) {
279
		$this->payment_method = $payment_method;
0 ignored issues
show
Documentation Bug introduced by
It seems like $payment_method of type string is incompatible with the declared type array of property $payment_method.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
280
	}
281
282
	/**
283
	 * Get the payment method.
284
	 *
285
	 * @return string ANS128 listString comma separated list
286
	 */
287
	public function get_payment_method() {
288
		return $this->payment_method;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->payment_method returns the type array which is incompatible with the documented return type string.
Loading history...
289
	}
290
291
	/**
292
	 * Get order ID
293
	 *
294
	 * @return string
295
	 */
296
	public function get_order_id() {
297
		return $this->order_id;
298
	}
299
300
	/**
301
	 * Set order ID
302
	 *
303
	 * @param string $order_id Order ID.
304
	 */
305
	public function set_order_id( $order_id ) {
306
		$this->order_id = $order_id;
307
	}
308
309
	/**
310
	 * Get payment ID
311
	 *
312
	 * @return int
313
	 */
314
	public function get_payment_id() {
315
		return $this->payment_id;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->payment_id returns the type string which is incompatible with the documented return type integer.
Loading history...
316
	}
317
318
	/**
319
	 * Set payment ID
320
	 *
321
	 * @param int $payment_id Payment ID.
322
	 */
323
	public function set_payment_id( $payment_id ) {
324
		$this->payment_id = $payment_id;
325
	}
326
327
	/**
328
	 * Get the transaction datetime.
329
	 *
330
	 * @param boolean $create_new Indicator for creating a new expire date.
331
	 *
332
	 * @return DateTime
333
	 */
334
	public function get_transaction_datetime( $create_new = false ) {
335
		if ( null === $this->transaction_datetime || $create_new ) {
336
			$this->transaction_datetime = new DateTime( null, new DateTimeZone( 'UTC' ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like new Pronamic\WordPress\D...me\DateTimeZone('UTC')) of type Pronamic\WordPress\DateTime\DateTime is incompatible with the declared type string of property $transaction_datetime.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
337
		}
338
339
		return $this->transaction_datetime;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->transaction_datetime also could return the type string which is incompatible with the documented return type Pronamic\WordPress\DateTime\DateTime.
Loading history...
340
	}
341
342
	/**
343
	 * Set transaction datetime.
344
	 *
345
	 * @param DateTime $datetime Transaction date time.
346
	 */
347
	public function set_transaction_datetime( DateTime $datetime ) {
348
		$this->transaction_datetime = $datetime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $datetime of type Pronamic\WordPress\DateTime\DateTime is incompatible with the declared type string of property $transaction_datetime.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
349
	}
350
351
	/**
352
	 * Get data
353
	 *
354
	 * @return array
355
	 */
356
	public function get_data() {
357
		// Required fields for payment request.
358
		$required_fields = array(
359
			'txntype'        => 'sale',
360
			// According the EMS documentation the timezone should be in `Area/Location` notation, but it seems like `UTC` is also working.
361
			'timezone'       => 'UTC',
362
			// In WordPress, PHP's `time()` will always return `UTC` and is the same as calling `current_time( 'timestamp', true )`.
363
			'txndatetime'    => $this->get_transaction_datetime()->format( 'Y:m:d-H:i:s' ),
364
			'hash_algorithm' => 'SHA256',
365
			'storename'      => $this->get_storename(),
366
			'mode'           => 'payonly',
367
			'chargetotal'    => number_format( ( $this->get_amount() / 100 ), 2, '.', '' ),
368
			'currency'       => $this->get_currency_numeric_code(),
369
		);
370
371
		// Optional fields for payment request.
372
		$optional_fields = array(
373
			'oid'                        => $this->get_order_id(),
374
			'language'                   => $this->get_language(),
375
			'paymentMethod'              => $this->get_payment_method(),
376
			'responseFailURL'            => $this->get_return_url(),
377
			'responseSuccessURL'         => $this->get_return_url(),
378
			'transactionNotificationURL' => $this->get_notification_url(),
379
			'idealIssuerID'              => $this->get_issuer_id(),
380
			'ems_notify_payment_id'      => $this->get_payment_id(),
381
		);
382
383
		// @link http://briancray.com/2009/04/25/remove-null-values-php-arrays/
384
		$optional_fields = array_filter( $optional_fields );
385
386
		// Data.
387
		$data = $required_fields + $optional_fields;
388
389
		return $data;
390
	}
391
392
	/**
393
	 * Get shared secret
394
	 *
395
	 * @return string
396
	 */
397
	public function get_secret() {
398
		return $this->secret;
399
	}
400
401
	/**
402
	 * Set shared secret.
403
	 *
404
	 * @param string $secret Secret.
405
	 *
406
	 * @return void
407
	 */
408
	public function set_secret( $secret ) {
409
		$this->secret = $secret;
410
	}
411
412
	/**
413
	 * Get hash
414
	 *
415
	 * @return string
416
	 */
417
	public function get_hash() {
418
		$data   = $this->get_data();
419
		$secret = $this->get_secret();
420
421
		$values = array(
422
			$data['storename'],
423
			$data['txndatetime'],
424
			$data['chargetotal'],
425
			$data['currency'],
426
			$secret,
427
		);
428
429
		return self::compute_hash( $values );
430
	}
431
432
	/**
433
	 * Compute hash
434
	 *
435
	 * @param array $values Values to compute hash for.
436
	 *
437
	 * @return string
438
	 */
439
	public static function compute_hash( $values ) {
440
		$value = implode( '', $values );
441
		$value = bin2hex( $value );
442
443
		return hash( self::HASH_ALGORITHM_SHA256, $value );
444
	}
445
446
	/**
447
	 * Get fields
448
	 *
449
	 * @since 1.0.0
450
	 * @return array
451
	 */
452
	public function get_fields() {
453
		$fields = $this->get_data();
454
455
		$fields['hash'] = $this->get_hash();
456
457
		return $fields;
458
	}
459
460
	/**
461
	 * Set issuer ID.
462
	 *
463
	 * @param string $issuer_id Issuer ID.
464
	 */
465
	public function set_issuer_id( $issuer_id ) {
466
		$this->issuer_id = $issuer_id;
467
	}
468
469
	/**
470
	 * Get issuer ID.
471
	 *
472
	 * @return string
473
	 */
474
	public function get_issuer_id() {
475
		return $this->issuer_id;
476
	}
477
}
478