Skip to content

Commit

Permalink
Prevent creating orders on subscription swaps while on trial (#182)
Browse files Browse the repository at this point in the history
* Fix issue 179
  • Loading branch information
sandervanhooft authored Jun 26, 2023
1 parent 0de1bb0 commit 180d8a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -810,7 +812,7 @@ public function restartCycleWithModifications(\Closure $applyNewSettings, ?Carbo

$this->save();

if ($invoiceNow) {
if (! $onTrial && $invoiceNow) {
$order = Cashier::$orderModel::createFromItems($orderItems);
$order->processPayment();
}
Expand Down
18 changes: 18 additions & 0 deletions tests/SwapSubscriptionPlanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 180d8a8

Please sign in to comment.