This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPayzeCardToken.php
229 lines (206 loc) · 6.14 KB
/
PayzeCardToken.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace PayzeIO\LaravelPayze\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Crypt;
/**
* PayzeIO\LaravelPayze\Models\PayzeCardToken
*
* @property int $id
* @property int|null $transaction_id
* @property int|null $model_id
* @property string|null $model_type
* @property bool $active
* @property bool $default
* @property string|null $card_mask
* @property string|null $cardholder
* @property string|null $brand
* @property \Illuminate\Support\Carbon|null $expiration_date
* @property string $token
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read Model|\Eloquent $model
* @property-read \PayzeIO\LaravelPayze\Models\PayzeTransaction|null $transaction
* @method static Builder|PayzeCardToken active()
* @method static Builder|PayzeCardToken default()
* @method static Builder|PayzeCardToken expired()
* @method static Builder|PayzeCardToken inactive()
* @method static Builder|PayzeCardToken newModelQuery()
* @method static Builder|PayzeCardToken newQuery()
* @method static Builder|PayzeCardToken query()
* @method static Builder|PayzeCardToken whereActive($value)
* @method static Builder|PayzeCardToken whereBrand($value)
* @method static Builder|PayzeCardToken whereCardMask($value)
* @method static Builder|PayzeCardToken whereCardholder($value)
* @method static Builder|PayzeCardToken whereCreatedAt($value)
* @method static Builder|PayzeCardToken whereDefault($value)
* @method static Builder|PayzeCardToken whereExpirationDate($value)
* @method static Builder|PayzeCardToken whereId($value)
* @method static Builder|PayzeCardToken whereModelId($value)
* @method static Builder|PayzeCardToken whereModelType($value)
* @method static Builder|PayzeCardToken whereToken($value)
* @method static Builder|PayzeCardToken whereTransactionId($value)
* @method static Builder|PayzeCardToken whereUpdatedAt($value)
* @method static Builder|PayzeCardToken withInactive()
* @mixin \Eloquent
*/
class PayzeCardToken extends Model
{
/**
* Guarded attributes.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'active' => 'boolean',
'default' => 'boolean',
'expiration_date' => 'datetime',
];
/**
* PayzeCardToken constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->table = config('payze.card_tokens_table', 'payze_card_tokens');
}
protected static function booted()
{
/*
* Add global active scope
*/
static::addGlobalScope('active', fn(Builder $builder) => $builder->where('active', true));
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function transaction(): BelongsTo
{
return $this->belongsTo(PayzeTransaction::class, 'transaction_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function model(): MorphTo
{
return $this->morphTo();
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWithInactive(Builder $query): Builder
{
return $query->withoutGlobalScope('active');
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDefault(Builder $query): Builder
{
return $query->where('default', true);
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive(Builder $query): Builder
{
return $query->where('expiration_date', '>=', Carbon::now());
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeInactive(Builder $query): Builder
{
return $query->withInactive()->where('active', false);
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExpired(Builder $query): Builder
{
return $query->where('expiration_date', '<', Carbon::now());
}
/**
* Encrypt a card token before saving to DB
*
* @param string $value
*/
public function setTokenAttribute(string $value): void
{
if (empty($value)) {
return;
}
$this->attributes['token'] = Crypt::encryptString($value);
}
/**
* Return a decrypted token
*
* @return string|null
*/
public function getToken(): string
{
return Crypt::decryptString($this->token);
}
/**
* Mark current card as default
* Unmark all other cards of the same model
*
* @return $this
*/
public function markAsDefault(): self
{
self::where('model_type', $this->model_type)
->where('model_id', $this->model_id)
->where('id', '!=', $this->id)
->where('default', true)
->update(['default' => false]);
return tap($this)->update([
'default' => true,
]);
}
/**
* Check if current card is not expired
*
* @return bool
*/
public function isActive(): bool
{
if (empty($this->expiration_date)) {
return true;
}
return $this->expiration_date->isFuture();
}
/**
* Check if current card is expired
*
* @return bool
*/
public function isExpired(): bool
{
if (empty($this->expiration_date)) {
return false;
}
return $this->expiration_date->isPast();
}
}