我正在整合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 = "rn";
$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 = "rn<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';
}
}
?>
一切都工作正常,但我得到失败响应。如果我忘了什么,请帮助我。
Thanks in advance
如果您正在使用沙箱进行测试,请确保您正在使用测试沙箱卖家帐户中的PDT令牌。
reason FOR FAIL, 4002, or, 4003 RESPONSE
-
确保你回"tx","cmd",和Auth值对于PayPal返回成功和交易细节,您的PDT脚本需要从返回字符串中提取"tx"变量,使用cmd"_notify-sync"从您的帐户发送验证令牌到PayPal。
-
无效的认证令牌检查令牌以确保它是准确的,并且被正确地传递回来。确保您的标记是一个连续的字符串,并且在脚本中没有换行符。
-
确保你没有发送回错误的URL。如果在Sandbox中进行测试,则需要确保将脚本发送回www.sandbox.paypal.com。如果您在活动站点上,则脚本应该发送回www.paypal.com。你将收到失败,如果你是在沙盒测试和你的脚本张贴回现场网站(反之亦然)
-
验证相同的"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账户进行支付时,通过选择返回按钮或不选择任何东西的返回方式激活客户。这意味着买方可以更改退货流程或退出浏览器,而不会传输任何数据。