Skip to content

Commit cf4795f

Browse files
author
Andy Coates
committed
Make item names no longer than 100 characters
1 parent 21e6a64 commit cf4795f

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Message/AbstractRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function filterItemName($name)
159159
$standardChars = "0-9a-zA-Z";
160160
$allowedSpecialChars = " +'/\\&:,.-{}";
161161
$pattern = '`[^'.$standardChars.preg_quote($allowedSpecialChars, '/').']`';
162-
$name = trim(preg_replace($pattern, '', $name));
162+
$name = trim(substr(preg_replace($pattern, '', $name), 0, 100));
163163

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

184184
return $name;
185185
}

tests/Message/DirectAuthorizeRequestTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public function testMixedBasketWithSpecialChars()
212212
{
213213
$items = new \Omnipay\Common\ItemBag(array(
214214
new \Omnipay\Common\Item(array(
215-
'name' => "Denisé's Odd & Wierd £name? #",
215+
'name' => "Denisé's Odd & Wierd £name? #12345678901234567890123456789012345678901234567890123456789012345678901234567890",
216216
'description' => 'Description',
217217
'quantity' => 2,
218218
'price' => 4.23,
@@ -224,20 +224,22 @@ public function testMixedBasketWithSpecialChars()
224224
'price' => -0.10,
225225
),
226226
array(
227-
'name' => 'Second Discount',
227+
// 101 character name
228+
'name' => '12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901',
228229
'description' => 'My 2nd Offer',
229230
'quantity' => 1,
230231
'price' => -1.60,
231232
)
232233
));
233234

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

243245
$this->request->setItems($items);

0 commit comments

Comments
 (0)