-
Notifications
You must be signed in to change notification settings - Fork 2
/
SubscriptionBuilder.php
245 lines (214 loc) · 5.61 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
<?php
namespace yii2mod\braintree;
use Braintree\Subscription as BraintreeSubscription;
use Carbon\Carbon;
use Yii;
use yii\base\Exception;
use yii2mod\braintree\models\SubscriptionModel;
/**
* Class SubscriptionBuilder
*
* @package yii2mod\braintree
*/
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 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;
/**
* 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 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;
}
/**
* Add a new Braintree subscription to the user.
*
* @param array $options
*
* @return SubscriptionModel
*/
public function add(array $options = [])
{
return $this->create(null, $options);
}
/**
* Create a new Braintree subscription.
*
* @param string|null $token
* @param array $customerOptions
* @param array $subscriptionOptions
*
* @return SubscriptionModel
*
* @throws Exception
*/
public function create($token = null, array $customerOptions = [], array $subscriptionOptions = [])
{
$payload = $this->getSubscriptionPayload(
$this->getBraintreeCustomer($token, $customerOptions), $subscriptionOptions
);
if ($this->coupon) {
$payload = $this->addCouponToPayload($payload);
}
$response = BraintreeSubscription::create($payload);
if (!$response->success) {
throw new Exception('Braintree failed to create subscription: ' . $response->message);
}
if ($this->skipTrial) {
$trialEndsAt = null;
} else {
$trialEndsAt = $this->trialDays ? Carbon::now()->addDays($this->trialDays) : null;
}
$subscriptionModel = Yii::createObject([
'class' => SubscriptionModel::className(),
'userId' => $this->user->id,
'name' => $this->name,
'braintreeId' => $response->subscription->id,
'braintreePlan' => $this->plan,
'trialEndAt' => $trialEndsAt,
'endAt' => null,
]);
if ($subscriptionModel->save()) {
return $subscriptionModel;
} else {
throw new Exception('Subscription was not saved.');
}
}
/**
* Get the base subscription payload for Braintree.
*
* @param \Braintree\Customer $customer
* @param array $options
*
* @return array
*/
protected function getSubscriptionPayload($customer, array $options = [])
{
$plan = BraintreeService::findPlan($this->plan);
if ($this->skipTrial) {
$trialDuration = 0;
} else {
$trialDuration = $this->trialDays ?: 0;
}
return array_merge([
'planId' => $this->plan,
'price' => $plan->price * (1 + ($this->user->taxPercentage() / 100)),
'paymentMethodToken' => $customer->paymentMethods[0]->token,
'trialPeriod' => $this->trialDays && !$this->skipTrial ? true : false,
'trialDurationUnit' => 'day',
'trialDuration' => $trialDuration,
], $options);
}
/**
* Add the coupon discount to the Braintree payload.
*
* @param array $payload
*
* @return array
*/
protected function addCouponToPayload(array $payload)
{
if (!isset($payload['discounts']['add'])) {
$payload['discounts']['add'] = [];
}
$payload['discounts']['add'][] = [
'inheritedFromId' => $this->coupon,
];
return $payload;
}
/**
* Get the Braintree customer instance for the current user and token.
*
* @param string|null $token
* @param array $options
*
* @return \Braintree\Customer
*/
protected function getBraintreeCustomer($token = null, array $options = [])
{
if (!$this->user->braintreeId) {
$customer = $this->user->createAsBraintreeCustomer($token, $options);
} else {
$customer = $this->user->asBraintreeCustomer();
if ($token) {
$this->user->updateCard($token);
}
}
return $customer;
}
}