Skip to content

Commit

Permalink
feat(revshare): self billed invoice creation and skip payments (#3067)
Browse files Browse the repository at this point in the history
## Roadmap

👉 https://getlago.canny.io/feature-requests/p/calculate-revenue-share

 ## Context

Current problem: companies with **partners** selling for them cannot
have a **revenue share** system in Lago.

We want to propose **self-billing** into Lago, a billing arrangement
where the **customer** creates and issues the invoice on **behalf** of
the **supplier** for goods or services received.

 ## Description

expose self_billed attribute in v1 invoices serializer

create invoice as self_billed when customer is a partner

skip payment creation for self_billed invoices.
  • Loading branch information
ancorcruz authored Jan 20, 2025
1 parent 902a842 commit 8ea7d91
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/serializers/v1/invoice_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def serialize
prepaid_credit_amount_cents: model.prepaid_credit_amount_cents,
file_url: model.file_url,
version_number: model.version_number,
self_billed: model.self_billed,
created_at: model.created_at.iso8601,
updated_at: model.updated_at.iso8601
}
Expand Down
3 changes: 2 additions & 1 deletion app/services/invoices/create_generating_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def call
issuing_date:,
payment_due_date:,
net_payment_term: customer.applicable_net_payment_term,
skip_charges:
skip_charges:,
self_billed: customer.partner_account?
)
result.invoice = invoice

Expand Down
1 change: 1 addition & 0 deletions app/services/invoices/payments/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def provider
end

def should_process_payment?
return false if invoice.self_billed?
return false if invoice.payment_succeeded? || invoice.voided?
return false if current_payment_provider.blank?

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@
end
end
end

trait :self_billed do
self_billed { true }
end
end
end
1 change: 1 addition & 0 deletions spec/serializers/v1/invoice_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
}
],
"version_number" => 4,
"self_billed" => invoice.self_billed,
"created_at" => invoice.created_at.iso8601,
"updated_at" => invoice.updated_at.iso8601
)
Expand Down
10 changes: 10 additions & 0 deletions spec/services/invoices/create_generating_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,15 @@
end
end
end

context "when customer is a partner account" do
let(:customer) { create(:customer, account_type: "partner") }

it "creates an invoice with self billed" do
result = create_service.call

expect(result.invoice.self_billed).to eq(true)
end
end
end
end
15 changes: 15 additions & 0 deletions spec/services/invoices/payments/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@
end
end

context "when invoice is self_billed" do
let(:invoice) do
create(:invoice, :self_billed, customer:, organization:, total_amount_cents: 100)
end

it "does not creates a payment" do
result = create_service.call

expect(result).to be_success
expect(result.invoice).to eq(invoice)
expect(result.payment).to be_nil
expect(provider_class).not_to have_received(:new)
end
end

context "when invoice is payment_succeeded" do
before { invoice.payment_succeeded! }

Expand Down

0 comments on commit 8ea7d91

Please sign in to comment.