1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nxmad\Larapay\Traits; |
6
|
|
|
|
7
|
|
|
use Nxmad\Larapay\Models\Transaction; |
8
|
|
|
|
9
|
|
|
trait HasTransactions |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Setup transaction for subject without saving to database. |
13
|
|
|
* |
14
|
|
|
* @param float $amount |
15
|
|
|
* @param array $meta |
16
|
|
|
* @param string $state |
17
|
|
|
* |
18
|
|
|
* @return mixed |
19
|
|
|
*/ |
20
|
|
|
public function setup(float $amount, $meta = [], string $state = Transaction::PENDING): Transaction |
21
|
|
|
{ |
22
|
|
|
$class = $this->getTransactionClass(); |
23
|
|
|
$meta = is_scalar($meta) ? ['description' => $meta] : $meta; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Transaction |
27
|
|
|
*/ |
28
|
|
|
$transaction = (new $class) |
29
|
|
|
->setSubject($this) |
30
|
|
|
->fill(compact('amount', 'meta', 'state')); |
31
|
|
|
|
32
|
|
|
return $transaction; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Setup transaction and save it immediately. |
37
|
|
|
* |
38
|
|
|
* @param float $amount |
39
|
|
|
* @param array $meta |
40
|
|
|
* @param string $state |
41
|
|
|
* |
42
|
|
|
* @return Transaction |
43
|
|
|
*/ |
44
|
|
|
public function transaction(float $amount, array $meta = [], string $state = Transaction::PENDING): Transaction |
45
|
|
|
{ |
46
|
|
|
$transaction = $this->setup(...func_get_args()); |
|
|
|
|
47
|
|
|
|
48
|
|
|
return $transaction($state); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Determine if Model's balance is enough for transaction |
53
|
|
|
* |
54
|
|
|
* @param Transaction $transaction |
55
|
|
|
* |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function canAfford(Transaction $transaction): bool |
59
|
|
|
{ |
60
|
|
|
$balance = self::KEEP ? $this->attributes[self::KEEP] : $this->recalculateBalance(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $balance >= abs($transaction->amount); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Recalculate subject's balance. |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function recalculateBalance() |
71
|
|
|
{ |
72
|
|
|
return $this->transactions() |
73
|
|
|
->where('state', Transaction::SUCCESSFUL) |
74
|
|
|
->get() |
75
|
|
|
->sum('amount'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get internal Transaction class name |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
protected function getTransactionClass(): string |
84
|
|
|
{ |
85
|
|
|
return app()->config->get('larapay.transaction'); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get primary value. |
90
|
|
|
* |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
|
|
public function getPrimaryValue() |
94
|
|
|
{ |
95
|
|
|
return $this->{$this->primaryKey}; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get polymorphic relation. |
100
|
|
|
* |
101
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
102
|
|
|
*/ |
103
|
|
|
public function transactions() |
104
|
|
|
{ |
105
|
|
|
return $this->morphMany($this->getTransactionClass(), 'subject'); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|