Skip to content

Commit 79f626f

Browse files
authored
Merge pull request #358 from getlago/ING-70-multi-billing-entity
[ING-70] feat(multi-entity): support billing_entity_code
2 parents b7a63f0 + 2247088 commit 79f626f

10 files changed

Lines changed: 185 additions & 5 deletions

File tree

lib/lago/api/resources/customers/wallets/whitelist_params.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def wallet(params)
2323
:transaction_name,
2424
:paid_top_up_min_amount_cents,
2525
:paid_top_up_max_amount_cents,
26+
:billing_entity_code,
2627
)
2728

2829
recurring_rules = recurring_rules_params(params[:recurring_transaction_rules])

lib/lago/api/resources/invoice.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def payment_url(invoice_id)
8686
def preview(params)
8787
path = "/api/v1/invoices/preview"
8888
payload = params.slice(
89-
:customer, :plan_code, :subscription_at, :billing_time, :coupons, :subscriptions
89+
:customer, :plan_code, :subscription_at, :billing_time, :coupons, :subscriptions, :billing_entity_code
9090
)
9191
response = connection.post(payload, path)[root_name]
9292

@@ -125,8 +125,9 @@ def one_off_params(params)
125125
external_customer_id: params[:external_customer_id],
126126
currency: params[:currency],
127127
net_payment_term: params[:net_payment_term],
128-
skip_psp: params[:skip_psp]
129-
}
128+
skip_psp: params[:skip_psp],
129+
billing_entity_code: params[:billing_entity_code]
130+
}.compact
130131

131132
fees = whitelist_fees(params[:fees])
132133
result[:fees] = fees unless fees.empty?

lib/lago/api/resources/subscription.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ def whitelist_params(params)
213213
ending_at: params[:ending_at],
214214
plan_overrides: params[:plan_overrides],
215215
consolidate_invoice: params[:consolidate_invoice],
216+
billing_entity_code: params[:billing_entity_code],
216217
}.compact
217218

218219
payment_method_params = whitelist_payment_method_params(params[:payment_method])

spec/factories/subscription.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
canceled_at { nil }
1717
created_at { '2022-05-05T12:27:30Z' }
1818
consolidate_invoice { true }
19+
billing_entity_code { 'acme_corp' }
1920
end
2021
end

spec/integration/customers/wallets_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ def assert_wallet_attributes_with_updated_balance(wallet, **attributes)
7373
end
7474

7575
def wait_for_balance_update(wallet)
76+
# balance_cents is synced synchronously when the wallet transaction settles, but the ongoing
77+
# balance and pending transactions are reconciled by RefreshWalletJob. Wait for both so any
78+
# subsequent destroy/assertion sees a fully settled wallet.
7679
new_wallet = nil
7780
wait_until do
7881
new_wallet = client.customers.wallets.get(wallet.external_customer_id, wallet.code)
79-
new_wallet.balance_cents == 2000
82+
new_wallet.balance_cents == 2000 && new_wallet.ongoing_balance_cents == 2000
8083
end
8184
new_wallet
8285
end

spec/integration/wallets_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,13 @@ def assert_wallet_attributes_with_updated_balance(wallet, **attributes)
7171
end
7272

7373
def wait_for_balance_update(wallet_id)
74+
# balance_cents is synced synchronously when the wallet transaction settles, but the ongoing
75+
# balance and pending transactions are reconciled by RefreshWalletJob. Wait for both so any
76+
# subsequent destroy/assertion sees a fully settled wallet.
7477
wallet = nil
7578
wait_until do
7679
wallet = client.wallets.get(wallet_id)
77-
wallet.balance_cents == 2000
80+
wallet.balance_cents == 2000 && wallet.ongoing_balance_cents == 2000
7881
end
7982
wallet
8083
end

spec/lago/api/resources/invoice_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@
160160
end
161161
end
162162
end
163+
164+
context 'when billing_entity_code is provided' do
165+
let(:params) do
166+
{
167+
external_customer_id: '_ID_',
168+
currency: 'EUR',
169+
net_payment_term: 0,
170+
skip_psp: true,
171+
billing_entity_code: 'eu_entity',
172+
fees: [
173+
{
174+
add_on_code: '123',
175+
description: 'desc',
176+
tax_codes: [tax.code],
177+
}
178+
]
179+
}
180+
end
181+
182+
before do
183+
stub_request(:post, 'https://api.getlago.com/api/v1/invoices')
184+
.with(body: { invoice: params })
185+
.to_return(body: invoice_response, status: 200)
186+
end
187+
188+
it 'returns invoice' do
189+
invoice = resource.create(params)
190+
191+
expect(invoice.lago_id).to eq(invoice_id)
192+
end
193+
end
163194
end
164195

165196
describe '#update' do
@@ -525,6 +556,27 @@
525556
expect { subject }.to raise_error(Lago::Api::HttpError)
526557
end
527558
end
559+
560+
context 'when billing_entity_code is provided' do
561+
let(:invoice_response) { load_fixture('invoice_preview') }
562+
let(:params) do
563+
{
564+
customer: { external_id: '_ID_' },
565+
plan_code: 'plan_code',
566+
billing_entity_code: 'eu_entity'
567+
}
568+
end
569+
570+
before do
571+
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
572+
.with(body: params)
573+
.to_return(body: invoice_response, status: 200)
574+
end
575+
576+
it 'forwards billing_entity_code to the preview endpoint' do
577+
expect(subject).to have_attributes(lago_id: nil)
578+
end
579+
end
528580
end
529581

530582
describe '#void (with params)' do

spec/lago/api/resources/payment_request_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@
9696
end
9797
end
9898

