diff --git a/src/app/Observers/EntitlementObserver.php b/src/app/Observers/EntitlementObserver.php --- a/src/app/Observers/EntitlementObserver.php +++ b/src/app/Observers/EntitlementObserver.php @@ -97,11 +97,21 @@ { // Start calculating the costs for the consumption of this entitlement if the // existing consumption spans >= 14 days. - // anything's free for 14 days + // + // Effect is that anything's free for the first 14 days if ($entitlement->created_at >= Carbon::now()->subDays(14)) { return; } + $owner = $entitlement->wallet->owner; + + // Determine if we're still within the free first month + $freeMonthEnds = $owner->created_at->copy()->addMonthsWithoutOverflow(1); + + if ($freeMonthEnds >= Carbon::now()) { + return; + } + $cost = 0; // get the discount rate applied to the wallet. diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -60,6 +60,22 @@ public function chargeEntitlements($apply = true) { + // This wallet has been created less than a month ago, this is the trial period + if ($this->owner->created_at >= Carbon::now()->subMonthsWithoutOverflow(1)) { + // Move all the current entitlement's updated_at timestamps forward to one month after + // this wallet was created. + $freeMonthEnds = $this->owner->created_at->copy()->addMonthsWithoutOverflow(1); + + foreach ($this->entitlements()->get()->fresh() as $entitlement) { + if ($entitlement->updated_at < $freeMonthEnds) { + $entitlement->updated_at = $freeMonthEnds; + $entitlement->save(); + } + } + + return 0; + } + $charges = 0; $discount = $this->getDiscountRate(); @@ -80,7 +96,7 @@ continue; } - // created more than a month ago -- was it billed? + // updated last more than a month ago -- was it billed? if ($entitlement->updated_at <= Carbon::now()->subMonthsWithoutOverflow(1)) { $diff = $entitlement->updated_at->diffInMonths(Carbon::now()); @@ -93,7 +109,9 @@ continue; } - $entitlement->updated_at = $entitlement->updated_at->copy()->addMonthsWithoutOverflow($diff); + $entitlement->updated_at = $entitlement->updated_at->copy() + ->addMonthsWithoutOverflow($diff); + $entitlement->save(); if ($cost == 0) { diff --git a/src/database/seeds/production/PackageSeeder.php b/src/database/seeds/production/PackageSeeder.php --- a/src/database/seeds/production/PackageSeeder.php +++ b/src/database/seeds/production/PackageSeeder.php @@ -15,6 +15,7 @@ */ public function run() { + $skuActiveSync = Sku::firstOrCreate(['title' => 'activesync']); $skuGroupware = Sku::firstOrCreate(['title' => 'groupware']); $skuMailbox = Sku::firstOrCreate(['title' => 'mailbox']); $skuStorage = Sku::firstOrCreate(['title' => 'storage']); @@ -31,7 +32,8 @@ $skus = [ $skuMailbox, $skuGroupware, - $skuStorage + $skuStorage, + $skuActiveSync ]; $package->skus()->saveMany($skus); diff --git a/src/tests/Feature/BillingTest.php b/src/tests/Feature/BillingTest.php --- a/src/tests/Feature/BillingTest.php +++ b/src/tests/Feature/BillingTest.php @@ -82,7 +82,10 @@ */ public function testFullTrial(): void { - $this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonthsWithoutOverflow(1)); + $this->backdateEntitlements( + $this->wallet->entitlements, + Carbon::now()->subMonthsWithoutOverflow(1) + ); $this->assertEquals(999, $this->wallet->expectedCharges()); } diff --git a/src/tests/Feature/WalletTest.php b/src/tests/Feature/WalletTest.php --- a/src/tests/Feature/WalletTest.php +++ b/src/tests/Feature/WalletTest.php @@ -44,8 +44,6 @@ parent::tearDown(); } - - /** * Test that turning wallet balance from negative to positive * unsuspends the account @@ -88,7 +86,10 @@ // User/entitlements created today, balance=0 $until = $wallet->balanceLastsUntil(); - $this->assertSame(Carbon::now()->toDateString(), $until->toDateString()); + $this->assertSame( + Carbon::now()->addMonthWithoutOverflow(1)->toDateString(), + $until->toDateString() + ); // User/entitlements created today, balance=-10 CHF $wallet->balance = -1000; @@ -103,7 +104,7 @@ $daysInLastMonth = \App\Utils::daysInLastMonth(); $this->assertSame( - Carbon::now()->addDays($daysInLastMonth)->toDateString(), + Carbon::now()->addMonthsWithoutOverflow(1)->addDays($daysInLastMonth)->toDateString(), $until->toDateString() ); diff --git a/src/tests/TestCase.php b/src/tests/TestCase.php --- a/src/tests/TestCase.php +++ b/src/tests/TestCase.php @@ -14,6 +14,10 @@ $entitlement->created_at = $targetDate; $entitlement->updated_at = $targetDate; $entitlement->save(); + + $owner = $entitlement->wallet->owner; + $owner->created_at = $targetDate; + $owner->save(); } }