Module: Reji::ManagesInvoices

Extended by:
ActiveSupport::Concern
Included in:
Billable
Defined in:
lib/reji/concerns/manages_invoices.rb

Instance Method Summary collapse

Instance Method Details

#download_invoice(id, data, filename = nil) ⇒ Object

Create an invoice download response.



95
96
97
98
99
# 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) ⇒ Object

Find an invoice by ID.



69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object

Find an invoice or throw a 404 or 403 error.

Raises:

  • (ActiveRecord::RecordNotFound)


82
83
84
85
86
87
88
89
90
91
92
# 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 = {}) ⇒ Object

Invoice the billable entity outside of the regular billing cycle.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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 = {}) ⇒ Object

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



22
23
24
25
26
# 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 = {}) ⇒ Object

Get a collection of the entity’s invoices.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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 = {}) ⇒ Object

Get an array of the entity’s invoices.



125
126
127
# File 'lib/reji/concerns/manages_invoices.rb', line 125

def invoices_include_pending(parameters = {})
  invoices(true, parameters)
end

#tab(description, amount, options = {}) ⇒ Object

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



8
9
10
11
12
13
14
15
16
17
18
19
# 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_invoiceObject

Get the entity’s upcoming invoice.



56
57
58
59
60
61
62
63
64
65
66
# 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