/wp-content/plugins/event-espresso.3.1.23.P/gateways/exact/Exact.php

https://bitbucket.org/anneivycat/ebcookhouse · PHP · 140 lines · 68 code · 13 blank · 59 comment · 9 complexity · 6ecce8acf7643509d5f4adac87a52941 MD5 · raw file

  1. <?php
  2. /**
  3. * Exact.com Class
  4. *
  5. * Author Seth Shoultes
  6. * @package Event Espresso Exact.com Gateway
  7. * @category Library
  8. */
  9. class Exact extends PaymentGateway {
  10. public $gateway_version = '1.0';
  11. /**
  12. * Login ID of authorize.net account
  13. *
  14. * @var string
  15. */
  16. public $login;
  17. /**
  18. * Secret key from authorize.net account
  19. *
  20. * @var string
  21. */
  22. public $secret;
  23. /*
  24. * Initialize the Exact.com gateway
  25. *
  26. * @param none
  27. * @return void
  28. */
  29. public function __construct() {
  30. parent::__construct();
  31. // Some default values of the class
  32. $this->gatewayUrl = 'https://checkout.e-xact.com/payment';
  33. $this->ipnLogFile = 'authorize.ipn_results.log';
  34. // Populate $fields array with a few default
  35. }
  36. /**
  37. * Enables the test mode
  38. *
  39. * @param none
  40. * @return none
  41. */
  42. public function enableTestMode() {
  43. $this->testMode = TRUE;
  44. $this->addField('x_Test_Request', 'TRUE'); //Used for non-dev testing
  45. }
  46. public function useTestServer() {
  47. $this->testMode = TRUE;
  48. $this->gatewayUrl = 'https://rpm-demo.e-xact.com/payment';
  49. }
  50. /**
  51. * Set login and secret key
  52. *
  53. * @param string user login
  54. * @param string secret key
  55. * @return void
  56. */
  57. public function setUserInfo($login, $key) {
  58. $this->login = $login;
  59. $this->secret = $key;
  60. }
  61. /**
  62. * Prepare a few payment information
  63. *
  64. * @param none
  65. * @return void
  66. */
  67. public function prepareSubmit() {
  68. $this->addField('x_login', $this->login);
  69. $this->addField('x_fp_timestamp', time());
  70. $data = $this->fields['x_login'] . '^' .
  71. $this->fields['x_fp_sequence'] . '^' .
  72. $this->fields['x_fp_timestamp'] . '^' .
  73. $this->fields['x_amount'] . '^';
  74. $this->addField('x_fp_hash', $this->hmac($this->secret, $data));
  75. }
  76. /**
  77. * Validate the IPN notification
  78. *
  79. * @param none
  80. * @return boolean
  81. */
  82. public function validateIpn() {
  83. foreach ($_POST as $field => $value) {
  84. $this->ipnData["$field"] = $value;
  85. }
  86. $invoice = intval($this->ipnData['x_invoice_num']);
  87. $pnref = $this->ipnData['x_trans_id'];
  88. $amount = doubleval($this->ipnData['x_amount']);
  89. $result = intval($this->ipnData['x_response_code']);
  90. $respmsg = $this->ipnData['x_response_reason_text'];
  91. $md5source = $this->secret . $this->login . $this->ipnData['x_trans_id'] . $this->ipnData['x_amount'];
  92. $md5 = md5($md5source);
  93. if ($result == '1') {
  94. // Valid IPN transaction.
  95. $this->logResults(true);
  96. return true;
  97. } else if ($result != '1') {
  98. $this->lastError = $respmsg;
  99. $this->logResults(false);
  100. return false;
  101. } else if (strtoupper($md5) != $this->ipnData['x_MD5_Hash']) {
  102. $this->lastError = 'MD5 mismatch';
  103. $this->logResults(false);
  104. return false;
  105. }
  106. }
  107. /**
  108. * RFC 2104 HMAC implementation for php.
  109. *
  110. * @author Lance Rushing
  111. * @param string key
  112. * @param string date
  113. * @return string encoded hash
  114. */
  115. private function hmac($key, $data) {
  116. $b = 64; // byte length for md5
  117. if (strlen($key) > $b) {
  118. $key = pack("H*", md5($key));
  119. }
  120. $key = str_pad($key, $b, chr(0x00));
  121. $ipad = str_pad('', $b, chr(0x36));
  122. $opad = str_pad('', $b, chr(0x5c));
  123. $k_ipad = $key ^ $ipad;
  124. $k_opad = $key ^ $opad;
  125. return md5($k_opad . pack("H*", md5($k_ipad . $data)));
  126. }
  127. }