当用户第一次通过 Paypal 重定向时,如何获取传递的已发布数据?

Posted

技术标签:

【中文标题】当用户第一次通过 Paypal 重定向时,如何获取传递的已发布数据?【英文标题】:How can I get posted data passed along when the user is first redirected through Paypal? 【发布时间】:2012-11-01 07:27:55 【问题描述】:

我已经尝试解决这个问题一段时间了。我有一个订单页面,客户必须在其中输入姓名、地址等信息以及一些更独特的字段。我已经用 Paypal 设置了 IPN,客户被重定向到我指定的页面。但数据并没有随之而来。我希望我能正确地问我的问题,这样我就不会被“关闭”。这是重定向到他们页面的 Paypal 提交按钮中的 html

<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="NT2YC6LP7SWBE">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="submit" >
<input type="hidden" name="child_name" id="child_name" value ="<? echo $child_name; ?>" maxlength="20"/>
<input type="hidden" name="age" id="age" value ="<? echo $age; ?>" maxlength="4"/>
<input type="hidden" name="hometown" id="hometown" value ="<? echo $hometown; ?>" maxlength="32"/>
<input type="hidden" name="boy_girl" id="boy_girl" value ="<? echo $boy_girl; ?>" maxlength="4"/>
<input type="hidden" name="first_name" id="first_name" value ="<? echo $first_name; ?>" maxlength="32"/>
<input type="hidden" name="last_name" id="last_name" value ="<? echo $last_name; ?>" maxlength="32"/>
<input type="hidden" name="email" id="email" value ="<? echo $email; ?>" maxlength="64"/>
<input type="hidden" name="address1" id="address1" value ="<? echo $address1; ?>" maxlength="64"/>
<input type="hidden" name="address2" id="address2" value ="<? echo $address2; ?>" maxlength="32"/>
<input type="hidden" name="city" id="city" value ="<? echo $city; ?>" maxlength="32"/>
<input type="hidden" name="state" id="state" value ="<? echo $state; ?>" maxlength="20"/>
<input type="hidden" name="zip" id="zip" value ="<? echo $zip; ?>" maxlength="10"/>
<input type="hidden" name="country" id="country" value ="<? echo $country; ?>" maxlength="32"/>
<input type="hidden" name="payment_type" id="payment_type" value ="paypal" maxlength="6"/>
<input type="hidden" name="paid" id="paid" value ="yes" maxlength="3"/>
<input type="hidden" name="mailed" id="mailed" value ="no" maxlength="3"/>
<img  border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif"  >
</form>

有人向我指出,有些变量不是“Paypal 变量”,我可以稍后修复,但在访问 Paypal 后,我的数据都没有返回到我指定的页面,即使是 Paypal 的变量支持“城市”。这是我在他们被重定向到的页面上的 php

$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval) 
  $keyval = explode ('=', $keyval);
  if (count($keyval) == 2)
     $myPost[$keyval[0]] = urldecode($keyval[1]);

$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc')) 
   $get_magic_quotes_exists = true;
 
foreach ($myPost as $key => $value)         
   if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)  
        $value = urlencode(stripslashes($value)); 
    else 
        $value = urlencode($value);
   
   $req .= "&$key=$value";


$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
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);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));

if( !($res = curl_exec($ch)) ) 
    // error_log("Got " . curl_error($ch) . " when processing IPN data");
    curl_close($ch);
    exit;

curl_close($ch);

if (strcmp ($res, "VERIFIED") == 0) 
        $child_name = htmlentities($_POST['child_name']);
        $age = $_POST['age'];
        $hometown = $_POST['hometown'];
        $boy_girl = $_POST['boy_girl'];
        $email = $_POST['email'];
        $first_name = htmlentities($_POST['first_name']);
        $last_name = htmlentities($_POST['last_name']);
        $address1 = htmlentities($_POST['address1']);
        $address2 = htmlentities($_POST['address2']);
        $city = htmlentities($_POST['city']);
        $state = $_POST['state'];
        $zip = htmlentities($_POST['zip']);
        $country = htmlentities($_POST['country']);
        $payment_type = $_POST['payment_type'];
        $paid = $_POST['paid'];
        $mailed = $_POST['mailed'];
 else if (strcmp ($res, "INVALID") == 0) 
       

        $query = "INSERT INTO customer_list (
        number, child_name, age, hometown, boy_girl, first_name, last_name, email,
        address1, address2, city, state, zip, country, payment_type, paid, mailed)
        VALUES ('', 
        '".mysql_real_escape_string($child_name)."',
        '".mysql_real_escape_string($age)."',
        '".mysql_real_escape_string($hometown)."',
        '".mysql_real_escape_string($boy_girl)."',
        '".mysql_real_escape_string($first_name)."',
        '".mysql_real_escape_string($last_name)."',
        '".mysql_real_escape_string($email)."',
        '".mysql_real_escape_string($address1)."',
        '".mysql_real_escape_string($address2)."',
        '".mysql_real_escape_string($city)."',
        '".mysql_real_escape_string($state)."',
        '".mysql_real_escape_string($zip)."',
        '".mysql_real_escape_string($country)."',
        '".mysql_real_escape_string($payment_type)."',
        '".mysql_real_escape_string($paid)."',
        '".mysql_real_escape_string($mailed)."')";
            if ($query_run = mysql_query($query)) 

                $subject = "Thank You for Your Order";
                $message = "Thank you for placing your order with My-Letter-From-Santa-Claus.com. \n\nYou can expect to receive your personalized letter soon. Please make note of your customer number. Your customer number is: ".mysql_insert_id().". \n\nPlease send all inquiries to : info@my-letter-from-santa-claus.com";
                $from = "info@my-letter-from-santa-claus.com";
                $headers = "From:" . $from;
                mail($email, $subject, $message, $headers);
                echo 'Thank you for your order.';
                echo 'Letter For '.$child_name;
             else 
                echo mysql_error();
            

