【问题标题】:Changing PHP custom shopping cart from _xclick to _cart将 PHP 自定义购物车从 _xclick 更改为 _cart
【发布时间】:2020-07-07 02:02:02
【问题描述】:

按照这里的教程paypal-payment-gateway-integration

我现在想更改此设置,以便我可以将单个项目传递给 PayPal,而不仅仅是总数量和价格。

所以只是为了澄清从方法1到方法2How do I add PayPal Checkout to my custom shopping cart?

方法 1: 将购物车总金额传递给 PayPal 您可以将购物车的总金额传递给 PayPal 的“立即购买”按钮代码,就好像它是单项购买一样

方法 2: 将单个项目传递给 PayPal

我已关注Passing-Individual-Items-to-PayPal 和我找到的其他链接。然而,这些没有我正在寻找的答案。

如何将下面使用上述方法 1 的代码转换为方法 2。 我已经将表格更改为:-

<input type="hidden" name="upload" value="1">
<input type="hidden" name="cmd" value="_cart">

但后来不知道如何传递各个项目和每个项目的数量。我假设我需要在这里的某个地方添加一个循环。

下面的完整代码

处理顺序.php

    <?php
    // Initialize the session
    session_start();
    // Check if the user is already logged in, if yes then redirect him to welcome page
    if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    }
    else {
        header("location: login.php");
        exit;
    }

    require_once "ShoppingCart.php";

    $member_id = 2; // integrate authentication module here to get logged in member

    $shoppingCart = new ShoppingCart();

    /* Calculate Cart Total Items */
    $cartItem = $shoppingCart->getMemberCartItem($member_id);
    $item_quantity = 0;
    $item_price = 0;
    if (! empty($cartItem)) {
        if (! empty($cartItem)) {
            foreach ($cartItem as $item) {
                $item_quantity = $item_quantity + $item["quantity"];
                $item_price = $item_price + ($item["price"] * $item["quantity"]);
            }
        }
    }

    if(!empty($_POST["proceed_payment"])) {
        $name = $_POST ['name'];
        $address = $_POST ['address'];
        $city = $_POST ['city'];
        $zip = $_POST ['zip'];
        $country = $_POST ['country'];
    }
    $order = 0;
    if (! empty ($name) && ! empty ($address) && ! empty ($city) && ! empty ($zip) && ! empty ($country)) {
        // able to insert into database

        $order = $shoppingCart->insertOrder ( $_POST, $member_id, $item_price);
        if(!empty($order)) {
            if (! empty($cartItem)) {
                if (! empty($cartItem)) {
                    foreach ($cartItem as $item) {
                        $shoppingCart->insertOrderItem ( $order, $item["id"], $item["price"], $item["quantity"]);
                    }
                }
            }
        }
    }
    ?>


    <HTML>
    <HEAD>
    <div class="topnav" id="myTopnav">
      <a href="index.php">Home</a>
      <a href="about.php">About</a>
      <a href="contact.php">Contact</a>
      <a href="javascript:void(0);" class="icon" onClick="myFunction()">
      <i class="fa fa-bars"></i>  </a></div>
    <TITLE>Process Order</TITLE>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="style.css" type="text/css" rel="stylesheet" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    </HEAD>
    <BODY>
    <div id="shopping-cart">
            <div class="txt-heading">
                <div class="txt-heading-label"><strong>Shopping Cart</strong></div>

                <a id="btnEmpty" href="index.php?action=empty"><img
                    src="image/empty-cart.png" alt="empty-cart"
                    title="Empty Cart" class="float-right" /></a>
                <div class="cart-status">
                    <div>Total Quantity: <strong><?php echo $item_quantity; ?></strong></div>
                    <div>Total Price: &pound; <strong><?php echo $item_price; ?></strong></div>
                </div>
            </div>
            <?php
            if (! empty($cartItem)) {
                ?>
    <?php
                require_once ("cart-list.php");
                ?>
    <?php
            } // End if !empty $cartItem
            ?>

    </div>

    <?php if(!empty($order)) { ?>

    <form name="frm_customer_detail" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
    <input type='hidden' name='business' value='payment@test.com'> 

    <input type="hidden" name="rm" value="2" />
    <input type="hidden" name="charset" value="utf-8" />

        <input type='hidden' name='item_name_1' value='<?php echo $item;?>'> 
        <input type='hidden' name='item_number_1' value="<?php echo $order;?>"> 
        <input type='hidden' name='amount_1' value='<?php echo $item_price; ?>'> 
        <input type='hidden' name='currency_code' value='GBP'> 
        <input type='hidden' name='notify_url' value='https://test.com/notify.php'> 
        <input type='hidden' name='return' value='https://test.com/response.php'> 
        <input type="hidden" name="upload" value="1">
        <input type="hidden" name="cmd" value="_cart">

        <div>
            <p align="center">
              <input type="submit" class="btn-action"
                    name="continue_payment" value="Continue Payment">
               <button class="btn-action" onClick="goBack()">Go Back</button>
            </p>
          </div>
        </form>

    <?php } else { ?>

    <div class="success">Problem in placing the order. Please try again!</div>
    <div>
    <button class="btn-action">Back</button>
    </div>

    <?php } ?>

    </BODY>
    </HTML>

    <?php
    $cartItem = $shoppingCart->getMemberCartItem($member_id);
    $item_quantity = 0;
    $item_price = 0;
    if (! empty($cartItem)) {
        if (! empty($cartItem)) {
            foreach ($cartItem as $item) {
                $item_quantity = $item_quantity + $item["quantity"];
                $item_price = $item_price + ($item["price"] * $item["quantity"]);
            }
        }
    }
    ?>

