forked from mollie/laravel-cashier-mollie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartSubscription.php
More file actions
344 lines (292 loc) · 10 KB
/
Copy pathStartSubscription.php
File metadata and controls
344 lines (292 loc) · 10 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
namespace Laravel\Cashier\FirstPayment\Actions;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Coupon\Contracts\CouponRepository;
use Laravel\Cashier\Order\OrderItemCollection;
use Laravel\Cashier\Plan\Contracts\PlanRepository;
use Laravel\Cashier\SubscriptionBuilder\Contracts\SubscriptionConfigurator;
use Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder;
use Money\Currency;
use Money\Money;
class StartSubscription extends BaseAction implements SubscriptionConfigurator
{
/** @var string */
protected $name;
/** @var \Laravel\Cashier\Plan\Plan */
protected $plan;
/** @var \Laravel\Cashier\Coupon\Coupon */
protected $coupon;
/** @var \Carbon\Carbon */
protected $nextPaymentAt;
/** @var null|int */
protected $trialDays;
/** @var null|\Carbon\Carbon */
protected $trialUntil;
/** @var bool */
protected $skipTrial;
/** @var null|\Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder */
protected $builder;
/** @var CouponRepository */
protected $couponRepository;
/**
* Create a new subscription builder instance.
*
* @param \Illuminate\Database\Eloquent\Model $owner
* @param string $name
* @param string $plan
*
* @throws \Laravel\Cashier\Exceptions\PlanNotFoundException
*/
public function __construct(Model $owner, string $name, string $plan)
{
$this->owner = $owner;
$this->taxPercentage = $this->owner->taxPercentage();
$this->name = $name;
$this->plan = app(PlanRepository::class)::findOrFail($plan);
$this->unitPrice = $this->plan->amount();
$this->description = $this->plan->description();
$this->currency = $this->unitPrice->getCurrency()->getCode();
$this->couponRepository = app()->make(CouponRepository::class);
}
/**
* @param array $payload
* @param \Illuminate\Database\Eloquent\Model $owner
* @return static
*
* @throws \Exception
*/
public static function createFromPayload(array $payload, Model $owner)
{
$action = new static($owner, $payload['name'], $payload['plan']);
// Already validated when preparing the first payment, so don't validate again
$action->builder()->skipCouponValidation();
// The coupon will be handled manually by this action
$action->builder()->skipCouponHandling();
if (isset($payload['taxPercentage'])) {
$action->withTaxPercentage($payload['taxPercentage']);
}
if (isset($payload['trialUntil'])) {
$action->trialUntil(Carbon::parse($payload['trialUntil']));
}
if (isset($payload['nextPaymentAt'])) {
$action->nextPaymentAt(Carbon::parse($payload['nextPaymentAt']));
}
if (isset($payload['trialDays'])) {
$action->trialDays($payload['trialDays']);
}
if (isset($payload['skipTrial']) && $payload['skipTrial']) {
$action->skipTrial();
}
if (isset($payload['quantity'])) {
$action->quantity($payload['quantity']);
}
if (isset($payload['coupon'])) {
$action->withCoupon($payload['coupon']);
}
return $action;
}
/**
* @return array
*/
public function getPayload()
{
return array_filter([
'handler' => self::class,
'description' => $this->getDescription(),
'subtotal' => money_to_mollie_array($this->getSubtotal()),
'taxPercentage' => $this->getTaxPercentage(),
'plan' => $this->plan->name(),
'name' => $this->name,
'trialExpires' => ! empty($this->trialExpires) ? $this->trialExpires->toIso8601String() : null,
'quantity' => ! empty($this->quantity) ? $this->quantity : null,
'nextPaymentAt' => ! empty($this->nextPaymentAt) ? $this->nextPaymentAt->toIso8601String() : null,
'trialDays' => $this->trialDays,
'trialUntil' => ! empty($this->trialUntil) ? $this->trialUntil->toIso8601String() : null,
'skipTrial' => $this->skipTrial,
'coupon' => ! empty($this->coupon) ? $this->coupon->name() : null,
]);
}
/**
* Prepare a stub of OrderItems processed with the payment.
*
* @return \Laravel\Cashier\Order\OrderItemCollection
*/
public function makeProcessedOrderItems()
{
return Cashier::$orderItemModel::make($this->processedOrderItemData())->toCollection();
}
/**
* @return array
*/
protected function processedOrderItemData()
{
return [
'owner_type' => $this->owner->getMorphClass(),
'owner_id' => $this->owner->getKey(),
'process_at' => now(),
'description' => $this->getDescription(),
'description_extra_lines' => [
trans('cashier::order_item.cycle', [
'from' => now()->format('Y-m-d'),
'to' => $this->plan->interval()->getEndOfNextSubscriptionCycle()->format('Y-m-d'),
]),
],
'currency' => $this->getCurrency(),
'unit_price' => $this->getUnitPrice()->getAmount(),
'tax_percentage' => $this->getTaxPercentage(),
'quantity' => $this->quantity,
];
}
/**
* Returns an OrderItemCollection ready for processing right away.
* Another OrderItem is scheduled for the next billing cycle.
*
* @return \Laravel\Cashier\Order\OrderItemCollection
*
* @throws \Laravel\Cashier\Exceptions\PlanNotFoundException
* @throws \Throwable
*/
public function execute()
{
if (empty($this->nextPaymentAt) && ! $this->isTrial()) {
$this->builder()->nextPaymentAt($this->plan->interval()->getEndOfNextSubscriptionCycle());
}
// The first payment has already been collected by Mollie, so the mandate
// is guaranteed to finalize even when it is still pending at this point
// (e.g. PayPal or Belfius, where finalization can take up to 72h and
// exceeds Mollie's webhook retry window). See issue #289.
$this->builder()->acceptPendingMandate();
// Create the subscription, scheduling the next payment
$subscription = $this->builder()->create();
// Create an additional OrderItem for the already processed payment
/** @var OrderItemCollection $processedItems */
$processedItems = $subscription->orderItems()
->create($this->processedOrderItemData())
->toCollection();
if ($this->coupon) {
$redeemedCoupon = Cashier::$redeemedCouponModel::record($this->coupon, $subscription);
if (! $this->isTrial()) {
$processedItems = $this->coupon->applyTo($redeemedCoupon, $processedItems);
}
}
return $processedItems;
}
/**
* Specify the number of days of the trial.
*
* @param int $trialDays
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\PlanNotFoundException
* @throws \Throwable
*/
public function trialDays(int $trialDays)
{
$this->trialDays = $trialDays;
$this->builder()->trialDays($trialDays);
$this->unitPrice = new Money(0, new Currency($this->getCurrency()));
return $this;
}
/**
* Specify the ending date of the trial.
*
* @param Carbon $trialUntil
* @return $this
*/
public function trialUntil(Carbon $trialUntil)
{
$this->trialUntil = $trialUntil;
$this->builder()->trialUntil($trialUntil);
$this->unitPrice = new Money(0, new Currency($this->getCurrency()));
return $this;
}
/**
* Force the trial to end immediately.
*
* @return $this
*/
public function skipTrial()
{
$this->skipTrial = true;
$this->trialUntil = null;
$this->builder()->skipTrial();
$this->unitPrice = $this->plan->amount();
return $this;
}
/**
* Specify the quantity of the subscription.
*
* @param int $quantity
* @return $this
*
* @throws \Throwable|\LogicException
*/
public function quantity(int $quantity)
{
throw_if($quantity < 1, new \LogicException('Subscription quantity must be at least 1.'));
$this->quantity = $quantity;
$this->builder()->quantity($quantity);
return $this;
}
/**
* @return \Laravel\Cashier\Coupon\Coupon|null
*/
public function coupon()
{
return $this->coupon;
}
/**
* Specify and validate the coupon code.
*
* @param string $coupon
* @return $this
*
* @throws \Laravel\Cashier\Exceptions\CouponNotFoundException
* @throws \Throwable
*/
public function withCoupon(string $coupon)
{
$this->coupon = $this->couponRepository->findOrFail($coupon);
$this->builder()->withCoupon($coupon);
return $this;
}
/**
* Override the default next payment date.
*
* @param \Carbon\Carbon $nextPaymentAt
* @return $this
*/
public function nextPaymentAt(Carbon $nextPaymentAt)
{
$this->nextPaymentAt = $nextPaymentAt;
$this->builder()->nextPaymentAt($nextPaymentAt);
return $this;
}
/**
* @return bool
*/
protected function isTrial()
{
return ! (empty($this->trialDays) && empty($this->trialUntil));
}
/**
* Retrieve the subscription builder
*
* @return \Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder
*
* @throws \Throwable|\Laravel\Cashier\Exceptions\PlanNotFoundException
*/
public function builder()
{
if ($this->builder === null) {
$this->builder = new MandatedSubscriptionBuilder(
$this->owner,
$this->name,
$this->plan->name()
);
}
return $this->builder;
}
}