1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nxmad\Larapay\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
|
9
|
|
|
class Transaction extends Model |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The state means user failed payment for Transaction. |
13
|
|
|
*/ |
14
|
|
|
const FAILED = 'failed'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The state means Transaction in pending. |
18
|
|
|
*/ |
19
|
|
|
const PENDING = 'pending'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The state means Transaction was canceled by user or admin. |
23
|
|
|
*/ |
24
|
|
|
const CANCELED = 'canceled'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The state means Transaction succeed. |
28
|
|
|
*/ |
29
|
|
|
const SUCCESSFUL = 'successful'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The list of allowed Transaction states. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
const STATES = [ |
37
|
|
|
self::FAILED, |
38
|
|
|
self::PENDING, |
39
|
|
|
self::CANCELED, |
40
|
|
|
self::SUCCESSFUL, |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The attributes that are mass assignable. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $fillable = ['amount', 'meta', 'state', 'subject_id', 'subject_type']; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The attributes that should be cast to native types. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $casts = [ |
56
|
|
|
'meta' => 'object' |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Determine if Subjects's balance is enough for transaction. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function affordable(): bool |
65
|
|
|
{ |
66
|
|
|
return $this->subject->canAfford($this); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set subject of transaction. |
71
|
|
|
* |
72
|
|
|
* @param $instance |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
|
|
public function setSubject($instance): self |
77
|
|
|
{ |
78
|
|
|
$this->subject_id = $instance->id; |
|
|
|
|
79
|
|
|
$this->subject_type = get_class($instance); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get polymorphic relation. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
88
|
|
|
*/ |
89
|
|
|
public function subject() |
90
|
|
|
{ |
91
|
|
|
return $this->morphTo(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get primary value. |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
public function getPrimaryValue() |
100
|
|
|
{ |
101
|
|
|
return $this->{$this->primaryKey}; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get Transaction amount. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function getAmount() |
110
|
|
|
{ |
111
|
|
|
return $this->amount; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get default payment description. |
116
|
|
|
* You have to override this method. |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getDescription(): string |
121
|
|
|
{ |
122
|
|
|
return 'Payment'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Invoke() to change the state. |
127
|
|
|
* |
128
|
|
|
* @param string|null $state |
129
|
|
|
* |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function __invoke(string $state = null) |
133
|
|
|
{ |
134
|
|
|
if ($this->exists && $this->state === $state) { |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (! is_null($state)) { |
139
|
|
|
$this->state = $state; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($this->state === self::SUCCESSFUL && defined($this->subject_type . '::KEEP')) { |
143
|
|
|
$this->subject()->increment(($this->subject_type)::KEEP, $this->amount); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->save(); |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Catch make* methods. |
153
|
|
|
* E.g. ->makeCanceled(); |
154
|
|
|
* |
155
|
|
|
* @param string $method |
156
|
|
|
* @param array $parameters |
157
|
|
|
* |
158
|
|
|
* @return self|mixed |
159
|
|
|
*/ |
160
|
|
|
public function __call($method, $parameters) |
161
|
|
|
{ |
162
|
|
|
if (strpos($method, 'make') !== false) { |
163
|
|
|
$state = mb_strtolower(str_replace('make', '', $method)); |
164
|
|
|
|
165
|
|
|
if (in_array($state, self::STATES)) { |
166
|
|
|
return $this($state); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return parent::__call($method, $parameters); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.