电子商务购物车&贝宝


Ecommerce Cart & Paypal

我面临的问题是如何在一个数组中添加多个项目发送数据到paypal我使用以下脚本的购物车和paypal集成。当我在我的购物车中添加一个项目,它的工作很好,但当我尝试添加多个项目,它会在购物车中添加项目,但它只会发送最后一个项目贝宝的交易。下面是我试图解释的一个实例http://anushn.hostingsiteforfree.com/ecommerce/index.php。
任何形式的帮助将不胜感激。

<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;
    // If the 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("item_id" => $pid, "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 == "item_id" && $value == $pid) {
                      // That item is in cart already so let's adjust its quantity using array_splice()
                      array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "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("item_id" => $pid, "quantity" => 1));
           }
           $total_quantity = count($_SESSION["cart_array"]);
    }
    header("location: cart.php"); 
    exit();
}
?>
<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//       Section 2 (If user wants to remove an item from cart)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(isset($_POST['item_to_remove']) && ($_POST['item_to_remove'] != ""))
{
    $key_to_remove = $_POST['item_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 3 (render the cart for the user to view)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cart_output="";
$cart_total = "";
$pp_checkout_btn = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
    $cart_output = "<h2 align='center'>Your Shopping Cart Is Empty</h2>";
}
else
{
    // Start the For Each loop
    $i=0;
    foreach($_SESSION["cart_array"] as $each_item)
    {
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while($row = mysql_fetch_array($sql))
        {
            $product_name = $row['product_name'];
            $price = $row['price'];
            $detail = $row['details'];
            $cat = $row['category'];
            $sub_cat = $row['subcategory'];
            $cover = $row['cover_location'];
        }
        $total_item_price = $price * $each_item['quantity'];
        $total_item_price2 = number_format($total_item_price, 2);
        $cart_total = $total_item_price + $cart_total;

        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        $cart_output .= "<div class='cart_output'>";
        $cart_output .= "<h2>Cart Item $i</h2>";
        $cart_output .= "<img src=$cover width='100' height='100'><br/><br/>";
        $cart_output .= "Item Name : " . $product_name . "<br/>";
        $cart_output .= "Unit Price : $" . $price . "<br/>";
        $cart_output .= "Total : $" . $total_item_price2 . "<br/>";
        $cart_output .= "<br /><form action='cart.php' method='POST' name='' id =''>
        <input type='submit' name='deleteBtn"."$item_id' id='' value='Remove Item'>
        <input type='hidden' name='item_to_remove' id='' value='$i'>
        </form>
        ";
        $cart_output .= "</div>";
        $i++;
    }
    $cart_total = number_format($cart_total,2);
    //Start PayPal Checkout Button
    $POST_DATA = array(
    "METHOD"     => "BMCreateButton",
    "VERSION"    => "65.2",
    "PWD"        => "090078601",
    "USER"        => "mystore.yahoo.com",
    "SIGNATURE"    => "adadaadadada",
    "BUTTONCODE"=> "ENCRYPTED",
    "BUTTONTYPE"=> "BUYNOW",
    "BUTTONSUBTYPE" => "SERVICES",
    "BUTTONCOUNTRY" => "US",
    "BUYNOWTEXT" => "PAYNOW",
    "BUTTONIMAGE" => "REG",
    "BUTTONIMAGEURL" => "https://www.paypalobjects.com/webstatic/en_US/btn/btn_pponly_142x27.png",
    "L_BUTTONVAR0" => "bussiness=mustafazahid43@yahoo.com",
    "L_BUTTONVAR1"  => "item_name=$product_name",
    "L_BUTTONVAR2"  => "amount=$price",
    "L_BUTTONVAR3"    => "custom=$product_id_array",
    "L_BUTTONVAR4" => "notify_url=http://yoursite.com/ecommerce/storescripts/my_ipn.php",
    "L_BUTTONVAR5" => "cancel_return=http://yoursite.com/ecommerce/paypal_cancel.php",
    "L_BUTTONVAR6" => "return=http://yoursite.com/ecommerce/checkout_complete.php",
    "L_BUTTONVAR7"  => "quantity=$each_item[quantity]",
    "L_BUTTONVAR8"  => "upload=0",
    );

  $context = stream_context_create(array(
    'http' => array(
      'method'  => 'POST',
      'header'  => "Content-type: application/x-www-form-urlencoded'r'n",
      'content' => http_build_query($POST_DATA),
      'timeout' => 10,
    ),
  ));
$response = file_get_contents("https://api-3t.sandbox.paypal.com/nvp/",true,$context);
parse_str($response, $output);
$button = str_replace("''","",$output["WEBSITECODE"]);
} 
?>
<html>
<body>
<div class="container">
  <div class="sub_container">
   <?php echo $cart_output; ?>  
   <br/>
   <br>
<?php echo $button;?>
 <div style="margin-top:20px; color:#CCC; float:right;"><b><?php if($cart_total != "") {echo "Total Shopping : $".$cart_total." USD";} ?></b></div>
  </div>
</div>
</body>
</html>

BMCreateButton用于单个项。如果你想使用PayPal的购物车,你需要创建购物车按钮而不是Buy Now按钮,并且每个项目都有一个单独的按钮。然后你还会有一个单独的查看购物车按钮,但这个购物车将全部由PayPal驱动。

这真的不是我推荐的。既然你已经有了自己的购物车,你所需要做的就是把数据发送到PayPal。您可以使用"购物车上传命令"方法很容易地做到这一点,这正是它的目的。

您将完全按照这里所做的做,但不是构建NVP字符串并进行API调用,而是简单地构建一个带有隐藏输入变量的HTML表单。您可以参考PayPal的标准变量参考,了解您可以包含的详细信息。

使用Express Checkout API会更好,因为您熟悉API调用。由于您正在使用PHP,我建议您查看我的PayPal类库。