|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Moecasts\Laravel\Wallet\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
7
|
|
|
use Moecasts\Laravel\Wallet\Exceptions\CurrencyInvalid; |
|
8
|
|
|
use Moecasts\Laravel\Wallet\Interfaces\Product; |
|
9
|
|
|
use Moecasts\Laravel\Wallet\Interfaces\Refundable; |
|
10
|
|
|
use Moecasts\Laravel\Wallet\Models\Transaction; |
|
11
|
|
|
use Moecasts\Laravel\Wallet\Models\Transfer; |
|
12
|
|
|
use Moecasts\Laravel\Wallet\Models\Wallet; |
|
13
|
|
|
|
|
14
|
|
|
trait HasWallets { |
|
15
|
28 |
|
public function wallets(): MorphMany |
|
16
|
|
|
{ |
|
17
|
28 |
|
return $this->morphMany(Wallet::class, 'holder'); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
29 |
|
public function getWallet(string $currency): ?Wallet |
|
21
|
|
|
{ |
|
22
|
29 |
|
if (! in_array($currency, config('wallet.currencies'))) { |
|
23
|
1 |
|
throw new CurrencyInvalid(trans('wallet::errors.currency_unsupported')); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
28 |
|
$wallet = $this->wallets()->where('currency', $currency)->first(); |
|
27
|
|
|
|
|
28
|
28 |
|
if (! $wallet) { |
|
29
|
28 |
|
$wallet = $this->wallets()->create([ |
|
30
|
28 |
|
'currency' => $currency, |
|
31
|
28 |
|
'balance' => 0 |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
28 |
|
return $wallet; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function transactions(): MorphMany |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->morphMany(Transaction::class, 'holder'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
public function transfers(): MorphMany |
|
44
|
|
|
{ |
|
45
|
6 |
|
return $this->morphMany(Transfer::class, 'from'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
public function paid(Product $product, string $action = Transfer::ACTION_PAID) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$action = [$action]; |
|
51
|
|
|
|
|
52
|
2 |
|
$query = $this->transfers(); |
|
53
|
|
|
|
|
54
|
|
|
return $query |
|
55
|
2 |
|
->where('refund', false) |
|
56
|
2 |
|
->where('to_type', $product->getMorphClass()) |
|
|
|
|
|
|
57
|
2 |
|
->where('to_id', $product->getKey()) |
|
|
|
|
|
|
58
|
2 |
|
->whereIn('action', $action) |
|
59
|
2 |
|
->orderBy('id', 'desc') |
|
60
|
2 |
|
->first(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function refund(Refundable $product, string $action = Transfer::ACTION_PAID) |
|
64
|
|
|
{ |
|
65
|
2 |
|
$transfer = $this->paid($product, $action); |
|
66
|
|
|
|
|
67
|
2 |
|
if (! $transfer) { |
|
68
|
2 |
|
throw (new ModelNotFoundException()) |
|
69
|
2 |
|
->setModel(config('wallet.transfer.model')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return \DB::transaction(function () use ($transfer) { |
|
73
|
1 |
|
$transfer->withdraw->update([ |
|
74
|
1 |
|
'confirmed' => false, |
|
75
|
|
|
]); |
|
76
|
|
|
|
|
77
|
1 |
|
$transfer->deposit->update([ |
|
78
|
1 |
|
'confirmed' => false, |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
1 |
|
$transfer->update([ |
|
82
|
1 |
|
'refund' => true, |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
1 |
|
return $transfer->fromWallet->refreshBalance() && |
|
86
|
1 |
|
$transfer->toWallet->refreshBalance(); |
|
87
|
1 |
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function safeRefund(Product $product, string $action = Transfer::ACTION_PAID) |
|
91
|
|
|
{ |
|
92
|
|
|
try { |
|
93
|
1 |
|
return $this->refund($product, $action); |
|
94
|
1 |
|
} catch (\Throwable $throwable) { |
|
95
|
1 |
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|