Skip to content

Commit

Permalink
Fix invoice item issues
Browse files Browse the repository at this point in the history
- Fixed invoice getting saved on InvoiceItem as a string of the object
- Added missing fields (fixes #15)
  • Loading branch information
jclusso committed Jul 5, 2023
1 parent 93cbb80 commit 5c81c97
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/fabric/app/models/fabric/invoice_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,54 @@ class InvoiceItem

belongs_to :customer, class_name: 'Fabric::Customer',
primary_key: :stripe_id, touch: true
belongs_to :invoice, class_name: 'Fabric::Invoice', primary_key: :stripe_id
belongs_to :subscription, class_name: 'Fabric::Subscription',
primary_key: :stripe_id

field :stripe_id, type: String
field :amount, type: Integer
field :invoice, type: String
field :currency, type: String
field :date, type: Time
field :description, type: String
field :discountable, type: Boolean
field :discounts, type: Array
field :livemode, type: Boolean
field :metadata, type: Hash
field :period, type: Hash
field :price, type: Hash
field :proration, type: Boolean
field :quantity, type: Integer
field :subscription_item, type: String
field :tax_rates, type: Array
field :unit_amount, type: Integer
field :unit_amount_decimal, type: String

validates_uniqueness_of :stripe_id
validates :stripe_id, presence: true, uniqueness: true

index({ stripe_id: 1 }, { background: true, unique: true })

def sync_with(invoice_item)
self.stripe_id = invoice_item.id
self.amount = invoice_item.amount
self.invoice = invoice_item.invoice
self.currency = invoice_item.currency
self.customer_id = handle_expanded(invoice_item.customer)
self.date = invoice_item.date
self.description = invoice_item.description
self.discountable = invoice_item.discountable
self.discounts = invoice_item.discounts
self.invoice_id = handle_expanded(invoice_item.invoice)
self.livemode = invoice_item.livemode
self.metadata = convert_metadata(invoice_item.metadata)
self.period = handle_hash(invoice_item.period)
self.price = handle_hash(invoice_item.price)
self.proration = invoice_item.proration
self.quantity = invoice_item.quantity
self.subscription_id = handle_expanded(invoice_item.subscription)
self.tax_rates = invoice_item.tax_rates.map do |tr|
handle_hash(tr)
end
self.unit_amount = invoice_item.unit_amount
self.unit_amount_decimal = invoice_item.unit_amount_decimal
end
end
end

0 comments on commit 5c81c97

Please sign in to comment.