Skip to content

Commit

Permalink
Filter characters from basket not allowed by SagePay
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Coates committed Jan 28, 2016
1 parent 93e72bb commit d1341fc
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,46 @@ protected function createResponse($data)
return $this->response = new Response($this, $data);
}

/**
* Filters out any characters that SagePay does not support from the item name.
*
* Believe it or not, SagePay actually have separate rules for allowed characters
* for item names and discount names, hence the need for two separate methods.
*
* @param string $name
*
* @return string
*/
protected function filterItemName($name)
{
$standardChars = "0-9a-zA-Z";
$allowedSpecialChars = " +'/\\&:,.-{}";
$pattern = '`[^'.$standardChars.preg_quote($allowedSpecialChars, '/').']`';
$name = trim(preg_replace($pattern, '', $name));

return $name;
}

/**
* Filters out any characters that SagePay does not support from the discount name.
*
* Believe it or not, SagePay actually have separate rules for allowed characters
* for item names and discount names, hence the need for two separate methods.
*
* @param string $name
*
* @return string
*/
protected function filterDiscountName($name)
{
$standardChars = "0-9a-zA-Z";
$allowedSpecialChars = " +'/\\:,.-{};_@()^\"~[]$=!#?|";
$pattern = '`[^'.$standardChars.preg_quote($allowedSpecialChars, '/').']`';
$name = trim(preg_replace($pattern, '', $name));

return $name;
}

/**
* Get an XML representation of the current cart items
*
Expand All @@ -168,7 +208,7 @@ protected function getItemData()
} else {
$total = ($basketItem->getQuantity() * $basketItem->getPrice());
$item = $xml->addChild('item');
$item->description = $basketItem->getName();
$item->description = $this->filterItemName($basketItem->getName());
$item->addChild('quantity', $basketItem->getQuantity());
$item->addChild('unitNetAmount', $basketItem->getPrice());
$item->addChild('unitTaxAmount', '0.00');
Expand All @@ -182,7 +222,7 @@ protected function getItemData()
if ($discountItem->getPrice() < 0) {
$discount = $discounts->addChild('discount');
$discount->addChild('fixed', ($discountItem->getPrice() * $discountItem->getQuantity()) * -1);
$discount->description = $discountItem->getName();
$discount->description = $this->filterDiscountName($discountItem->getName());
}
}
}
Expand Down

0 comments on commit d1341fc

Please sign in to comment.