Skip to content

Commit efc5403

Browse files
committed
patch gift
1 parent fee1578 commit efc5403

File tree

4 files changed

+141
-9
lines changed

4 files changed

+141
-9
lines changed

src/Traits/CanBePaid.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,24 @@ public function forceRefund(Product $product, bool $gifts = false): bool
161161
return $this->refund($product, true, $gifts);
162162
}
163163

164+
/**
165+
* @param Product $product
166+
* @param bool $force
167+
* @return bool
168+
*/
169+
public function refundGift(Product $product, bool $force = false): bool
170+
{
171+
return $this->refund($product, $force, true);
172+
}
173+
174+
/**
175+
* @param Product $product
176+
* @return bool
177+
* @throws
178+
*/
179+
public function forceRefundGift(Product $product): bool
180+
{
181+
return $this->refundGift($product, true);
182+
}
183+
164184
}

src/Traits/HasGift.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,74 @@ trait HasGift
2424
*
2525
* @param Wallet $to
2626
* @param Product $product
27+
* @param bool $force
2728
* @return Transfer
2829
*/
29-
public function gift(Wallet $to, Product $product): Transfer
30+
public function gift(Wallet $to, Product $product, bool $force = false): Transfer
3031
{
3132
/**
32-
* this comment is needed for syntax highlighting )
33-
* @var \Bavix\Wallet\Models\Wallet $to
33+
* Who's giving? Let's call him Santa Claus
3434
*/
35-
return DB::transaction(function () use ($to, $product) {
35+
$santa = $this;
36+
37+
/**
38+
* @return Transfer
39+
*/
40+
$callback = function () use ($santa, $product, $force) {
3641
$amount = $product->getAmountProduct();
3742
$meta = $product->getMetaProduct();
3843
$fee = Tax::fee($product, $amount);
39-
$withdraw = $this->withdraw($amount + $fee, $meta);
44+
45+
/**
46+
* Santa pays taxes
47+
*/
48+
if ($force) {
49+
$withdraw = $santa->withdraw($amount + $fee, $meta);
50+
} else {
51+
$withdraw = $santa->forceWithdraw($amount + $fee, $meta);
52+
}
53+
4054
$deposit = $product->deposit($amount, $meta);
41-
return $to->assemble($product, $withdraw, $deposit);
42-
});
55+
return $this->assemble($product, $withdraw, $deposit);
56+
};
57+
58+
/**
59+
* Unfortunately,
60+
* I think it is wrong to make the "assemble" method public.
61+
* That's why I address him like this!
62+
*/
63+
return DB::transaction(
64+
$callback->bindTo($to, \get_class($to))
65+
);
4366
}
4467

4568
/**
4669
* Give the goods safely.
4770
*
4871
* @param Wallet $to
4972
* @param Product $product
73+
* @param bool $force
5074
* @return Transfer|null
5175
*/
52-
public function safeGift(Wallet $to, Product $product): ?Transfer
76+
public function safeGift(Wallet $to, Product $product, bool $force = false): ?Transfer
5377
{
5478
try {
55-
return $this->gift($to, $product);
79+
return $this->gift($to, $product, $force);
5680
} catch (\Throwable $throwable) {
5781
return null;
5882
}
5983
}
6084

85+
/**
86+
* to give force)
87+
*
88+
* @param Wallet $to
89+
* @param Product $product
90+
* @return Transfer
91+
*/
92+
public function forceGift(Wallet $to, Product $product): Transfer
93+
{
94+
return $this->gift($to, $product, true);
95+
}
96+
6197
}

src/Traits/HasWallet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null):
164164
/**
165165
* this method adds a new transfer to the transfer table
166166
*
167+
* @param string $status
167168
* @param Wallet $wallet
168169
* @param Transaction $withdraw
169170
* @param Transaction $deposit
@@ -172,10 +173,17 @@ public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null):
172173
*/
173174
protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $deposit): Transfer
174175
{
176+
$status = Transfer::STATUS_PAID;
177+
if ($this->getMorphClass() !== $withdraw->payable_type ||
178+
$this->getKey() !== $withdraw->payable_id) {
179+
$status = Transfer::STATUS_GIFT;
180+
}
181+
175182
/**
176183
* @var Model $wallet
177184
*/
178185
return \app('bavix.wallet::transfer')->create([
186+
'status' => $status,
179187
'deposit_id' => $deposit->getKey(),
180188
'withdraw_id' => $withdraw->getKey(),
181189
'from_type' => $this->getMorphClass(),

tests/GiftTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Bavix\Wallet\Test;
4+
5+
use Bavix\Wallet\Test\Models\Buyer;
6+
use Bavix\Wallet\Test\Models\Item;
7+
8+
class GiftTest extends TestCase
9+
{
10+
11+
/**
12+
* @return void
13+
*/
14+
public function testGift(): void
15+
{
16+
/**
17+
* @var Buyer $first
18+
* @var Buyer $second
19+
* @var Item $product
20+
*/
21+
[$first, $second] = factory(Buyer::class, 2)->create();
22+
$product = factory(Item::class)->create([
23+
'quantity' => 1,
24+
]);
25+
26+
$this->assertEquals($first->balance, 0);
27+
$this->assertEquals($second->balance, 0);
28+
29+
$first->deposit($product->getAmountProduct());
30+
$this->assertEquals($first->balance, $product->getAmountProduct());
31+
32+
$first->wallet->gift($second, $product);
33+
$this->assertEquals($first->balance, 0);
34+
$this->assertEquals($second->balance, 0);
35+
$this->assertNull($first->paid($product, true));
36+
$this->assertNotNull($second->paid($product, true));
37+
}
38+
39+
/**
40+
* @return void
41+
*/
42+
public function testRefund(): void
43+
{
44+
/**
45+
* @var Buyer $first
46+
* @var Buyer $second
47+
* @var Item $product
48+
*/
49+
[$first, $second] = factory(Buyer::class, 2)->create();
50+
$product = factory(Item::class)->create([
51+
'quantity' => 1,
52+
]);
53+
54+
$this->assertEquals($first->balance, 0);
55+
$this->assertEquals($second->balance, 0);
56+
57+
$first->deposit($product->getAmountProduct());
58+
$this->assertEquals($first->balance, $product->getAmountProduct());
59+
60+
$first->wallet->gift($second, $product);
61+
$this->assertEquals($first->balance, 0);
62+
$this->assertEquals($second->balance, 0);
63+
64+
$this->assertFalse($second->wallet->safeRefund($product));
65+
$this->assertTrue($second->wallet->refundGift($product));
66+
}
67+
68+
}

0 commit comments

Comments
 (0)