8
8
use Illuminate \Database \Eloquent \Model ;
9
9
use Stripe \Subscription as StripeSubscription ;
10
10
use Laravel \Cashier \Exceptions \IncompletePayment ;
11
+ use Laravel \Cashier \Exceptions \SubscriptionUpdateFailure ;
11
12
12
13
class Subscription extends Model
13
14
{
@@ -81,7 +82,7 @@ public function valid()
81
82
*/
82
83
public function incomplete ()
83
84
{
84
- return $ this ->status === 'incomplete ' ;
85
+ return $ this ->stripe_status === 'incomplete ' ;
85
86
}
86
87
87
88
/**
@@ -92,17 +93,28 @@ public function incomplete()
92
93
*/
93
94
public function scopeIncomplete ($ query )
94
95
{
95
- $ query ->where ('status ' , 'incomplete ' );
96
+ $ query ->where ('stripe_status ' , 'incomplete ' );
96
97
}
97
98
98
99
/**
99
- * Mark the subscription as incomplete .
100
+ * Determine if the subscription is past due .
100
101
*
102
+ * @return bool
103
+ */
104
+ public function pastDue ()
105
+ {
106
+ return $ this ->stripe_status === 'past_due ' ;
107
+ }
108
+
109
+ /**
110
+ * Filter query by past due.
111
+ *
112
+ * @param \Illuminate\Database\Eloquent\Builder $query
101
113
* @return void
102
114
*/
103
- public function markAsIncomplete ( )
115
+ public function scopePastDue ( $ query )
104
116
{
105
- $ this -> fill ([ ' status ' => ' incomplete ' ])-> save ( );
117
+ $ query -> where ( ' stripe_status ' , ' past_due ' );
106
118
}
107
119
108
120
/**
@@ -124,22 +136,12 @@ public function active()
124
136
public function scopeActive ($ query )
125
137
{
126
138
$ query ->whereNull ('ends_at ' )
127
- ->where ('status ' , '!= ' , 'incomplete ' )
139
+ ->where ('stripe_status ' , '!= ' , 'incomplete ' )
128
140
->orWhere (function ($ query ) {
129
141
$ query ->onGracePeriod ();
130
142
});
131
143
}
132
144
133
- /**
134
- * Mark the subscription as active.
135
- *
136
- * @return void
137
- */
138
- public function markAsActive ()
139
- {
140
- $ this ->fill (['status ' => 'active ' ])->save ();
141
- }
142
-
143
145
/**
144
146
* Determine if the subscription is recurring and not on trial.
145
147
*
@@ -325,9 +327,15 @@ public function decrementQuantity($count = 1)
325
327
* @param int $quantity
326
328
* @param \Stripe\Customer|null $customer
327
329
* @return $this
330
+ *
331
+ * @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
328
332
*/
329
333
public function updateQuantity ($ quantity , $ customer = null )
330
334
{
335
+ if ($ this ->incomplete ()) {
336
+ throw SubscriptionUpdateFailure::incompleteSubscription ($ this );
337
+ }
338
+
331
339
$ subscription = $ this ->asStripeSubscription ();
332
340
333
341
$ subscription ->quantity = $ quantity ;
@@ -394,9 +402,14 @@ public function skipTrial()
394
402
* @return $this
395
403
*
396
404
* @throws \Laravel\Cashier\Exceptions\IncompletePayment
405
+ * @throws \Laravel\Cashier\Exceptions\SubscriptionUpdateFailure
397
406
*/
398
407
public function swap ($ plan , $ options = [])
399
408
{
409
+ if ($ this ->incomplete ()) {
410
+ throw SubscriptionUpdateFailure::incompleteSubscription ($ this );
411
+ }
412
+
400
413
$ subscription = $ this ->asStripeSubscription ();
401
414
402
415
$ subscription ->plan = $ plan ;
@@ -429,7 +442,7 @@ public function swap($plan, $options = [])
429
442
$ subscription ->quantity = $ this ->quantity ;
430
443
}
431
444
432
- $ subscription ->save ();
445
+ $ subscription = $ subscription ->save ();
433
446
434
447
$ this ->fill ([
435
448
'stripe_plan ' => $ plan ,
@@ -439,7 +452,9 @@ public function swap($plan, $options = [])
439
452
try {
440
453
$ this ->user ->invoice (['subscription ' => $ subscription ->id ]);
441
454
} catch (IncompletePayment $ exception ) {
442
- $ this ->markAsIncomplete ();
455
+ $ this ->fill ([
456
+ 'stripe_status ' => $ exception ->payment ->invoice ->subscription ->status ,
457
+ ])->save ();
443
458
444
459
throw $ exception ;
445
460
}
@@ -458,7 +473,9 @@ public function cancel()
458
473
459
474
$ subscription ->cancel_at_period_end = true ;
460
475
461
- $ subscription ->save ();
476
+ $ subscription = $ subscription ->save ();
477
+
478
+ $ this ->stripe_status = $ subscription ->status ;
462
479
463
480
// If the user was on trial, we will set the grace period to end when the trial
464
481
// would have ended. Otherwise, we'll retrieve the end of the billing period
@@ -499,7 +516,10 @@ public function cancelNow()
499
516
*/
500
517
public function markAsCancelled ()
501
518
{
502
- $ this ->fill (['ends_at ' => Carbon::now ()])->save ();
519
+ $ this ->fill ([
520
+ 'stripe_status ' => 'canceled ' ,
521
+ 'ends_at ' => Carbon::now (),
522
+ ])->save ();
503
523
}
504
524
505
525
/**
@@ -529,12 +549,15 @@ public function resume()
529
549
$ subscription ->trial_end = 'now ' ;
530
550
}
531
551
532
- $ subscription ->save ();
552
+ $ subscription = $ subscription ->save ();
533
553
534
554
// Finally, we will remove the ending timestamp from the user's record in the
535
555
// local database to indicate that the subscription is active again and is
536
556
// no longer "cancelled". Then we will save this record in the database.
537
- $ this ->fill (['ends_at ' => null ])->save ();
557
+ $ this ->fill ([
558
+ 'stripe_status ' => $ subscription ->status ,
559
+ 'ends_at ' => null ,
560
+ ])->save ();
538
561
539
562
return $ this ;
540
563
}
@@ -553,6 +576,16 @@ public function syncTaxPercentage()
553
576
$ subscription ->save ();
554
577
}
555
578
579
+ /**
580
+ * Determine if the subscription has an incomplete payment.
581
+ *
582
+ * @return bool
583
+ */
584
+ public function hasIncompletePayment ()
585
+ {
586
+ return $ this ->pastDue () || $ this ->incomplete ();
587
+ }
588
+
556
589
/**
557
590
* Get the latest payment for a Subscription.
558
591
*
0 commit comments