Cart Class

The Cart class represents a purchase cart used in myPOS IPC transactions. It allows you to manage items being purchased, calculate the total amount, and validate the contents before sending the data to the payment gateway.

Class Details

  • Purpose: Manage a list of cart items for a purchase request
  • Namespace: Mypos\IPC
  • Location: Cart.php

Method Summary

MethodReturn TypeDescription
add(string $itemName, int $quantity, float $price)Mypos\IPC\CartAdds a new item to the cart with name, quantity, and price.
getTotal()floatReturns the total value of all items in the cart.
getItemsCount()intReturns the total number of items in the cart.
validate()boolValidates the cart (e.g., checks for missing fields or zero quantities).
getCart()arrayReturns the full list of cart items as an array.

Example Usage

<?php
namespace Mypos\IPC;
/**
 * Purchase cart object
 */
class Cart
{
    /**
     * Array containing cart items
     *
     * @var array
     */
    private $cart;
    /**
     *
     * @param string $itemName Item name
     * @param int $quantity Items quantity
     * @param float $price Single item price
     *
     * @return Cart
     * @throws IPC_Exception
     */
    public function add($itemName, $quantity, $price)
    {
        if (empty($itemName)) {
            throw new IPC_Exception('Invalid cart item name');
        }
        if (empty($quantity) || !Helper::isValidCartQuantity($quantity)) {
            throw new IPC_Exception('Invalid cart item quantity');
        }
        if (empty($price) || !Helper::isValidAmount($price)) {
            throw new IPC_Exception('Invalid cart item price');
        }
        $this->cart[] = [
            'name' => $itemName,
            'quantity' => $quantity,
            'price' => $price,
        ];
        return $this;
    }
    /**
     * Returns cart total amount
     *
     * @return float
     */
    public function getTotal()
    {
        $sum = 0;
        if (!empty($this->cart)) {
            foreach ($this->cart as $v) {
                $sum += $v['quantity'] * $v['price'];
            }
        }
        return $sum;
    }
    /**
     * Returns count of items in cart
     *
     * @return int
     */
    public function getItemsCount()
    {
        return (is_array($this->cart) && !empty($this->cart)) ? count($this->cart) : 0;
    }
    /**
     * Validate cart items
     *
     * @return boolean
     * @throws IPC_Exception
     */
    public function validate()
    {
        if (!$this->getCart() || count($this->getCart()) == 0) {
            throw new IPC_Exception('Missing cart items');
        }
        return true;
    }
    /**
     * Return cart array
     *
     * @return array
     */
    public function getCart()
    {
        return $this->cart;
    }
}

Typical Use Case

The Cart object is typically passed to payment methods such as Purchase , allowing for:

  • More detailed transaction information
  • Customer-visible itemization
  • Easier invoice and confirmation generation