Posted By

Arvi on 03/28/11


Tagged

PayPal IPN


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

notturnale
czehner


PayPal IPN Snippet


 / Published in: PHP
 

Source: http://net.tutsplus.com/tutorials/php/using-paypals-instant-payment-notification-with-php/

  1. <?php
  2.  
  3. mysql_connect("localhost", "user", "password") or die(mysql_error());
  4.  
  5. // read the post from PayPal system and add 'cmd'
  6. $req = 'cmd=_notify-validate';
  7. foreach ($_POST as $key => $value) {
  8. $value = urlencode(stripslashes($value));
  9. $req .= "&$key=$value";
  10. }
  11. // post back to PayPal system to validate
  12. $header = "POST /cgi-bin/webscr HTTP/1.0
  13. ";
  14. $header .= "Content-Type: application/x-www-form-urlencoded
  15. ";
  16. $header .= "Content-Length: " . strlen($req) . "
  17.  
  18. ";
  19.  
  20. $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
  21.  
  22. if (!$fp) {
  23. // HTTP ERROR
  24. } else {
  25. fputs ($fp, $header . $req);
  26. while (!feof($fp)) {
  27. $res = fgets ($fp, 1024);
  28. if (strcmp ($res, "VERIFIED") == 0) {
  29.  
  30. // PAYMENT VALIDATED & VERIFIED!
  31.  
  32. }
  33.  
  34. else if (strcmp ($res, "INVALID") == 0) {
  35.  
  36. // PAYMENT INVALID & INVESTIGATE MANUALY!
  37.  
  38. }
  39. }
  40. fclose ($fp);
  41. }
  42. ?>

Report this snippet  

You need to login to post a comment.