Skip to content

Commit

Permalink
Make item names no longer than 100 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Coates committed Jan 28, 2016
1 parent 21e6a64 commit cf4795f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function filterItemName($name)
$standardChars = "0-9a-zA-Z";
$allowedSpecialChars = " +'/\\&:,.-{}";
$pattern = '`[^'.$standardChars.preg_quote($allowedSpecialChars, '/').']`';
$name = trim(preg_replace($pattern, '', $name));
$name = trim(substr(preg_replace($pattern, '', $name), 0, 100));

return $name;
}
Expand All @@ -179,7 +179,7 @@ protected function filterDiscountName($name)
$standardChars = "0-9a-zA-Z";
$allowedSpecialChars = " +'/\\:,.-{};_@()^\"~[]$=!#?|";
$pattern = '`[^'.$standardChars.preg_quote($allowedSpecialChars, '/').']`';
$name = trim(preg_replace($pattern, '', $name));
$name = trim(substr(preg_replace($pattern, '', $name), 0, 100));

return $name;
}
Expand Down
10 changes: 6 additions & 4 deletions tests/Message/DirectAuthorizeRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function testMixedBasketWithSpecialChars()
{
$items = new \Omnipay\Common\ItemBag(array(
new \Omnipay\Common\Item(array(
'name' => "Denisé's Odd & Wierd £name? #",
'name' => "Denisé's Odd & Wierd £name? #12345678901234567890123456789012345678901234567890123456789012345678901234567890",
'description' => 'Description',
'quantity' => 2,
'price' => 4.23,
Expand All @@ -224,20 +224,22 @@ public function testMixedBasketWithSpecialChars()
'price' => -0.10,
),
array(
'name' => 'Second Discount',
// 101 character name
'name' => '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901',
'description' => 'My 2nd Offer',
'quantity' => 1,
'price' => -1.60,
)
));

// Names/descriptions should be max 100 characters in length, once invalid characters have been removed.
$expected = '<basket><item>'
. '<description>Denis\'s Odd &amp; Wierd name</description><quantity>2</quantity>'
. '<description>Denis\'s Odd &amp; Wierd name 123456789012345678901234567890123456789012345678901234567890123456789012345</description><quantity>2</quantity>'
. '<unitNetAmount>4.23</unitNetAmount><unitTaxAmount>0.00</unitTaxAmount>'
. '<unitGrossAmount>4.23</unitGrossAmount><totalGrossAmount>8.46</totalGrossAmount>'
. '</item><discounts>'
. '<discount><fixed>0.2</fixed><description>Denis\'s "Odd" Wierd discount? #</description></discount>'
. '<discount><fixed>1.6</fixed><description>Second Discount</description></discount>'
. '<discount><fixed>1.6</fixed><description>1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</description></discount>'
. '</discounts></basket>';

$this->request->setItems($items);
Expand Down

0 comments on commit cf4795f

Please sign in to comment.