PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/PaymentGateway/Paypal.php

https://github.com/hukumonline/quart80
PHP | 123 lines | 62 code | 19 blank | 42 comment | 2 complexity | d14a6aa6688f8f73b4a4920665d180b7 MD5 | raw file
  1. <?php
  2. /**
  3. * Paypal Class
  4. *
  5. * Integrate the Paypal payment gateway in your site using this easy
  6. * to use library. Just see the example code to know how you should
  7. * proceed. Btw, this library does not support the recurring payment
  8. * system. If you need that, drop me a note and I will send to you.
  9. *
  10. * @package Payment Gateway
  11. * @category Library
  12. * @author Md Emran Hasan <phpfour@gmail.com>
  13. * @link http://www.phpfour.com
  14. */
  15. include_once ('PaymentGateway.php');
  16. class Paypal extends PaymentGateway
  17. {
  18. /**
  19. * Initialize the Paypal gateway
  20. *
  21. * @param none
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. // Some default values of the class
  28. $this->gatewayUrl = 'https://www.paypal.com/cgi-bin/webscr';
  29. $this->ipnLogFile = 'paypal.ipn_results.log';
  30. // Populate $fields array with a few default
  31. $this->addField('rm', '2');
  32. // Return method = POST
  33. $this->addField('cmd', '_cart');
  34. $this->addField('upload', 1);
  35. }
  36. /**
  37. * Enables the test mode
  38. *
  39. * @param none
  40. * @return none
  41. */
  42. public function enableTestMode()
  43. {
  44. $this->testMode = TRUE;
  45. $this->gatewayUrl = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
  46. }
  47. /**
  48. * Validate the IPN notification
  49. *
  50. * @param none
  51. * @return boolean
  52. */
  53. public function validateIpn()
  54. {
  55. // parse the paypal URL
  56. $urlParsed = parse_url($this->gatewayUrl);
  57. // generate the post string from the _POST vars
  58. $postString = '';
  59. foreach ($_POST as $field=>$value)
  60. {
  61. $this->ipnData["$field"] = $value;
  62. $postString .= $field .'=' . urlencode(stripslashes($value)) . '&';
  63. }
  64. $postString .="cmd=_notify-validate"; // append ipn command
  65. // open the connection to paypal
  66. $fp = fsockopen($urlParsed['host'], "80", $errNum, $errStr, 30);
  67. if(!$fp)
  68. {
  69. // Could not open the connection, log error if enabled
  70. $this->lastError = "fsockopen error no. $errNum: $errStr";
  71. $this->logResults(false);
  72. return false;
  73. }
  74. else
  75. {
  76. // Post the data back to paypal
  77. fputs($fp, "POST ".$urlParsed['path']." HTTP/1.1\r\n");
  78. fputs($fp, "Host: ".$urlParsed['host']."\r\n");
  79. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  80. fputs($fp, "Content-length: " . strlen($postString) . "\r\n");
  81. fputs($fp, "Connection: close\r\n\r\n");
  82. fputs($fp, $postString . "\r\n\r\n");
  83. // loop through the response from the server and append to variable
  84. while(!feof($fp))
  85. {
  86. $this->ipnResponse .= fgets($fp, 1024);
  87. }
  88. fclose($fp); // close connection
  89. }
  90. if (eregi("VERIFIED", $this->ipnResponse))
  91. {
  92. // Valid IPN transaction.
  93. $this->logResults(true);
  94. return true;
  95. }
  96. else
  97. {
  98. // Invalid IPN transaction. Check the log for details.
  99. $this->lastError = "IPN Validation Failed . $urlParsed[path] : $urlParsed[host]";
  100. $this->logResults(false);
  101. return false;
  102. }
  103. }
  104. }