Skip to content

Commit d5e8dfb

Browse files
committed
Api WIP for v2 plan change
1 parent 4de3fce commit d5e8dfb

File tree

20 files changed

+274
-0
lines changed

20 files changed

+274
-0
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ def update_v2_subscription(subscription_id, plan_data)
104104
handle_v2_subscription_response(response)
105105
end
106106

107+
def v2_plans_for_organization(organization_id)
108+
connection.get("/v2/plans_for/organization/#{organization_id}").body.map do |plan_data|
109+
Travis::API::V3::Models::V2PlanConfig.new(plan_data)
110+
end
111+
end
112+
113+
def v2_plans_for_user
114+
connection.get("/v2/plans_for/user").body.map do |plan_data|
115+
Travis::API::V3::Models::V2PlanConfig.new(plan_data)
116+
end
117+
end
118+
107119
def cancel_subscription(id, reason_data)
108120
response = connection.post("/subscriptions/#{id}/cancel", reason_data)
109121
handle_subscription_response(response)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Travis::API::V3
2+
class Models::V2PlanConfig
3+
attr_reader :id, :name, :private_repos, :default_addons, :starting_price, :starting_users
4+
5+
def initialize(attrs)
6+
@id = attrs.fetch('id')
7+
@name = attrs.fetch('name')
8+
@private_repos = attrs.fetch('private_repos')
9+
@default_addons = attrs.fetch('default_addons')
10+
@starting_price = attrs.fetch('starting_price')
11+
@starting_users = attrs.fetch('starting_users')
12+
end
13+
end
14+
end

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Travis::API::V3
2+
class Queries::V2Plans < Query
3+
def all(user_id)
4+
client = BillingClient.new(user_id)
5+
if params['organization.id']
6+
client.v2_plans_for_organization(params['organization.id'])
7+
else
8+
client.v2_plans_for_user
9+
end
10+
end
11+
end
12+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Travis::API::V3
2+
class Queries::V2Subscription < Query
3+
def update_address(user_id)
4+
address_data = params.dup.tap { |h| h.delete('subscription.id') }
5+
client = BillingClient.new(user_id)
6+
client.update_v2_address(params['subscription.id'], address_data)
7+
end
8+
9+
def cancel(user_id)
10+
reason_data = params.dup.tap { |h| h.delete('subscription.id') }
11+
client = BillingClient.new(user_id)
12+
client.cancel_subscription(params['subscription.id'], reason_data)
13+
end
14+
15+
def update_creditcard(user_id)
16+
client = BillingClient.new(user_id)
17+
client.update_v2_creditcard(params['subscription.id'], params['token'])
18+
end
19+
20+
def update_plan(user_id)
21+
plan_data = params.dup.tap { |h| h.delete('subscription.id') }
22+
client = BillingClient.new(user_id)
23+
client.update_v2_subscription(params['subscription.id'], plan_data)
24+
end
25+
26+
def resubscribe(user_id)
27+
client = BillingClient.new(user_id)
28+
client.resubscribe(params['subscription.id'])
29+
end
30+
31+
def invoices(user_id)
32+
client = BillingClient.new(user_id)
33+
client.get_invoices_for_subscription(params['subscription.id'])
34+
end
35+
36+
def trial(user_id)
37+
client = BillingClient.new(user_id)
38+
client.get_trial_info_for_subscription(params['subscription.id'])
39+
end
40+
41+
def pay(user_id)
42+
client = BillingClient.new(user_id)
43+
client.pay(params['subscription.id'])
44+
end
45+
end
46+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module Travis::API::V3
2+
class Queries::V2Subscriptions < Query
3+
params :plan, :coupon, :organization_id, :client_secret
4+
params :first_name, :last_name, :company, :address, :address2, :city, :country, :state, :vat_id, :zip_code, :billing_email, prefix: :billing_info
5+
params :token, prefix: :credit_card_info
6+
7+
def all(user_id)
8+
client = BillingClient.new(user_id)
9+
client.all_v2
10+
end
11+
12+
def create(user_id)
13+
client = BillingClient.new(user_id)
14+
client.create_v2_subscription(
15+
:plan => params['plan'],
16+
:client_secret => params['client_secret'],
17+
:coupon => params['coupon'],
18+
:organization_id => params['organization_id'],
19+
:billing_info => billing_info_params,
20+
:credit_card_info => credit_card_info_params
21+
)
22+
end
23+
end
24+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::V2PlanConfig < ModelRenderer
3+
representation(:standard, :id, :name, :private_repos, :default_addons, :starting_price, :starting_users)
4+
representation(:minimal, :id, :name, :private_repos, :default_addons, :starting_price, :starting_users)
5+
end
6+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Travis::API::V3
2+
class Renderer::V2Plans < CollectionRenderer
3+
type :v2_plans
4+
collection_key :v2_plans
5+
end
6+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Travis::API::V3
2+
class Renderer::V2Subscription < ModelRenderer
3+
representation(:standard, :id, :plan, :addons, :status, :source, :owner, :billing_info, :credit_card_info, :payment_intent, :created_at)
4+
5+
def billing_info
6+
Renderer.render_model(model.billing_info, mode: :standard) unless model.billing_info.nil?
7+
end
8+
9+
def credit_card_info
10+
Renderer.render_model(model.credit_card_info, mode: :standard) unless model.credit_card_info.nil?
11+
end
12+
13+
def plan
14+
Renderer.render_model(model.plan, mode: :standard) unless model.plan.nil?
15+
end
16+
17+
def addons
18+
Renderer.render_model(model.addons, mode: :standard) unless model.addons.nil?
19+
end
20+
21+
def payment_intent
22+
Renderer.render_model(model.payment_intent, mode: :standard) unless model.payment_intent.nil?
23+
end
24+
end
25+
end

