class Reji::Invoice

Attributes

owner[R]

Get the Stripe model instance.

Public Class Methods

new(owner, invoice) click to toggle source
# File lib/reji/invoice.rb, line 7
def initialize(owner, invoice)
  raise Reji::InvalidInvoiceError.invalid_owner(invoice, owner) if owner.stripe_id != invoice.customer

  @owner = owner
  @invoice = invoice
  @items = nil
  @taxes = nil
end

Public Instance Methods

amount_off() click to toggle source

Get the discount amount for the invoice.

# File lib/reji/invoice.rb, line 88
def amount_off
  format_amount(raw_amount_off)
end
as_stripe_invoice() click to toggle source

Get the Stripe invoice instance.

# File lib/reji/invoice.rb, line 203
def as_stripe_invoice
  @invoice
end
coupon() click to toggle source

Get the coupon code applied to the invoice.

# File lib/reji/invoice.rb, line 71
def coupon
  return @invoice[:discount][:coupon][:id] if @invoice[:discount]
end
date() click to toggle source

Get a date for the invoice.

# File lib/reji/invoice.rb, line 17
def date
  Time.zone.at(@invoice.created || @invoice.date)
end
discount() click to toggle source

Get the discount amount.

# File lib/reji/invoice.rb, line 57
def discount
  format_amount(raw_discount)
end
discount?() click to toggle source

Determine if the invoice has a discount.

# File lib/reji/invoice.rb, line 52
def discount?
  raw_discount > 0
end
discount_is_percentage() click to toggle source

Determine if the discount is a percentage.

# File lib/reji/invoice.rb, line 76
def discount_is_percentage
  return false unless @invoice[:discount]

  !!@invoice[:discount][:coupon][:percent_off]
end
download(data) click to toggle source

Create an invoice download response.

# File lib/reji/invoice.rb, line 181
def download(data)
  filename = "#{data[:product]}_#{date.month}_#{date.year}"

  download_as(filename, data)
end
download_as(filename, data) click to toggle source

Create an invoice download response with a specific filename.

# File lib/reji/invoice.rb, line 188
def download_as(filename, data)
  { data: pdf(data), filename: filename }
end
invoice_items() click to toggle source

Get all of the “invoice item” line items.

# File lib/reji/invoice.rb, line 141
def invoice_items
  invoice_line_items_by_type('invoiceitem')
end
invoice_line_items_by_type(type) click to toggle source

Get all of the invoice items by a given type.

# File lib/reji/invoice.rb, line 151
def invoice_line_items_by_type(type)
  if @items.nil?
    refresh_with_expanded_tax_rates

    @items = @invoice.lines.auto_paging_each
  end

  @items
    .select { |item| item.type == type }
    .map { |item| InvoiceLineItem.new(self, item) }
end
method_missing(key) click to toggle source

Dynamically get values from the Stripe invoice.

# File lib/reji/invoice.rb, line 208
def method_missing(key)
  @invoice[key]
end
not_tax_exempt?() click to toggle source

Determine if the customer is not exempted from taxes.

# File lib/reji/invoice.rb, line 126
def not_tax_exempt?
  @invoice[:customer_tax_exempt] == 'none'
end
pdf(data) click to toggle source

Capture the invoice as a PDF and return the raw bytes.

# File lib/reji/invoice.rb, line 176
def pdf(data)
  WickedPdf.new.pdf_from_string(view(data))
end
percent_off() click to toggle source

Get the discount percentage for the invoice.

# File lib/reji/invoice.rb, line 83
def percent_off
  coupon ? @invoice[:discount][:coupon][:percent_off] : 0
end
raw_amount_off() click to toggle source

Get the raw discount amount for the invoice.

# File lib/reji/invoice.rb, line 93
def raw_amount_off
  amount_off = @invoice[:discount][:coupon][:amount_off]

  amount_off || 0
end
raw_discount() click to toggle source

Get the raw discount amount.

# File lib/reji/invoice.rb, line 62
def raw_discount
  return 0 unless @invoice.discount

  return (@invoice.subtotal * (percent_off / 100)).round.to_i if discount_is_percentage

  raw_amount_off
end
raw_starting_balance() click to toggle source

