class Pay::Stripe::PaymentMethod

Attributes

pay_payment_method[R]

Public Class Methods

extract_attributes(payment_method) click to toggle source

Extracts payment method details from a Stripe::PaymentMethod object

# File lib/pay/stripe/payment_method.rb, line 30
def self.extract_attributes(payment_method)
  details = payment_method.send(payment_method.type)

  {
    payment_method_type: payment_method.type,
    brand: details.try(:brand)&.capitalize,
    last4: details.try(:last4).to_s,
    exp_month: details.try(:exp_month).to_s,
    exp_year: details.try(:exp_year).to_s,
    bank: details.try(:bank_name) || details.try(:bank) # eps, fpx, ideal, p24, acss_debit, etc
  }
end
new(pay_payment_method) click to toggle source
# File lib/pay/stripe/payment_method.rb, line 8
def initialize(pay_payment_method)
  @pay_payment_method = pay_payment_method
end
sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) click to toggle source
# File lib/pay/stripe/payment_method.rb, line 12
def self.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1)
  object ||= ::Stripe::PaymentMethod.retrieve(id, {stripe_account: stripe_account}.compact)

  pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.customer)
  return unless pay_customer

  default_payment_method_id = pay_customer.customer.invoice_settings.default_payment_method
  default = (id == default_payment_method_id)

  attributes = extract_attributes(object).merge(default: default, stripe_account: stripe_account)

  pay_customer.payment_methods.update_all(default: false) if default
  pay_payment_method = pay_customer.payment_methods.where(processor_id: object.id).first_or_initialize
  pay_payment_method.update!(attributes)
  pay_payment_method
end

Public Instance Methods

detach() click to toggle source

Remove payment method

# File lib/pay/stripe/payment_method.rb, line 49
def detach
  ::Stripe::PaymentMethod.detach(processor_id, stripe_options)
end
make_default!() click to toggle source

Sets payment method as default

# File lib/pay/stripe/payment_method.rb, line 44
def make_default!
  ::Stripe::Customer.update(customer.processor_id, {invoice_settings: {default_payment_method: processor_id}}, stripe_options)
end

Private Instance Methods

stripe_options() click to toggle source

Options for Stripe requests

# File lib/pay/stripe/payment_method.rb, line 56
def stripe_options
  {stripe_account: customer.stripe_account}.compact
end