1
votes

After following a tutorial located here paypal-payment-gateway-integration

I would now like to change this so I can pass individual items to PayPal rather then the just the total quantity and price.

So just to clarify from method 1 to method 2 How do I add PayPal Checkout to my custom shopping cart?

Method 1: Passing the aggregate cart amount to PayPal You can pass the total amount of your shopping cart into PayPal's Buy Now Button code as though it were a single-item purchase

Method 2: Passing individual items to PayPal

I have followed Passing-Individual-Items-to-PayPal and other links that I have found. However these don't have the answers I am looking for.

How can I convert the code below which is working using method 1 as described above to method 2. I have already changed the form to:-

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

But then didn't know how to pass the individual items and quantity for each. I'm assuming I need to add a loop in here somewhere.

Complete code below

process-order.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='[email protected]'> 

    <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"]);
            }
        }
    }
    ?>
1

1 Answers

0
votes

Cart upload is a very old web 1.0 thing. Still supported for legacy websites that have integrated it, but it's a bad experience that involves redirecting the payer away from your website. The best solution is to not use it at all.

Instead, integrate a modern in-context PayPal Checkout. Here is a simple demo pattern that is pure HTML/javascript (no server-side code): https://developer.paypal.com/demo/checkout/#/pattern/client

Multiple items go in the purchase_units array, documented at v2/orders. It can be hard to understand all the required breakdown parameters, which must add up or the checkout will error and not open -- so here is an example with two items:

"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",
        },
      ],
    }
  ]

There is also a server demo pattern version of the same front-end, for if you want to implement v2/order creation and capture APIs from your server. This offers some advantages, but obviously requires integrating those APIs on your server.