Get the raw starting balance for the invoice.

# File lib/reji/invoice.rb, line 47
def raw_starting_balance
  @invoice[:starting_balance] || 0
end
raw_total() click to toggle source

Get the raw total amount that was paid (or will be paid).

# File lib/reji/invoice.rb, line 27
def raw_total
  @invoice.total + raw_starting_balance
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/reji/invoice.rb, line 212
def respond_to_missing?(method_name, include_private = false)
  super
end
reverse_charge_applies() click to toggle source

Determine if reverse charge applies to the customer.

# File lib/reji/invoice.rb, line 136
def reverse_charge_applies
  @invoice[:customer_tax_exempt] == 'reverse'
end
starting_balance() click to toggle source

Get the starting balance for the invoice.

# File lib/reji/invoice.rb, line 42
def starting_balance
  Reji.format_amount(raw_starting_balance)
end
starting_balance?() click to toggle source

Determine if the account had a starting balance.

# File lib/reji/invoice.rb, line 37
def starting_balance?
  raw_starting_balance < 0
end
subscriptions() click to toggle source

Get all of the “subscription” line items.

# File lib/reji/invoice.rb, line 146
def subscriptions
  invoice_line_items_by_type('subscription')
end
subtotal() click to toggle source

Get the total of the invoice (before discounts).

# File lib/reji/invoice.rb, line 32
def subtotal
  Reji.format_amount(@invoice[:subtotal])
end
tax() click to toggle source

Get the total tax amount.

# File lib/reji/invoice.rb, line 100
def tax
  format_amount(@invoice.tax)
end
tax?() click to toggle source

Determine if the invoice has tax applied.

# File lib/reji/invoice.rb, line 105
def tax?
  line_items = invoice_items + subscriptions

  line_items.any?(&:tax_rates?)
end
tax_exempt?() click to toggle source

Determine if the customer is exempted from taxes.

# File lib/reji/invoice.rb, line 131
def tax_exempt?
  @invoice[:customer_tax_exempt] == 'exempt'
end
taxes() click to toggle source

Get the taxes applied to the invoice.

# File lib/reji/invoice.rb, line 112
def taxes
  return @taxes unless @taxes.nil?

  refresh_with_expanded_tax_rates

  @taxes = @invoice.total_tax_amounts
    .sort_by(&:inclusive)
    .reverse
    .map { |tax_amount| Tax.new(tax_amount.amount, @invoice.currency, tax_amount.tax_rate) }

  @taxes
end
total() click to toggle source

Get the total amount that was paid (or will be paid).

# File lib/reji/invoice.rb, line 22
def total
  Reji.format_amount(raw_total)
end
view(data) click to toggle source

Get the View instance for the invoice.

# File lib/reji/invoice.rb, line 164
def view(data)
  ActionController::Base.new.render_to_string(
    template: 'receipt',
    locals: data.merge({
      invoice: self,
      owner: owner,
      user: owner,
    })
  )
end
void(options = {}) click to toggle source

Void the Stripe invoice.

# File lib/reji/invoice.rb, line 193
def void(options = {})
  @invoice = @invoice.void_invoice(options, @owner.stripe_options)

  self
end

Protected Instance Methods

format_amount(amount) click to toggle source

Format the given amount into a displayable currency.

# File lib/reji/invoice.rb, line 240
          def format_amount(amount)
  Reji.format_amount(amount, @invoice.currency)
end
refresh_with_expanded_tax_rates() click to toggle source

Refresh the invoice with expanded TaxRate objects.

# File lib/reji/invoice.rb, line 217
          def refresh_with_expanded_tax_rates
  @invoice =
    if @invoice.id
      Stripe::Invoice.retrieve({
        id: @invoice.id,
        expand: [
          'lines.data.tax_amounts.tax_rate',
          'total_tax_amounts.tax_rate',
        ],
      }, @owner.stripe_options)
    else
      # If no invoice ID is present then assume this is the customer's upcoming invoice...
      Stripe::Invoice.upcoming({
        customer: @owner.stripe_id,
        expand: [
          'lines.data.tax_amounts.tax_rate',
          'total_tax_amounts.tax_rate',
        ],
      }, @owner.stripe_options)
    end
end