Skip to Content Skip to Search
Methods
D
N

Class Public methods

data(stripe_invoice)

# File app/services/service/invoice_policy/stripe_invoice.rb, line 4
def self.data(stripe_invoice)
  if stripe_invoice.try(:object) == "invoice"
    # If this method exists, that tells us it's not just a hash
    # If it returns the string `invoice` then it's the expected Stripe API object
    # - checking this method and its value seems stable enough
    # - feel free to implement any better idea
    new(stripe_invoice).data
  else
    Stripe::Invoice.new
  end
end

new(stripe_invoice)

# File app/services/service/invoice_policy/stripe_invoice.rb, line 16
def initialize(stripe_invoice)
  @data = stripe_invoice
  @items = generate_items
end

Instance Public methods

data()

# File app/services/service/invoice_policy/stripe_invoice.rb, line 21
def data
  Stripe::Invoice.construct_from(
    {
      id: @data.try(:id), # Upcoming invoices don't have an ID
      customer_id: @data.customer,
      status: @data.paid ? "Paid" : "Not Paid",
      timestamp: @data.created,
      timestamp_start: @data.period_start,
      timestamp_end: @data.period_end,
      description: get_description,
      total: @data.total,
      applied_balance: applied_balance,
      amount_due: @data.amount_due,
      currency: @data.currency,
      coupon: get_coupon,
      amount_refunded: @data.try(:amount_refunded), # Not all invoices have `amount_refunded`
      items: @items,
      tax: @data.tax,
      tax_percent: @data.tax_percent
    },
    {stripe_version: Rails.application.config.legacy_stripe_api_version}
  )
end