How to test Paypal IPN notification

吃可爱长大的小学妹 提交于 2020-12-13 05:57:59

问题


I'm trying to get this PayPal IPN script work but it seems that i don't get the response from paypal . I don't know what's the problem, is in the form or in the script:

<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input name="return" type="hidden" value="http://localhost/home/menu/index.php" />
    <input name="cancel_return" type="hidden" value="http://localhost/home/menu/index.php" />
    <input name="notify_url" type="hidden" value="http://localhost/home/menu/php/inni.php" /> 

and this is the script inni.php:

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$item_name = $_POST['item_name'];
if (!$fp) {

} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
    $res = fgets ($fp, 1024);
    if (strcmp ($res, "VERIFIED") == 0) {
        // vérifier que payment_status a la valeur Completed
        if ( $payment_status == "Completed") {
                         if ( $email_account == $receiver_email) {
//insert in db
          }
        }
        else {
                // Statut de paiement: Echec
        }
        exit();
   }
    else if (strcmp ($res, "INVALID") == 0) {
        // Transaction invalide
    }
}
fclose ($fp);
}   

回答1:


The problem is that you are sending PayPal to localhost.

Change the links (notify_url) to a publicly accessible URL or IP address.

PayPal sends notifications from their server, so sending to localhost will not reach you as localhost in this case would be the PayPal server itself.




回答2:


You need to forward PayPal IPN requests to your local computer for it to work. There are several projects dealing with this, both free and commercial.

The free and open source variant being: https://github.com/antoniomika/sish

One commercial variant: https://ngrok.com/

Ngrok is somewhat faster to implement since you don't need to configure the server.

To forward http requests to localhost with Ngrok

./ngrok http -host-header=localhost 80

Then in your browser open

http://localhost:4040/inspect/http

Copy your ngrok url from the status page

https://d1c7361aebf0.ngrok.io

Assuming your IPN notification url is

https://www.example.com/api/paypal-ipn

Update it to and start testing instant payment notifications

https://d1c7361aebf0.ngrok.io/api/paypal-ipn

Note: Make sure you disable all domain name related redirects for it to work.



来源:https://stackoverflow.com/questions/24224196/how-to-test-paypal-ipn-notification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!