-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathSubscriptionTrait.php
More file actions
235 lines (207 loc) · 5.05 KB
/
SubscriptionTrait.php
File metadata and controls
235 lines (207 loc) · 5.05 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
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
230
231
232
233
234
235
<?php
namespace Codecasts\Support\Subscription;
/**
* Class SubscriptionTrait.
*/
trait SubscriptionTrait
{
/**
* Valid Subscription plans.
*
* @todo move plans to configuration
*
* @return array List of valid plan identifiers
*/
public function validPlans()
{
return ['P1M', 'P3M', 'P6M', 'P1Y'];
}
/**
* If a user has a customer id already.
*
* @return bool
*/
public function hasCustomerId()
{
return $this->customer_id ? true : false;
}
/**
* If a user has a subscription id already.
*
* @return bool
*/
public function hasSubscriptionId()
{
return $this->subscription_id ? true : false;
}
/**
* Is the user a guest?
*
* @return bool
*/
public function isGuest()
{
return $this->guest_until >= date('Y-m-d');
}
/**
* Is the user Admin?
*
* @return bool
*/
public function isAdmin()
{
return $this->admin ? true : false;
}
/**
* Is the user Active?
*
* @return bool
*/
public function isActive()
{
return $this->subscription_active ? true : false;
}
/**
* Is the user Suspended?
*
* @return bool
*/
public function isSuspended()
{
return $this->subscription_suspended ? true : false;
}
/**
* Is the User on a paid plan?
*
* @return bool
*/
public function hasPaidPlan()
{
return in_array($this->subscription_plan, $this->validPlans());
}
/**
* Is the user's subscription expired?
*
* @return bool
*/
public function isExpired()
{
return $this->expires_at <= date('Y-m-d');
}
/**
* Create the customer id if he don't have one.
*
* @return string
*/
public function createCustomerId()
{
if (!$this->hasCustomerId()) {
$customer = \Iugu_Customer::create([
'email' => $this->email,
'name' => $this->name,
'notes' => $this->id.'-'.$this->username,
]);
if ($customer) {
$this->customer_id = $customer->id;
$this->save();
}
}
return $this->customer_id;
}
/**
* Subscribe the user into a plan.
*
* @param $plan The plan's name
*
* @return string The ID of created subscription
*/
public function subscribe($plan)
{
if (!$this->hasSubscriptionId()) {
if (!$this->hasCustomerId()) {
$this->createCustomerId();
}
if (in_array($plan, $this->validPlans())) {
$subscription = \Iugu_Subscription::create([
'plan_identifier' => $plan,
'customer_id' => $this->customer_id,
]);
if ($subscription) {
$this->subscription_id = $subscription->id;
}
$this->save();
}
}
return $this->subscription_id;
}
/**
* Load the subscription data from Iugu.
*
* @return \Iugu_SearchResult|null|void
*/
public function loadSubscription()
{
if ($this->hasSubscriptionId()) {
$subscription = \Iugu_Subscription::fetch($this->subscription_id);
if ($subscription) {
if ($subscription->expires_at) {
$this->expires_at = $subscription->expires_at;
}
$this->subscription_plan = $subscription->plan_identifier;
$this->subscription_active = $subscription->active;
$this->subscription_suspended = $subscription->suspended;
}
$this->save();
return $subscription;
}
}
/**
* Change User's plan.
*
* @param string $plan New plan identifier
*
* @return \Iugu_SearchResult|null|void
*/
public function changePlan($plan)
{
if ($this->hasSubscriptionId() && in_array($plan, $this->validPlans())) {
$subscription = $this->loadSubscription();
$subscription->change_plan($plan);
}
return $this->loadSubscription();
}
/**
* Suspend a user subscription.
*/
public function suspend()
{
$subscription = $this->loadSubscription();
if ($this->isActive()) {
$subscription->suspend();
}
}
/**
* Active a user subscription.
*/
public function activate()
{
$subscription = $this->loadSubscription();
if ($this->isSuspended()) {
$subscription->activate();
}
}
/**
* Check for the current subscription status.
*
* @return bool
*/
public function subscribed()
{
if (!$this->isExpired()) {
return true;
} elseif ($this->isExpired() && ($this->isAdmin() || $this->isGuest())) {
return true;
}
return false;
}
}