Skip to content

Commit 7316915

Browse files
authored
Billing client - new v2 subscriptions (#1125)
* billing-client updateed for v2 subscriptions * fixes and specs
1 parent c040a15 commit 7316915

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

lib/travis/api/v3/billing_client.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,38 @@ def all
1616
Travis::API::V3::Models::SubscriptionsCollection.new(subscriptions, permissions)
1717
end
1818

19+
def all_v2
20+
data = connection.get('/v2/subscriptions').body
21+
subscriptions = data.fetch('plans').map do |subscription_data|
22+
Travis::API::V3::Models::V2Subscription.new(subscription_data)
23+
end
24+
permissions = data.fetch('permissions')
25+
26+
Travis::API::V3::Models::SubscriptionsCollection.new(subscriptions, permissions)
27+
end
28+
1929
def get_subscription(id)
2030
response = connection.get("/subscriptions/#{id}")
2131
handle_subscription_response(response)
2232
end
2333

34+
def get_v2_subscription(id)
35+
response = connection.get("/v2/subscriptions/#{id}")
36+
handle_v2_subscription_response(response)
37+
end
38+
2439
def get_invoices_for_subscription(id)
2540
connection.get("/subscriptions/#{id}/invoices").body.map do |invoice_data|
2641
Travis::API::V3::Models::Invoice.new(invoice_data)
2742
end
2843
end
2944

45+
def get_invoices_for_v2_subscription(id)
46+
connection.get("/v2/subscriptions/#{id}/invoices").body.map do |invoice_data|
47+
Travis::API::V3::Models::Invoice.new(invoice_data)
48+
end
49+
end
50+
3051
def trials
3152
connection.get('/trials').body.map do | trial_data |
3253
Travis::API::V3::Models::Trial.new(trial_data)
@@ -43,11 +64,21 @@ def update_address(subscription_id, address_data)
4364
handle_subscription_response(response)
4465
end
4566

67+
def update_v2_address(subscription_id, address_data)
68+
response = connection.patch("/v2/subscriptions/#{subscription_id}/address", address_data)
69+
handle_v2_subscription_response(response)
70+
end
71+
4672
def update_creditcard(subscription_id, creditcard_token)
4773
response = connection.patch("/subscriptions/#{subscription_id}/creditcard", token: creditcard_token)
4874
handle_subscription_response(response)
4975
end
5076

77+
def update_v2_creditcard(subscription_id, creditcard_token)
78+
response = connection.patch("/v2/subscriptions/#{subscription_id}/creditcard", token: creditcard_token)
79+
handle_v2_subscription_response(response)
80+
end
81+
5182
def update_plan(subscription_id, plan_data)
5283
response = connection.patch("/subscriptions/#{subscription_id}/plan", plan_data)
5384
handle_subscription_response(response)
@@ -58,6 +89,11 @@ def create_subscription(subscription_data)
5889
handle_subscription_response(response)
5990
end
6091

92+
def create_v2_subscription(subscription_data)
93+
response = connection.post('/v2/subscriptions', subscription_data)
94+
handle_v2_subscription_response(response)
95+
end
96+
6197
def cancel_subscription(id, reason_data)
6298
response = connection.post("/subscriptions/#{id}/cancel", reason_data)
6399
handle_subscription_response(response)
@@ -85,6 +121,11 @@ def pay(id)
85121
handle_subscription_response(response)
86122
end
87123

124+
def pay_v2(id)
125+
response = connection.post("/v2/subscriptions/#{id}/pay")
126+
handle_v2_subscription_response(response)
127+
end
128+
88129
def get_coupon(code)
89130
response = connection.get("/coupons/#{code}")
90131
handle_coupon_response(response)
@@ -101,6 +142,10 @@ def handle_subscription_response(response)
101142
handle_errors_and_respond(response) { |r| Travis::API::V3::Models::Subscription.new(r) }
102143
end
103144

145+
def handle_v2_subscription_response(response)
146+
handle_errors_and_respond(response) { |r| Travis::API::V3::Models::V2Subscription.new(r) }
147+
end
148+
104149
def handle_coupon_response(response)
105150
handle_errors_and_respond(response) { |r| Travis::API::V3::Models::Coupon.new(r) }
106151
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Travis::API::V3
2+
class Models::V2Subscription
3+
include Models::Owner
4+
5+
attr_reader :id, :permissions, :status, :source, :billing_info, :credit_card_info, :owner, :payment_intent, :addons, :created_at
6+
7+
def initialize(attributes = {})
8+
@id = attributes.fetch('id')
9+
@permissions = Models::BillingPermissions.new(attributes.fetch('permissions'))
10+
@status = attributes.fetch('status')
11+
@source = attributes.fetch('source')
12+
@billing_info = attributes['billing_info'] && Models::BillingInfo.new(@id, attributes['billing_info'])
13+
@credit_card_info = attributes['credit_card_info'] && Models::CreditCardInfo.new(@id, attributes['credit_card_info'])
14+
@payment_intent = attributes['payment_intent'] && Models::PaymentIntent.new(attributes['payment_intent'])
15+
@owner = fetch_owner(attributes.fetch('owner'))
16+
@addons = attributes.fetch('addons')
17+
@created_at = attributes.fetch('created_at')
18+
end
19+
end
20+
end

spec/support/billing_spec_helper.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,86 @@ def billing_subscription_response_body(attributes={})
4343
}.deep_merge(attributes)
4444
end
4545

