开发者

Paypal: Invalid IPN problem

开发者 https://www.devze.com 2023-02-18 21:41 出处:网络
Hi I know that there are many questions already in SO related to my problem but I have not got solution from any of them. I have implemented paypal. It is working well. Now I want to implement ipn in

Hi I know that there are many questions already in SO related to my problem but I have not got solution from any of them. I have implemented paypal. It is working well. Now I want to implement ipn in my paypal implementation. I have searched through and found some code. I have implemented that but I am getting invalid ipn. I can get all details from paypal transaction but for ipn it is always invalid. I have used following code in DoExpressCheckoutPayment.php file

$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

// post back to PayPal system to validate

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";

// If testing on Sandbox use: 
$header .= "Host: www.sandbox.paypal.com:443\r\n";
//$header .= "Host: www.paypal.com:443\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

// If testing on Sandbox use:
//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$fp =fsockopen('ssl://www.sandbox.paypal.com',443,$err_num,$err_str,30);
echo('<br>'.$req);
// assign posted variables to local variables
/*$item_name = $_POST['item_name'];
$item_number = $_POST['item_number'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$payment_currency = $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];*/

if (!$fp)
{
    echo(' HTTP ERROR');
}
else
{
    fputs ($fp, $header . $req);
    while (!feof($fp))
    {
        $res = fgets ($fp, 1024);
    开发者_如何学JAVA    echo('<br> res is '.$res);
    if (strcmp ($res, "VERIFIED") == 0)
    {
        // check the payment_status is Completed
        // check that txn_id has not been previously processed
        // check that receiver_email is your Primary PayPal email
        // check that payment_amount/payment_currency are correct
        // process payment

        $mail_From = "From: me@mybiz.com";
        $mail_To = "xxxx@gmail.com";
        $mail_Subject = "VERIFIED IPN";
        $mail_Body = $req;
        foreach ($_SESSION as $key => $value)
        {
            $emailtext .= $key . " = " .$value ."\n\n";
        }
        if(mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From))
        echo('<br>mail 1 sent');
        else
        echo('<br>mail1 not sent');
    }
    else if (strcmp ($res, "INVALID") == 0)
    {
        // log for manual investigation
        $mail_From = "From: me@mybiz.com";
        $mail_To = "xxx@gmail.com";
        $mail_Subject = "INVALID IPN";
        $mail_Body = $req;
        foreach ($_SESSION as $key => $value)
        {
            $emailtext .= $key . " = " .$value ."\n\n";
        }
        if(mail($mail_To, $mail_Subject, $emailtext . "\n\n" . $mail_Body, $mail_From))
        echo('<br>mail sent');
        else
        echo('<br>not sent');
    }
}
fclose ($fp);
}

I am setting notify_url in other file which is directing to this file like this

&lt;input type="hidden" name="notify_url" value="http://www.mysite.com/paypal/DoExpressCheckoutPayment.php"/>

I am getting the following email:

**notify_url = http://www.mysite.com/paypal/DoExpressCheckoutPayment.php
cmd=_notify-validate&notify_url=http%3A%2F%2Fwww.mysite.com%2Fpaypal%2FDoExpressCheckoutPayment.php**

One thing that I have notice that I am not getting any thing from $_POST. My $_POST is empty. Please tell me where I am wrong. Thanks


Try this code here. It is slightly different to the one you used above. It works for me.

    <?php
    $req = 'cmd=_notify-validate';

    foreach ($_POST as $key => $value) {
           $value = urlencode(stripslashes($value));
           $req .= "&$key=$value";
        }

    // post back to PayPal system to validate
    $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);

    // assign posted variables to local variables
    $item_name          = $_POST['item_name'];
    $item_number        = $_POST['item_number'];
    $payment_status     = $_POST['payment_status'];
    $payment_amount     = $_POST['mc_gross'];
    $payment_currency   = $_POST['mc_currency'];
    $txn_id             = $_POST['txn_id'];
    $receiver_email     = $_POST['receiver_email'];
    $payer_email        = $_POST['payer_email'];

    if (!$fp) {

        // HTTP ERROR

    } else {
        fputs ($fp, $header . $req);
            while (!feof($fp)) {
            $res = fgets ($fp, 1024);
            if (strcmp ($res, "VERIFIED") == 0) {
                //Process Order
            }else if (strcmp ($res, "INVALID") == 0) {
                //Send Email To You 
            }
        }

        fclose ($fp);
    }
    ?>


URL provided as return link is called 2 times! First time it is coming from PayPal and sending POST data, second time user is redirected. You will see some GET variables, but they are quite useless (at least for requesting transaction).

The only way to see if POST data are coming (in first request) is using error_log or something like that.

0

精彩评论

暂无评论...
验证码 换一张
取 消