Class: Pay::Stripe::PaymentMethod

Inherits:
PaymentMethod
  • Object
show all
Extended by:
Pay::Sync
Defined in:
app/models/pay/stripe/payment_method.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pay::Sync

find_pay_customer, pay_processor, sync_with_retries

Class Method Details

.extract_attributes(payment_method) ⇒ Object

Extracts payment method details from a Stripe::PaymentMethod object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/pay/stripe/payment_method.rb', line 48

def self.extract_attributes(payment_method)
  details = payment_method.try(payment_method.type)

  {
    payment_method_type: payment_method.type,
    email: details.try(:email), # Link
    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, retries: 1) ⇒ Object

Syncs PaymentMethod objects from Stripe



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/pay/stripe/payment_method.rb', line 27

def self.sync(id, object: nil, stripe_account: nil, retries: 1)
  sync_with_retries(retries: retries) do
    payment_method = object || ::Stripe::PaymentMethod.retrieve(id, {stripe_account: }.compact)
    return unless (pay_customer = find_pay_customer(payment_method.customer))
     ||= pay_customer.

    default_payment_method_id = pay_customer.api_record.invoice_settings&.default_payment_method
    default = (id == default_payment_method_id)

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

    where(customer: pay_customer).update_all(default: false) if default
    pay_payment_method = where(customer: pay_customer, processor_id: payment_method.id).first_or_initialize
    pay_payment_method.update!(attributes)
    pay_payment_method
  end
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

.sync_payment_intent(id, stripe_account: nil) ⇒ Object

Syncs a PaymentIntent's payment method to the database



7
8
9
10
11
12
13
14
# File 'app/models/pay/stripe/payment_method.rb', line 7

def self.sync_payment_intent(id, stripe_account: nil)
  payment_intent = ::Stripe::PaymentIntent.retrieve({id: id, expand: ["payment_method"]}, {stripe_account: }.compact)
  payment_method = payment_intent.payment_method
  return unless payment_method
  Pay::Stripe::PaymentMethod.sync(payment_method.id, object: payment_method, stripe_account: )
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

.sync_setup_intent(id, stripe_account: nil) ⇒ Object

Syncs a SetupIntent's payment method to the database



17
18
19
20
21
22
23
24
# File 'app/models/pay/stripe/payment_method.rb', line 17

def self.sync_setup_intent(id, stripe_account: nil)
  setup_intent = ::Stripe::SetupIntent.retrieve({id: id, expand: ["payment_method"]}, {stripe_account: }.compact)
  payment_method = setup_intent.payment_method
  return unless payment_method
  Pay::Stripe::PaymentMethod.sync(payment_method.id, object: payment_method, stripe_account: )
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

Instance Method Details

#detachObject

Remove payment method



75
76
77
78
79
# File 'app/models/pay/stripe/payment_method.rb', line 75

def detach
  ::Stripe::PaymentMethod.detach(processor_id, {}, stripe_options)
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end

#make_default!Object

Sets payment method as default



63
64
65
66
67
68
69
70
71
72
# File 'app/models/pay/stripe/payment_method.rb', line 63

def make_default!
  return if default?

  ::Stripe::Customer.update(customer.processor_id, {invoice_settings: {default_payment_method: processor_id}}, stripe_options)

  customer.payment_methods.update_all(default: false)
  update!(default: true)
rescue ::Stripe::StripeError => e
  raise Pay::Stripe::Error, e
end