Class: Pay::Stripe::PaymentMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/stripe/payment_method.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_payment_method) ⇒ PaymentMethod

Returns a new instance of PaymentMethod.


8
9
10
# File 'lib/pay/stripe/payment_method.rb', line 8

def initialize(pay_payment_method)
  @pay_payment_method = pay_payment_method
end

Instance Attribute Details

#pay_payment_methodObject (readonly)

Returns the value of attribute pay_payment_method.


4
5
6
# File 'lib/pay/stripe/payment_method.rb', line 4

def pay_payment_method
  @pay_payment_method
end

Class Method Details

.extract_attributes(payment_method) ⇒ Object

Extracts payment method details from a Stripe::PaymentMethod object


30
31
32
33
34
35
36
37
38
39
40
41
# 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

.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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: }.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: )

  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

Instance Method Details

#detachObject

Remove payment method


49
50
51
# File 'lib/pay/stripe/payment_method.rb', line 49

def detach
  ::Stripe::PaymentMethod.detach(processor_id, stripe_options)
end

#make_default!Object

Sets payment method as default


44
45
46
# 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