From 5fa250a07c3217975a910efc4f7e8c26e8c10b13 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Fri, 15 Mar 2019 22:37:46 +0200 Subject: [PATCH 1/3] Add support for options on subscription swap --- readme.md | 2 +- src/Subscription.php | 11 ++++++++++- tests/CashierTest.php | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) 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..5bf6f04a 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,14 @@ public function swap($plan) $subscription->billing_cycle_anchor = $this->billingCycleAnchor; } + // Set additional options on the subscription update, such as + // [ + // 'coupon' => 'TEST_COUPON', + // ] + 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..2cdbd98f 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([ From c04d83bca1f934aeebbadc100b07e83b50506693 Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Fri, 15 Mar 2019 22:44:26 +0200 Subject: [PATCH 2/3] Style fix --- tests/CashierTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CashierTest.php b/tests/CashierTest.php index 2cdbd98f..5fe826b8 100644 --- a/tests/CashierTest.php +++ b/tests/CashierTest.php @@ -145,7 +145,7 @@ public function test_swapping_subscription_with_coupon() 'email' => 'taylor@laravel.com', 'name' => 'Taylor Otwell', ]); - + $user->newSubscription('main', 'monthly-10-1')->create($this->getTestToken()); $subscription = $user->subscription('main'); From fa1d72375f74058a3f127a793d4aa1dcfed69d8e Mon Sep 17 00:00:00 2001 From: Keith Brink Date: Sat, 16 Mar 2019 18:44:45 +0200 Subject: [PATCH 3/3] Remove comment --- src/Subscription.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Subscription.php b/src/Subscription.php index 5bf6f04a..f1cecc78 100644 --- a/src/Subscription.php +++ b/src/Subscription.php @@ -362,10 +362,6 @@ public function swap($plan, $options = []) $subscription->billing_cycle_anchor = $this->billingCycleAnchor; } - // Set additional options on the subscription update, such as - // [ - // 'coupon' => 'TEST_COUPON', - // ] foreach ($options as $key => $option) { $subscription->$key = $option; }