Class: Pay::Stripe::PaymentMethod

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_attributes(payment_method) ⇒ Object

Extracts payment method details from a Stripe::PaymentMethod object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/pay/stripe/payment_method.rb', line 57

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, try: 0, retries: 1) ⇒ Object

Syncs PaymentMethod objects from Stripe



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/pay/stripe/payment_method.rb', line 21

def self.sync(id, object: nil, stripe_account: nil, try: 0, retries: 1)
  object ||= ::Stripe::PaymentMethod.retrieve(id, {stripe_account: }.compact)
  if object.customer.blank?
    Rails.logger.debug "Stripe PaymentMethod #{object.id} does not have a customer"
    return
  end

  pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.customer)
  if pay_customer.blank?
    Rails.logger.debug "Pay::Customer #{object.customer} is not in the database while syncing Stripe PaymentMethod #{object.id}"
    return
  end

  # Record the same Stripe Connect account as the customer when one wasn't given
   ||= pay_customer.

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

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

  where(customer: pay_customer).update_all(default: false) if default
  pay_payment_method = where(customer: pay_customer, processor_id: object.id).first_or_initialize
  pay_payment_method.update!(attributes)
  pay_payment_method
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
  if try > retries
    raise
  else
    try += 1
    sleep 0.15 * try
    retry
  end
end

.sync_payment_intent(id, stripe_account: nil) ⇒ Object

Syncs a PaymentIntent's payment method to the database



5
6
7
8
9
10
# File 'app/models/pay/stripe/payment_method.rb', line 5

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: )
end

.sync_setup_intent(id, stripe_account: nil) ⇒ Object

Syncs a SetupIntent's payment method to the database



13
14
15
16
17
18
# File 'app/models/pay/stripe/payment_method.rb', line 13

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: )
end

Instance Method Details

#detachObject

Remove payment method



82
83
84
# File 'app/models/pay/stripe/payment_method.rb', line 82

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

#make_default!Object

Sets payment method as default



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

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)
end