Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def wallet(params)
:transaction_name,
:paid_top_up_min_amount_cents,
:paid_top_up_max_amount_cents,
:billing_entity_code,
)

recurring_rules = recurring_rules_params(params[:recurring_transaction_rules])
Expand Down
7 changes: 4 additions & 3 deletions lib/lago/api/resources/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def payment_url(invoice_id)
def preview(params)
path = "/api/v1/invoices/preview"
payload = params.slice(
:customer, :plan_code, :subscription_at, :billing_time, :coupons, :subscriptions
:customer, :plan_code, :subscription_at, :billing_time, :coupons, :subscriptions, :billing_entity_code
)
response = connection.post(payload, path)[root_name]

Expand Down Expand Up @@ -125,8 +125,9 @@ def one_off_params(params)
external_customer_id: params[:external_customer_id],
currency: params[:currency],
net_payment_term: params[:net_payment_term],
skip_psp: params[:skip_psp]
}
skip_psp: params[:skip_psp],
billing_entity_code: params[:billing_entity_code]
}.compact

fees = whitelist_fees(params[:fees])
result[:fees] = fees unless fees.empty?
Expand Down
1 change: 1 addition & 0 deletions lib/lago/api/resources/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def whitelist_params(params)
ending_at: params[:ending_at],
plan_overrides: params[:plan_overrides],
consolidate_invoice: params[:consolidate_invoice],
billing_entity_code: params[:billing_entity_code],
}.compact

payment_method_params = whitelist_payment_method_params(params[:payment_method])
Expand Down
1 change: 1 addition & 0 deletions spec/factories/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
canceled_at { nil }
created_at { '2022-05-05T12:27:30Z' }
consolidate_invoice { true }
billing_entity_code { 'acme_corp' }
end
end
5 changes: 4 additions & 1 deletion spec/integration/customers/wallets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ def assert_wallet_attributes_with_updated_balance(wallet, **attributes)
end

def wait_for_balance_update(wallet)
# balance_cents is synced synchronously when the wallet transaction settles, but the ongoing
# balance and pending transactions are reconciled by RefreshWalletJob. Wait for both so any
# subsequent destroy/assertion sees a fully settled wallet.
new_wallet = nil
wait_until do
new_wallet = client.customers.wallets.get(wallet.external_customer_id, wallet.code)
new_wallet.balance_cents == 2000
new_wallet.balance_cents == 2000 && new_wallet.ongoing_balance_cents == 2000
end
new_wallet
end
Expand Down
5 changes: 4 additions & 1 deletion spec/integration/wallets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ def assert_wallet_attributes_with_updated_balance(wallet, **attributes)
end

def wait_for_balance_update(wallet_id)
# balance_cents is synced synchronously when the wallet transaction settles, but the ongoing
# balance and pending transactions are reconciled by RefreshWalletJob. Wait for both so any
# subsequent destroy/assertion sees a fully settled wallet.
wallet = nil
wait_until do
wallet = client.wallets.get(wallet_id)
wallet.balance_cents == 2000
wallet.balance_cents == 2000 && wallet.ongoing_balance_cents == 2000
end
wallet
end
Expand Down
52 changes: 52 additions & 0 deletions spec/lago/api/resources/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@
end
end
end

context 'when billing_entity_code is provided' do
let(:params) do
{
external_customer_id: '_ID_',
currency: 'EUR',
net_payment_term: 0,
skip_psp: true,
billing_entity_code: 'eu_entity',
fees: [
{
add_on_code: '123',
description: 'desc',
tax_codes: [tax.code],
}
]
}
end

before do
stub_request(:post, 'https://api.getlago.com/api/v1/invoices')
.with(body: { invoice: params })
.to_return(body: invoice_response, status: 200)
end

it 'returns invoice' do
invoice = resource.create(params)

expect(invoice.lago_id).to eq(invoice_id)
end
end
end

describe '#update' do
Expand Down Expand Up @@ -525,6 +556,27 @@
expect { subject }.to raise_error(Lago::Api::HttpError)
end
end

context 'when billing_entity_code is provided' do
let(:invoice_response) { load_fixture('invoice_preview') }
let(:params) do
{
customer: { external_id: '_ID_' },
plan_code: 'plan_code',
billing_entity_code: 'eu_entity'
}
end

before do
stub_request(:post, 'https://api.getlago.com/api/v1/invoices/preview')
.with(body: params)
.to_return(body: invoice_response, status: 200)
end

it 'forwards billing_entity_code to the preview endpoint' do
expect(subject).to have_attributes(lago_id: nil)
end
end
end

