Skip to content

Commit 54a4c9a

Browse files
authored
auto refill (#1195)
auto refill functionality
1 parent bbeec9d commit 54a4c9a

File tree

13 files changed

+90
-9
lines changed

13 files changed

+90
-9
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ rvm: 2.6.5
77

88
script: "bundle exec rake knapsack:rspec"
99

10-
addons:
11-
- snaps:
12-
- name: docker
13-
channel: latest/beta
14-
1510
env:
1611
global:
1712
- PATH=/snap/bin:$PATH

lib/travis/api/v3/billing_client.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ def update_organization_billing_permission(organization_id, billing_admin_only)
205205
handle_subscription_response(response)
206206
end
207207

208+
def create_auto_refill(plan_id, is_enabled)
209+
response = connection.post('/auto_refill', {plan: plan_id, enabled: is_enabled})
210+
handle_errors_and_respond(response)
211+
end
212+
213+
def get_auto_refill(plan_id)
214+
response = connection.get("/auto_refill?plan_id=#{plan_id}")
215+
handle_errors_and_respond(response) { |r| Travis::API::V3::Models::AutoRefill.new(r) }
216+
end
217+
208218
private
209219

210220
def handle_subscription_response(response)
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 Models::AutoRefill
3+
4+
attr_reader :enabled, :threshold, :amount
5+
def initialize(attributes = {})
6+
@enabled = attributes.key?('enabled') ? attributes.fetch('enabled') : true
7+
@threshold = attributes.key?('refill_threshold') ? attributes.fetch('refill_threshold') : 25000
8+
@amount = attributes.key?('refill_amount') ? attributes.fetch('refill_amount'): 10000
9+
end
10+
end
11+
12+
end

lib/travis/api/v3/models/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 Models::V2PlanConfig
33
attr_reader :id, :name, :private_repos, :starting_price, :starting_users, :plan_type,
4-
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons
4+
:private_credits, :public_credits, :addon_configs, :concurrency_limit, :available_standalone_addons, :auto_refill_enabled
55

66
def initialize(attrs)
77
@id = attrs.fetch('id')

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Models::V2Subscription
33
include Models::Owner
44

55
attr_reader :id, :plan, :permissions, :source, :billing_info, :credit_card_info, :owner, :status, :valid_to, :canceled_at,
6-
:client_secret, :payment_intent, :addons, :available_standalone_addons, :created_at
6+
:client_secret, :payment_intent, :addons, :auto_refill, :available_standalone_addons, :created_at
77

88
def initialize(attributes = {})
99
@id = attributes.fetch('id')
@@ -15,7 +15,17 @@ def initialize(attributes = {})
1515
@payment_intent = attributes['payment_intent'] && Models::PaymentIntent.new(attributes['payment_intent'])
1616
@owner = fetch_owner(attributes.fetch('owner'))
1717
@client_secret = attributes.fetch('client_secret')
18-
@addons = attributes['addons'].select { |addon| addon['current_usage']['status'] != 'expired' }.map { |addon| Models::V2Addon.new(addon) }
18+
@addons = attributes['addons'].select { |addon| addon['current_usage']['status'] != 'expired' if addon['current_usage'] }.map { |addon| Models::V2Addon.new(addon) }
19+
refill = attributes['addons'].detect { |addon| addon['addon_config_id'] === 'auto_refill' } || {"enabled" => false};
20+
default_refill = @plan.respond_to?('available_standalone_addons') ?
21+
@plan.available_standalone_addons.detect { |addon| addon['id'] === 'auto_refill' } : nil
22+
23+
refill['enabled'] = attributes['auto_refill_enabled']
24+
if default_refill
25+
refill['refill_threshold'] = default_refill['refill_threshold'] unless refill.key?('refill_threshold')
26+
refill['refill_amount'] = default_refill['refill_amount'] unless refill.key?('refill_amount')
27+
end
28+
@auto_refill = Models::AutoRefill.new(refill)
1929
@created_at = attributes.fetch('created_at')
2030
@status = attributes.fetch('status')
2131
@valid_to = attributes.fetch('valid_to')

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Travis::API::V3
22
class Queries::V2Subscription < Query
3+
params :enabled
4+
35
def update_address(user_id)
46
address_data = params.dup.tap { |h| h.delete('subscription.id') }
57
client = BillingClient.new(user_id)
@@ -37,5 +39,15 @@ def pay(user_id)
3739
client = BillingClient.new(user_id)
3840
client.pay_v2(params['subscription.id'])
3941
end
42+
43+
def get_auto_refill(user_id, plan_id)
44+
client = BillingClient.new(user_id)
45+
client.get_auto_refill(plan_id)
46+
end
47+
48+
def toggle_auto_refill(user_id, plan_id)
49+
client = BillingClient.new(user_id)
50+
client.create_auto_refill(plan_id, enabled)
51+
end
4052
end
4153
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::AutoRefill < ModelRenderer
3+
representation(:standard, :enabled, :threshold, :amount)
4+
representation(:minimal, :enabled, :threshold, :amount)
5+
end
6+
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Travis::API::V3
22
class Renderer::V2Subscription < ModelRenderer
3-
representation(:standard, :id, :plan, :addons, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at)
3+
representation(:standard, :id, :plan, :addons, :auto_refill, :status, :valid_to, :canceled_at, :source, :owner, :client_secret, :billing_info, :credit_card_info, :payment_intent, :created_at)
44

55
def billing_info
66
Renderer.render_model(model.billing_info, mode: :standard) unless model.billing_info.nil?

lib/travis/api/v3/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ module Routes
367367
post :buy_addon, '/addon/{addon.id}'
368368
get :user_usages, '/user_usages'
369369
get :invoices, '/invoices'
370+
get :auto_refill, '/auto_refill'
371+
patch :toggle_auto_refill, '/auto_refill'
370372
end
371373

372374
hidden_resource :trials do
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Travis::API::V3
2+
class Services::V2Subscription::AutoRefill < Service
3+
result_type :auto_refill
4+
5+
def run!
6+
raise LoginRequired unless access_control.full_access_or_logged_in?
7+
result query.get_auto_refill(access_control.user.id, params['subscription.id']), status: 200
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)