46+
def billing_v2_subscription_response_body(attributes={})
47+
{
48+
"permissions" => { "read" => true, "write" => true },
49+
"id" => 81,
50+
"addons" => billing_addons_response_body,
51+
"source" => "manual",
52+
"status" => "",
53+
"created_at" => "2017-11-28T00:09:59.502Z",
54+
"billing_info" => {
55+
"first_name" => "ana",
56+
"last_name" => "rosas",
57+
"company" => "",
58+
"billing_email" => "[email protected]",
59+
"zip_code" => "28450",
60+
"address" => "Luis Spota",
61+
"address2" => "",
62+
"city" => "Comala",
63+
"state" => nil,
64+
"country" => "Mexico",
65+
"vat_id" => "123456"
66+
},
67+
"credit_card_info" => {
68+
"card_owner" => "ana",
69+
"last_digits" => "4242",
70+
"expiration_date" => "9/2021"
71+
},
72+
"owner" => {
73+
"type" => "Organization",
74+
"id" => 43
75+
}
76+
}.deep_merge(attributes)
77+
end
78+
79+
def billing_addons_response_body(attributes={})
80+
{
81+
"addons" => [
82+
{
83+
"id" => "1",
84+
"plan_id" => "1",
85+
"name" => "OSS Build Credits",
86+
"addon_type" => "credit_public",
87+
"created_at" => "2020-07-09T12:06:13.293Z",
88+
"updated_at" => "2020-07-09T12:07:03.619Z",
89+
"current_usage_id" => 1,
90+
"current_usage" => {
91+
"id" => 1,
92+
"addon_id" => 1,
93+
"addon_quantity" => 10000,
94+
"addon_usage" => 0,
95+
"purchase_date" => "2020-07-09T12:06:27.919Z",
96+
"valid_to" => nil,
97+
"status" => "active",
98+
"created_at" => "2020-07-09T12:06:27.944Z",
99+
"updated_at" => "2020-07-09T12:06:27.944Z"
100+
}
101+
},
102+
{
103+
"id" => 2,
104+
"plan_id" => 1,
105+
"name" => "Build Credits",
106+
"addon_type" => "credit_private",
107+
"created_at" => "2020-07-09T12:06:17.003Z",
108+
"updated_at" => "2020-07-09T12:07:09.067Z",
109+
"current_usage_id" => 2,
110+
"current_usage" => {
111+
"id" => 2,
112+
"addon_id" => 2,
113+
"addon_quantity" => 10000,
114+
"addon_usage" => 0,
115+
"purchase_date" => "2020-07-09T12:06:31.739Z",
116+
"valid_to" => nil,
117+
"status" => "active",
118+
"created_at" => "2020-07-09T12:06:31.741Z",
119+
"updated_at" => "2020-07-09T12:06:31.741Z"
120+
}
121+
}
122+
]
123+
}.deep_merge(attributes)
124+
end
125+
46126
def billing_plan_response_body(attributes={})
47127
{
48128
"id" => "travis-ci-ten-builds",

spec/v3/billing_client_spec.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@
6060
end
6161
end
6262

63+
describe '#get_invoices_for_v2_subscription' do
64+
subject { billing.get_invoices_for_v2_subscription(subscription_id) }
65+
66+
it 'returns a list of invoices' do
67+
stub_billing_request(:get, "/v2/subscriptions/#{subscription_id}/invoices", auth_key: auth_key, user_id: user_id)
68+
.to_return(body: JSON.dump([{'id' => invoice_id, 'created_at' => Time.now, 'url' => 'https://billing-test.travis-ci.com/invoices/111.pdf', amount_due: 999 }]))
69+
expect(subject.first).to be_a(Travis::API::V3::Models::Invoice)
70+
expect(subject.first.id).to eq(invoice_id)
71+
expect(subject.first.amount_due).to eq(999)
72+
end
73+
74+
it 'returns an empty list if there are no invoices' do
75+
stub_billing_request(:get, "/v2/subscriptions/#{subscription_id}/invoices", auth_key: auth_key, user_id: user_id)
76+
.to_return(body: JSON.dump([]))
77+
78+
expect(subject.size).to eq 0
79+
end
80+
end
81+
6382
describe '#all' do
6483
subject { billing.all }
6584

@@ -75,6 +94,21 @@
7594
end
7695
end
7796

97+
describe '#all_v2' do
98+
subject { billing.all_v2 }
99+
100+
let(:permissions) { [{'owner' => {'type' => 'Organization', 'id' => 1}, 'create' => true}] }
101+
102+
it 'returns the list of subscriptions' do
103+
stub_billing_request(:get, '/v2/subscriptions', auth_key: auth_key, user_id: user_id)
104+
.to_return(body: JSON.dump(subscriptions: [billing_v2_subscription_response_body('id' => subscription_id, 'owner' => { 'type' => 'Organization', 'id' => organization.id })], permissions: permissions))
105+
106+
expect(subject.subscriptions.size).to eq 1
107+
expect(subject.subscriptions.first.id).to eq(subscription_id)
108+
expect(subject.permissions).to eq(permissions)
109+
end
110+
end
111+
78112
describe '#update_address' do
79113
let(:address_data) { { 'address' => 'Rigaer Strasse' } }
80114
subject { billing.update_address(subscription_id, address_data) }
@@ -89,6 +123,20 @@
89123
end
90124
end
91125

126+
describe '#update_v2_address' do
127+
let(:address_data) { { 'address' => 'Rigaer Strasse' } }
128+
subject { billing.update_v2_address(subscription_id, address_data) }
129+
130+
it 'requests the update' do
131+
stubbed_request = stub_billing_request(:patch, "/v2/subscriptions/#{subscription_id}/address", auth_key: auth_key, user_id: user_id)
132+
.with(body: JSON.dump(address_data))
133+
.to_return(status: 204)
134+
135+
expect { subject }.to_not raise_error
136+
expect(stubbed_request).to have_been_made
137+
end
138+
end
139+
92140
describe '#update_plan' do
93141
let(:plan_data) { { 'plan' => 'travis-ci-ten-builds' } }
94142
subject { billing.update_plan(subscription_id, plan_data) }
@@ -117,6 +165,20 @@
117165
end
118166
end
119167

168+
describe '#update_v2_creditcard' do
169+
let(:creditcard_token) { 'token' }
170+
subject { billing.update_v2_creditcard(subscription_id, creditcard_token) }
171+
172+
it 'requests the update' do
173+
stubbed_request = stub_billing_request(:patch, "/v2/subscriptions/#{subscription_id}/creditcard", auth_key: auth_key, user_id: user_id)
174+
.with(body: JSON.dump(token: creditcard_token))
175+
.to_return(status: 204)
176+
177+
expect { subject }.to_not raise_error
178+
expect(stubbed_request).to have_been_made
179+
end
180+
end
181+
120182
describe '#resubscribe' do
121183
subject { billing.resubscribe(subscription_id) }
122184

@@ -156,6 +218,20 @@
156218
end
157219
end
158220

221+
describe '#create_v2_subscription' do
222+
let(:subscription_data) {{ 'address' => 'Rigaer' }}
223+
subject { billing.create_v2_subscription(subscription_data) }
224+
225+
it 'requests the creation and returns the representation' do
226+
stubbed_request = stub_billing_request(:post, "/v2/subscriptions", auth_key: auth_key, user_id: user_id)
227+
.with(body: JSON.dump(subscription_data))
228+
.to_return(status: 201, body: JSON.dump(billing_v2_subscription_response_body('id' => 456, 'owner' => { 'type' => 'Organization', 'id' => organization.id })))
229+
230+
expect(subject.id).to eq(456)
231+
expect(stubbed_request).to have_been_made
232+
end
233+
end
234+
159235
describe '#pay' do
160236
subject { billing.pay(subscription_id) }
161237

@@ -168,6 +244,18 @@
168244
end
169245
end
170246

247+
describe '#pay_v2' do
248+
subject { billing.pay_v2(subscription_id) }
249+
250+
it 'requests to retry payment' do
251+
stubbed_request = stub_billing_request(:post, "/v2/subscriptions/#{subscription_id}/pay", auth_key: auth_key, user_id: user_id)
252+
.to_return(status: 200, body: JSON.dump(billing_v2_subscription_response_body('id' => subscription_id, 'owner' => { 'type' => 'Organization', 'id' => organization.id })))
253+
254+
expect { subject }.to_not raise_error
255+
expect(stubbed_request).to have_been_made
256+
end
257+
end
258+
171259
describe '#trials' do
172260
subject { billing.trials }
173261
let(:trial_id) { rand(999) }

0 commit comments

Comments
 (0)