Skip to Content Skip to Search
Methods
A
C
D
F
I
M
N
P
S
U

Attributes

[R] customer
[R] error
[R] expand
[R] member
[R] payment_method
[R] site

Class Public methods

attach_payment_method(member:, id:)

# File app/models/stripe_customer.rb, line 10
def self.attach_payment_method(member:, id:)
  new(member:).attach_payment_method(id)
end

default_payment_method_id(member:)

# File app/models/stripe_customer.rb, line 6
def self.default_payment_method_id(member:)
  new(member:).default_payment_method_id
end

for(member:, payment_method: nil)

# File app/models/stripe_customer.rb, line 2
def self.for(member:, payment_method: nil)
  new(member:, payment_method:).customer
end

new(attributes = {})

# File app/models/stripe_customer.rb, line 16
def initialize(attributes = {})
  @member = attributes.fetch(:member)
  @payment_method = attributes[:payment_method]
  @expand = attributes[:expand]
  @site = member.site

  retrieve_remote_customer || create_remote_customer
  attach_payment_method if attach_payment_method?
end

Instance Public methods

attach_payment_method(id = payment_method, source = nil, conversion_method = nil)

# File app/models/stripe_customer.rb, line 97
def attach_payment_method(id = payment_method, source = nil, conversion_method = nil)
  return if id.nil?

  @customer = PaymentGateway.attach_payment_method(member:, id:, expand:)

  record_updated_card_event(source)
  member.remove_default_subscription_payments
  member.handle_outstanding_invoices(conversion_method)

  true
rescue Stripe::StripeError => e
  @error = e
  false
end

charges(options = {})

# File app/models/stripe_customer.rb, line 57
def charges(options = {})
  return [] unless customer

  PaymentGateway.list_charges(
    site:,
    customer: id,
    expand: ["data.invoice"],
    api_version: Rails.application.config.legacy_stripe_api_version,
    **options
  )
end

customer_id()

Alias for: id

default_card()

# File app/models/stripe_customer.rb, line 32
def default_card
  @default_card ||= default_payment_method&.card
end

default_payment_method()

# File app/models/stripe_customer.rb, line 36
def default_payment_method
  return if customer_missing_or_deleted?

  @default_payment_method ||= payment_method_from_invoice_settings ||
    payment_method_from_default_source ||
    first_payment_method
rescue Stripe::StripeError
end

default_payment_method_id()

# File app/models/stripe_customer.rb, line 112
def default_payment_method_id
  return if customer_missing_or_deleted?

  @default_payment_method_id ||= invoice_settings_payment_method_id ||
    default_source_id ||
    first_payment_method&.id
end

description()

# File app/models/stripe_customer.rb, line 79
def description
  "#{member.first_name} #{member.last_name}"
end

id()

Also aliased as: customer_id, payment_gateway_id
# File app/models/stripe_customer.rb, line 26
def id
  customer&.id
end

invoice_settings()

# File app/models/stripe_customer.rb, line 83
def invoice_settings
  return unless payment_method.present?
  {default_payment_method: payment_method}
end

metadata()

# File app/models/stripe_customer.rb, line 88
def metadata
  return if !member.rewardful_referral.present? && create_remote_customer?

  {
    referral: member.rewardful_referral,
    referral_provider: "rewardful"
  }
end

params()

# File app/models/stripe_customer.rb, line 69
def params
  {
    payment_method:,
    description:,
    invoice_settings:,
    metadata:,
    email: member.email
  }
end

payment_gateway_id()

Alias for: id

sync_email!()

# File app/models/stripe_customer.rb, line 51
def sync_email!
  return unless customer
  customer.email = member.email
  customer.save
end

update_card(token:, source: nil, conversion_method: nil)

# File app/models/stripe_customer.rb, line 45
def update_card(token:, source: nil, conversion_method: nil)
  return attach_payment_method(token, source, conversion_method) if customer
  @error = {message: I18n.t("errors.messages.invalid_request")}
  false
end