|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
8
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\PlanInterval; |
|
9
|
|
|
use Sagitarius29\LaravelSubscriptions\Entities\Subscription; |
|
10
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanContract; |
|
11
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\SubscriptionContact; |
|
12
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanIntervalContract; |
|
13
|
|
|
use Sagitarius29\LaravelSubscriptions\Exceptions\SubscriptionErrorException; |
|
14
|
|
|
|
|
15
|
|
|
trait HasSubscriptions |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param PlanContract|PlanIntervalContract $planOrInterval |
|
19
|
|
|
* @return Model|SubscriptionContact |
|
20
|
|
|
*/ |
|
21
|
|
|
public function subscribeTo($planOrInterval): SubscriptionContact |
|
22
|
|
|
{ |
|
23
|
|
|
if ($planOrInterval instanceof PlanContract) { |
|
24
|
|
|
return $this->subscribeToPlan($planOrInterval); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return $this->subscribeToInterval($planOrInterval); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param PlanContract $plan |
|
32
|
|
|
* @return Model|SubscriptionContact |
|
33
|
|
|
* @throws SubscriptionErrorException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function subscribeToPlan(PlanContract $plan): SubscriptionContact |
|
36
|
|
|
{ |
|
37
|
|
|
if ($plan->hasManyIntervals()) { |
|
38
|
|
|
throw new SubscriptionErrorException( |
|
39
|
|
|
'This plan has many intervals, please use subscribeToInterval() function' |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
44
|
|
|
$start_at = null; |
|
|
|
|
|
|
45
|
|
|
$end_at = null; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if ($currentSubscription == null) { |
|
48
|
|
|
$start_at = now(); |
|
49
|
|
|
} else { |
|
50
|
|
|
$start_at = $currentSubscription->getExpirationDate(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($plan->isFree()) { |
|
54
|
|
|
$end_at = null; |
|
55
|
|
|
} else { |
|
56
|
|
|
$end_at = $this->calculateExpireDate($start_at, optional($plan->intervals())->first()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$subscription = Subscription::make($plan, $start_at, $end_at); |
|
60
|
|
|
$subscription = $this->subscriptions()->save($subscription); |
|
61
|
|
|
|
|
62
|
|
|
return $subscription; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getActiveSubscription(): ?SubscriptionContact |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->subscriptions() |
|
68
|
|
|
->current() |
|
69
|
|
|
->first(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function subscriptions(): MorphMany |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->morphMany(config('subscriptions.entities.plan_subscription'), 'subscriber'); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function calculateExpireDate(Carbon $start_at, PlanIntervalContract $interval) |
|
78
|
|
|
{ |
|
79
|
|
|
$end_at = Carbon::createFromTimestamp($start_at->timestamp); |
|
80
|
|
|
|
|
81
|
|
|
switch ($interval->getType()) { |
|
82
|
|
|
case PlanInterval::DAY: |
|
83
|
|
|
return $end_at->addDays($interval->getUnit()); |
|
84
|
|
|
break; |
|
|
|
|
|
|
85
|
|
|
case PlanInterval::MONTH: |
|
86
|
|
|
return $end_at->addMonths($interval->getUnit()); |
|
87
|
|
|
break; |
|
|
|
|
|
|
88
|
|
|
case PlanInterval::YEAR: |
|
89
|
|
|
return $end_at->addYears($interval->getUnit()); |
|
90
|
|
|
break; |
|
|
|
|
|
|
91
|
|
|
default: |
|
92
|
|
|
throw new SubscriptionErrorException( |
|
93
|
|
|
'The interval \''.$interval->getType().'\' selected is not available.' |
|
94
|
|
|
); |
|
95
|
|
|
break; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function subscribeToInterval(PlanIntervalContract $interval): SubscriptionContact |
|
100
|
|
|
{ |
|
101
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
102
|
|
|
$start_at = null; |
|
|
|
|
|
|
103
|
|
|
$end_at = null; |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if ($currentSubscription == null) { |
|
106
|
|
|
$start_at = now(); |
|
107
|
|
|
} else { |
|
108
|
|
|
$start_at = $currentSubscription->getExpirationDate(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$end_at = $this->calculateExpireDate($start_at, $interval); |
|
112
|
|
|
|
|
113
|
|
|
$subscription = Subscription::make($interval->plan, $start_at, $end_at); |
|
|
|
|
|
|
114
|
|
|
$subscription = $this->subscriptions()->save($subscription); |
|
115
|
|
|
|
|
116
|
|
|
return $subscription; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function changePlanTo(PlanContract $plan, PlanIntervalContract $interval = null) |
|
120
|
|
|
{ |
|
121
|
|
|
if (! $this->hasActiveSubscription()) { |
|
122
|
|
|
throw new SubscriptionErrorException('You need a subscription for upgrade to other.'); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($plan->hasManyIntervals() && $interval == null) { |
|
126
|
|
|
throw new SubscriptionErrorException('The plan has many intervals, please indicate a interval.'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
130
|
|
|
$currentPlan = $currentSubscription->plan; |
|
131
|
|
|
$currentIntervalPrice = $currentPlan->isFree() ? 0.00 : $currentPlan->getInterval()->getPrice(); |
|
132
|
|
|
|
|
133
|
|
|
$toInterval = $plan->getInterval(); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
if ($currentPlan->id == $plan->id) { |
|
|
|
|
|
|
136
|
|
|
throw new SubscriptionErrorException('You can\'t change to same plan. You need change to other plan.'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ($interval !== null) { |
|
140
|
|
|
$toInterval = $interval; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if ($currentIntervalPrice < $toInterval->getPrice()) { |
|
144
|
|
|
return $this->upgradeTo($toInterval); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->downgradeTo($toInterval); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function hasActiveSubscription(): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->subscriptions() |
|
153
|
|
|
->current() |
|
154
|
|
|
->exists(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
protected function upgradeTo(PlanIntervalContract $interval): SubscriptionContact |
|
158
|
|
|
{ |
|
159
|
|
|
if (! $this->hasActiveSubscription()) { |
|
160
|
|
|
throw new SubscriptionErrorException('You need a subscription for upgrade to other.'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$this->forceUnsubscribe(); |
|
164
|
|
|
|
|
165
|
|
|
return $this->subscribeToInterval($interval); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function forceUnsubscribe() |
|
169
|
|
|
{ |
|
170
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
171
|
|
|
$currentSubscription->end_at = now()->subSecond(); |
|
172
|
|
|
$currentSubscription->cancelled_at = now(); |
|
173
|
|
|
$currentSubscription->save(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
protected function downgradeTo(PlanIntervalContract $interval): SubscriptionContact |
|
177
|
|
|
{ |
|
178
|
|
|
if (! $this->hasActiveSubscription()) { |
|
179
|
|
|
throw new SubscriptionErrorException('You need a subscription for upgrade to other.'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this->subscribeToInterval($interval); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function renewSubscription(PlanIntervalContract $interval = null) |
|
186
|
|
|
{ |
|
187
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
188
|
|
|
|
|
189
|
|
|
if ($interval === null) { |
|
190
|
|
|
$plan = $currentSubscription->plan; |
|
191
|
|
|
|
|
192
|
|
|
if ($plan->hasManyIntervals()) { |
|
193
|
|
|
throw new SubscriptionErrorException( |
|
194
|
|
|
'The plan you want will subscribe has many intervals, please consider renew to a interval of plan' |
|
195
|
|
|
); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$interval = $plan->intervals()->first(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
$newExpireDate = $this->calculateExpireDate($currentSubscription->end_at, $interval); |
|
202
|
|
|
|
|
203
|
|
|
$currentSubscription->end_at = $newExpireDate; |
|
204
|
|
|
$currentSubscription->save(); |
|
205
|
|
|
|
|
206
|
|
|
return $currentSubscription; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
public function unsubscribe() |
|
210
|
|
|
{ |
|
211
|
|
|
$currentSubscription = $this->getActiveSubscription(); |
|
212
|
|
|
$currentSubscription->cancelled_at = now(); |
|
213
|
|
|
$currentSubscription->save(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getConsumables() |
|
217
|
|
|
{ |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.