我直接从 x.com 复制了大部分内容,当然除了更改为我自己的变量。没有任何东西被发布到数据库中,但这不是问题,因为我什至无法回显数据。有几件事正在输入数据库,但不是来自我的订单页面的数据 - 它是在 Paypal 上作为信用卡信息输入的名字和姓氏,对于 payment_type,它显示“instan”(我最大化了) 6 个字符),但正如您从 HTML 中的隐藏输入字段中看到的那样,我想发布值“paypal”。我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

您是否使用 Paypal 沙盒进行测试?如果没有,我强烈推荐:https://developer.paypal.com/,手册:https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_Sandbox_UserGuide.pdf。

我在您的代码中没有看到字段 name="business"。这是 Paypal 用来识别您的卖家帐户的字段。但也许它只与“cmd”参数有关(我使用 _xclick)。

这是目前对我有用的一组字段,也许对你有帮助:

<form name="_xclick" action="https://www.paypal.com/de/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_xclick">
    <input type="hidden" name="business" value="seller_email_here">
    <input type="hidden" name="notify_url" value="ipn_url_here">
    <input type="hidden" name="return" value="redirect_url">
    <input type="hidden" name="cancel_return" value="redirect_if_cancelled_url">
    <input type="hidden" name="amount" value="number_of_items">
    <input type="hidden" name="currency_code" value="EUR">
    <input type="hidden" name="item_name" value="product_name">
    <input type="hidden" name="item_number" value="product_number">

【讨论】:

我现场测试过。我没有使用业务参数,但我怀疑这是我的问题的本质。【参考方案2】:

您的问题是您使用的是托管按钮。您不能在这些的通用 HTML 中包含自定义内容。唯一可以包含的自定义字段必须在 PayPal 按钮创建向导中完成。

您还需要确保没有混淆 IPN 和 PDT。如果您只是希望数据返回到用户在完成付款后发送到的返回 URL,这将是 PDT。不过,不建议您使用它来处理订单后处理,因为无法保证用户会到达那里。

IPN 可以用于此,因为无论如何它都会被触发,但同样,这与您的结帐和 PDT 完全分开。

我强烈建议您使用 Express Checkout 而不是标准按钮,因为您似乎也熟悉 PHP。我有一个PHP class library for PayPal,这对您来说非常简单,如果您需要,我可以提供 30 分钟的免费培训,这通常足以让人们开始并运行,只要您了解 PHP以及如何处理数组数据。

您也可以使用 Standard 来做到这一点,但您必须不再使用 @AndreiHardau 显示的托管按钮。这将允许您包含 additional fields,例如 address1、address2、address_overide 等,然后这些字段将在 IPN、PDT 和 GetTransactionDetails 中返回。

【讨论】:

我下载了你的图书馆。哪些 PHP 文件适合我正在尝试做的事情? @CTViking 如果您想使用 Express Checkout,您最终会使用 SetExpressCheckout、GetExpressCheckoutDetails(可选)和 DoExpressCheckoutPayment。如果您想安排一次 30 分钟的免费培训课程,请告诉我,我可以指导您完成这些步骤。 哇,能培训我 30 分钟真是太好了。希望我们可以通过一些简短的交流来解决这个问题,而我不会占用您 30 分钟的时间。除了它需要的 2 个文件之外,我还通读了 SetExpressCheckout。它非常广泛,有几个与我需要的无关的细节。如果您可以花点时间,请转到我网站的订单屏幕my-letter-from-santa-claus.com/order.php 并查看我的字段。当它们被填写并且用户点击提交时,他们被定向到一个页面,该页面提供了通过邮件支付的选项............ ...... 或通过 Paypal。如果他们通过邮件付款。然后将它们定向到可打印页面,所有数据都保存到 SQL 数据库中。但是,如果他们选择 Paypal,他们会转到 Paypal 页面,付款,然后被重定向到我指定的页面。但他们输入的数据并没有随之而来。 @CTViking 是的,里面有很多你不需要的东西,但你只需将这些参数留空并填写你需要的东西。我认为从长远来看你会更喜欢它,因为它的限制要少得多。不过,我更新了答案,提供了有关使用标准执行此操作的更多详细信息。

以上是关于当用户第一次通过 Paypal 重定向时,如何获取传递的已发布数据?的主要内容,如果未能解决你的问题,请参考以下文章

Django在外面付款时保持登录丢失

以编程方式将用户重定向到 Paypal 站点以及来自代码隐藏的数据

如何在django oscar中设置Paypal重定向网址?

将数据提交到 db 后将用户重定向到 PayPal

paypal如何避免PDT数据的双重回复

当应用程序在本地运行时如何修复 PHP 上的重定向错误