【问题标题】:Paypal Response getting "Fail"贝宝响应“失败”
【发布时间】:2013-02-14 18:05:53
【问题描述】:

我正在将 paypal 集成到我的购物车中 这是我的价值转移表格

      <h3>By Paypal</h3><hr/>
       <label>Click Here</label>
       <br/>
     <?php
      include 'controller/connection.php';
      $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
      $paypal_id = 'my-sellerID_biz@yahoo.com';                                                                                                                                $cancel_url = 'http://my-website/index.php?action=cancel';
      $return_url = 'http://my-website/store.php';
      $i = 0;
      $products = array();
      $qry = mysql_query("select * from temp_cart where tokenId='$token'");
      while ($row = mysql_fetch_array($qry)) {
      $pid = $row[0];
      $pname = $row[1];
      $qty = $row[3];
      $price = $row[4];
      $amount = $row[5];
      $products[] = array('pid' => $pid, 'pname' => $pname, 'qty' => $qty, 'price' => $price, 'amount' => $amount);
       }
    ?>
     <form action="<?php echo $paypal_url;?>" method="post">
     <input type="hidden" name="cmd" value="_cart">
     <input type="hidden" name="upload" value="1">
     <input type="hidden" name="add" value="1">
     <input type="hidden" name="business" value="<?php echo $paypal_id;?>">
             <?php
             foreach ($products as $product) {
             $i++;
              //echo $product['amount'];
              ?>
             <input type="hidden" name="item_name_<?php echo $i;?>" value="<?php echo $product['pname']?>">
             <input type='hidden' name='item_number_<?php echo $i;?>' value='<?php echo $product['pid'] ?>' />

             <input type="hidden" name="amount_<?php echo $i;?>" value="<?php echo $product['amount']?>">
            <?php
              }
             ?>

        <input type='image' src='https://www.sandbox.paypal.com/en_US/i/btn/btn_buynow_SM.gif' name='submit' style='display:{$display_button}' />
   </form>

这工作正常。 但付款后,当我从贝宝重定向时,我得到了失败响应。但在我的发送箱帐户中,它显示我完成。并显示我购物车。我从贝宝获取响应的代码是:-

<?php
session_start();

//include('connection.php');
/*
 update: 06/27/2011
  - updated to use cURL for better security, assumes PHP version 5.3
*/

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];

$pp_hostname = "www.sandbox.paypal.com"; 

// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-synch';

$tx_token = $_GET['tx'];
$auth_token = "MY API TOKEN";
$req .= "&tx=$tx_token&at=$auth_token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://$pp_hostname/cgi-bin/webscr");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
//set cacert.pem verisign certificate path in curl using 'CURLOPT_CAINFO' field here,
//if your server does not bundled with default verisign certificates.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: $pp_hostname"));
$res = curl_exec($ch);
curl_close($ch);

if(!$res){
    //HTTP ERROR
}else{
     // parse the data
    $lines = explode("\n", $res);
    $keyarray = array();
    echo $lines[0];

    echo "<br>";
    if (strcmp ($lines[0], "SUCCESS") == 0) {
       echo $lines[0];
        for ($i=1; $i<count($lines);$i++){
        list($key,$val) = explode("=", $lines[$i]);
        $keyarray[urldecode($key)] = urldecode($val);
    }
    //echo "skjdhckh";
    // 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




    $user_email = 'myID@gmail.com';

     $site_name = 'Company name';
     $eol = "\r\n";
     $headers  = 'MIME-Version: 1.0' . $eol;
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . $eol;
     $headers .= 'From:' . $site_name . ' <mailID@gmail.com>'. $eol;
     $headers .= 'Reply-To:' . $site_name . ' <mailID@gmail.com>' . $eol;
     $headers .= 'Return-Path:' . $site_name . ' <mailID@gmail.com>' . $eol;

     $eol = "\r\n<br />";
     $thanks_text = $eol.$eol.'Thanks!'. $eol;
    if(isset($_SESSION['user']) && ($_SESSION['token']))
        {
                include('connection.php');

                $user=$_SESSION['user'];
                $_SESSION['user']=$user;
                $token=$_SESSION['token'];
                $_SESSION['token']=$token;
                //$sub=$_SESSION['coursename'];
                date_default_timezone_set('Australia/Melbourne'); 
                $date = date('y-m-d h:i:s a', time());
                $qry="update allorder set status='completed',date='$date'  where token='$token' and name1='$user'";
                $run_qry=mysql_query($qry);

                    if($run_qry)
                    {

    /*$select_id="select email from register where username='$username'";
    $run_qry2=mysql_query($select_id);
     if($row=mysql_fetch_row($run_qry2))
    {   */
      $email=$row[0];   
      $email_tpl = 'Hello Buyer,' . $eol;
      $email_tpl .= "We have received your payment of $amount $cc." . $eol;
      $email_tpl .= "Thanks for the purchase." . $eol;
      $email_tpl .= $eol . $thanks_text;

    mail ($email, 'Payment Received', $email_tpl, $headers);
    echo $email;
    $_SESSION['username']=$user;
                ?>
                 <script language="javascript" type="text/javascript">
            // Print a message
            alert('Thank you for your purchase! your Payment was successfull. Now You can print and download your certificate');
                // Redirect to some page of the site.
            window.location = 'index.php';
                </script>
                <?php
    //}

                }

                    else
                    {
                    echo mysql_error();
                    }

    }

    }
    else if (strcmp ($lines[0], "FAIL") == 0) {
       echo 'Transaction Failed';
    }
}

