From 180d8a83cdc45f66d4ac5900931248b2a780aafe Mon Sep 17 00:00:00 2001 From: Sander van Hooft Date: Mon, 26 Jun 2023 12:39:18 +0200 Subject: [PATCH] Prevent creating orders on subscription swaps while on trial (#182) * Fix issue 179 --- src/Subscription.php | 6 ++++-- tests/SwapSubscriptionPlanTest.php | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Subscription.php b/src/Subscription.php index 04f7959d..833fe336 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -794,7 +794,9 @@ public function restartCycleWithModifications(\Closure $applyNewSettings, ?Carbo // Apply new subscription settings call_user_func($applyNewSettings); - if ($this->onTrial()) { + $onTrial = $this->onTrial(); + + if ($onTrial) { // Reschedule next cycle's OrderItem using the new subscription settings $orderItems[] = $this->scheduleNewOrderItemAt($this->trial_ends_at); @@ -810,7 +812,7 @@ public function restartCycleWithModifications(\Closure $applyNewSettings, ?Carbo $this->save(); - if ($invoiceNow) { + if (! $onTrial && $invoiceNow) { $order = Cashier::$orderModel::createFromItems($orderItems); $order->processPayment(); } diff --git a/tests/SwapSubscriptionPlanTest.php b/tests/SwapSubscriptionPlanTest.php index 34264693..e6bb7512 100644 --- a/tests/SwapSubscriptionPlanTest.php +++ b/tests/SwapSubscriptionPlanTest.php @@ -158,6 +158,24 @@ public function swappingACancelledSubscriptionAtNextCycleResumesIt() $this->assertFalse($subscription->cancelled()); } + /** @test */ + public function swappingOnTrialDoesNotCreateAnOrderEvenWhenInvoiceNowIsTrue() + { + $subscription = $this->getUser()->subscriptions()->save( + SubscriptionFactory::new()->make([ + 'trial_ends_at' => now()->addWeek(), + 'plan' => 'monthly-20-1', + ]) + ); + + $this->assertTrue($subscription->onTrial()); + $this->assertEquals(0, Order::count()); + + $subscription->swap('weekly-20-1', true); + + $this->assertEquals(0, Order::count()); + } + /** @test */ public function canSwapNextCycle() {