88use Illuminate \Database \Eloquent \Model ;
99use Illuminate \Database \Eloquent \Relations \BelongsTo ;
1010use Illuminate \Database \Eloquent \Relations \HasOne ;
11+ use Illuminate \Support \Carbon ;
1112
1213class PluginLicense extends Model
1314{
@@ -47,6 +48,14 @@ public function pluginBundle(): BelongsTo
4748 return $ this ->belongsTo (PluginBundle::class);
4849 }
4950
51+ /**
52+ * @return BelongsTo<User, PluginLicense>
53+ */
54+ public function refundedBy (): BelongsTo
55+ {
56+ return $ this ->belongsTo (User::class, 'refunded_by ' );
57+ }
58+
5059 public function wasPurchasedAsBundle (): bool
5160 {
5261 return $ this ->plugin_bundle_id !== null ;
@@ -59,10 +68,11 @@ public function wasPurchasedAsBundle(): bool
5968 #[Scope]
6069 protected function active (Builder $ query ): Builder
6170 {
62- return $ query ->where (function ($ q ): void {
63- $ q ->whereNull ('expires_at ' )
64- ->orWhere ('expires_at ' , '> ' , now ());
65- });
71+ return $ query ->whereNull ('refunded_at ' )
72+ ->where (function ($ q ): void {
73+ $ q ->whereNull ('expires_at ' )
74+ ->orWhere ('expires_at ' , '> ' , now ());
75+ });
6676 }
6777
6878 /**
@@ -87,6 +97,10 @@ protected function forPlugin(Builder $query, Plugin $plugin): Builder
8797
8898 public function isActive (): bool
8999 {
100+ if ($ this ->isRefunded ()) {
101+ return false ;
102+ }
103+
90104 if ($ this ->expires_at === null ) {
91105 return true ;
92106 }
@@ -99,13 +113,40 @@ public function isExpired(): bool
99113 return ! $ this ->isActive ();
100114 }
101115
116+ public function isRefunded (): bool
117+ {
118+ return $ this ->refunded_at !== null ;
119+ }
120+
121+ public function isRefundable (): bool
122+ {
123+ if ($ this ->isRefunded ()) {
124+ return false ;
125+ }
126+
127+ if ($ this ->is_grandfathered ) {
128+ return false ;
129+ }
130+
131+ if ($ this ->price_paid <= 0 ) {
132+ return false ;
133+ }
134+
135+ if (! $ this ->stripe_payment_intent_id ) {
136+ return false ;
137+ }
138+
139+ return $ this ->purchased_at ->diffInDays (Carbon::now ()) <= 14 ;
140+ }
141+
102142 protected function casts (): array
103143 {
104144 return [
105145 'price_paid ' => 'integer ' ,
106146 'is_grandfathered ' => 'boolean ' ,
107147 'purchased_at ' => 'datetime ' ,
108148 'expires_at ' => 'datetime ' ,
149+ 'refunded_at ' => 'datetime ' ,
109150 ];
110151 }
111152}
0 commit comments