Skip to content

Commit acda015

Browse files
authored
Updates for separate addons purchase (#1141)
1 parent e63eaa2 commit acda015

File tree

13 files changed

+125
-3
lines changed

13 files changed

+125
-3
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ def update_v2_subscription(subscription_id, plan_data)
120120
handle_v2_subscription_response(response)
121121
end
122122

123+
def purchase_addon(subscription_id, addon_config_id)
124+
response = connection.patch("/v2/subscriptions/#{subscription_id}/addon", { addon: addon_config_id })
125+
handle_v2_subscription_response(response)
126+
end
127+
123128
def v2_plans_for_organization(organization_id)
124129
connection.get("/v2/plans_for/organization/#{organization_id}").body.map do |plan_data|
125130
Travis::API::V3::Models::V2PlanConfig.new(plan_data)

lib/travis/api/v3/models/v2_plan_config.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Travis::API::V3
22
class Models::V2PlanConfig
33
attr_reader :id, :name, :private_repos, :starting_price, :starting_users, :private_credits, :public_credits,
4-
:addon_configs
4+
:addon_configs, :available_standalone_addons
55

66
def initialize(attrs)
77
@id = attrs.fetch('id')
@@ -12,6 +12,7 @@ def initialize(attrs)
1212
@private_credits = attrs.fetch('private_credits')
1313
@public_credits = attrs.fetch('public_credits')
1414
@addon_configs = attrs.fetch('addon_configs')
15+
@available_standalone_addons = attrs.fetch('available_standalone_addons')
1516
end
1617
end
1718
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Travis::API::V3
22
class Models::V2Subscription
33
include Models::Owner
44

5-
attr_reader :id, :plan, :permissions, :source, :billing_info, :credit_card_info, :owner, :client_secret, :payment_intent, :addons, :created_at
5+
attr_reader :id, :plan, :permissions, :source, :billing_info, :credit_card_info, :owner, :client_secret, :payment_intent, :addons, :available_standalone_addons, :created_at
66

77
def initialize(attributes = {})
88
@id = attributes.fetch('id')

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ def update_plan(user_id)
2323
client.update_v2_subscription(params['subscription.id'], plan_data)
2424
end
2525

26+
def buy_addon(user_id)
27+
client = BillingClient.new(user_id)
28+
client.purchase_addon(params['subscription.id'], params['addon.id'])
29+
end
30+
2631
def invoices(user_id)
2732
client = BillingClient.new(user_id)
2833
client.get_invoices_for_v2_subscription(params['subscription.id'])
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::V2AddonConfig < ModelRenderer
3+
representation(:standard, :id, :name, :price, :quantity, :type)
4+
representation(:minimal, :id, :name, :price, :quantity, :type)
5+
end
6+
end

lib/travis/api/v3/renderer/v2_plan_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Travis::API::V3
22
class Renderer::V2PlanConfig < ModelRenderer
33
representation(:standard, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
4-
:public_credits, :addon_configs)
4+
:public_credits, :addon_configs, :available_standalone_addons)
55
representation(:minimal, :id, :name, :private_repos, :starting_price, :starting_users, :private_credits,
66
:public_credits, :addon_configs)
77
end

lib/travis/api/v3/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ module Routes
320320
patch :changetofree, '/changetofree'
321321
patch :update_plan, '/plan'
322322
post :pay, '/pay'
323+
post :buy_addon, '/addon/{addon.id}'
323324
get :invoices, '/invoices'
324325
end
325326

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::BuyAddon < Service
3+
params :plan
4+
5+
def run!
6+
raise LoginRequired unless access_control.full_access_or_logged_in?
7+
query.buy_addon(access_control.user.id)
8+
no_content
9+
end
10+
end
11+
end

spec/support/billing_spec_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ def billing_v2_subscription_response_body(attributes={})
5555
'starting_users' => 10000,
5656
'private_credits' => 500000,
5757
'public_credits' => 40000,
58+
'available_standalone_addons' => [
59+
{
60+
'id' => 'credits_25k',
61+
'name' => '25 000 credits (2,5k Linux build minutes)',
62+
'price' => 1500,
63+
'quantity' => 25000,
64+
'type' => 'credit_private'
65+
},
66+
{
67+
'id' => 'credits_500k',
68+
'name' => '500 000 credits (50k Linux build minutes)',
69+
'price' => 30000,
70+
'quantity' => 500000,
71+
'type' => 'credit_private'
72+
}
73+
],
5874
'addon_configs' => {
5975
'free_tier_credits' => {
6076
'name' => 'Free 10 000 credits (renewed monthly)',

spec/v3/billing_client_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@
260260
end
261261
end
262262

263+
describe '#purchase_addon' do
264+
let(:addon_config_id) { 'credits_500k' }
265+
subject { billing.purchase_addon(subscription_id, addon_config_id) }
266+
267+
it 'requests the update' do
268+
stubbed_request = stub_billing_request(:patch, "/v2/subscriptions/#{subscription_id}/addon", auth_key: auth_key, user_id: user_id)
269+
.with(body: JSON.dump(addon: addon_config_id))
270+
.to_return(status: 204)
271+
272+
expect { subject }.to_not raise_error
273+
end
274+
end
275+
263276
describe '#pay' do
264277
subject { billing.pay(subscription_id) }
265278

0 commit comments

Comments
 (0)