Basic PayPal IPN script


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

PHP 4.1


Copy this code and paste it in your HTML
  1. // read the post from PayPal system and add 'cmd'
  2. $req = 'cmd=_notify-validate';
  3.  
  4. foreach ($_POST as $key => $value) {
  5. $value = urlencode(stripslashes($value));
  6. $req .= "&$key=$value";
  7. }
  8.  
  9. // post back to PayPal system to validate
  10. $header .= "POST /cgi-bin/webscr HTTP/1.0
  11. ";
  12. $header .= "Content-Type: application/x-www-form-urlencoded
  13. ";
  14. $header .= "Content-Length: " . strlen($req) . "
  15.  
  16. ";
  17. $fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
  18.  
  19. // assign posted variables to local variables
  20. $item_name = $_POST['item_name'];
  21. $item_number = $_POST['item_number'];
  22. $payment_status = $_POST['payment_status'];
  23. $payment_amount = $_POST['mc_gross'];
  24. $payment_currency = $_POST['mc_currency'];
  25. $txn_id = $_POST['txn_id'];
  26. $receiver_email = $_POST['receiver_email'];
  27. $payer_email = $_POST['payer_email'];
  28.  
  29. if (!$fp) {
  30. // HTTP ERROR
  31. } else {
  32. fputs ($fp, $header . $req);
  33. while (!feof($fp)) {
  34. $res = fgets ($fp, 1024);
  35. if (strcmp ($res, "VERIFIED") == 0) {
  36. // check the payment_status is Completed
  37. // check that txn_id has not been previously processed
  38. // check that receiver_email is your Primary PayPal email
  39. // check that payment_amount/payment_currency are correct
  40. // process payment
  41. }
  42. else if (strcmp ($res, "INVALID") == 0) {
  43. // log for manual investigation
  44. }
  45. }
  46. fclose ($fp);
  47. }
  48. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.