PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/event-espresso.3.1.23.P/gateways/worldpay/Worldpay.php

https://bitbucket.org/anneivycat/ebcookhouse
PHP | 177 lines | 113 code | 15 blank | 49 comment | 11 complexity | 4e1aa52464d5466b4cc6e34453d2f6b5 MD5 | raw file
  1. <?php
  2. /**
  3. * Worldpay Class
  4. *
  5. * Author Seth Shoultes
  6. * @package Event Espresso Worldpay Gateway
  7. * @category Library
  8. */
  9. class Worldpay extends PaymentGateway {
  10. public $worldpay_gateway_version = '1.0';
  11. /**
  12. * Initialize the WorldPay gateway
  13. *
  14. * @param none
  15. * @return void
  16. */
  17. public function __construct() {
  18. parent::__construct();
  19. // Some default values of the class
  20. $this->gatewayUrl = 'https://secure.worldpay.com/wcc/purchase';
  21. $this->ipnLogFile = 'worldpay.ipn_results.log';
  22. $this->worldpay_gateway_version = '1.0';
  23. }
  24. /**
  25. * Enables the test mode
  26. *
  27. * @param none
  28. * @return none
  29. */
  30. public function enableTestMode() {
  31. $this->testMode = TRUE;
  32. }
  33. public function logErrors($errors) {
  34. if (!$this->logIpn)
  35. return;
  36. // Timestamp
  37. $text = '[' . date('m/d/Y g:i A') . '] - ';
  38. // Success or failure being logged?
  39. $text .= "Errors from IPN Validation:\n";
  40. $text .= $errors;
  41. // Write to log
  42. $fp = @fopen($this->ipnLogFile, 'a');
  43. @fwrite($fp, $text . "\n\n");
  44. @fclose($fp);
  45. }
  46. /**
  47. * Validate the IPN notification
  48. *
  49. * @param none
  50. * @return boolean
  51. */
  52. public function validateIpn() {
  53. global $org_options;
  54. do_action('action_hook_espresso_log', __FILE__, __FUNCTION__, '');
  55. if (function_exists('curl_init')) {
  56. //new WorldPay code//
  57. // parse the WorldPay URL
  58. $urlParsed = parse_url($this->gatewayUrl);
  59. // generate the post string from the _POST vars
  60. $req = '';
  61. $errors = "\nUsing BUILT-IN PHP curl methods\n";
  62. // Run through the posted array
  63. foreach ($_POST as $key => $value) {
  64. $this->ipnData["$key"] = $value;
  65. $errors .= "key = " . $key . "\nvalue = " . $value . "\n";
  66. $value = urlencode(stripslashes($value));
  67. $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i', '${1}%0D%0A${3}', $value); // IPN fix
  68. $req .= $key . '=' . $value . '&';
  69. }
  70. $req .= 'cmd=_notify-validate';
  71. $url = $this->gatewayUrl;
  72. $ch = curl_init(); // Starts the curl handler
  73. $error = array();
  74. $error["set_host"] = curl_setopt($ch, CURLOPT_URL, $url); // Sets the WorldPay address for curl
  75. $error["set_fail_on_error"] = curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  76. $error["set_return_transfer"] = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns result to a variable instead of echoing
  77. $error["set_timeout"] = curl_setopt($ch, CURLOPT_TIMEOUT, 45); // Sets a time limit for curl in seconds (do not set too low)
  78. $error["set_post"] = curl_setopt($ch, CURLOPT_POST, 1); // Set curl to send data using post
  79. $error["set_post_fields"] = curl_setopt($ch, CURLOPT_POSTFIELDS, $req); // Add the request parameters to the post
  80. $errors .= $error["set_host"] ? "Success" : "Failure";
  81. $errors .= " Setting host: " . $url . "\n";
  82. $errors .= $error["set_post"] ? "Success" : "Failure";
  83. $errors .= " Setting request type to post\n";
  84. $errors .= $error["set_post_fields"] ? "Success" : "Failure";
  85. $errors .= " Setting post fields: " . htmlspecialchars($req) . "\n";
  86. $errors .= $error["set_fail_on_error"] ? "Success" : "Failure";
  87. $errors .= " Setting Fain On Error\n";
  88. $errors .= $error["set_return_transfer"] ? "Success" : "Failure";
  89. $errors .= " Setting return transfer\n";
  90. $errors .= $error["set_timeout"] ? "Success" : "Failure";
  91. $errors .= " Setting Timeout\n";
  92. $error["set_verbose"] = curl_setopt($ch, CURLOPT_VERBOSE, 1);
  93. $errors .= $error["set_verbose"] ? "Success" : "Failure";
  94. $errors .= " Setting verbose mode\n";
  95. $result = curl_exec($ch); // run the curl process (and return the result to $result
  96. $this->ipnResponse = $result;
  97. $error["result"] = curl_error($ch);
  98. curl_close($ch);
  99. $errors .= "Errors resulting from the execution of curl transfer: " . $error["result"];
  100. $this->logErrors($errors);
  101. if (strcmp($result, "VERIFIED") == 0) { // It may seem strange but this function returns 0 if the result matches the string So you MUST check it is 0 and not just do strcmp ($result, "VERIFIED") (the if will fail as it will equate the result as false)
  102. // Do some checks to ensure that the payment has been sent to the correct person
  103. // Check and ensure currency and amount are correct
  104. // Check that the transaction has not been processed before
  105. // Ensure the payment is complete
  106. // Valid IPN transaction.
  107. $this->logResults(true);
  108. return true;
  109. } else {
  110. // Log an invalid request to look into
  111. // Invalid IPN transaction. Check the log for details.
  112. $this->lastError = "IPN Validation Failed . $urlParsed[path] : $urlParsed[host]";
  113. $this->logResults(false);
  114. return false;
  115. }
  116. } else {
  117. //Old WorldPay code
  118. // parse the WorldPay URL
  119. $urlParsed = parse_url($this->gatewayUrl);
  120. // generate the post string from the _POST vars
  121. $postString = '';
  122. foreach ($_POST as $field => $value) {
  123. $this->ipnData["$key"] = $value;
  124. $value = urlencode(stripslashes($value));
  125. $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i', '${1}%0D%0A${3}', $value); // IPN fix
  126. $req .= $key . '=' . $value . '&';
  127. }
  128. $postString .="cmd=_notify-validate"; // append ipn command
  129. // open the connection to WorldPay
  130. $fp = fsockopen($urlParsed[host], "80", $errNum, $errStr, 30);
  131. if (!$fp) {
  132. // Could not open the connection, log error if enabled
  133. $this->lastError = "fsockopen error no. $errNum: $errStr";
  134. $this->logResults(false);
  135. return false;
  136. } else {
  137. // Post the data back to WorldPay
  138. fputs($fp, "POST $urlParsed[path] HTTP/1.1\r\n");
  139. fputs($fp, "Host: $urlParsed[host]\r\n");
  140. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  141. fputs($fp, "Content-length: " . strlen($postString) . "\r\n");
  142. fputs($fp, "Connection: close\r\n\r\n");
  143. fputs($fp, $postString . "\r\n\r\n");
  144. // loop through the response from the server and append to variable
  145. while (!feof($fp)) {
  146. $this->ipnResponse .= fgets($fp, 1024);
  147. }
  148. fclose($fp); // close connection
  149. }
  150. if (eregi("VERIFIED", $this->ipnResponse)) {
  151. // Valid IPN transaction.
  152. $this->logResults(true);
  153. return true;
  154. } else {
  155. // Invalid IPN transaction. Check the log for details.
  156. $this->lastError = "IPN Validation Failed . $urlParsed[path] : $urlParsed[host]";
  157. $this->logResults(false);
  158. return false;
  159. }
  160. }
  161. }
  162. }