PayPal IPN支付网关

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PayPal IPN支付网关相关的知识,希望对你有一定的参考价值。

  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 =================================

以上是关于PayPal IPN支付网关的主要内容,如果未能解决你的问题,请参考以下文章

多次收到 Paypal IPN 响应

Paypal 沙盒 IPN 模拟器 - 502 Bad Gateway

使用非托管 PayPal 按钮时如何使用 IPN 验证 PayPal 数据

Paypal 链式支付 - 支付密钥和 IPN

PayPal IPN 在沙箱中返回无效

如何使用 sandbox.paypal 获取 IPN 进行支付