Skip to content

Commit

Permalink
fix payment tolerance definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baettig committed Mar 13, 2020
1 parent 3ecd102 commit 5917d44
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion config/subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
'feature' => Feature::class,
],

'paymentToleranceDays' => env('SUBSCRIPTIONS_PAYMENT_TOLERANCE_DAYS', 0)
'paymentToleranceDays' => env('SUBSCRIPTIONS_PAYMENT_TOLERANCE_DAYS', 1)
];
1 change: 1 addition & 0 deletions database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'price' => $faker->randomElement([9900,29900,59900]),
'currency' => 'CHF',
'is_recurring' => $faker->randomElement([true,false]),
'payment_tolerance_ends_at' => Carbon::now()->addDay()->endOfDay()
];
});

Expand Down
2 changes: 1 addition & 1 deletion database/migrations/2019_10_01_123211_plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function up(): void
$table->boolean('is_recurring')->default(true);

$table->timestamp('test_ends_at')->nullable()->default(Carbon::now());
$table->timestamp('payment_tolerance_ends_at')->nullable()->default(config('subscriptions.paymentToleranceDays') > 0 ? Carbon::now()->addDays(config('subscriptions.paymentToleranceDays'))->endOfDay() : Carbon::now());
$table->timestamp('payment_tolerance_ends_at');
$table->timestamp('paid_at')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('renewed_at')->nullable();
Expand Down
1 change: 1 addition & 0 deletions src/Models/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function subscribeTo(Plan $plan,
'test_ends_at' => $dateProcessor->getTestEndsDate(),
'starts_at' => $dateProcessor->getStartDate(),
'expires_at' => $dateProcessor->getExpirationDate(),
'payment_tolerance_ends_at' => config('subscriptions.paymentToleranceDays') > 0 ? Carbon::now()->addDays(config('subscriptions.paymentToleranceDays'))->endOfDay() : Carbon::now(),
'cancelled_at' => null,
'paid_at' => $plan->price === 0 ? Carbon::now() : null,
'price' => $plan->price,
Expand Down
5 changes: 3 additions & 2 deletions tests/feature/SubscribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function setUp(): void
*/
public function can_subscribe_to_a_recurring_yearly_plan(): void
{
config(['subscriptions.paymentToleranceDays' => 30]);
Event::fake();
$plan = factory(Plan::class)->states(['active', 'yearly'])->create();
$this->user->subscribeTo($plan);
Expand All @@ -45,13 +46,13 @@ public function can_subscribe_to_a_recurring_yearly_plan(): void
$this->assertEquals($plan->price, $subscription->price);
$this->assertEquals($plan->currency, $subscription->currency);
$this->assertEquals(Carbon::now()->addYear()->diffInDays(Carbon::now()), $subscription->remaining_days);

$this->assertEqualsWithDelta(Carbon::now()->addDays(30)->endOfDay(), $subscription->payment_tolerance_ends_at, 1);
$this->assertTrue($subscription->is_recurring);
$this->assertNull($subscription->refunded_at);
$this->assertNull($subscription->cancelled_at);
$this->assertFalse($subscription->is_testing);
$this->assertFalse($subscription->is_paid);
$this->assertFalse($subscription->is_active);
$this->assertTrue($subscription->is_active);
Event::assertDispatched(NewSubscription::class);

$subscription->markAsPaid();
Expand Down

0 comments on commit 5917d44

Please sign in to comment.