【问题讨论】:

    标签: php paypal paypal-sandbox shopping-cart


    【解决方案1】:

    购物车上传是一个非常古老的 web 1.0 事物。仍然支持已集成它的旧网站,但这是一种糟糕的体验,涉及将付款人从您的网站重定向。最好的解决方案是根本不使用它。

    相反,集成一个现代的上下文 PayPal Checkout。这是一个纯 HTML/javascript(无服务器端代码)的简单演示模式:https://developer.paypal.com/demo/checkout/#/pattern/client

    多个项目进入purchase_units 数组,记录在at v2/orders。可能很难理解所有必需的细分参数,必须加起来,否则结帐会出错并且无法打开——所以这里有一个包含两个项目的示例:

    "purchase_units": [{
          "description": "Stuff",
          "amount": {
            "value": "20.00",
            "currency_code": "USD",
            "breakdown": {
              "item_total": {
                "currency_code": "USD",
                "value": "20.00"
              },
            }
          },
          "items": [
            {
              "unit_amount": {
                "currency_code": "USD",
                "value": "10.00"
              },
              "quantity": "1",
              "name": "Item 1",
            },
            {
              "unit_amount": {
                "currency_code": "USD",
                "value": "10.00"
              },
              "quantity": "1",
              "name": "Item 2",
            },
          ],
        }
      ]
    

    同样的前端还有一个服务器演示模式版本,如果您想实现 v2/订单创建并从您的服务器捕获 API。这提供了一些优势,但显然需要在您的服务器上集成这些 API。

    【讨论】:

    • 谢谢,我会检查一下,尽管我仍然想更改当前设置以使其正常工作,因为我已经花时间实现了这个!
    • 我真的不建议在购物车上传上花费更多时间。这是古老的。但这里是资源,如果你真的要这样做:developer.paypal.com/docs/paypal-payments-standard/…,以及项目的变量引用developer.paypal.com/docs/paypal-payments-standard/…
    • 好的,我会试试这条路线!所以我从您发布的链接中复制了代码,可以看到值为 0.01。然后我如何将我的变量从我的自定义购物车传递给这个?
    • 演示是一个简单的 0.01 付款,没有项目。对于传递订单项,请参阅我的答案中的purchase_units 数组示例
    • 只是为了澄清我将如何将局部变量传递给值?像这样的“价值”:“$item_name”,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2015-12-10
    • 1970-01-01
    • 2019-08-07
    • 2020-04-23
    相关资源
    最近更新 更多