-
Notifications
You must be signed in to change notification settings - Fork 2
/
paypal_ipn.php
191 lines (150 loc) · 7 KB
/
paypal_ipn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
class paypal_ipn
{
var $paypal_post_vars;
var $paypal_response;
var $timeout;
// error logging info
var $error_log_file;
var $error_email;
function paypal_ipn($paypal_post_vars)
{
$this->paypal_post_vars = $paypal_post_vars;
$this->timeout = 120;
}
function send_response()
{
if(extension_loaded('curl'))
$this->send_response_curl();
else
$this->send_response_fsockopen();
}
function send_response_curl()
{
// put all POST variables received from Paypal back into a URL encoded string
foreach($this->paypal_post_vars AS $key => $value)
{
// if magic quotes gpc is on, PHP added slashes to the values so we need
// to strip them before we send the data back to Paypal.
if( @get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
// make an array of URL encoded values
$values[] = "$key" . "=" . urlencode($value);
}
// join the values together into one url encoded string
$this->url_string .= @implode("&", $values);
// add paypal cmd variable
$this->url_string .= "&cmd=_notify-validate";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->url_string);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; www.iTechScripts.com; PayPal IPN Class)");
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, $this->timeout);
$this->paypal_response = curl_exec ($ch);
curl_close($ch);
if($this->paypal_response == "" || ($this->paypal_response != "VERIFIED" && $this->paypal_response != "INVALID"))
$this->send_response_fsockopen();
} // end function send_response_curl()
// sends response back to paypal
function send_response_fsockopen()
{
$fp = @fsockopen( "www.paypal.com", 80, &$errno, &$errstr, 120 );
if( !$fp )
{
$this->error_out("PHP fsockopen() error: " . $errstr);
}
else
{
// put all POST variables received from Paypal back into a URL encoded string
foreach($this->paypal_post_vars AS $key => $value)
{
// if magic quotes gpc is on, PHP added slashes to the values so we need
// to strip them before we send the data back to Paypal.
if( @get_magic_quotes_gpc() )
{
$value = stripslashes($value);
}
// make an array of URL encoded values
$values[] = "$key" . "=" . urlencode($value);
}
// join the values together into one url encoded string
$response = @implode("&", $values);
// add paypal cmd variable
$response .= "&cmd=_notify-validate";
fputs( $fp, "POST /cgi-bin/webscr HTTP/1.0\r\n" );
fputs( $fp, "Host: www.paypal.com\r\n" );
fputs( $fp, "User-Agent: Mozilla/4.0 (compatible; www.iTechScripts.com; PayPal IPN Class)\r\n" );
fputs( $fp, "Accept: */*\r\n" );
fputs( $fp, "Accept: image/gif\r\n" );
fputs( $fp, "Accept: image/x-xbitmap\r\n" );
fputs( $fp, "Accept: image/jpeg\r\n" );
fputs( $fp, "Content-type: application/x-www-form-urlencoded\r\n" );
fputs( $fp, "Content-length: " . strlen($response) . "\r\n\n" );
// send url encoded string of data
fputs( $fp, "$response\n\r" );
fputs( $fp, "\r\n" );
$this->send_time = time();
$this->paypal_response = "";
// get response from paypal
while( !feof( $fp ) )
{
$this->paypal_response .= fgets( $fp, 1024 );
// waited too long?
if( $this->send_time < time() - $this->timeout )
{
$this->error_out("Timed out waiting for a response from PayPal. ($this->timeout seconds)");
}
} // end while
fclose( $fp );
} // end else
} // end function send_response_fsockopen()
// returns true if paypal says the order is good, false if not
function is_verified()
{
if( ereg("VERIFIED", $this->paypal_response) )
{
return true;
}
else
{
return false;
}
} // end function is_verified
// returns the paypal payment status
function get_payment_status()
{
return $this->paypal_post_vars['payment_status'];
}
// writes error to logfile, exits script
function error_out($message)
{
$date = date("D M j G:i:s T Y", time());
// add on the data we sent:
$message .= "\n\nThe following input was received from (and sent back to) PayPal:\n\n";
@reset($this->paypal_post_vars);
while( @list($key,$value) = @each($this->paypal_post_vars) )
{
$message .= $key . ':' . " \t$value\n";
}
// log to file?
if( $this->error_log_file )
{
@fopen($this->error_log_file, 'a');
$message = "$date\n\n" . $message . "\n\n";
@fputs($fp, $message);
@fclose($fp);
}
// email errors?
if( $this->error_email )
{
$additional_headers = "From: \"$fromname\" <$from>\nReply-To: $from";
mail($this->error_email, "[$date] paypay_ipn error", $message, $additional_headers);
}
exit;
} // end function error_out
} // end class paypal_ipn
?>