99+
context 'when billing_entity_codes is given' do
100+
before do
101+
stub_request(
102+
:get,
103+
'https://api.getlago.com/api/v1/payment_requests?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
104+
).to_return(body: payment_requests_response, status: 200)
105+
end
106+
107+
it 'returns payment requests filtered by billing_entity_codes' do
108+
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })
109+
110+
expect(response['payment_requests'].first['lago_id']).to eq(payment_request_id)
111+
expect(response['meta']['current_page']).to eq(1)
112+
end
113+
end
114+
99115
context 'when there is an issue' do
100116
before do
101117
stub_request(:get, 'https://api.getlago.com/api/v1/payment_requests')

spec/lago/api/resources/subscription_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@
196196
expect { resource.create(params_with_invalid_pm) }.to raise_error Lago::Api::HttpError
197197
end
198198
end
199+
200+
context 'when billing_entity_code is provided' do
201+
let(:params_with_billing_entity) { params.merge(billing_entity_code: 'eu_entity') }
202+
let(:body_with_billing_entity) { { 'subscription' => params_with_billing_entity } }
203+
204+
before do
205+
stub_request(:post, 'https://api.getlago.com/api/v1/subscriptions')
206+
.with(body: body_with_billing_entity)
207+
.to_return(body: response, status: 200)
208+
end
209+
210+
it 'returns subscription with billing_entity_code' do
211+
subscription = resource.create(params_with_billing_entity)
212+
213+
expect(subscription.billing_entity_code).to eq(factory_subscription.billing_entity_code)
214+
end
215+
end
199216
end
200217

201218
describe '#delete' do
@@ -356,6 +373,23 @@
356373
expect { resource.update(params_with_invalid_pm, '123') }.to raise_error Lago::Api::HttpError
357374
end
358375
end
376+
377+
context 'when billing_entity_code is provided' do
378+
let(:params_with_billing_entity) { { name: 'new name', billing_entity_code: 'us_entity' } }
379+
let(:body_with_billing_entity) { { 'subscription' => params_with_billing_entity } }
380+
381+
before do
382+
stub_request(:put, 'https://api.getlago.com/api/v1/subscriptions/123')
383+
.with(body: body_with_billing_entity)
384+
.to_return(body: response, status: 200)
385+
end
386+
387+
it 'returns subscription with billing_entity_code' do
388+
subscription = resource.update(params_with_billing_entity, '123')
389+
390+
expect(subscription.billing_entity_code).to eq(factory_subscription.billing_entity_code)
391+
end
392+
end
359393
end
360394

361395
describe '#get' do
@@ -432,6 +466,22 @@
432466
end
433467
end
434468

469+
context 'when billing_entity_codes is given' do
470+
before do
471+
stub_request(
472+
:get,
473+
'https://api.getlago.com/api/v1/subscriptions?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
474+
).to_return(body: response, status: 200)
475+
end
476+
477+
it 'returns subscriptions filtered by billing_entity_codes' do
478+
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })
479+
480+
expect(response['subscriptions'].first['lago_id']).to eq(factory_subscription.lago_id)
481+
expect(response['meta']['current_page']).to eq(1)
482+
end
483+
end
484+
435485
context 'when there is an issue' do
436486
before do
437487
stub_request(:get, 'https://api.getlago.com/api/v1/subscriptions')

spec/lago/api/resources/wallet_spec.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,42 @@
333333
end
334334
end
335335
end
336+
337+
context 'when billing_entity_code is provided' do
338+
let(:params_with_billing_entity) { params.merge(billing_entity_code: 'eu_entity') }
339+
let(:body_with_billing_entity) do
340+
body['wallet']['billing_entity_code'] = 'eu_entity'
341+
body
342+
end
343+
let(:response_with_billing_entity) do
344+
{
345+
'wallet' => {
346+
'lago_id' => 'this-is-lago-id',
347+
'lago_customer_id' => factory_wallet.id,
348+
'name' => factory_wallet.name,
349+
'billing_entity_code' => 'eu_entity',
350+
'expiration_at' => factory_wallet.expiration_at,
351+
'balance_cents' => 10_000,
352+
'rate_amount' => factory_wallet.rate_amount,
353+
'created_at' => '2022-04-29T08:59:51Z',
354+
'recurring_transaction_rules' => factory_wallet.recurring_transaction_rules,
355+
'applies_to' => factory_wallet.applies_to,
356+
}
357+
}.to_json
358+
end
359+
360+
before do
361+
stub_request(:post, 'https://api.getlago.com/api/v1/wallets')
362+
.with(body: body_with_billing_entity)
363+
.to_return(body: response_with_billing_entity, status: 200)
364+
end
365+
366+
it 'returns a wallet with billing_entity_code' do
367+
wallet = resource.create(params_with_billing_entity)
368+
369+
expect(wallet.billing_entity_code).to eq('eu_entity')
370+
end
371+
end
336372
end
337373

338374
describe '#update' do
@@ -675,6 +711,22 @@
675711
end
676712
end
677713

714+
context 'when billing_entity_codes is given' do
715+
before do
716+
stub_request(
717+
:get,
718+
'https://api.getlago.com/api/v1/wallets?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
719+
).to_return(body: response, status: 200)
720+
end
721+
722+
it 'returns wallets filtered by billing_entity_codes' do
723+
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })
724+
725+
expect(response['wallets'].first['lago_id']).to eq('this-is-lago-id')
726+
expect(response['meta']['current_page']).to eq(1)
727+
end
728+
end
729+
678730
context 'when there is an issue' do
679731
before do
680732
stub_request(:get, 'https://api.getlago.com/api/v1/wallets')

0 commit comments

Comments
 (0)