KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paypal.class.php 9.4KB

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