Overview

Namespaces

  • Apptus
    • ESales
      • Connector
        • Report
        • Time
    • Util
      • Cache
  • PHP
  • Overview
  • Namespace
  • Class
  • Tree
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 
<?php
namespace Apptus\ESales\Connector;

use Apptus\Util\String\ListCodec;
use Apptus\Util\String\MapCodec;

/**
 * A sale order from a customer session. Use Session.notifyPayment to notify eSales
 * that an order has been paid by a customer in a session.
 */
class Order {
    private $lines = array ();

    /**
     * Serialize this order into a string for transfer to another process.
     *
     * @internal
     * @return string The serialized order as a string.
     */
    public function serialize() {
        $mapCodec = new MapCodec('|');
        $lineCodec = new ListCodec(',');
        $lineStrings = array ();

        foreach ($this->lines as $line) {
            $annot = $mapCodec->encodeMap($line->annotations);
            $lineStrings[] = $lineCodec->encodeList(array ($line->key, $line->keyType, $annot));
        }
        return (new ListCodec(';'))->encodeList($lineStrings);
    }

    /**
     * Adds an order line for the specified product and return the new order line.
     *
     * @param string
     *              The key of the purchased product.
     * @throws \InvalidArgumentException if productKey is invalid.
     * @return OrderLine The added order line.
     */
    public function addProduct($productKey) {
        if (!AttributeValidation::forProducts()->validItemKey($productKey)) {
            throw new \InvalidArgumentException('Invalid product key: ' . $productKey);
        }
        $orderLine = new OrderLine($productKey, 'PRODUCT');
        $this->lines[] = $orderLine;
        return $orderLine;
    }

    /**
     * Adds an order line for the specified variant and return the new order line.
     * If the product key is omitted, the variant will be coupled to a product by
     * looking it up in the product catalog currently loaded in eSales.
     *
     * @param string|null
     *              The key of the product to which the variant belongs.
     * @param string
     *              The key of the purchased variant.
     * @throws \InvalidArgumentException if the arguments are not valid keys.
     * @return OrderLine The added order line.
     */
    public function addVariant() {
        $nargs = func_num_args();
        if ($nargs === 1) {
            $variantKey = func_get_arg(0);
            if (!AttributeValidation::forVariants()->validItemKey($variantKey)) {
                throw new \InvalidArgumentException('Invalid variant key: ' . $variantKey);
            }
            $orderLine = new OrderLine($variantKey, 'VARIANT');
            $this->lines[] = $orderLine;
            return $orderLine;
        } elseif ($nargs === 2) {
            $productKey = func_get_arg(0);
            $variantKey = func_get_arg(1);
            if (!AttributeValidation::forProducts()->validItemKey($productKey)) {
                throw new \InvalidArgumentException('Invalid product key: ' . $productKey);
            }
            if (!AttributeValidation::forVariants()->validItemKey($variantKey)) {
                throw new \InvalidArgumentException('Invalid variant key: ' . $variantKey);
            }
            $orderLine = new OrderLine($productKey . ',' . $variantKey, 'VARIANT_WITH_PRODUCT');
            $this->lines[] = $orderLine;
            return $orderLine;
        } else {
            throw new \InvalidArgumentException('Invalid number of arguments. Expected a variant key or a product key and a variant key.');
        }
    }

    /**
     * Adds an order line for the variant or product specified by a ticket.
     *
     * @param string
     *              The ticket as retrieved from an earlier panel request.
     * @return OrderLine
     *              The added order line.
     */
    public function addTicket($ticket) {
        $orderLine = new OrderLine($ticket, 'TICKET');
        $this->lines[] = $orderLine;
        return $orderLine;
    }
}
Apptus ESales Connector PHP API documentation generated by ApiGen