php 交易后从PayPal获取信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2434937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get information from PayPal after a transaction
提问by jmasterx
I want to create a simple transaction on my Web Site where after the person's transaction completes, I want paypal to redirect the user to go to a place on my site and I want PayPal to provide me with details so I can use PHP to parse it and email them the link to their purchase. I'm not sure what notify_url does? Thanks
我想在我的网站上创建一个简单的交易,在该人的交易完成后,我希望 paypal 将用户重定向到我网站上的某个位置,我希望 PayPal 向我提供详细信息,以便我可以使用 PHP 对其进行解析并将购买链接通过电子邮件发送给他们。我不确定 notify_url 有什么作用?谢谢
回答by Jason Williams
PayPal works like this:
贝宝的工作方式是这样的:
You have a form with a "buy" button. When that is clicked, it sends information (product, price, your account name, etc) to PayPal.
您有一个带有“购买”按钮的表单。单击该按钮后,它会将信息(产品、价格、您的帐户名称等)发送到 PayPal。
The buyer then agrees to pay you and when the transaction is completed, PayPal send an "IPN" (instant payment notification) to your notify URL - it sends POST data to that URL for your server to process. You reply to PayPal to ask if they sent the POST data (rather than an imposter) and if they then answer that it is a real transaction, you can release the product to the customer. Note that this all happens in the background while your buyer is still "at" the PayPal website.
然后买家同意向您付款,交易完成后,PayPal 会向您的通知 URL 发送一个“IPN”(即时付款通知)——它将 POST 数据发送到该 URL 以供您的服务器处理。您回复 PayPal 询问他们是否发送了 POST 数据(而不是冒名顶替者),如果他们回答这是真实交易,您就可以将产品发布给客户。请注意,这一切都在后台发生,而您的买家仍在“访问”PayPal 网站。
There is a final optional stage, which is that PayPal returns the buyer to your website. In this case, they send the buyer back to your "return" url, and they can (optionally) pass back the transaction data again, (they call this PDT). And you can again check with Paypal if this is a valid transaction and provide a download etc at that point.
还有最后一个可选阶段,即 PayPal 将买家返回到您的网站。在这种情况下,他们将买家发送回您的“退货”网址,并且他们可以(可选)再次传回交易数据(他们称之为 PDT)。您可以再次与 Paypal 核实这是否是有效交易,并在那时提供下载等。
The most difficult bit that nobody explains is that the buyerdoesn't get redirected to your notify URL. i.e. the "visitor" to your website's notify URL is PayPal, not the buyer, so this doesn't happen as part of your buyer's session. If you wish to persist a session across the three parts of this process, then you need to create a means of tracking the buyer in your form, and pass that to PayPal in a field of the form called "custom". This data is passed back to you in the IPN and PDT data, and you can use this to re-establish a connection with the original user session.
没有人解释的最困难的一点是买家不会被重定向到您的通知 URL。即,您网站通知 URL 的“访问者”是 PayPal,而不是买家,因此这不会作为买家会话的一部分发生。如果您希望在此过程的三个部分中保持会话,那么您需要在表单中创建一种跟踪买家的方法,并将其传递给 PayPal 在名为“自定义”的表单字段中。这些数据在 IPN 和 PDT 数据中传回给您,您可以使用它重新建立与原始用户会话的连接。
You really need to implement both IPN and PDT - if the IPN email fails then you have PDT as a backup. And if the user closes their web browser before they are redirected back to your PDT page, then you have sent an IPN email as a backup.
您确实需要同时实施 IPN 和 PDT - 如果 IPN 电子邮件失败,那么您可以使用 PDT 作为备份。如果用户在重定向回您的 PDT 页面之前关闭了他们的 Web 浏览器,那么您已经发送了一封 IPN 电子邮件作为备份。
Search on IPN and PDT and you'll find quite a lot of information. PayPalalso have full documentation and example scripts.
在 IPN 和 PDT 上搜索,您会发现相当多的信息。PayPal也有完整的文档和示例脚本。
回答by dusoft
Notify URL should lead to the script that saves the returned data from PayPal, such as:
Notify URL 应该指向保存从 PayPal 返回的数据的脚本,例如:
/** Fetch order from PayPal (IPN reply)
* @return int received ID of inserted row if received correctly, 0 otherwise
*/
function FetchOrder()
{
$transactionID=$_POST["txn_id"];
$item=$_POST["item_name"];
$amount=$_POST["mc_gross"];
$currency=$_POST["mc_currency"];
$datefields=explode(" ",$_POST["payment_date"]);
$time=$datefields[0];
$date=str_replace(",","",$datefields[2])." ".$datefields[1]." ".$datefields[3];
$timestamp=strtotime($date." ".$time);
$status=$_POST["payment_status"];
$firstname=$_POST["first_name"];
$lastname=$_POST["last_name"];
$email=$_POST["payer_email"];
$custom=$_POST["option_selection1"];
if ($transactionID AND $amount)
{
// query to save data
return $this->insertID;
}
else
{
return 0;
}
}
You can also choose to verify an order later on:
您还可以选择稍后验证订单:
/** Verify PayPal order (IPN)
* PayPal returns VERIFIED or INVALID on request
* @return bool verified 1 if verified, 0 if invalid
*/
function VerifyOrder()
{
$_POST["cmd"]="_notify-validate";
$ch=curl_init();
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERAGENT,"your agent - replace");
curl_setopt($ch,CURLOPT_URL,"https://www.paypal.com/cgi-bin/webscr");
curl_setopt($ch,CURLOPT_POST, 1);
foreach ($_POST as $key=>$value)
{
$string.="&".$key."=".urlencode(stripslashes($value));
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
$result=curl_exec($ch);
if ($result=="VERIFIED") return 1;
else return 0;
}
回答by krishna singh
$tx=$_REQUEST['tx'];
$paypal_url='https://www.paypal.com/cgi-bin/webscr?cmd=_notify-synch&tx='.$tx.'&at=token here';
$curl = curl_init($paypal_url);
$data = array(
"cmd" => "_notify-synch",
"tx" => $tx,
"at" => "token here"
);
$data_string = json_encode($data);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 1);
$headers = array (
'Content-Type: application/x-www-form-urlencoded',
'Host: www.paypal.com',
'Connection: close'
);
curl_setopt ($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$lines = explode("\n", $response);
$keyarray = array();
if (strcmp ($lines[0], "SUCCESS") == 0) {
for ($i=1; $i<count($lines);$i++){
list($key,$val) = explode("=", $lines[$i]);
$keyarray[urldecode($key)] = urldecode($val);
}
$first_name=$keyarray['first_name'];
$last_name=$keyarray['last_name'];
$payment_status=$keyarray['payment_status'];
$business=$keyarray['business'];
$payer_email=$keyarray['payer_email'];
$payment_gross=$keyarray['payment_gross'];
$mc_currency=$keyarray['mc_currency'];
}
回答by EthanK
When parsing the PDT response I'm using parse_str. Since the body of the response is url encoded it's just a matter of replacing the line breaks with ampersands- like this
解析 PDT 响应时,我使用parse_str。由于响应的正文是 url 编码的,因此只需用符号替换换行符即可 - 就像这样
$result = curl_exec($ch);
//replace the breaks with '&'
$r_string = str_replace("\n", "&", $result);
//parse the response into a key->value array
parse_str($r_string, $this->details);
if(!isset($this->details['SUCCESS'])){
//the "SUCCESS" or "FAIL" response is the first key
return FALSE;
}
else{
//the values of the response are now in an array
return TRUE;
}
depending on the application you can even leave out the second parameter ($this->details) and the values are set as global variables.
根据应用程序,您甚至可以省略第二个参数($this->details),并将值设置为全局变量。

