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.0] Add options to subscription swap #620

Merged
merged 4 commits into from
Mar 19, 2019
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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You can set these variables in the `phpunit.xml` file.

#### Coupons

* coupon-1 ($5)
* coupon-1 ($5) (Duration longer than once)

## Contributing

Expand Down
7 changes: 6 additions & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,10 @@ public function skipTrial()
* Swap the subscription to a new Stripe plan.
*
* @param string $plan
* @param array $options
* @return $this
*/
public function swap($plan)
public function swap($plan, $options = [])
{
$subscription = $this->asStripeSubscription();

Expand All @@ -361,6 +362,10 @@ public function swap($plan)
$subscription->billing_cycle_anchor = $this->billingCycleAnchor;
}

foreach ($options as $key => $option) {
$subscription->$key = $option;
}

// If no specific trial end date has been set, the default behavior should be
// to maintain the current trial state, whether that is "active" or to run
// the swap out with the exact number of days left on this current plan.
Expand Down
18 changes: 18 additions & 0 deletions tests/CashierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public function test_subscriptions_can_be_created()
$this->assertInstanceOf(Carbon::class, $invoice->date());
}

public function test_swapping_subscription_with_coupon()
{
$user = User::create([
'email' => 'taylor@laravel.com',
'name' => 'Taylor Otwell',
]);

$user->newSubscription('main', 'monthly-10-1')->create($this->getTestToken());
$subscription = $user->subscription('main');

// Swap Plan with Coupon
$subscription->swap('monthly-10-2', [
'coupon' => 'coupon-1',
]);

$this->assertEquals('coupon-1', $subscription->asStripeSubscription()->discount->coupon->id);
}

public function test_creating_subscription_with_coupons()
{
$user = User::create([
Expand Down