Skip to content

Commit 48922c1

Browse files
committed
Merge branch 'line-items'
2 parents ebf4770 + 551584e commit 48922c1

File tree

9 files changed

+462
-6
lines changed

9 files changed

+462
-6
lines changed

src/Omnipay/Common/Item.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Omnipay\Common;
4+
5+
use Symfony\Component\HttpFoundation\ParameterBag;
6+
7+
/**
8+
* Cart Item interface
9+
*/
10+
class Item implements ItemInterface
11+
{
12+
/**
13+
* @var \Symfony\Component\HttpFoundation\ParameterBag
14+
*/
15+
protected $parameters;
16+
17+
/**
18+
* Create a new item with the specified parameters
19+
*
20+
* @param array|null $parameters An array of parameters to set on the new object
21+
*/
22+
public function __construct($parameters = null)
23+
{
24+
$this->initialize($parameters);
25+
}
26+
27+
/**
28+
* Initialize this item with the specified parameters
29+
*
30+
* @param array|null $parameters An array of parameters to set on this object
31+
*/
32+
public function initialize($parameters = null)
33+
{
34+
$this->parameters = new ParameterBag;
35+
36+
Helper::initialize($this, $parameters);
37+
38+
return $this;
39+
}
40+
41+
public function getParameters()
42+
{
43+
return $this->parameters->all();
44+
}
45+
46+
protected function getParameter($key)
47+
{
48+
return $this->parameters->get($key);
49+
}
50+
51+
protected function setParameter($key, $value)
52+
{
53+
$this->parameters->set($key, $value);
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* {@inheritDoc}
60+
*/
61+
public function getName()
62+
{
63+
return $this->getParameter('name');
64+
}
65+
66+
/**
67+
* Set the item name
68+
*/
69+
public function setName($value)
70+
{
71+
return $this->setParameter('name', $value);
72+
}
73+
74+
/**
75+
* {@inheritDoc}
76+
*/
77+
public function getDescription()
78+
{
79+
return $this->getParameter('description');
80+
}
81+
82+
/**
83+
* Set the item description
84+
*/
85+
public function setDescription($value)
86+
{
87+
return $this->setParameter('description', $value);
88+
}
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
public function getQuantity()
94+
{
95+
return $this->getParameter('quantity');
96+
}
97+
98+
/**
99+
* Set the item quantity
100+
*/
101+
public function setQuantity($value)
102+
{
103+
return $this->setParameter('quantity', $value);
104+
}
105+
106+
/**
107+
* {@inheritDoc}
108+
*/
109+
public function getPrice()
110+
{
111+
return $this->getParameter('price');
112+
}
113+
114+
/**
115+
* Set the item price
116+
*/
117+
public function setPrice($value)
118+
{
119+
return $this->setParameter('price', $value);
120+
}
121+
}

src/Omnipay/Common/ItemBag.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Omnipay\Common;
4+
5+
class ItemBag implements \IteratorAggregate, \Countable
6+
{
7+
/**
8+
* Item storage
9+
*
10+
* @var array
11+
*/
12+
protected $items;
13+
14+
/**
15+
* Constructor
16+
*
17+
* @param array $items An array of items
18+
*/
19+
public function __construct(array $items = array())
20+
{
21+
$this->replace($items);
22+
}
23+
24+
/**
25+
* Return all the items
26+
*
27+
* @return array An array of items
28+
*/
29+
public function all()
30+
{
31+
return $this->items;
32+
}
33+
34+
/**
35+
* Replace the contents of this bag with the specified items
36+
*
37+
* @param array $items An array of items
38+
*/
39+
public function replace(array $items = array())
40+
{
41+
$this->items = array();
42+
43+
foreach ($items as $item) {
44+
$this->add($item);
45+
}
46+
}
47+
48+
/**
49+
* Add an item to the bag
50+
*
51+
* @param ItemInterface|array $item An existing item, or associative array of item parameters
52+
*/
53+
public function add($item)
54+
{
55+
if ($item instanceof ItemInterface) {
56+
$this->items[] = $item;
57+
} else {
58+
$this->items[] = new Item($item);
59+
}
60+
}
61+
62+
/**
63+
* Returns an iterator for items
64+
*
65+
* @return \ArrayIterator An \ArrayIterator instance
66+
*/
67+
public function getIterator()
68+
{
69+
return new \ArrayIterator($this->items);
70+
}
71+
72+
/**
73+
* Returns the number of items
74+
*
75+
* @return int The number of items
76+
*/
77+
public function count()
78+
{
79+
return count($this->items);
80+
}
81+
}

src/Omnipay/Common/ItemInterface.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Omnipay\Common;
4+
5+
/**
6+
* Cart Item interface
7+
*/
8+
interface ItemInterface
9+
{
10+
/**
11+
* Name of the item
12+
*/
13+
public function getName();
14+
15+
/**
16+
* Description of the item
17+
*/
18+
public function getDescription();
19+
20+
/**
21+
* Quantity of the item
22+
*/
23+
public function getQuantity();
24+
25+
/**
26+
* Price of the item
27+
*/
28+
public function getPrice();
29+
}

src/Omnipay/Common/Message/AbstractRequest.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Omnipay\Common\Exception\InvalidRequestException;
99
use Omnipay\Common\Exception\RuntimeException;
1010
use Omnipay\Common\Helper;
11+
use Omnipay\Common\ItemBag;
1112
use Symfony\Component\HttpFoundation\ParameterBag;
1213
use Symfony\Component\HttpFoundation\Request as HttpRequest;
1314

@@ -171,12 +172,7 @@ public function getAmount()
171172
);
172173
}
173174

