diff --git a/readme.md b/readme.md index b844a91a..4c58d056 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/Subscription.php b/src/Subscription.php index e39a7f26..f1cecc78 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -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(); @@ -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. diff --git a/tests/CashierTest.php b/tests/CashierTest.php index d80f2796..5fe826b8 100644 --- a/tests/CashierTest.php +++ b/tests/CashierTest.php @@ -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([