module Reji::ManagesInvoices

Public Instance Methods

download_invoice(id, data, filename = nil) click to toggle source

Create an invoice download response.

# File lib/reji/concerns/manages_invoices.rb, line 95
def download_invoice(id, data, filename = nil)
  invoice = find_invoice_or_fail(id)

  filename ? invoice.download_as(filename, data) : invoice.download(data)
end
find_invoice(id) click to toggle source

Find an invoice by ID.

# File lib/reji/concerns/manages_invoices.rb, line 69
def find_invoice(id)
  stripe_invoice = nil

  begin
    stripe_invoice = Stripe::Invoice.retrieve(id, stripe_options)
  rescue StandardError => _e
    #
  end

  stripe_invoice ? Invoice.new(self, stripe_invoice) : nil
end
find_invoice_or_fail(id) click to toggle source

Find an invoice or throw a 404 or 403 error.

# File lib/reji/concerns/manages_invoices.rb, line 82
def find_invoice_or_fail(id)
  begin
    invoice = find_invoice(id)
  rescue InvalidInvoiceError => e
    raise Reji::AccessDeniedHttpError, e.message
  end

  raise ActiveRecord::RecordNotFound if invoice.nil?

  invoice
end
invoice(options = {}) click to toggle source

Invoice the billable entity outside of the regular billing cycle.

# File lib/reji/concerns/manages_invoices.rb, line 29
def invoice(options = {})
  assert_customer_exists

  begin
    stripe_invoice = Stripe::Invoice.create(options.merge({ customer: stripe_id }), stripe_options)

    stripe_invoice =
      if stripe_invoice.collection_method == 'charge_automatically'
        stripe_invoice.pay
      else
        stripe_invoice.send_invoice
      end

    Invoice.new(self, stripe_invoice)
  rescue Stripe::InvalidRequestError => _e
    false
  rescue Stripe::CardError => _e
    Payment.new(
      Stripe::PaymentIntent.retrieve(
        { id: stripe_invoice.payment_intent, expand: ['invoice.subscription'] },
        stripe_options
      )
    ).validate
  end
end
invoice_for(description, amount, tab_options = {}, invoice_options = {}) click to toggle source

Invoice the customer for the given amount and generate an invoice immediately.

# File lib/reji/concerns/manages_invoices.rb, line 22
def invoice_for(description, amount, tab_options = {}, invoice_options = {})
  tab(description, amount, tab_options)

  invoice(invoice_options)
end
invoices(include_pending = false, parameters = {}) click to toggle source

Get a collection of the entity's invoices.

# File lib/reji/concerns/manages_invoices.rb, line 102
def invoices(include_pending = false, parameters = {})
  return [] unless stripe_id?

  invoices = []

  parameters = { limit: 24 }.merge(parameters)

  stripe_invoices = Stripe::Invoice.list(
    { customer: stripe_id }.merge(parameters),
    stripe_options
  )

  # Here we will loop through the Stripe invoices and create our own custom Invoice
  # instances that have more helper methods and are generally more convenient to
  # work with than the plain Stripe objects are. Then, we'll return the array.
  stripe_invoices&.data&.each do |invoice|
    invoices << Invoice.new(self, invoice) if invoice.paid || include_pending
  end

  invoices
end
invoices_include_pending(parameters = {}) click to toggle source

Get an array of the entity's invoices.

# File lib/reji/concerns/manages_invoices.rb, line 125
def invoices_include_pending(parameters = {})
  invoices(true, parameters)
end
tab(description, amount, options = {}) click to toggle source

Add an invoice item to the customer's upcoming invoice.

# File lib/reji/concerns/manages_invoices.rb, line 8
def tab(description, amount, options = {})
  assert_customer_exists

  options = {
    customer: stripe_id,
    amount: amount,
    currency: preferred_currency,
    description: description,
  }.merge(options)

  Stripe::InvoiceItem.create(options, stripe_options)
end
upcoming_invoice() click to toggle source

Get the entity's upcoming invoice.

# File lib/reji/concerns/manages_invoices.rb, line 56
def upcoming_invoice
  return unless stripe_id?

  begin
    stripe_invoice = Stripe::Invoice.upcoming({ customer: stripe_id }, stripe_options)

    Invoice.new(self, stripe_invoice)
  rescue Stripe::InvalidRequestError => _e
    #
  end
end