|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Product; |
|
6
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
|
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
|
8
|
|
|
use Bavix\Wallet\Tax; |
|
9
|
|
|
use Illuminate\Support\Facades\DB; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trait HasGift |
|
13
|
|
|
* @package Bavix\Wallet\Traits |
|
14
|
|
|
* |
|
15
|
|
|
* This trait should be used with the trait HasWallet. |
|
16
|
|
|
*/ |
|
17
|
|
|
trait HasGift |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* From this moment on, each user (wallet) can give |
|
22
|
|
|
* the goods to another user (wallet). |
|
23
|
|
|
* This functionality can be organized for gifts. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Wallet $to |
|
26
|
|
|
* @param Product $product |
|
27
|
|
|
* @return Transfer |
|
28
|
|
|
*/ |
|
29
|
|
|
public function gift(Wallet $to, Product $product): Transfer |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* this comment is needed for syntax highlighting ) |
|
33
|
|
|
* @var \Bavix\Wallet\Models\Wallet $to |
|
34
|
|
|
*/ |
|
35
|
|
|
return DB::transaction(function () use ($to, $product) { |
|
36
|
|
|
$amount = $product->getAmountProduct(); |
|
37
|
|
|
$meta = $product->getMetaProduct(); |
|
38
|
|
|
$fee = Tax::fee($product, $amount); |
|
39
|
|
|
$withdraw = $this->withdraw($amount + $fee, $meta); |
|
|
|
|
|
|
40
|
|
|
$deposit = $product->deposit($amount, $meta); |
|
41
|
|
|
return $to->assemble($product, $withdraw, $deposit); |
|
42
|
|
|
}); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Give the goods safely. |
|
47
|
|
|
* |
|
48
|
|
|
* @param Wallet $to |
|
49
|
|
|
* @param Product $product |
|
50
|
|
|
* @return Transfer|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public function safeGift(Wallet $to, Product $product): ?Transfer |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
|
|
return $this->gift($to, $product); |
|
56
|
|
|
} catch (\Throwable $throwable) { |
|
57
|
|
|
return null; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|