2011-03-19 81 views
1

嗨,我知道有很多问题已经在与我的问题有关,但我没有得到任何解决方案。我已经实现了paypal。它运作良好。现在我想在我的PayPal实现中实现ipn。我已经搜遍并找到一些代码。我已经实现了,但我越来越无效ipn。我可以从贝宝交易中获得所有细节,但对于ipn,它总是无效的。我用下面的代码在DoExpressCheckoutPayment.php文件贝宝:无效的IPN问题

$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); 
     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: [email protected]"; 
     $mail_To = "[email protected]"; 
     $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: [email protected]"; 
     $mail_To = "[email protected]"; 
     $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); 
} 

我设置的其他文件notify_url其定向到这个文件中像这样

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

我得到以下电子邮件:

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

有一件事我注意到我没有从$ _POST获取任何东西。我的$ _POST是空的。 请告诉我我错在哪里。 谢谢

+0

什么不工作?像这样的东西很少可以通过查看代码来排序,而是通过在实时环境中进行调试。 – 2011-03-19 09:09:18

+0

你是否去过你的paypay配置文件并将ipn设置为on? https://www.paypal.com/cgi-bin/customerprofileweb?cmd=_profile-ipn-notify – 2011-03-19 09:11:07

+0

@Awais - 尝试将电子邮件功能放在有效/无效语句之外,并将自己收到的$ req字符串发送给自己PP。然后你可以看看为什么它没有被正确处理。 – Kit 2011-03-19 09:34:00

回答

1

试试这里的代码。它与上面使用的稍有不同。这个对我有用。

<?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); 
    } 
    ?> 
+0

@Christopher。但是哥们没有在后期...... $ _POST是空的。好吧,告诉我一件事,$ _POST中的值是由PP还是由我设置的?如果它由PP设置,那么我没有得到任何东西。如果它是由我设定的,请告诉我在哪里设置这些? – 2011-03-19 10:22:17

+0

您收到的无效电子邮件是否包含'$ req'字符串的内容? – Kit 2011-03-19 10:23:45

+0

是它有,这是** cmd = _notify验证和notify_url = http%3A%2F%2Fwww.mysite.com/paypal%2FDoExpressCheckoutPayment.php ** – 2011-03-19 10:25:53

1

作为回报链接提供的URL被称为2次!第一次来自PayPal并发送POST数据时,第二次用户被重定向。你会看到一些GET变量,但它们是无用的(至少对于请求事务)。

查看POST数据是否到达(在第一个请求中)的唯一方法是使用error_log或类似的东西。