Skip to content

Commit 107b40b

Browse files
committed
add forceRefund method
1 parent 2a11a16 commit 107b40b

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/Traits/CanBePaid.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ public function paid(Product $product): ?Transfer
6060

6161
/**
6262
* @param Product $product
63+
* @param bool $force
6364
* @return bool
6465
* @throws
6566
*/
66-
public function refund(Product $product): bool
67+
public function refund(Product $product, bool $force = false): bool
6768
{
6869
$transfer = $this->paid($product);
6970

@@ -72,23 +73,38 @@ public function refund(Product $product): bool
7273
->setModel($this->transfers()->getMorphClass());
7374
}
7475

75-
return DB::transaction(function() use ($product, $transfer) {
76-
$product->transfer($this, $product->getAmountProduct(), $product->getMetaProduct());
76+
return DB::transaction(function() use ($product, $transfer, $force) {
77+
if ($force) {
78+
$product->forceTransfer($this, $product->getAmountProduct(), $product->getMetaProduct());
79+
} else {
80+
$product->transfer($this, $product->getAmountProduct(), $product->getMetaProduct());
81+
}
82+
7783
return $transfer->update(['refund' => 1]);
7884
});
7985
}
8086

8187
/**
8288
* @param Product $product
89+
* @param bool $force
8390
* @return bool
8491
*/
85-
public function safeRefund(Product $product): bool
92+
public function safeRefund(Product $product, bool $force = false): bool
8693
{
8794
try {
88-
return $this->refund($product);
95+
return $this->refund($product, $force);
8996
} catch (\Throwable $throwable) {
9097
return false;
9198
}
9299
}
93100

101+
/**
102+
* @param Product $product
103+
* @return bool
104+
*/
105+
public function forceRefund(Product $product): bool
106+
{
107+
return $this->refund($product, true);
108+
}
109+
94110
}

tests/ProductTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,45 @@ public function testRefund(): void
5454
$this->assertEquals($buyer->balance, 0);
5555
}
5656

57+
/**
58+
* @return void
59+
*/
60+
public function testForceRefund(): void
61+
{
62+
/**
63+
* @var Buyer $buyer
64+
* @var Item $product
65+
*/
66+
$buyer = factory(Buyer::class)->create();
67+
$product = factory(Item::class)->create([
68+
'quantity' => 1,
69+
]);
70+
71+
$this->assertNotEquals($product->balance, 0);
72+
$product->withdraw($product->balance);
73+
74+
$this->assertEquals($buyer->balance, 0);
75+
$buyer->deposit($product->price);
76+
77+
$this->assertEquals($buyer->balance, $product->price);
78+
79+
$buyer->pay($product);
80+
$this->assertEquals($buyer->balance, 0);
81+
$this->assertEquals($product->balance, $product->price);
82+
83+
$product->withdraw($product->balance);
84+
$this->assertEquals($product->balance, 0);
85+
86+
$this->assertFalse($buyer->safeRefund($product));
87+
$this->assertTrue($buyer->forceRefund($product));
88+
89+
$this->assertEquals($product->balance, -$product->price);
90+
$this->assertEquals($buyer->balance, $product->price);
91+
$product->deposit(-$product->balance);
92+
$buyer->withdraw($buyer->balance);
93+
94+
$this->assertEquals($product->balance, 0);
95+
$this->assertEquals($buyer->balance, 0);
96+
}
97+
5798
}

0 commit comments

Comments
 (0)