我正在将贝宝整合到我的购物车这是我的价值转移表格
<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';
}
}
?>
一切正常,但我收到失败响应。如果我忘记了什么,请帮助我。
提前致谢