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.



98
99
100
101
102
# File 'lib/reji/concerns/manages_invoices.rb', line 98

def download_invoice(id, data, filename = nil)
  invoice = self.find_invoice_or_fail(id)

  filename ? invoice.download_as(filename, data) : invoice.download(data)
end

#find_invoice(id) ⇒ Object

Find an invoice by ID.



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/reji/concerns/manages_invoices.rb', line 72

def find_invoice(id)
  stripe_invoice = nil

  begin
    stripe_invoice = Stripe::Invoice.retrieve(id, self.stripe_options)
  rescue => 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)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/reji/concerns/manages_invoices.rb', line 85

def find_invoice_or_fail(id)
  begin
    invoice = self.find_invoice(id)
  rescue InvalidInvoiceError => e
    raise Reji::AccessDeniedHttpError.new(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
54
55
56
# File 'lib/reji/concerns/manages_invoices.rb', line 29

def invoice(options = {})
  self.assert_customer_exists

  parameters = options.merge({:customer => self.stripe_id})

  begin
    stripe_invoice = Stripe::Invoice.create(parameters, self.stripe_options)

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

    Invoice.new(self, stripe_invoice)
  rescue Stripe::InvalidRequestError => e
    false
  rescue Stripe::CardError => e
    payment = Payment.new(
      Stripe::PaymentIntent.retrieve(
        {:id => stripe_invoice.payment_intent, :expand => ['invoice.subscription']},
        self.stripe_options
      )
    )

    payment.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 = {})
  self.tab(description, amount, tab_options)

  self.invoice(invoice_options)
end

#invoices(include_pending = false, parameters = {}) ⇒ Object

Get a collection of the entity’s invoices.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/reji/concerns/manages_invoices.rb', line 105

def invoices(include_pending = false, parameters = {})
  return [] unless self.has_stripe_id

  invoices = []

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

  stripe_invoices = Stripe::Invoice.list(
    {:customer => self.stripe_id}.merge(parameters),
    self.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.
  unless stripe_invoices.nil?
    stripe_invoices.data.each do |invoice|
      if invoice.paid || include_pending
        invoices << Invoice.new(self, invoice)
      end
    end
  end

  invoices
end

#invoices_include_pending(parameters = {}) ⇒ Object

Get an array of the entity’s invoices.



132
133
134
# File 'lib/reji/concerns/manages_invoices.rb', line 132

def invoices_include_pending(parameters = {})
  self.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 = {})
  self.assert_customer_exists

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

  Stripe::InvoiceItem.create(options, self.stripe_options)
end

#upcoming_invoiceObject

Get the entity’s upcoming invoice.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/reji/concerns/manages_invoices.rb', line 59

def upcoming_invoice
  return unless self.has_stripe_id

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

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