Sha256: f38b5fd28ed4a0c08e80195b4c12562ba589ffe259291a699a3605ad2ab77775

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

class StripeCharge < StripeModelCallbacks::ApplicationRecord
  belongs_to :stripe_customer, 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)
    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

3 entries across 3 versions & 1 rubygems

Version Path
stripe_model_callbacks-0.1.2 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.1 lib/stripe_model_callbacks/models/stripe_charge.rb
stripe_model_callbacks-0.1.0 lib/stripe_model_callbacks/models/stripe_charge.rb