简体   繁体   English

PHP购物车未转移到Paypal

[英]Php Cart is not transferring to Paypal

Hi I am attemting to create an online ecommerce shop I have everything working fine despite the paypal cart upload feature I have been following a series of videos from developphp.com Here is the code from my file cart.php 嗨,我打算创建一个在线电子商务商店,尽管使用了贝宝购物车上传功能,但我的所有工作都很好,我一直在关注来自developerphp.com的一系列视频,这是我的文件cart.php中的代码

<?php
session_start();//start session first thing in script
//script error Reporting
error_reporting(E_ALL);
ini_set('display_errors','1');
//connect to mysql
include './store_scripts/DB_Connection.php';
?>
<?php
///////////////////////////////////////////////////////////////////////////////////
//  Section 1 (if user attempts to add something to there cart from products)    //
///////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['p_code'])){
$p_code=$_POST['p_code'];
$wasFound = false;
$i=0;
//if cart session variable is not set or cart array is empty
if(!isset($_SESSION["Cart_Array"])||count($_SESSION["Cart_Array"])<1){
//run if the cart is empty or not set
$_SESSION["Cart_Array"]= array(0=>array("product_code" =>$p_code,"quantity" =>1));
}else{
//run if the cart has at least one item in it
foreach($_SESSION["Cart_Array"] as $each_item){
    $i++;
    while(list($key,$value) =each($each_item)){
        if($key =="product_code" && $value==$p_code){
            //That item is in the cart already so lets adjust its quantity using the array_splice()
            array_splice($_SESSION["Cart_Array"],$i-1,1,array(array("product_code"=>$p_code,"quantity"=>$each_item['quantity'] + 1)));
            $wasFound = true;
        }//close if condition
    }//close while loop
}//close foreach loop
if ($wasFound==false){
    array_push($_SESSION["Cart_Array"],array("product_code"=>$p_code,"quantity"=> 1));
}
}
header("location:cart.php");
exit();
}
?>
<?php
////////////////////////////////////////////////////////////
//      Section 2(if the user emptys there shopping cart) // 
////////////////////////////////////////////////////////////
if (isset($_GET['cmd'])&&$_GET['cmd']=="emptycart")
    unset($_SESSION["Cart_Array"]);
?>
<?php
/////////////////////////////////////////////////////////////////
//      Section 3(if the user chooses to adjust item quantity) // 
/////////////////////////////////////////////////////////////////
if (isset($_POST['item_to_adjust'])&&$_POST['item_to_adjust']!=""){
    //execute some code
    $item_to_adjust =$_POST['item_to_adjust'];
    $quantity =$_POST['quantity'];
    $quantity = preg_replace ('#[^0-9]#i','',$quantity);//filter everything but numbers
    $i=0;
    foreach($_SESSION["Cart_Array"] as $each_item){
        $i++;
        while(list($key,$value) =each($each_item)){
            if($key =="product_code" && $value==$item_to_adjust){
                //That item is in the cart already so lets adjust its quantity using the array_splice()
                array_splice($_SESSION["Cart_Array"],$i-1,1,array(array("product_code"=>$item_to_adjust,"quantity"=>$quantity)));
            }//close if condition
        }//close while loop
    }//close foreach loop
}

