-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Credits::ProgressiveBillingService and add it to Invoices::…
…CalculateFeesService (#2443) ## Context AI companies want their users to pay before the end of a period if usage skyrockets. The problem being that self-serve companies can overuse their API without paying, triggering lots of costs on their side. ## Description Adds a service for calculating how much to deduct from the subscription invoice based on previously billed progressive invoices.
- Loading branch information
Showing
8 changed files
with
377 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module Credits | ||
class ProgressiveBillingService < BaseService | ||
def initialize(invoice:) | ||
@invoice = invoice | ||
super | ||
end | ||
|
||
def call | ||
result.credits = [] | ||
return result unless should_create_progressive_billing_credit? | ||
|
||
invoice.invoice_subscriptions.each do |invoice_subscription| | ||
subscription = invoice_subscription.subscription | ||
progressive_billing_invoices = subscription | ||
.invoices | ||
.progressive_billing | ||
.finalized | ||
.where(issuing_date: invoice_subscription.charges_from_datetime...invoice_subscription.charges_to_datetime) | ||
.order(issuing_date: :asc) | ||
|
||
total_subscription_amount = invoice.fees.charge.where(subscription: subscription).sum(:amount_cents) | ||
|
||
remaining_to_credit = total_subscription_amount | ||
|
||
progressive_billing_invoices.each do |progressive_billing_invoice| | ||
amount_to_credit = progressive_billing_invoice.fees.progressive_billing.sum(:amount_cents) | ||
|
||
if amount_to_credit > remaining_to_credit | ||
# TODO: create credit note for (amount_to_credit - remaining_credit) | ||
invoice.negative_amount_cents -= (amount_to_credit - remaining_to_credit) | ||
amount_to_credit = remaining_to_credit | ||
end | ||
|
||
if amount_to_credit.positive? | ||
credit = Credit.create!( | ||
invoice:, | ||
progressive_billing_invoice:, | ||
amount_cents: amount_to_credit, | ||
amount_currency: invoice.currency, | ||
before_taxes: true | ||
) | ||
|
||
invoice.sub_total_excluding_taxes_amount_cents -= credit.amount_cents | ||
invoice.progressive_billing_credit_amount_cents += credit.amount_cents | ||
result.credits << credit | ||
|
||
remaining_to_credit -= amount_to_credit | ||
end | ||
end | ||
end | ||
result | ||
end | ||
|
||
private | ||
|
||
def should_create_progressive_billing_credit? | ||
invoice.invoice_subscriptions.any? do |invoice_subscription| | ||
invoice_subscription.subscription.invoices.progressive_billing | ||
.finalized | ||
.where(issuing_date: invoice_subscription.charges_from_datetime...invoice_subscription.charges_to_datetime) | ||
.exists? | ||
end | ||
end | ||
|
||
attr_reader :invoice | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
db/migrate/20240820125840_add_progressive_billing_credit_amount_cents_to_invoices.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddProgressiveBillingCreditAmountCentsToInvoices < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :invoices, :progressive_billing_credit_amount_cents, :bigint, default: 0, null: false | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.