Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Implement extending trials #884

Merged
merged 1 commit into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Laravel\Cashier;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use InvalidArgumentException;
use Laravel\Cashier\Exceptions\IncompletePayment;
use Laravel\Cashier\Exceptions\SubscriptionUpdateFailure;
use LogicException;
Expand Down Expand Up @@ -425,6 +427,33 @@ public function skipTrial()
return $this;
}

/**
* Extend an existing subscription's trial period.
*
* @param \Carbon\CarbonInterface $date
* @return $this
*/
public function extendTrial(CarbonInterface $date)
{
if (! $date->isFuture()) {
throw new InvalidArgumentException("Extending a subscription's trial requires a date in the future.");
}

$subscription = $this->asStripeSubscription();

$subscription->prorate = $this->prorate;

$subscription->trial_end = $date->getTimestamp();

$subscription->save();

$this->trial_ends_at = $date;

$this->save();

return $this;
}

/**
* Swap the subscription to a new Stripe plan.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,20 @@ public function test_creating_subscription_with_explicit_trial()
$this->assertEquals(Carbon::tomorrow()->hour(3)->minute(15), $subscription->trial_ends_at);
}

public function test_trials_can_be_extended()
{
$user = $this->createCustomer('trials_can_be_extended');

$subscription = $user->newSubscription('main', static::$planId)->create('pm_card_visa');

$this->assertNull($subscription->trial_ends_at);

$subscription->extendTrial($trialEndsAt = now()->addDays()->floor());

$this->assertTrue($trialEndsAt->equalTo($subscription->trial_ends_at));
$this->assertEquals($subscription->asStripeSubscription()->trial_end, $trialEndsAt->getTimestamp());
}

public function test_applying_coupons_to_existing_customers()
{
$user = $this->createCustomer('applying_coupons_to_existing_customers');
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Cashier\Tests\Unit;

use InvalidArgumentException;
use Laravel\Cashier\Exceptions\SubscriptionUpdateFailure;
use Laravel\Cashier\Subscription;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -94,4 +95,11 @@ public function test_incomplete_subscriptions_cannot_update_their_quantity()

$subscription->updateQuantity(5);
}

public function test_extending_a_trial_requires_a_date_in_the_future()
{
$this->expectException(InvalidArgumentException::class);

(new Subscription)->extendTrial(now()->subDay());
}
}