From 9e94c0ffb9838d97d1e9eec87cd801ff31bd9ae8 Mon Sep 17 00:00:00 2001 From: Ben Doe Date: Thu, 18 Feb 2021 11:47:04 +0000 Subject: [PATCH 1/5] end trial function --- src/Subscription.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Subscription.php b/src/Subscription.php index ae719977..594df950 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -576,6 +576,29 @@ public function skipTrial() return $this; } + /** + * Force the trial to end immediately. + * Similar to skipTrial(), however this method cannot be combined with swap, resume, etc. and immediately updates the Stripe subscription. + * + * This method must be combined with swap, resume, etc. + * + * @return $this + */ + public function endTrial() + { + $subscription = $this->asStripeSubscription(); + + $subscription->trial_end = 'now'; + + $subscription->save(); + + $this->trial_ends_at = null; + + $this->save(); + + return $this; + } + /** * Extend an existing subscription's trial period. * From f12255a27047cf696e471f18a7bf3fe89d032308 Mon Sep 17 00:00:00 2001 From: Ben Doe Date: Thu, 18 Feb 2021 11:53:48 +0000 Subject: [PATCH 2/5] removed code duplication --- src/Subscription.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Subscription.php b/src/Subscription.php index 594df950..afdd8036 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -586,17 +586,9 @@ public function skipTrial() */ public function endTrial() { - $subscription = $this->asStripeSubscription(); - - $subscription->trial_end = 'now'; - - $subscription->save(); - - $this->trial_ends_at = null; + $this->setStripeSubscriptionTrialEnd('now'); - $this->save(); - - return $this; + return tap($this->skipTrial())->save(); } /** @@ -611,11 +603,7 @@ public function extendTrial(CarbonInterface $date) throw new InvalidArgumentException("Extending a subscription's trial requires a date in the future."); } - $subscription = $this->asStripeSubscription(); - - $subscription->trial_end = $date->getTimestamp(); - - $subscription->save(); + $this->setStripeSubscriptionTrialEnd($date->getTimestamp()); $this->trial_ends_at = $date; @@ -624,6 +612,21 @@ public function extendTrial(CarbonInterface $date) return $this; } + /** + * Sets the Stripe subscription trial end date. + * + * @param string|int $timestamp + * @return void + */ + protected function setStripeSubscriptionTrialEnd($timestamp) + { + $subscription = $this->asStripeSubscription(); + + $subscription->trial_end = $timestamp; + + $subscription->save(); + } + /** * Swap the subscription to new Stripe plans. * From 15356fe62f7fb8807ccf120811c94dc5c22c36e1 Mon Sep 17 00:00:00 2001 From: Ben Doe Date: Thu, 18 Feb 2021 11:53:52 +0000 Subject: [PATCH 3/5] test --- tests/Feature/SubscriptionsTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Feature/SubscriptionsTest.php b/tests/Feature/SubscriptionsTest.php index 9a9f3cde..6597fd17 100644 --- a/tests/Feature/SubscriptionsTest.php +++ b/tests/Feature/SubscriptionsTest.php @@ -602,6 +602,19 @@ public function test_trials_can_be_extended() $this->assertEquals($subscription->asStripeSubscription()->trial_end, $trialEndsAt->getTimestamp()); } + public function test_trials_can_be_ended() + { + $user = $this->createCustomer('trials_can_be_ended'); + + $subscription = $user->newSubscription('main', static::$planId) + ->trialDays(10) + ->create('pm_card_visa'); + + $subscription->endTrial(); + + $this->assertNull($subscription->trial_ends_at); + } + public function test_applying_coupons_to_existing_customers() { $user = $this->createCustomer('applying_coupons_to_existing_customers'); From 3896b1038dcf6339f8b06ce4e6a1c0c902986e59 Mon Sep 17 00:00:00 2001 From: Ben Doe Date: Thu, 18 Feb 2021 12:01:49 +0000 Subject: [PATCH 4/5] update docblock --- src/Subscription.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Subscription.php b/src/Subscription.php index afdd8036..76fa4dd3 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -578,7 +578,7 @@ public function skipTrial() /** * Force the trial to end immediately. - * Similar to skipTrial(), however this method cannot be combined with swap, resume, etc. and immediately updates the Stripe subscription. + * Similar to skipTrial(), however this method immediately updates the Stripe subscription. * * This method must be combined with swap, resume, etc. * From 689292938de72e62b729fec1bc5406357706c372 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Thu, 18 Feb 2021 14:32:47 +0100 Subject: [PATCH 5/5] Cleanup --- src/Subscription.php | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/Subscription.php b/src/Subscription.php index 76fa4dd3..42a4b67c 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -577,18 +577,27 @@ public function skipTrial() } /** - * Force the trial to end immediately. - * Similar to skipTrial(), however this method immediately updates the Stripe subscription. - * - * This method must be combined with swap, resume, etc. + * Force the subscription's trial to end immediately. * * @return $this */ public function endTrial() { - $this->setStripeSubscriptionTrialEnd('now'); + if (is_null($this->trial_ends_at)) { + return $this; + } - return tap($this->skipTrial())->save(); + $subscription = $this->asStripeSubscription(); + + $subscription->trial_end = 'now'; + + $subscription->save(); + + $this->trial_ends_at = null; + + $this->save(); + + return $this; } /** @@ -603,7 +612,11 @@ public function extendTrial(CarbonInterface $date) throw new InvalidArgumentException("Extending a subscription's trial requires a date in the future."); } - $this->setStripeSubscriptionTrialEnd($date->getTimestamp()); + $subscription = $this->asStripeSubscription(); + + $subscription->trial_end = $date->getTimestamp(); + + $subscription->save(); $this->trial_ends_at = $date; @@ -612,21 +625,6 @@ public function extendTrial(CarbonInterface $date) return $this; } - /** - * Sets the Stripe subscription trial end date. - * - * @param string|int $timestamp - * @return void - */ - protected function setStripeSubscriptionTrialEnd($timestamp) - { - $subscription = $this->asStripeSubscription(); - - $subscription->trial_end = $timestamp; - - $subscription->save(); - } - /** * Swap the subscription to new Stripe plans. *