paypal.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /*******************************************************************************
  3. * PHP Paypal IPN Integration Class
  4. *******************************************************************************
  5. * Author: Micah Carrick
  6. * Email: [email protected]
  7. * Website: http://www.micahcarrick.com
  8. *
  9. * File: paypal.class.php
  10. * Version: 1.3.0
  11. * Copyright: (c) 2005 - Micah Carrick
  12. * You are free to use, distribute, and modify this software
  13. * under the terms of the GNU General Public License. See the
  14. * included license.txt file.
  15. *
  16. *******************************************************************************
  17. * VERION HISTORY:
  18. * v1.3.0 [10.10.2005] - Fixed it so that single quotes are handled the
  19. * right way rather than simple stripping them. This
  20. * was needed because the user could still put in
  21. * quotes.
  22. *
  23. * v1.2.1 [06.05.2005] - Fixed typo from previous fix :)
  24. *
  25. * v1.2.0 [05.31.2005] - Added the optional ability to remove all quotes
  26. * from the paypal posts. The IPN will come back
  27. * invalid sometimes when quotes are used in certian
  28. * fields.
  29. *
  30. * v1.1.0 [05.15.2005] - Revised the form output in the submit_paypal_post
  31. * method to allow non-javascript capable browsers
  32. * to provide a means of manual form submission.
  33. *
  34. * v1.0.0 [04.16.2005] - Initial Version
  35. *
  36. *******************************************************************************
  37. * DESCRIPTION:
  38. *
  39. * NOTE: See www.micahcarrick.com for the most recent version of this class
  40. * along with any applicable sample files and other documentaion.
  41. *
  42. * This file provides a neat and simple method to interface with paypal and
  43. * The paypal Instant Payment Notification (IPN) interface. This file is
  44. * NOT intended to make the paypal integration "plug 'n' play". It still
  45. * requires the developer (that should be you) to understand the paypal
  46. * process and know the variables you want/need to pass to paypal to
  47. * achieve what you want.
  48. *
  49. * This class handles the submission of an order to paypal aswell as the
  50. * processing an Instant Payment Notification.
  51. *
  52. * This code is based on that of the php-toolkit from paypal. I've taken
  53. * the basic principals and put it in to a class so that it is a little
  54. * easier--at least for me--to use. The php-toolkit can be downloaded from
  55. * http://sourceforge.net/projects/paypal.
  56. *
  57. * To submit an order to paypal, have your order form POST to a file with:
  58. *
  59. * $p = new paypal_class;
  60. * $p->add_field('business', '[email protected]');
  61. * $p->add_field('first_name', $_POST['first_name']);
  62. * ... (add all your fields in the same manor)
  63. * $p->submit_paypal_post();
  64. *
  65. * To process an IPN, have your IPN processing file contain:
  66. *
  67. * $p = new paypal_class;
  68. * if ($p->validate_ipn()) {
  69. * ... (IPN is verified. Details are in the ipn_data() array)
  70. * }
  71. *
  72. *
  73. * In case you are new to paypal, here is some information to help you:
  74. *
  75. * 1. Download and read the Merchant User Manual and Integration Guide from
  76. * http://www.paypal.com/en_US/pdf/integration_guide.pdf. This gives
  77. * you all the information you need including the fields you can pass to
  78. * paypal (using add_field() with this class) aswell as all the fields
  79. * that are returned in an IPN post (stored in the ipn_data() array in
  80. * this class). It also diagrams the entire transaction process.
  81. *
  82. * 2. Create a "sandbox" account for a buyer and a seller. This is just
  83. * a test account(s) that allow you to test your site from both the
  84. * seller and buyer perspective. The instructions for this is available
  85. * at https://developer.paypal.com/ as well as a great forum where you
  86. * can ask all your paypal integration questions. Make sure you follow
  87. * all the directions in setting up a sandbox test environment, including
  88. * the addition of fake bank accounts and credit cards.
  89. *
  90. *******************************************************************************
  91. */
  92. class paypal_class {
  93. var $last_error; // holds the last error encountered
  94. var $ipn_log; // bool: log IPN results to text file?
  95. var $ipn_log_file; // filename of the IPN log
  96. var $ipn_response; // holds the IPN response from paypal
  97. var $ipn_data = array(); // array contains the POST values for IPN
  98. var $fields = array(); // array holds the fields to submit to paypal
  99. function paypal_class() {
  100. // initialization constructor. Called when class is created.
  101. $this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
  102. $this->last_error = '';
  103. $this->ipn_log_file = '.ipn_results.log';
  104. $this->ipn_log = true;
  105. $this->ipn_response = '';
  106. // populate $fields array with a few default values. See the paypal
  107. // documentation for a list of fields and their data types. These defaul
  108. // values can be overwritten by the calling script.
  109. $this->add_field('rm','2'); // Return method = POST
  110. $this->add_field('cmd','_xclick');
  111. }
  112. function add_field($field, $value) {
  113. // adds a key=>value pair to the fields array, which is what will be
  114. // sent to paypal as POST variables. If the value is already in the
  115. // array, it will be overwritten.
  116. $this->fields["$field"] = $value;
  117. }
  118. function submit_paypal_post() {
  119. // this function actually generates an entire HTML page consisting of
  120. // a form with hidden elements which is submitted to paypal via the
  121. // BODY element's onLoad attribute. We do this so that you can validate
  122. // any POST vars from you custom form before submitting to paypal. So
  123. // basically, you'll have your own form which is submitted to your script
  124. // to validate the data, which in turn calls this function to create
  125. // another hidden form and submit to paypal.
  126. // The user will briefly see a message on the screen that reads:
  127. // "Please wait, your order is being processed..." and then immediately
  128. // is redirected to paypal.
  129. echo "<head><title>Processing Payment...</title></head>\n";
  130. echo "<body onLoad=\"document.forms['paypal_form'].submit();\">\n";
  131. echo "<form method=\"post\" name=\"paypal_form\" ";
  132. echo "action=\"".$this->paypal_url."\">\n";
  133. echo '<input type="hidden" name="cmd" value="_xclick">';
  134. foreach ($this->fields as $name => $value) {
  135. echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
  136. }
  137. echo "</form>\n";
  138. echo "</body>\n";
  139. }
  140. function validate_ipn() {
  141. // parse the paypal URL
  142. $url_parsed=parse_url($this->paypal_url);
  143. // generate the post string from the _POST vars aswell as load the
  144. // _POST vars into an arry so we can play with them from the calling
  145. // script.
  146. $post_string = '';
  147. foreach ($_POST as $field=>$value) {
  148. $this->ipn_data["$field"] = $value;
  149. $post_string .= $field.'='.urlencode(stripslashes($value)).'&';
  150. }
  151. $post_string.="cmd=_notify-validate"; // append ipn command
  152. // open the connection to paypal
  153. $fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30);
  154. if(!$fp) {
  155. // could not open the connection. If loggin is on, the error message
  156. // will be in the log.
  157. $this->last_error = "fsockopen error no. $errnum: $errstr";
  158. $this->log_ipn_results(false);
  159. return false;
  160. } else {
  161. // Post the data back to paypal
  162. fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
  163. fputs($fp, "Host: $url_parsed[host]\r\n");
  164. fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
  165. fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
  166. fputs($fp, "Connection: close\r\n\r\n");
  167. fputs($fp, $post_string . "\r\n\r\n");
  168. // loop through the response from the server and append to variable
  169. while(!feof($fp)) {
  170. $this->ipn_response .= fgets($fp, 1024);
  171. }
  172. fclose($fp); // close connection
  173. }
  174. if (eregi("VERIFIED",$this->ipn_response)) {
  175. // Valid IPN transaction.
  176. $this->log_ipn_results(true);
  177. return true;
  178. } else {
  179. // Invalid IPN transaction. Check the log for details.
  180. $this->last_error = 'IPN Validation Failed.';
  181. $this->log_ipn_results(false);
  182. return false;
  183. }
  184. }
  185. function log_ipn_results($success) {
  186. if (!$this->ipn_log) return; // is logging turned off?
  187. // Timestamp
  188. $text = '['.date('m/d/Y g:i A').'] - ';
  189. // Success or failure being logged?
  190. if ($success) $text .= "SUCCESS!\n";
  191. else $text .= 'FAIL: '.$this->last_error."\n";
  192. // Log the POST variables
  193. $text .= "IPN POST Vars from Paypal:\n";
  194. foreach ($this->ipn_data as $key=>$value) {
  195. $text .= "$key=$value, ";
  196. }
  197. // Log the response from the paypal server
  198. $text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;
  199. // Write to log
  200. $fp=fopen($this->ipn_log_file,'a');
  201. fwrite($fp, $text . "\n\n");
  202. fclose($fp); // close file
  203. }
  204. function dump_fields() {
  205. // Used for debugging, this function will output all the field/value pairs
  206. // that are currently defined in the instance of the class using the
  207. // add_field() function.
  208. echo "<h3>paypal_class->dump_fields() Output:</h3>";
  209. echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
  210. <tr>
  211. <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
  212. <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
  213. </tr>";
  214. ksort($this->fields);
  215. foreach ($this->fields as $key => $value) {
  216. echo "<tr><td>$key</td><td>".urldecode($value)."&nbsp;</td></tr>";
  217. }
  218. echo "</table><br>";
  219. }
  220. }