Posted By


mafhh14 on 08/17/11

Tagged


Statistics


Viewed 764 times
Favorited by 0 user(s)

PayPal IPN Payment Gateway


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



Copy this code and paste it in your HTML
  1. <?php
  2. // for live
  3. // $url = "https://www.paypal.com/cgi-bin/webscr";
  4. // for sandbox
  5. $url = "https://www.sandbox.paypal.com/cgi-bin/webscr";
  6. ?>
  7. <form name="paypal_ipn" action="<?php echo $url ?>" method="POST">
  8. <input type="hidden" name="upload" value="1" />
  9. <input type="hidden" name="cmd" value="_cart" />
  10. <!-- The business email address, where you want to receive the payment -->
  11. <input type="hidden" name="business" value="[email protected]" />
  12. <!-- The customer email address -->
  13. <input type="hidden" name="receiver_email" value="[email protected]" />
  14. <input type="hidden" name="item_name_1" value="test product" />
  15. <input type="hidden" name="quantity_1" value="1" />
  16. <input type="hidden" name="amount_1" value="25.58" />
  17. <input type="hidden" name="currency_code" value="USD" />
  18. <input type="hidden" name="invoice" value="12345" />
  19. <input type="hidden" name="amount" value="25.58" />
  20. <!-- Where you want to return after PayPal Payment -->
  21. <input type="hidden" name="return" value="http://www.yoursite.com/finished.php" />
  22. <!-- A back-end notification send to the specific page after successful payment -->
  23. <input type="hidden" name="notify_url" value="https://www.yoursite.com/finished_secure.php" />
  24. <!-- Where you want to return after cancel the PayPal Payment -->
  25. <input type="hidden" name="cancel_return" value="http://www.yoursite.com" />
  26. <input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" border=0 />
  27. </form>
  28.  
  29. ============================ Start finished_secure.php file ===============================
  30. <?php
  31. $req = 'cmd=_notify-validate';
  32. foreach($_POST as $key => $value) {
  33. $value = urlencode(stripslashes($value));
  34. $req .= "&$key=$value";
  35. }
  36. $header .= "POST /cgi-bin/webscr HTTP/1.0
  37. ";
  38. $header .= "Content-Type: application/x-www-form-urlencoded
  39. ";
  40. $header .= "Content-Length: " . strlen($req) . "
  41. ";
  42. //$url = "ssl://www.paypal.com"; // for live
  43. $url = "ssl://www.sandbox.paypal.com"; // for sandbox
  44. $fp = fsockopen ("$url", 443, $errno, $errstr, 30);
  45.  
  46. $paypal_payment_amount = $_POST['mc_gross'];
  47.  
  48. //print_r($_POST); To print all the variables get form PayPal
  49.  
  50. if(!$fp){
  51. echo "PayPal socket not opened.";
  52. }
  53. else{
  54. fputs ($fp, $header . $req);
  55. while(!feof($fp)){
  56. $res = fgets ($fp, 1024);
  57. if(strcmp($res, "VERIFIED") == 0){
  58. // write any code to here
  59. }
  60. elseif(strcmp ($res, "INVALID") == 0){
  61. // error
  62. }
  63. } // end while
  64. }
  65. fclose($fp);
  66. ?>
  67. ============================== End finished_secure.php file =================================

URL: http://www.phpmoot.com/php-paypal-ipn-payment-gateway/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.