PayPal IPN Shell


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. <?php
  2. $req = 'cmd=_notify-validate';
  3. foreach ($_POST as $key => $value) {
  4. $value = urlencode(stripslashes($value));
  5. $req .= "&$key=$value";
  6. }
  7. $header .= "POST /cgi-bin/webscr HTTP/1.0
  8. ";
  9. $header .= "Host: www.paypal.com:80
  10. ";
  11. // $header .= "Host: www.sandbox.paypal.com:80
  12. ";
  13. $header .= "Content-Type: application/x-www-form-urlencoded
  14. ";
  15. $header .= "Content-Length: " . strlen($req) . "
  16.  
  17. ";
  18. $fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
  19. // $fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
  20. if (!$fp) {
  21. // Do some code when your server cannot communicate with PayPal!
  22. mail('[email protected]', 'IPN ERROR', "HTTP ERROR\n\n" . $res);
  23. } else {
  24. fputs ($fp, $header . $req);
  25. while (!feof($fp)) {
  26. $res = fgets ($fp, 1024);
  27. if (strcmp ($res, "VERIFIED") == 0) {
  28. if ($_POST['payment_status'] == 'Completed') {
  29. // Do some code when PayPal returns payment completed status!
  30. foreach ($_POST as $key => $value){
  31. $emailtext .= $key . ": " . $value . "\n";
  32. }
  33. mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
  34. } else {
  35. // Do some code when PayPal returns a different status!
  36. foreach ($_POST as $key => $value) {
  37. $emailtext .= $key . ": " . $value . "\n";
  38. }
  39. mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
  40. }
  41. } elseif (strcmp ($res, "INVALID") == 0) {
  42. // Do some code when PayPal returns some error!
  43. foreach ($_POST as $key => $value){
  44. $emailtext .= $key . ": " . $value . "\n";
  45. }
  46. mail('[email protected]', "IPN VARIABLES", $emailtext . "\n\nREQUEST STRING:\n" . $req);
  47. }
  48. }
  49. fclose ($fp);
  50. }
  51. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.