class Reji::Invoice
Attributes
Get the Stripe model instance.
Public Class Methods
# 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
Get the discount amount for the invoice.
# File lib/reji/invoice.rb, line 88 def amount_off format_amount(raw_amount_off) end
Get the Stripe invoice instance.
# File lib/reji/invoice.rb, line 203 def as_stripe_invoice @invoice end
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
Get a date for the invoice.
# File lib/reji/invoice.rb, line 17 def date Time.zone.at(@invoice.created || @invoice.date) end
Get the discount amount.
# File lib/reji/invoice.rb, line 57 def discount format_amount(raw_discount) end
Determine if the invoice has a discount.
# File lib/reji/invoice.rb, line 52 def discount? raw_discount > 0 end
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
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
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
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
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
Dynamically get values from the Stripe invoice.
# File lib/reji/invoice.rb, line 208 def method_missing(key) @invoice[key] end
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
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
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
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
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
Get the raw starting balance for the invoice.
# File lib/reji/invoice.rb, line 47 def raw_starting_balance @invoice[:starting_balance] || 0 end
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
# File lib/reji/invoice.rb, line 212 def respond_to_missing?(method_name, include_private = false) super end
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
Get the starting balance for the invoice.
# File lib/reji/invoice.rb, line 42 def starting_balance Reji.format_amount(raw_starting_balance) end
Determine if the account had a starting balance.
# File lib/reji/invoice.rb, line 37 def starting_balance? raw_starting_balance < 0 end
Get all of the “subscription” line items.
# File lib/reji/invoice.rb, line 146 def subscriptions invoice_line_items_by_type('subscription') end
Get the total of the invoice (before discounts).
# File lib/reji/invoice.rb, line 32 def subtotal Reji.format_amount(@invoice[:subtotal]) end
Get the total tax amount.
# File lib/reji/invoice.rb, line 100 def tax format_amount(@invoice.tax) end
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
Determine if the customer is exempted from taxes.
# File lib/reji/invoice.rb, line 131 def tax_exempt? @invoice[:customer_tax_exempt] == 'exempt' end
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
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
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 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 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 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