Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电子商务购物车&;贝宝_Php_Paypal - Fatal编程技术网

Php 电子商务购物车&;贝宝

Php 电子商务购物车&;贝宝,php,paypal,Php,Paypal,我面临的问题是,如何在一个数组中添加多个项目以向paypal发送数据,我正在使用下面的脚本进行cart和paypal的集成。当我在购物车中添加一个项目时,它工作正常,但当我尝试添加多个项目时,它会在购物车中添加项目,但它只会将最后一个项目发送到paypal进行交易。下面是我试图解释的实例

我面临的问题是,如何在一个数组中添加多个项目以向paypal发送数据,我正在使用下面的脚本进行cart和paypal的集成。当我在购物车中添加一个项目时,它工作正常,但当我尝试添加多个项目时,它会在购物车中添加项目,但它只会将最后一个项目发送到paypal进行交易。下面是我试图解释的实例<任何形式的帮助都将不胜感激

<?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按钮,并且每个商品都有一个按钮。然后你也会有一个单独的查看购物车按钮,但购物车将全部由贝宝驱动

这真的不是我推荐的。既然你已经有了自己的购物车,你所需要做的就是将数据发送到贝宝。使用“”方法可以很容易地做到这一点,这正是它的用途

您将完全按照此处所做的操作进行,但是您不需要构建NVP字符串和进行API调用,只需构建一个包含隐藏输入变量的HTML表单。有关可包括的内容的详细信息,请参阅


更好的方法是使用,因为您熟悉API调用。既然您使用的是PHP,我建议您看看我的。这将使快速签出非常快速,并且易于集成。

有没有办法保护html隐藏变量?您可以查看或。BMCreateButton是动态创建加密按钮的唯一方法?我不想在paypal网站上托管button是的,button API将是您的最佳选择。我想像woo commerce一样设置paypal集成。你能告诉我他们在贝宝上用什么方法吗?