wp-pay-gateways /
ems-e-commerce
| 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 DateTime |
||
| 136 | */ |
||
| 137 | private $transaction_datetime; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Construct and initialize 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 | * @return void |
||
| 159 | */ |
||
| 160 | public function set_action_url( $url ) { |
||
| 161 | $this->action_url = $url; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get the currency numeric code |
||
| 166 | * |
||
| 167 | * @return string currency numeric code |
||
| 168 | */ |
||
| 169 | public function get_currency_numeric_code() { |
||
| 170 | return $this->currency_numeric_code; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the currency code |
||
| 175 | * |
||
| 176 | * @param string $code Currency numeric code. |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | public function set_currency_numeric_code( $code ) { |
||
| 180 | $this->currency_numeric_code = $code; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get storename |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function get_storename() { |
||
| 189 | return $this->storename; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set the storename |
||
| 194 | * |
||
| 195 | * @param string $storename Storename. |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function set_storename( $storename ) { |
||
| 199 | $this->storename = $storename; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get normal return URL |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function get_return_url() { |
||
| 208 | return $this->return_url; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set the normal return URL |
||
| 213 | * |
||
| 214 | * LET OP! De URL mag geen parameters bevatten. |
||
| 215 | * |
||
| 216 | * @param string $return_url Return URL. |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | public function set_return_url( $return_url ) { |
||
| 220 | $this->return_url = $return_url; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get amount |
||
| 225 | * |
||
| 226 | * @return float |
||
| 227 | */ |
||
| 228 | public function get_amount() { |
||
| 229 | return $this->amount; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set amount |
||
| 234 | * |
||
| 235 | * @param float $amount Amount. |
||
| 236 | * @return void |
||
| 237 | */ |
||
| 238 | public function set_amount( $amount ) { |
||
| 239 | $this->amount = $amount; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get notification URL |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function get_notification_url() { |
||
| 248 | return $this->notification_url; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set notification URL |
||
| 253 | * |
||
| 254 | * @param string $notification_url Notification URL. |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function set_notification_url( $notification_url ) { |
||
| 258 | $this->notification_url = $notification_url; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get language. |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function get_language() { |
||
| 267 | return $this->language; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set language. |
||
| 272 | * |
||
| 273 | * @param string $language Language. |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | public function set_language( $language ) { |
||
| 277 | $this->language = $language; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Set the payment method. |
||
| 282 | * |
||
| 283 | * @param string $payment_method Payment method. |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function set_payment_method( $payment_method ) { |
||
| 287 | $this->payment_method = $payment_method; |
||
|
0 ignored issues
–
show
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...
|
|||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Get the payment method. |
||
| 292 | * |
||
| 293 | * @return string ANS128 listString comma separated list |
||
| 294 | */ |
||
| 295 | public function get_payment_method() { |
||
| 296 | return $this->payment_method; |
||
|
0 ignored issues
–
show
|
|||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get order ID |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function get_order_id() { |
||
| 305 | return $this->order_id; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set order ID |
||
| 310 | * |
||
| 311 | * @param string $order_id Order ID. |
||
| 312 | * @return void |
||
| 313 | */ |
||
| 314 | public function set_order_id( $order_id ) { |
||
| 315 | $this->order_id = $order_id; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get payment ID |
||
| 320 | * |
||
| 321 | * @return int |
||
| 322 | */ |
||
| 323 | public function get_payment_id() { |
||
| 324 | return $this->payment_id; |
||
|
0 ignored issues
–
show
|
|||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set payment ID |
||
| 329 | * |
||
| 330 | * @param int $payment_id Payment ID. |
||
| 331 | * @return void |
||
| 332 | */ |
||
| 333 | public function set_payment_id( $payment_id ) { |
||
| 334 | $this->payment_id = $payment_id; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the transaction datetime. |
||
| 339 | * |
||
| 340 | * @param boolean $create_new Indicator for creating a new expire date. |
||
| 341 | * @return DateTime |
||
| 342 | */ |
||
| 343 | public function get_transaction_datetime( $create_new = false ) { |
||
| 344 | if ( null === $this->transaction_datetime || $create_new ) { |
||
| 345 | $this->transaction_datetime = new DateTime( null, new DateTimeZone( 'UTC' ) ); |
||
| 346 | } |
||
| 347 | |||
| 348 | return $this->transaction_datetime; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set transaction datetime. |
||
| 353 | * |
||
| 354 | * @param DateTime $datetime Transaction date time. |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function set_transaction_datetime( DateTime $datetime ) { |
||
| 358 | $this->transaction_datetime = $datetime; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get data |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | public function get_data() { |
||
| 367 | // Required fields for payment request. |
||
| 368 | $required_fields = array( |
||
| 369 | 'txntype' => 'sale', |
||
| 370 | // According the EMS documentation the timezone should be in `Area/Location` notation, but it seems like `UTC` is also working. |
||
| 371 | 'timezone' => 'UTC', |
||
| 372 | // In WordPress, PHP's `time()` will always return `UTC` and is the same as calling `current_time( 'timestamp', true )`. |
||
| 373 | 'txndatetime' => $this->get_transaction_datetime()->format( 'Y:m:d-H:i:s' ), |
||
| 374 | 'hash_algorithm' => 'SHA256', |
||
| 375 | 'storename' => $this->get_storename(), |
||
| 376 | 'mode' => 'payonly', |
||
| 377 | 'chargetotal' => number_format( ( $this->get_amount() / 100 ), 2, '.', '' ), |
||
| 378 | 'currency' => $this->get_currency_numeric_code(), |
||
| 379 | ); |
||
| 380 | |||
| 381 | // Optional fields for payment request. |
||
| 382 | $optional_fields = array( |
||
| 383 | 'oid' => $this->get_order_id(), |
||
| 384 | 'language' => $this->get_language(), |
||
| 385 | 'paymentMethod' => $this->get_payment_method(), |
||
| 386 | 'responseFailURL' => $this->get_return_url(), |
||
| 387 | 'responseSuccessURL' => $this->get_return_url(), |
||
| 388 | 'transactionNotificationURL' => $this->get_notification_url(), |
||
| 389 | 'idealIssuerID' => $this->get_issuer_id(), |
||
| 390 | 'ems_notify_payment_id' => $this->get_payment_id(), |
||
| 391 | ); |
||
| 392 | |||
| 393 | // @link http://briancray.com/2009/04/25/remove-null-values-php-arrays/ |
||
| 394 | $optional_fields = array_filter( $optional_fields ); |
||
| 395 | |||
| 396 | // Data. |
||
| 397 | $data = $required_fields + $optional_fields; |
||
| 398 | |||
| 399 | return $data; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get shared secret |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | public function get_secret() { |
||
| 408 | return $this->secret; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set shared secret. |
||
| 413 | * |
||
| 414 | * @param string $secret Secret. |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | public function set_secret( $secret ) { |
||
| 418 | $this->secret = $secret; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get hash |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | public function get_hash() { |
||
| 427 | $data = $this->get_data(); |
||
| 428 | $secret = $this->get_secret(); |
||
| 429 | |||
| 430 | $values = array( |
||
| 431 | $data['storename'], |
||
| 432 | $data['txndatetime'], |
||
| 433 | $data['chargetotal'], |
||
| 434 | $data['currency'], |
||
| 435 | $secret, |
||
| 436 | ); |
||
| 437 | |||
| 438 | return self::compute_hash( $values ); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Compute hash |
||
| 443 | * |
||
| 444 | * @param array $values Values to compute hash for. |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public static function compute_hash( $values ) { |
||
| 448 | $value = implode( '', $values ); |
||
| 449 | $value = bin2hex( $value ); |
||
| 450 | |||
| 451 | return hash( self::HASH_ALGORITHM_SHA256, $value ); |
||
| 452 | } |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Get fields |
||
| 456 | * |
||
| 457 | * @since 1.0.0 |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function get_fields() { |
||
| 461 | $fields = $this->get_data(); |
||
| 462 | |||
| 463 | $fields['hash'] = $this->get_hash(); |
||
| 464 | |||
| 465 | return $fields; |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Set issuer ID. |
||
| 470 | * |
||
| 471 | * @param string $issuer_id Issuer ID. |
||
| 472 | * @return void |
||
| 473 | */ |
||
| 474 | public function set_issuer_id( $issuer_id ) { |
||
| 475 | $this->issuer_id = $issuer_id; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get issuer ID. |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function get_issuer_id() { |
||
| 484 | return $this->issuer_id; |
||
| 485 | } |
||
| 486 | } |
||
| 487 |