Skip to content

Commit 01b4bbb

Browse files
authored
Add separator between Refund and Partial Refund (#17)
Partial Refunds requires Items to be passed
2 parents 102749d + 627fe10 commit 01b4bbb

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Event/Refund.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ class Refund extends Model\Event implements Facade\Refund
1818
protected $tax;
1919
protected $items = [];
2020

21+
private $isFullRefund = false;
22+
23+
/**
24+
* Full refunds does not require items to be passed. \
25+
* This will skip the items check if true
26+
*
27+
* @param boolean $is
28+
* @return static
29+
*/
30+
public function isFullRefund(bool $is)
31+
{
32+
$this->isFullRefund = $is;
33+
return $this;
34+
}
35+
2136
public function getName(): string
2237
{
2338
return 'refund';
@@ -52,7 +67,11 @@ public function getRequiredParams(): array
5267
}
5368

5469
$return[] = 'transaction_id';
55-
$return[] = 'items';
70+
71+
if (!$this->isFullRefund) {
72+
$return[] = 'items';
73+
}
74+
5675
return $return;
5776
}
5877

test/AnalyticsTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use AlexWestergaard\PhpGa4\Analytics;
4+
use AlexWestergaard\PhpGa4\Event\Refund;
45
use AlexWestergaard\PhpGa4\Item;
56
use AlexWestergaard\PhpGa4\UserProperty;
67
use AlexWestergaard\PhpGa4\GA4Exception;
@@ -104,6 +105,45 @@ public function testUserProperty()
104105
$this->assertTrue($this->analytics->post());
105106
}
106107

108+
public function testFullRefundNoItems()
109+
{
110+
$this->prepareSituation();
111+
112+
$refund = Refund::new()->setTransactionId(1)->isFullRefund(true);
113+
114+
$this->analytics->addEvent($refund);
115+
116+
$this->assertTrue($this->analytics->post());
117+
}
118+
119+
public function testPartialRefundWithItems()
120+
{
121+
$this->prepareSituation();
122+
123+
$refund = Refund::new()->setTransactionId(1)->addItem($this->item);
124+
125+
$this->analytics->addEvent($refund);
126+
127+
$arr = $this->analytics->toArray();
128+
$this->assertTrue(is_array($arr));
129+
130+
$arr = $refund->toArray();
131+
$this->assertArrayHasKey('params', $arr);
132+
$arr = $arr['params'];
133+
$this->assertArrayHasKey('items', $arr);
134+
}
135+
136+
public function testPartialRefundNoItemsThrows()
137+
{
138+
$this->prepareSituation();
139+
140+
$refund = Refund::new()->setTransactionId(1);
141+
142+
$this->expectException(GA4Exception::class);
143+
144+
$this->analytics->addEvent($refund);
145+
}
146+
107147
public function testPrebuildEvents()
108148
{
109149
$this->prepareSituation();

0 commit comments

Comments
 (0)