forked from mollie/laravel-cashier-mollie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMandatedSubscriptionBuilder.php
More file actions
264 lines (226 loc) · 6.39 KB
/
Copy pathMandatedSubscriptionBuilder.php
File metadata and controls
264 lines (226 loc) · 6.39 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
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
<?php
namespace Laravel\Cashier\SubscriptionBuilder;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Laravel\Cashier\Coupon\Contracts\CouponRepository;
use Laravel\Cashier\Plan\Contracts\PlanRepository;
use Laravel\Cashier\Subscription;
use Laravel\Cashier\SubscriptionBuilder\Contracts\SubscriptionBuilder as Contract;
/**
* Creates and configures a subscription for an existing Mollie Mandate.
*/
class MandatedSubscriptionBuilder implements Contract
{
/**
* The model that is subscribing.
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $owner;
/**
* The name of the subscription.
*
* @var string
*/
protected $name;
/**
* The quantity of the subscription.
*
* @var int
*/
protected $quantity = 1;
/**
* The date and time the trial will expire.
*
* @var Carbon
*/
protected $trialExpires;
/**
* When the first (next) payment should be processed once the subscription has been created.
*
* @var Carbon
*/
protected $nextPaymentAt;
/**
* The Plan being subscribed to.
*
* @var \Laravel\Cashier\Plan\Plan
*/
protected $plan;
/** @var \Laravel\Cashier\Coupon\Coupon */
protected $coupon;
/** @var bool */
protected $handleCoupon = true;
/** @var bool */
protected $validateCoupon = true;
/** @var bool */
protected $pendingMandateAccepted = false;
/**
* Create a new subscription builder instance.
*
* @param mixed $owner
* @param string $name
* @param string $plan
*
* @throws \Laravel\Cashier\Exceptions\PlanNotFoundException
*/
public function __construct(Model $owner, string $name, string $plan)
{
$this->name = $name;
$this->owner = $owner;
$this->nextPaymentAt = Carbon::now();
$this->plan = app(PlanRepository::class)::findOrFail($plan);
}
/**
* Create a new Cashier subscription.
*
* @return Subscription
* \Laravel\Cashier\Exceptions\CouponException
*
* @throws \Laravel\Cashier\Exceptions\InvalidMandateException
*/
public function create()
{
$this->owner->guardMollieMandate($this->pendingMandateAccepted);
$now = now();
return DB::transaction(function () use ($now) {
$subscription = $this->makeSubscription($now);
$subscription->save();
if ($this->coupon) {
if ($this->validateCoupon) {
$this->coupon->validateFor($subscription);
if ($this->handleCoupon) {
$this->coupon->redeemFor($subscription);
}
}
}
$subscription->scheduleNewOrderItemAt($this->nextPaymentAt);
$subscription->save();
$this->owner->cancelGenericTrial();
return $subscription;
});
}
/**
* Prepare a not yet persisted Subscription model
*
* @param null|Carbon $now
* @return Subscription $subscription
*/
public function makeSubscription($now = null)
{
return $this->owner->subscriptions()->make([
'name' => $this->name,
'plan' => $this->plan->name(),
'quantity' => $this->quantity,
'tax_percentage' => $this->owner->taxPercentage() ?: 0,
'trial_ends_at' => $this->trialExpires,
'cycle_started_at' => $now ?: now(),
'cycle_ends_at' => $this->nextPaymentAt,
]);
}
/**
* Specify the number of days of the trial.
*
* @param int $trialDays
* @return $this
*/
public function trialDays(int $trialDays)
{
return $this->trialUntil(now()->addDays($trialDays));
}
/**
* Specify the ending date of the trial.
*
* @param Carbon $trialUntil
* @return $this
*/
public function trialUntil(Carbon $trialUntil)
{
$this->trialExpires = $trialUntil;
$this->nextPaymentAt = $trialUntil;
return $this;
}
/**
* Force the trial to end immediately.
*
* @return \Laravel\Cashier\SubscriptionBuilder\Contracts\SubscriptionBuilder|void
*/
public function skipTrial()
{
$this->trialExpires = null;
$this->nextPaymentAt = now();
return $this;
}
/**
* Specify the quantity of the subscription.
*
* @param int $quantity
* @return $this
*/
public function quantity(int $quantity)
{
$this->quantity = $quantity;
return $this;
}
/**
* Specify a coupon.
*
* @param string $coupon
* @return $this|\Laravel\Cashier\SubscriptionBuilder\Contracts\SubscriptionBuilder
*
* @throws \Laravel\Cashier\Exceptions\CouponNotFoundException
*/
public function withCoupon(string $coupon)
{
/** @var CouponRepository $repository */
$repository = app()->make(CouponRepository::class);
$this->coupon = $repository->findOrFail($coupon);
return $this;
}
/**
* Override the default next payment date. This is superseded by the trial end date.
*
* @param \Carbon\Carbon $nextPaymentAt
* @return MandatedSubscriptionBuilder
*/
public function nextPaymentAt(Carbon $nextPaymentAt)
{
$this->nextPaymentAt = $nextPaymentAt;
return $this;
}
/**
* Skip validating the coupon when creating the subscription.
*
* @return $this
*/
public function skipCouponValidation()
{
$this->validateCoupon = false;
return $this;
}
/**
* Skip handling the coupon completely when creating the subscription.
*
* @return $this
*/
public function skipCouponHandling()
{
$this->handleCoupon = false;
return $this;
}
/**
* Treat a pending Mollie mandate as valid when creating the subscription.
*
* Used by the first-payment flow: Mollie has already collected the payment
* and guarantees the mandate will finalize, even when finalization takes
* longer than the webhook retry window (PayPal, Belfius). See issue #289.
*
* @return $this
*/
public function acceptPendingMandate(bool $accept = true)
{
$this->pendingMandateAccepted = $accept;
return $this;
}
}