10 |
rodolico |
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.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', 'somebody@domain.com');
|
|
|
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 |
|
|
|
93 |
class paypal_class {
|
|
|
94 |
|
|
|
95 |
var $last_error; // holds the last error encountered
|
|
|
96 |
|
|
|
97 |
var $ipn_log; // bool: log IPN results to text file?
|
|
|
98 |
|
|
|
99 |
var $ipn_log_file; // filename of the IPN log
|
|
|
100 |
var $ipn_response; // holds the IPN response from paypal
|
|
|
101 |
var $ipn_data = array(); // array contains the POST values for IPN
|
|
|
102 |
|
|
|
103 |
var $fields = array(); // array holds the fields to submit to paypal
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
function paypal_class() {
|
|
|
107 |
|
|
|
108 |
// initialization constructor. Called when class is created.
|
|
|
109 |
|
|
|
110 |
$this->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
|
|
|
111 |
|
|
|
112 |
$this->last_error = '';
|
|
|
113 |
|
|
|
114 |
$this->ipn_log_file = '.ipn_results.log';
|
|
|
115 |
$this->ipn_log = true;
|
|
|
116 |
$this->ipn_response = '';
|
|
|
117 |
|
|
|
118 |
// populate $fields array with a few default values. See the paypal
|
|
|
119 |
// documentation for a list of fields and their data types. These defaul
|
|
|
120 |
// values can be overwritten by the calling script.
|
|
|
121 |
|
|
|
122 |
$this->add_field('rm','2'); // Return method = POST
|
|
|
123 |
$this->add_field('cmd','_xclick');
|
|
|
124 |
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
function add_field($field, $value) {
|
|
|
128 |
|
|
|
129 |
// adds a key=>value pair to the fields array, which is what will be
|
|
|
130 |
// sent to paypal as POST variables. If the value is already in the
|
|
|
131 |
// array, it will be overwritten.
|
|
|
132 |
|
|
|
133 |
$this->fields["$field"] = $value;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
function submit_paypal_post() {
|
|
|
137 |
|
|
|
138 |
// this function actually generates an entire HTML page consisting of
|
|
|
139 |
// a form with hidden elements which is submitted to paypal via the
|
|
|
140 |
// BODY element's onLoad attribute. We do this so that you can validate
|
|
|
141 |
// any POST vars from you custom form before submitting to paypal. So
|
|
|
142 |
// basically, you'll have your own form which is submitted to your script
|
|
|
143 |
// to validate the data, which in turn calls this function to create
|
|
|
144 |
// another hidden form and submit to paypal.
|
|
|
145 |
|
|
|
146 |
// The user will briefly see a message on the screen that reads:
|
|
|
147 |
// "Please wait, your order is being processed..." and then immediately
|
|
|
148 |
// is redirected to paypal.
|
|
|
149 |
|
|
|
150 |
echo "<html>\n";
|
|
|
151 |
echo "<head><title>Processing Payment...</title></head>\n";
|
|
|
152 |
echo "<body onLoad=\"document.forms['paypal_form'].submit();\">\n";
|
|
|
153 |
echo "<center><h2>Please wait, your order is being processed and you";
|
|
|
154 |
echo " will be redirected to the paypal website.</h2></center>\n";
|
|
|
155 |
echo "<form method=\"post\" name=\"paypal_form\" ";
|
|
|
156 |
echo "action=\"".$this->paypal_url."\">\n";
|
|
|
157 |
|
|
|
158 |
foreach ($this->fields as $name => $value) {
|
|
|
159 |
echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
|
|
|
160 |
}
|
|
|
161 |
echo "<center><br/><br/>If you are not automatically redirected to ";
|
|
|
162 |
echo "paypal within 5 seconds...<br/><br/>\n";
|
|
|
163 |
echo "<input type=\"submit\" value=\"Click Here\"></center>\n";
|
|
|
164 |
|
|
|
165 |
echo "</form>\n";
|
|
|
166 |
echo "</body></html>\n";
|
|
|
167 |
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
function validate_ipn() {
|
|
|
171 |
|
|
|
172 |
// parse the paypal URL
|
|
|
173 |
$url_parsed=parse_url($this->paypal_url);
|
|
|
174 |
|
|
|
175 |
// generate the post string from the _POST vars aswell as load the
|
|
|
176 |
// _POST vars into an arry so we can play with them from the calling
|
|
|
177 |
// script.
|
|
|
178 |
$post_string = '';
|
|
|
179 |
foreach ($_POST as $field=>$value) {
|
|
|
180 |
$this->ipn_data["$field"] = $value;
|
|
|
181 |
$post_string .= $field.'='.urlencode(stripslashes($value)).'&';
|
|
|
182 |
}
|
|
|
183 |
$post_string.="cmd=_notify-validate"; // append ipn command
|
|
|
184 |
|
|
|
185 |
// open the connection to paypal
|
|
|
186 |
$fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30);
|
|
|
187 |
if(!$fp) {
|
|
|
188 |
|
|
|
189 |
// could not open the connection. If loggin is on, the error message
|
|
|
190 |
// will be in the log.
|
|
|
191 |
$this->last_error = "fsockopen error no. $errnum: $errstr";
|
|
|
192 |
$this->log_ipn_results(false);
|
|
|
193 |
return false;
|
|
|
194 |
|
|
|
195 |
} else {
|
|
|
196 |
|
|
|
197 |
// Post the data back to paypal
|
|
|
198 |
fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
|
|
|
199 |
fputs($fp, "Host: $url_parsed[host]\r\n");
|
|
|
200 |
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
|
|
|
201 |
fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
|
|
|
202 |
fputs($fp, "Connection: close\r\n\r\n");
|
|
|
203 |
fputs($fp, $post_string . "\r\n\r\n");
|
|
|
204 |
|
|
|
205 |
// loop through the response from the server and append to variable
|
|
|
206 |
while(!feof($fp)) {
|
|
|
207 |
$this->ipn_response .= fgets($fp, 1024);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
fclose($fp); // close connection
|
|
|
211 |
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if (eregi("VERIFIED",$this->ipn_response)) {
|
|
|
215 |
|
|
|
216 |
// Valid IPN transaction.
|
|
|
217 |
$this->log_ipn_results(true);
|
|
|
218 |
return true;
|
|
|
219 |
|
|
|
220 |
} else {
|
|
|
221 |
|
|
|
222 |
// Invalid IPN transaction. Check the log for details.
|
|
|
223 |
$this->last_error = 'IPN Validation Failed.';
|
|
|
224 |
$this->log_ipn_results(false);
|
|
|
225 |
return false;
|
|
|
226 |
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
function log_ipn_results($success) {
|
|
|
232 |
|
|
|
233 |
if (!$this->ipn_log) return; // is logging turned off?
|
|
|
234 |
|
|
|
235 |
// Timestamp
|
|
|
236 |
$text = '['.date('m/d/Y g:i A').'] - ';
|
|
|
237 |
|
|
|
238 |
// Success or failure being logged?
|
|
|
239 |
if ($success) $text .= "SUCCESS!\n";
|
|
|
240 |
else $text .= 'FAIL: '.$this->last_error."\n";
|
|
|
241 |
|
|
|
242 |
// Log the POST variables
|
|
|
243 |
$text .= "IPN POST Vars from Paypal:\n";
|
|
|
244 |
foreach ($this->ipn_data as $key=>$value) {
|
|
|
245 |
$text .= "$key=$value, ";
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
// Log the response from the paypal server
|
|
|
249 |
$text .= "\nIPN Response from Paypal Server:\n ".$this->ipn_response;
|
|
|
250 |
|
|
|
251 |
// Write to log
|
|
|
252 |
$fp=fopen($this->ipn_log_file,'a');
|
|
|
253 |
fwrite($fp, $text . "\n\n");
|
|
|
254 |
|
|
|
255 |
fclose($fp); // close file
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
function dump_fields() {
|
|
|
259 |
|
|
|
260 |
// Used for debugging, this function will output all the field/value pairs
|
|
|
261 |
// that are currently defined in the instance of the class using the
|
|
|
262 |
// add_field() function.
|
|
|
263 |
|
|
|
264 |
echo "<h3>paypal_class->dump_fields() Output:</h3>";
|
|
|
265 |
echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
|
|
|
266 |
<tr>
|
|
|
267 |
<td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
|
|
|
268 |
<td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
|
|
|
269 |
</tr>";
|
|
|
270 |
|
|
|
271 |
ksort($this->fields);
|
|
|
272 |
foreach ($this->fields as $key => $value) {
|
|
|
273 |
echo "<tr><td>$key</td><td>".urldecode($value)." </td></tr>";
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
echo "</table><br>";
|
|
|
277 |
}
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|