-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathPluginLicense.php
More file actions
152 lines (128 loc) · 3.35 KB
/
PluginLicense.php
File metadata and controls
152 lines (128 loc) · 3.35 KB
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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
class PluginLicense extends Model
{
use HasFactory;
protected $guarded = [];
/**
* @return BelongsTo<User, PluginLicense>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return BelongsTo<Plugin, PluginLicense>
*/
public function plugin(): BelongsTo
{
return $this->belongsTo(Plugin::class);
}
/**
* @return HasOne<PluginPayout>
*/
public function payout(): HasOne
{
return $this->hasOne(PluginPayout::class);
}
/**
* @return BelongsTo<PluginBundle, PluginLicense>
*/
public function pluginBundle(): BelongsTo
{
return $this->belongsTo(PluginBundle::class);
}
/**
* @return BelongsTo<User, PluginLicense>
*/
public function refundedBy(): BelongsTo
{
return $this->belongsTo(User::class, 'refunded_by');
}
public function wasPurchasedAsBundle(): bool
{
return $this->plugin_bundle_id !== null;
}
/**
* @param Builder<PluginLicense> $query
* @return Builder<PluginLicense>
*/
#[Scope]
protected function active(Builder $query): Builder
{
return $query->whereNull('refunded_at')
->where(function ($q): void {
$q->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
}
/**
* @param Builder<PluginLicense> $query
* @return Builder<PluginLicense>
*/
#[Scope]
protected function forUser(Builder $query, User $user): Builder
{
return $query->where('user_id', $user->id);
}
/**
* @param Builder<PluginLicense> $query
* @return Builder<PluginLicense>
*/
#[Scope]
protected function forPlugin(Builder $query, Plugin $plugin): Builder
{
return $query->where('plugin_id', $plugin->id);
}
public function isActive(): bool
{
if ($this->isRefunded()) {
return false;
}
if ($this->expires_at === null) {
return true;
}
return $this->expires_at->isFuture();
}
public function isExpired(): bool
{
return ! $this->isActive();
}
public function isRefunded(): bool
{
return $this->refunded_at !== null;
}
public function isRefundable(): bool
{
if ($this->isRefunded()) {
return false;
}
if ($this->is_grandfathered) {
return false;
}
if ($this->price_paid <= 0) {
return false;
}
if (! $this->stripe_payment_intent_id) {
return false;
}
return $this->purchased_at->diffInDays(Carbon::now()) <= 14;
}
protected function casts(): array
{
return [
'price_paid' => 'integer',
'is_grandfathered' => 'boolean',
'purchased_at' => 'datetime',
'expires_at' => 'datetime',
'refunded_at' => 'datetime',
];
}
}