lib/travis/api/v3/routes.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ module Routes
296296
post :create
297297
end
298298

299+
hidden_resource :v2_subscriptions do
300+
route '/v2_subscriptions'
301+
get :all
302+
post :create
303+
end
304+
299305
hidden_resource :subscription do
300306
route '/subscription/{subscription.id}'
301307
patch :update_address, '/address'
@@ -307,6 +313,17 @@ module Routes
307313
get :invoices, '/invoices'
308314
end
309315

316+
hidden_resource :v2_subscription do
317+
route '/v2_subscription/{subscription.id}'
318+
patch :update_address, '/address'
319+
patch :update_creditcard, '/creditcard'
320+
patch :update_plan, '/plan'
321+
patch :resubscribe, '/resubscribe'
322+
post :cancel, '/cancel'
323+
post :pay, '/pay'
324+
get :invoices, '/invoices'
325+
end
326+
310327
hidden_resource :trials do
311328
route '/trials'
312329
get :all
@@ -325,6 +342,13 @@ module Routes
325342

326343
end
327344

345+
hidden_resource :v2_plans do
346+
route '/v2_plans_for'
347+
get :all, '/user'
348+
get :all, '/organization/{organization.id}'
349+
350+
end
351+
328352
if ENV['GDPR_ENABLED']
329353
hidden_resource :gdpr do
330354
route '/gdpr'

lib/travis/api/v3/services.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module Services
3636
Organizations = Module.new { extend Services }
3737
Owner = Module.new { extend Services }
3838
Plans = Module.new { extend Services }
39+
V2Plans = Module.new { extend Services }
3940
Preferences = Module.new { extend Services }
4041
Preference = Module.new { extend Services }
4142
Queues = Module.new { extend Services }
@@ -47,6 +48,8 @@ module Services
4748
Stages = Module.new { extend Services }
4849
Subscription = Module.new { extend Services }
4950
Subscriptions = Module.new { extend Services }
51+
V2Subscription = Module.new { extend Services }
52+
V2Subscriptions = Module.new { extend Services }
5053
Trials = Module.new { extend Services }
5154
User = Module.new { extend Services }
5255
UserSetting = Module.new { extend Services }

0 commit comments

Comments
 (0)