-
Notifications
You must be signed in to change notification settings - Fork 25
/
SubscriptionBuilder.php
268 lines (240 loc) · 5.67 KB
/
SubscriptionBuilder.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
namespace yii2mod\cashier;
use Carbon\Carbon;
use yii\base\Exception;
use yii2mod\cashier\models\SubscriptionModel;
/**
* Class SubscriptionBuilder
*
* @package yii2mod\cashier
*/
class SubscriptionBuilder
{
/**
* The user model that is subscribing.
*
* @var \yii\db\ActiveRecord
*/
protected $user;
/**
* The name of the subscription.
*
* @var string
*/
protected $name;
/**
* The name of the plan being subscribed to.
*
* @var string
*/
protected $plan;
/**
* The quantity of the subscription.
*
* @var int
*/
protected $quantity = 1;
/**
* The number of trial days to apply to the subscription.
*
* @var int|null
*/
protected $trialDays;
/**
* Indicates that the trial should end immediately.
*
* @var bool
*/
protected $skipTrial = false;
/**
* The coupon code being applied to the customer.
*
* @var string|null
*/
protected $coupon;
/**
* The metadata to apply to the subscription.
*
* @var array|null
*/
protected $metadata;
/**
* Create a new subscription builder instance.
*
* @param mixed $user
* @param string $name
* @param string $plan
*/
public function __construct($user, $name, $plan)
{
$this->user = $user;
$this->name = $name;
$this->plan = $plan;
}
/**
* Specify the quantity of the subscription.
*
* @param int $quantity
*
* @return $this
*/
public function quantity($quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Specify the ending date of the trial.
*
* @param int $trialDays
*
* @return $this
*/
public function trialDays($trialDays)
{
$this->trialDays = $trialDays;
return $this;
}
/**
* Force the trial to end immediately.
*
* @return $this
*/
public function skipTrial()
{
$this->skipTrial = true;
return $this;
}
/**
* The coupon to apply to a new subscription.
*
* @param string $coupon
*
* @return $this
*/
public function withCoupon($coupon)
{
$this->coupon = $coupon;
return $this;
}
/**
* The metadata to apply to a new subscription.
*
* @param array $metadata
*
* @return $this
*/
public function withMetadata($metadata)
{
$this->metadata = $metadata;
return $this;
}
/**
* Add a new Stripe subscription to the user.
*
* @param array $options
*
* @return SubscriptionModel
*/
public function add(array $options = [])
{
return $this->create(null, $options);
}
/**
* Create a new Stripe subscription.
*
* @param string|null $token
* @param array $options
*
* @return SubscriptionModel
*
* @throws Exception
*/
public function create($token = null, array $options = [])
{
$customer = $this->getStripeCustomer($token, $options);
$subscription = $customer->subscriptions->create($this->buildPayload());
if ($this->skipTrial) {
$trialEndsAt = null;
} else {
$trialEndsAt = $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null;
}
$subscriptionModel = new SubscriptionModel([
'user_id' => $this->user->id,
'name' => $this->name,
'stripe_id' => $subscription->id,
'stripe_plan' => $this->plan,
'quantity' => $this->quantity,
'trial_ends_at' => $trialEndsAt,
'ends_at' => null,
]);
if ($subscriptionModel->save()) {
return $subscriptionModel;
} else {
throw new Exception('Subscription was not saved.');
}
}
/**
* Get the Stripe customer instance for the current user and token.
*
* @param string|null $token
* @param array $options
*
* @return \Stripe\Customer
*/
protected function getStripeCustomer($token = null, array $options = [])
{
if (!$this->user->stripe_id) {
$customer = $this->user->createAsStripeCustomer(
$token, array_merge($options, array_filter(['coupon' => $this->coupon]))
);
} else {
$customer = $this->user->asStripeCustomer();
if ($token) {
$this->user->updateCard($token);
}
}
return $customer;
}
/**
* Build the payload for subscription creation.
*
* @return array
*/
protected function buildPayload()
{
return array_filter([
'plan' => $this->plan,
'quantity' => $this->quantity,
'coupon' => $this->coupon,
'trial_end' => $this->getTrialEndForPayload(),
'tax_percent' => $this->getTaxPercentageForPayload(),
'metadata' => $this->metadata,
]);
}
/**
* Get the trial ending date for the Stripe payload.
*
* @return int|null
*/
protected function getTrialEndForPayload()
{
if ($this->skipTrial) {
return 'now';
}
if ($this->trialDays) {
return Carbon::now()->addDays($this->trialDays)->getTimestamp();
}
}
/**
* Get the tax percentage for the Stripe payload.
*
* @return int|null
*/
protected function getTaxPercentageForPayload()
{
if ($taxPercentage = $this->user->taxPercentage()) {
return $taxPercentage;
}
}
}