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