/ Published in: PHP
This is a basic shell for PayPal IPN.
It is by no means a complete solution, but it should provide a good starting point for anyone that wants to work out IPN.
Or it can be used as is to do basic IPN.
It is by no means a complete solution, but it should provide a good starting point for anyone that wants to work out IPN.
Or it can be used as is to do basic IPN.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $req .= "&$key=$value"; } $header .= "POST /cgi-bin/webscr HTTP/1.0 "; $header .= "Host: www.paypal.com:80 "; // $header .= "Host: www.sandbox.paypal.com:80 "; $header .= "Content-Type: application/x-www-form-urlencoded "; $header .= "Content-Length: " . strlen($req) . " "; $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30); // $fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30); if (!$fp) { // Do some code when your server cannot communicate with PayPal! mail('[email protected]', 'IPN ERROR', "HTTP ERROR\n\n" . $res); } else { fputs ($fp, $header . $req); while (!feof($fp)) { $res = fgets ($fp, 1024); if (strcmp ($res, "VERIFIED") == 0) { if ($_POST['payment_status'] == 'Completed') { // Do some code when PayPal returns payment completed status! foreach ($_POST as $key => $value){ $emailtext .= $key . ": " . $value . "\n"; } } else { // Do some code when PayPal returns a different status! foreach ($_POST as $key => $value) { $emailtext .= $key . ": " . $value . "\n"; } } } elseif (strcmp ($res, "INVALID") == 0) { // Do some code when PayPal returns some error! foreach ($_POST as $key => $value){ $emailtext .= $key . ": " . $value . "\n"; } } } fclose ($fp); } ?>