174-
return number_format(
175-
$amount,
176-
$this->getCurrencyDecimalPlaces(),
177-
'.',
178-
''
179-
);
175+
return $this->formatCurrency($amount);
180176
}
181177
}
182178

@@ -221,6 +217,16 @@ private function getCurrencyDecimalFactor()
221217
return pow(10, $this->getCurrencyDecimalPlaces());
222218
}
223219

220+
public function formatCurrency($amount)
221+
{
222+
return number_format(
223+
$amount,
224+
$this->getCurrencyDecimalPlaces(),
225+
'.',
226+
''
227+
);
228+
}
229+
224230
public function getDescription()
225231
{
226232
return $this->getParameter('description');
@@ -251,6 +257,30 @@ public function setTransactionReference($value)
251257
return $this->setParameter('transactionReference', $value);
252258
}
253259

260+
/**
261+
* A list of items in this order
262+
*
263+
* @return ItemBag|null A bag containing items in this order
264+
*/
265+
public function getItems()
266+
{
267+
return $this->getParameter('items');
268+
}
269+
270+
/**
271+
* Set the items in this order
272+
*
273+
* @param ItemBag|array $items An array of items in this order
274+
*/
275+
public function setItems($items)
276+
{
277+
if ($items && !$items instanceof ItemBag) {
278+
$items = new ItemBag($items);
279+
}
280+
281+
return $this->setParameter('items', $items);
282+
}
283+
254284
public function getClientIp()
255285
{
256286
return $this->getParameter('clientIp');

src/Omnipay/PayPal/Message/ExpressAuthorizeRequest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ public function getData()
5050
$data['EMAIL'] = $card->getEmail();
5151
}
5252

53+
$items = $this->getItems();
54+
if ($items) {
55+
foreach ($items as $n => $item) {
56+
$data["L_PAYMENTREQUEST_0_NAME$n"] = $item->getName();
57+
$data["L_PAYMENTREQUEST_0_DESC$n"] = $item->getDescription();
58+
$data["L_PAYMENTREQUEST_0_QTY$n"] = $item->getQuantity();
59+
$data["L_PAYMENTREQUEST_0_AMT$n"] = $this->formatCurrency($item->getPrice());
60+
}
61+
}
62+
5363
return $data;
5464
}
5565

tests/Omnipay/Common/ItemBagTest.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Omnipay\Common;
4+
5+
use Omnipay\TestCase;
6+
7+
class ItemBagTest extends TestCase
8+
{
9+
public function setUp()
10+
{
11+
$this->bag = new ItemBag;
12+
}
13+
14+
public function testConstruct()
15+
{
16+
$bag = new ItemBag(array(array('name' => 'Floppy Disk')));
17+
$this->assertCount(1, $bag);
18+
}
19+
20+
public function testAll()
21+
{
22+
$items = array(new Item, new Item);
23+
$bag = new ItemBag($items);
24+
25+
$this->assertSame($items, $bag->all());
26+
}
27+
28+
public function testReplace()
29+
{
30+
$items = array(new Item, new Item);
31+
$this->bag->replace($items);
32+
33+
$this->assertSame($items, $this->bag->all());
34+
}
35+
36+
public function testAddWithItem()
37+
{
38+
$item = new Item;
39+
$item->setName('CD-ROM');
40+
$this->bag->add($item);
41+
42+
$contents = $this->bag->all();
43+
$this->assertSame($item, $contents[0]);
44+
}
45+
46+
public function testAddWithArray()
47+
{
48+
$item = array('name' => 'CD-ROM');
49+
$this->bag->add($item);
50+
51+
$contents = $this->bag->all();
52+
$this->assertInstanceOf('\Omnipay\Common\Item', $contents[0]);
53+
$this->assertSame('CD-ROM', $contents[0]->getName());
54+
}
55+
56+
public function testGetIterator()
57+
{
58+
$item = new Item;
59+
$item->setName('CD-ROM');
60+
$this->bag->add($item);
61+
62+
foreach ($this->bag as $bagItem) {
63+
$this->assertSame($item, $bagItem);
64+
}
65+
}
66+
67+
public function testCount()
68+
{
69+
$this->bag->add(new Item);
70+
$this->bag->add(new Item);
71+
$this->bag->add(new Item);
72+
73+
$this->assertSame(3, count($this->bag));
74+
}
75+
}

0 commit comments

Comments
 (0)