-
Notifications
You must be signed in to change notification settings - Fork 659
/
ManagesSubscriptions.php
192 lines (167 loc) · 4.51 KB
/
ManagesSubscriptions.php
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
<?php
namespace Laravel\Cashier\Concerns;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Subscription;
use Laravel\Cashier\SubscriptionBuilder;
trait ManagesSubscriptions
{
/**
* Begin creating a new subscription.
*
* @param string $name
* @param string|string[] $plans
* @return \Laravel\Cashier\SubscriptionBuilder
*/
public function newSubscription($name, $plans)
{
return new SubscriptionBuilder($this, $name, $plans);
}
/**
* Determine if the Stripe model is on trial.
*
* @param string $name
* @param string|null $plan
* @return bool
*/
public function onTrial($name = 'default', $plan = null)
{
if (func_num_args() === 0 && $this->onGenericTrial()) {
return true;
}
$subscription = $this->subscription($name);
if (! $subscription || ! $subscription->onTrial()) {
return false;
}
return $plan ? $subscription->hasPlan($plan) : true;
}
/**
* Determine if the Stripe model is on a "generic" trial at the model level.
*
* @return bool
*/
public function onGenericTrial()
{
return $this->trial_ends_at && $this->trial_ends_at->isFuture();
}
/**
* Get the ending date of the trial.
*
* @param string $name
* @return \Illuminate\Support\Carbon|null
*/
public function trialEndsAt($name = 'default')
{
if ($subscription = $this->subscription($name)) {
return $subscription->trial_ends_at;
}
return $this->trial_ends_at;
}
/**
* Determine if the Stripe model has a given subscription.
*
* @param string $name
* @param string|null $plan
* @return bool
*/
public function subscribed($name = 'default', $plan = null)
{
$subscription = $this->subscription($name);
if (! $subscription || ! $subscription->valid()) {
return false;
}
return $plan ? $subscription->hasPlan($plan) : true;
}
/**
* Get a subscription instance by name.
*
* @param string $name
* @return \Laravel\Cashier\Subscription|null
*/
public function subscription($name = 'default')
{
return $this->subscriptions->where('name', $name)->first();
}
/**
* Get all of the subscriptions for the Stripe model.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subscriptions()
{
return $this->hasMany(Cashier::$subscriptionModel, $this->getForeignKey())->orderBy('created_at', 'desc');
}
/**
* Determine if the customer's subscription has an incomplete payment.
*
* @param string $name
* @return bool
*/
public function hasIncompletePayment($name = 'default')
{
if ($subscription = $this->subscription($name)) {
return $subscription->hasIncompletePayment();
}
return false;
}
/**
* Determine if the Stripe model is actively subscribed to one of the given plans.
*
* @param string|string[] $plans
* @param string $name
* @return bool
*/
public function subscribedToPlan($plans, $name = 'default')
{
$subscription = $this->subscription($name);
if (! $subscription || ! $subscription->valid()) {
return false;
}
foreach ((array) $plans as $plan) {
if ($subscription->hasPlan($plan)) {
return true;
}
}
return false;
}
/**
* Determine if the customer has a valid subscription on the given plan.
*
* @param string $plan
* @return bool
*/
public function onPlan($plan)
{
return ! is_null($this->subscriptions->first(function (Subscription $subscription) use ($plan) {
return $subscription->valid() && $subscription->hasPlan($plan);
}));
}
/**
* Get the tax percentage to apply to the subscription.
*
* @return int|float
*
* @deprecated Please migrate to the new Tax Rates API.
*/
public function taxPercentage()
{
return 0;
}
/**
* Get the tax rates to apply to the subscription.
*
* @return array
*/
public function taxRates()
{
return [];
}
/**
* Get the tax rates to apply to individual subscription items.
*
* @return array
*/
public function planTaxRates()
{
return [];
}
}