Sha256: ad16ee0b8fed6f5d406ee1c9127e48e3440f84bd271b68568b534ced3aa81993

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

class StripeCharge < StripeModelCallbacks::ApplicationRecord
  belongs_to :stripe_customer, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_invoice, optional: true, primary_key: "stripe_id"
  belongs_to :stripe_source, optional: true, primary_key: "stripe_id"
  has_many :stripe_orders, primary_key: "stripe_id"
  has_many :stripe_refunds, primary_key: "stripe_id"
  has_many :stripe_reviews, primary_key: "stripe_id"

  monetize :amount_cents
  monetize :amount_refunded_cents, allow_nil: true
  monetize :application_cents, allow_nil: true

  def self.stripe_class
    Stripe::Charge
  end

  def assign_from_stripe(object)
    check_object_is_stripe_class(object)
    assign_attributes(
      created: Time.zone.at(object.created),
      stripe_customer_id: object.customer,
      livemode: object.livemode,
      stripe_invoice_id: object.invoice,
      metadata: JSON.generate(object.metadata),
      stripe_order_id: object.order,
      stripe_source_id: object.source
    )

    assign_amounts_from_stripe(object)

    StripeModelCallbacks::AttributesAssignerService.execute!(
      model: self,
      stripe_model: object,
      attributes: %w[
        captured currency description dispute outcome refunded fraud_details failure_message failure_code on_behalf_of paid
        receipt_email receipt_number review shipping source_transfer statement_descriptor status transfer_group
      ]
    )
  end

private

  def assign_amounts_from_stripe(object)
    assign_attributes(
      amount: Money.new(object.amount, object.currency),
      amount_refunded: object.amount_refunded ? Money.new(object.amount_refunded, object.currency) : nil,
      application: object.application ? Money.new(object.application, object.currency) : nil
    )
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stripe_model_callbacks-0.1.7 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.6 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.5 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.4 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.3 lib/stripe_model_callbacks/models/stripe_charge.rb