?>
<?php
////////////////////////////////////////////////////////////
//      Section 4(If user wants to remove an item)        //
////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove'])&& $_POST['index_to_remove']!=""){
    //access the array and run code to remove that array index

    $key_to_remove = $_POST['index_to_remove'];
    if(count($_SESSION["Cart_Array"])<=1){
        unset ($_SESSION["Cart_Array"]);
    }else{
        unset($_SESSION["Cart_Array"]["$key_to_remove"]);
        sort($_SESSION["Cart_Array"]); 
    }
}
?>
<?php
////////////////////////////////////////////////////////////
//      Section 5(render the cart for user to view)       //
////////////////////////////////////////////////////////////
$cartOutput="";
$cartTotal ='';
$pp_checkout_btn ='';
if(!isset($_SESSION["Cart_Array"])||count($_SESSION["Cart_Array"])<1){
$cartOutput ="<h2 align='center'>Your Shopping Cart is empty</h2>";
}else{
//start paypal checkout button
    $pp_checkout_btn.='<FORM ACTION="https://www.paypal.com/uk/cgi-bin/webscr" method="post"/>
                        <INPUT TYPE="hidden" name="cmd" value="_cart"/>
                        <INPUT TYPE="hidden" name="Upload" value="1">
                        <INPUT TYPE="hidden" name="Business" value="info@easterninspiration.co.uk"/>';
    //start the foreach loop
    $i =0;
    foreach($_SESSION["Cart_Array"] as $each_item){

    $Product_Name = $each_item['product_code'];
    $sql=mysql_query("SELECT * FROM products WHERE product_name = '$Product_Name' LIMIT 1");
    while($row = mysql_fetch_array($sql)){
        $item_price = $row["price"];
        $details = $row["details"];
    }
    $Total_price =$item_price*$each_item['quantity'];
    $cartTotal = $Total_price+$cartTotal;
    $cart_VAT = 20/100*$cartTotal;
    $postage = 5.00;
    $final_Total = $cartTotal+$cart_VAT+$postage; 
    //dynamic checkout btn Assembly
    $x =$i+1;
    $pp_checkout_btn .= '<INPUT TYPE="hidden" name="item_name_' . $x . '" value="'.$Product_Name.'"/> 
                         <INPUT TYPE="hidden" name="amount_ ' . $x . '" value="'.$item_price.'"/>
                         <INPUT TYPE="hidden" name="quantity_' . $x . '" value="'.$each_item['quantity'].'"/>';
    //Dynamic table row assembly
    $cartOutput .='<tr>';
    $cartOutput .= '<td><a href="product.php?p_name='.$Product_Name.'">'.$Product_Name .'</a><br/><img src="product_images/'.$Product_Name.'.jpg" alt ="'.$Product_Name.'" width="70" height="70" border ="1"/></td>';
    $cartOutput .='<td>'.$details.'</td>';
    $cartOutput .='<td>£'.number_format($item_price,2).'</td>';
    $cartOutput .='<td><form action ="cart.php" method ="post" >
    <input name="quantity" type="text" value="'.$each_item['quantity'].'" size ="1" maxlength="2"/>
    <input name="adjustBtn '.$Product_Name .'" type="Submit" style="width: auto; margin: 0 auto;" value="change '.$Product_Name.'">
    <input name="item_to_adjust" type ="hidden" value="'.$Product_Name .'"/>
    </form></td>';
    $cartOutput .='<td>£'.number_format($Total_price,2).'</td>';
    $cartOutput .='<td><form action ="cart.php" method ="post" ><input name="DeleteBtn '.$Product_Name .'" type="Submit" style="width: auto; margin: 0 auto;" value="Remove '.$Product_Name.'"><input name="index_to_remove" type ="hidden" value="'.$i.'"/></form></td>';
    $cartOutput .='</tr>';
    $i++;
    }
    $cartTotal = "<div style='color:red'; align='right'>Total excluding VAT: £".number_format($cartTotal,2)."</div>";   
    $cartTotal .="<div style='color:red'; align='right'>VAT: £".number_format($cart_VAT,2)."</div>";    
    $cartTotal .="<div style='color:red'; align='right'>Shipping: £".number_format($postage,2)."</div>";    
    $cartTotal .="<div style='color:red'; align='right'>Total: £".number_format($final_Total,2)."</div>";   
    //finish the paypal checkout button
    $pp_checkout_btn .='<INPUT TYPE="hidden" name="notify_url" value="http://www.yoursite.com/storescripts/my_ipn.php"/>
                        <INPUT TYPE="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php"/>
                        <INPUT TYPE="hidden" name="rm" value="2"/>
                        <INPUT TYPE="hidden" name="cbt" value="Return to The Store"/>
                        <INPUT TYPE="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php"/>
                        <INPUT TYPE="hidden" name="lc" value="GB"/>
                        <INPUT TYPE="hidden" name="currency_code" value="GBP"/>
                        <INPUT TYPE="hidden" name="tax_cart" value="'.number_format($cart_VAT,2).'"/> 
                        <INPUT TYPE="image" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110401-1/en_US/i/scr/pixel.gif" width="1" height="1">
                        </form>';
}
?>
<html>
<head>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<Title>Your Shopping Cart</title>
</head>
<body>
<div align="center"id ="mainWrapper">
<?php include_once("template_header.php");?>
<div id ="pageContent">
    <div style ="margin:24px;text-align:left;">

<table width="100%" border ="5" >
<tr>
    <td width="7%" bgcolor="#0045f3"><B>Product</B></td>
    <td width="47%" bgcolor="#0045f3"><B>Product Description</B></td>
    <td width="10%" bgcolor="#0045f3"><B>unit Price</B></td>
    <td width="9%" bgcolor="#0045f3"><B>Quantity</B></td>
    <td width="7%" bgcolor="#0045f3"><B>Total</B></td>
    <td width="9%" bgcolor="#0045f3"><B></B></td>
</tr>
<?php echo $cartOutput;?>

</table>

<br/>
<br/>
    <a href="cart.php?cmd=emptycart">Click here to Empty your shopping cart</a>
</div>
<?php echo $cartTotal;?>
<br/>
<br/>
<?php echo $pp_checkout_btn;?>
<br/>
<br/>
</div>
<?php include_once('template_footer.php');?>
</div>
</body>
</html>

The error I get in Paypal is the following: PayPal cannot process this transaction because of a problem with the seller's website. Please contact the seller directly to resolve this problem 我在Paypal中遇到的错误如下: PayPal cannot process this transaction because of a problem with the seller's website. Please contact the seller directly to resolve this problem PayPal cannot process this transaction because of a problem with the seller's website. Please contact the seller directly to resolve this problem

I really appreciate all the help I can get 我真的很感谢我能得到的所有帮助

Regards 问候

Kunal Verma 库纳尔·维尔玛(Kunal Verma)

The Upload and Business parameters should be in lower case -- eg: UploadBusiness参数应使用小写字母-例如:

$pp_checkout_btn.='<FORM ACTION="https://www.paypal.com/uk/cgi-bin/webscr" method="post"/>
                    <INPUT TYPE="hidden" name="cmd" value="_cart"/>
                    <INPUT TYPE="hidden" name="Upload" value="1">
                    <INPUT TYPE="hidden" name="Business" value="info@easterninspiration.co.uk"/>';

Should be: 应该:

$pp_checkout_btn.='<FORM ACTION="https://www.paypal.com/uk/cgi-bin/webscr" method="post"/>
                    <INPUT TYPE="hidden" name="cmd" value="_cart"/>
                    <INPUT TYPE="hidden" name="upload" value="1">
                    <INPUT TYPE="hidden" name="business" value="info@easterninspiration.co.uk"/>';

Thank you for your help I managed to solve the problem It was with the item name lines as it was sending item_name_'1' to paypal so I formatted the string to remove the comma . 谢谢您的帮助,我设法解决了该问题,原因是项目名称行将其发送给paypal的item_name_'1',因此我格式化了字符串以删除逗号。 and its formatting correctly in paypal now 并且它现在在贝宝中正确格式化

Thank you I appreciate your help 谢谢,谢谢您的帮助

Kunal 库纳尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM