Skip to content

Commit d1f8498

Browse files
authored
V2 Subscription - change to free plan (formerly cancelation) (#1138)
V2 Subscription - change to free plan
1 parent 3a6b263 commit d1f8498

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ def create_v2_subscription(subscription_data)
110110
handle_v2_subscription_response(response)
111111
end
112112

113+
def changetofree_v2_subscription(subscription_id, data)
114+
response = connection.patch("/v2/subscriptions/#{subscription_id}/changetofree", data)
115+
handle_v2_subscription_response(response)
116+
end
117+
113118
def update_v2_subscription(subscription_id, plan_data)
114119
response = connection.patch("/v2/subscriptions/#{subscription_id}/plan", plan_data)
115120
handle_v2_subscription_response(response)

lib/travis/api/v3/queries/v2_subscription.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ def update_creditcard(user_id)
1111
client.update_v2_creditcard(params['subscription.id'], params['token'])
1212
end
1313

14+
def changetofree(user_id)
15+
data = params.dup.tap { |h| h.delete('subscription.id') }
16+
client = BillingClient.new(user_id)
17+
client.changetofree_v2_subscription(params['subscription.id'], data)
18+
end
19+
1420
def update_plan(user_id)
1521
plan_data = params.dup.tap { |h| h.delete('subscription.id') }
1622
client = BillingClient.new(user_id)

lib/travis/api/v3/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ module Routes
317317
route '/v2_subscription/{subscription.id}'
318318
patch :update_address, '/address'
319319
patch :update_creditcard, '/creditcard'
320+
patch :changetofree, '/changetofree'
320321
patch :update_plan, '/plan'
321322
post :pay, '/pay'
322323
get :invoices, '/invoices'
@@ -337,7 +338,6 @@ module Routes
337338
route '/plans_for'
338339
get :all, '/user'
339340
get :all, '/organization/{organization.id}'
340-
341341
end
342342

343343
hidden_resource :v2_plans do
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Travis::API::V3
2+
class Services::V2Subscription::Changetofree < Service
3+
params :plan
4+
5+
def run!
6+
raise LoginRequired unless access_control.full_access_or_logged_in?
7+
query.changetofree(access_control.user.id)
8+
no_content
9+
end
10+
end
11+
end

spec/v3/billing_client_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,20 @@
232232
end
233233
end
234234

235+
describe '#changetofree_v2_subscription' do
236+
let(:plan_data) { { 'plan' => 'enterprise_tier_plan' } }
237+
subject { billing.changetofree_v2_subscription(subscription_id, plan_data) }
238+
239+
it 'requests the update' do
240+
stubbed_request = stub_billing_request(:patch, "/v2/subscriptions/#{subscription_id}/changetofree", auth_key: auth_key, user_id: user_id)
241+
.with(body: JSON.dump(plan_data))
242+
.to_return(status: 204)
243+
244+
expect { subject }.to_not raise_error
245+
expect(stubbed_request).to have_been_made
246+
end
247+
end
248+
235249
describe '#update_v2_subscription' do
236250
let(:plan_data) { { 'plan' => 'enterprise_tier_plan' } }
237251
subject { billing.update_v2_subscription(subscription_id, plan_data) }

0 commit comments

Comments
 (0)