describe '#void (with params)' do
Expand Down
16 changes: 16 additions & 0 deletions spec/lago/api/resources/payment_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@
end
end

context 'when billing_entity_codes is given' do
before do
stub_request(
:get,
'https://api.getlago.com/api/v1/payment_requests?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
).to_return(body: payment_requests_response, status: 200)
end

it 'returns payment requests filtered by billing_entity_codes' do
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })

expect(response['payment_requests'].first['lago_id']).to eq(payment_request_id)
expect(response['meta']['current_page']).to eq(1)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/payment_requests')
Expand Down
50 changes: 50 additions & 0 deletions spec/lago/api/resources/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@
expect { resource.create(params_with_invalid_pm) }.to raise_error Lago::Api::HttpError
end
end

context 'when billing_entity_code is provided' do
let(:params_with_billing_entity) { params.merge(billing_entity_code: 'eu_entity') }
let(:body_with_billing_entity) { { 'subscription' => params_with_billing_entity } }

before do
stub_request(:post, 'https://api.getlago.com/api/v1/subscriptions')
.with(body: body_with_billing_entity)
.to_return(body: response, status: 200)
end

it 'returns subscription with billing_entity_code' do
subscription = resource.create(params_with_billing_entity)

expect(subscription.billing_entity_code).to eq(factory_subscription.billing_entity_code)
end
end
end

describe '#delete' do
Expand Down Expand Up @@ -356,6 +373,23 @@
expect { resource.update(params_with_invalid_pm, '123') }.to raise_error Lago::Api::HttpError
end
end

context 'when billing_entity_code is provided' do
let(:params_with_billing_entity) { { name: 'new name', billing_entity_code: 'us_entity' } }
let(:body_with_billing_entity) { { 'subscription' => params_with_billing_entity } }

before do
stub_request(:put, 'https://api.getlago.com/api/v1/subscriptions/123')
.with(body: body_with_billing_entity)
.to_return(body: response, status: 200)
end

it 'returns subscription with billing_entity_code' do
subscription = resource.update(params_with_billing_entity, '123')

expect(subscription.billing_entity_code).to eq(factory_subscription.billing_entity_code)
end
end
end

describe '#get' do
Expand Down Expand Up @@ -432,6 +466,22 @@
end
end

context 'when billing_entity_codes is given' do
before do
stub_request(
:get,
'https://api.getlago.com/api/v1/subscriptions?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
).to_return(body: response, status: 200)
end

it 'returns subscriptions filtered by billing_entity_codes' do
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })

expect(response['subscriptions'].first['lago_id']).to eq(factory_subscription.lago_id)
expect(response['meta']['current_page']).to eq(1)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/subscriptions')
Expand Down
52 changes: 52 additions & 0 deletions spec/lago/api/resources/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,42 @@
end
end
end

context 'when billing_entity_code is provided' do
let(:params_with_billing_entity) { params.merge(billing_entity_code: 'eu_entity') }
let(:body_with_billing_entity) do
body['wallet']['billing_entity_code'] = 'eu_entity'
body
end
let(:response_with_billing_entity) do
{
'wallet' => {
'lago_id' => 'this-is-lago-id',
'lago_customer_id' => factory_wallet.id,
'name' => factory_wallet.name,
'billing_entity_code' => 'eu_entity',
'expiration_at' => factory_wallet.expiration_at,
'balance_cents' => 10_000,
'rate_amount' => factory_wallet.rate_amount,
'created_at' => '2022-04-29T08:59:51Z',
'recurring_transaction_rules' => factory_wallet.recurring_transaction_rules,
'applies_to' => factory_wallet.applies_to,
}
}.to_json
end

before do
stub_request(:post, 'https://api.getlago.com/api/v1/wallets')
.with(body: body_with_billing_entity)
.to_return(body: response_with_billing_entity, status: 200)
end

it 'returns a wallet with billing_entity_code' do
wallet = resource.create(params_with_billing_entity)

expect(wallet.billing_entity_code).to eq('eu_entity')
end
end
end

describe '#update' do
Expand Down Expand Up @@ -675,6 +711,22 @@
end
end

context 'when billing_entity_codes is given' do
before do
stub_request(
:get,
'https://api.getlago.com/api/v1/wallets?billing_entity_codes%5B%5D=eu_entity&billing_entity_codes%5B%5D=us_entity',
).to_return(body: response, status: 200)
end

it 'returns wallets filtered by billing_entity_codes' do
response = resource.get_all({ 'billing_entity_codes[]': %w[eu_entity us_entity] })

expect(response['wallets'].first['lago_id']).to eq('this-is-lago-id')
expect(response['meta']['current_page']).to eq(1)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/wallets')
Expand Down
Loading