Skip to content

Commit

Permalink
Issue #27 Add basket discount support (not yet working).
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Oct 10, 2015
1 parent 0d9b0fc commit dda37c0
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/Academe/SagePay/Model/BasketAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ abstract class BasketAbstract extends XmlAbstract

protected $hotel = null;

/**
* The list of discounts applied to the order..
*/

protected $discounts = array();

/**
* Set delivery details.
* All [currency] amounts will be formatted according to the currency.
Expand Down Expand Up @@ -126,6 +132,26 @@ public function setHotelField($name, $value)
return $this;
}

/**
* Add a discount.
* Multiple discounts can be added.
*/

public function addDiscount($fixed, $description = null)
{
// The fixed amount is mandatory.
$discount = array(
'fixed' => Helper::formatAmount($fixed, $this->currency),
);

// The description is optional.
if (isset($description)) {
$discount['description'] = $description;
}

$this->discounts[]['discount'] = $discount;
}

/**
* Add a simple product line, with values similar to the old (non-XML) basket.
* This adds a product line array to the lines array. The more complex lines with other details
Expand Down Expand Up @@ -165,11 +191,10 @@ public function addLine(ProductLineAbstract $line)
}

/**
* Return the basket as an XML string.
* There are no attributes in this XML, which makes things a little simpler.
* Convert the basket to an array.
*/

public function toXml()
public function toArray()
{
$structure = array();

Expand Down Expand Up @@ -203,7 +228,21 @@ public function toXml()
$structure = array_merge($structure, array('hotel' => $this->hotel));
}

return $this->xmlFragment(array('basket' => $structure));
if ( ! empty($this->discounts)) {
$structure = array_merge($structure, array('discounts' => $this->discounts));
}

return $structure;
}

/**
* Return the basket as an XML string.
* There are no attributes in this XML, which makes things a little simpler.
*/

public function toXml()
{
return $this->xmlFragment(array('basket' => $this->toArray()));
}
}

0 comments on commit dda37c0

Please sign in to comment.