Bus*_*hid 4 php paypal payment-gateway
我正在将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>
Run Code Online (Sandbox Code Playgroud)
这是正常的.但付款后我从PayPal重定向我得到FAIL响应.但在我的发送框帐户中,它显示我COMPLETE.And向我显示CART.我从paypal获取响应的代码是: -
<?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';
}
}
?>
Run Code Online (Sandbox Code Playgroud)
每件事情都正常,但我得到了失败的回应.如果我忘了什么,请帮助我.
提前致谢
如果您正在使用沙箱进行测试,请确保使用测试沙盒卖家帐户中的PDT令牌.
失败,4002或4003响应的原因
确保您回发"tx","cmd"和Auth值对于PayPal返回SUCCESS和交易详情,您的PDT脚本需要发布从返回字符串中提取的"tx"变量,来自您帐户的Auth令牌使用cmd"_notify-synch"到PayPal.
无效的身份验证令牌检查令牌以确保准确并正在正确传回.确保您的令牌是一个连续的字符串,并且脚本中没有换行符.对于某些语言,这可能会有所不同.
确保您没有回到错误的URL.如果您在Sandbox中进行测试,则需要确保将脚本发回www.sandbox.paypal.com.如果您在实际网站上,该脚本应回发到www.paypal.com.如果您在Sandbox中进行测试并且您的脚本发回现场网站(反之亦然),您将收到FAIL
验证相同的"tx"超过5次检查用户行为.获得失败的另一种方法是,如果你引用相同的"tx"超过5次.示例:如果用户在看到详细信息后刷新PDT页面5次,则他们将在上次刷新时看到FAIL.这不是错误.这是出于安全目的而完成的,因此无法无限期地访问PDT创建的URL以检索特定于事务的数据.
无效或缺少"tx"值"tx"值是用于收集付款信息的交易ID.如果此项无效或不存在,您将收到失败回复,因为无法检索付款数据.
PDT未收到
与付款关联的电子邮件地址要使PDT正常工作,您必须在按钮代码中确认用作业务价值的电子邮件地址.如果未确认此电子邮件地址,则PDT将无法工作,因为您将看不到附加到返回URL的PDT查询字符串.
确保使用格式正确的返回URL.启用PDT时,您用作默认自动返回URL的URL必须是有效的主机名或IP地址,而不是内部Intranet URL,否则用户将不会自动返回到该URL.确保使用有效的URL作为您希望用户返回的URL,否则您将看不到预期的结果.
订阅样式付款按钮PDT不适用于订阅,因为如果您有免费试用订阅,您将看不到返回页面/ PDT过程中的数据.这是因为PDT转移支付数据,如果在注册时,如果没有支付,则表示没有支付数据要转移.
用户行为PDT是通过返回方法激活客户,方法是选择返回按钮,或者在客户登录其PayPal帐户进行支付时不选择任何内容.这意味着买方可以更改退货流程或退出浏览器,并且不会传输任何数据.