?>

一切正常,但我收到失败响应。 如果我忘记了什么,请帮助我。

提前致谢

【问题讨论】:

    标签: php paypal payment-gateway


    【解决方案1】:

    如果您正在使用沙箱进行测试,请确保您使用的是来自您的测试沙箱卖家账户的 PDT 令牌。

    失败、4002 或 4003 响应的原因

    • 确保回发“tx”、“cmd”和 Auth 值 为了让 PayPal 返回 SUCCESS 和交易详细信息,您的 PDT 脚本需要使用 cmd“_notify-synch”将从返回字符串中提取的“tx”变量、来自您帐户的 Auth 令牌发布到 PayPal。

      李>
    • 无效的身份验证令牌 检查令牌以确保其准确无误并正确传回。确保您的令牌是一个连续的字符串,并且在您的脚本中没有换行符。对于某些语言,这可能会有所不同。

    • 确保您没有发回错误的 URL。 如果您在沙盒中进行测试,您需要确保您的脚本回传到 www.sandbox.paypal.com。如果您在实时站点上,则该脚本应发回 www.paypal.com。如果您在沙盒中进行测试并且您的脚本发布回实时站点(反之亦然),您将收到 FAIL

    • 验证相同的“tx”超过 5 次 检查用户行为。获得 FAIL 的另一种方法是,如果您引用相同的“tx”超过 5 次。示例:如果用户在看到详细信息后刷新 PDT 页面 5 次,他们将在最后一次刷新时看到 FAIL。这不是错误。这样做是出于安全考虑,因此不能无限期访问 PDT 创建的 URL 以检索特定于事务的数据。

    • “tx”值无效或缺失 “tx”值是用于收集付款信息的交易 ID。如果这无效或不存在,您将收到 FAIL 响应,因为无法检索付款数据。

    未收到 PDT

    • 与付款相关的电子邮件地址 要使 PDT 起作用,您必须在按钮代码中确认用作业务价值的电子邮件地址。如果未确认此电子邮件地址,PDT 将不起作用,因为您将看不到附加到返回 URL 的 PDT 查询字符串。

    • 确保您使用的是格式正确的返回 URL。 当您启用 PDT 时,您用作默认自动返回 URL 的 URL 必须是有效的主机名或 IP 地址,而不是内部 Intranet URL,否则用户将不会自动返回到该 URL。确保您使用有效的 URL 作为您希望用户返回的 URL,否则您将看不到预期的结果。

    • 订阅式付款按钮 PDT 不适用于订阅,如果您有免费试用订阅,您将在返回页面/PDT 流程中看不到任何数据。这是因为 PDT 传输支付数据,如果在注册时,如果没有支付,那么就意味着没有支付数据要传输。

    • 用户行为 PDT 是客户通过退货方法激活的,方法是选择退货按钮或在客户登录其 PayPal 账户进行支付的情况下不选择任何内容。这意味着买家可以更改退货流程或退出浏览器,并且不会传输任何数据。

    【讨论】:

    • 另外,“tx”参数区分大小写,PayPal 交易 ID 都是大写的。如果您的代码(或a PayPal bug)中的某些内容将 tx 小写,您将收到错误 4002。
    • @medmunds - 哇!您关于 tx 字段值区分大小写的评论让我免于浪费一周甚至更多的时间来尝试修复此错误!逆天! TX!!!!!!
    猜你喜欢
    • 2023-03-11
    • 2013-07-18
    • 2017-08-06
    • 2018-05-31
    • 1970-01-01
    • 2019-02-19
    • 1970-01-01
    • 2014-10-26
    相关资源
    最近更新 更多