Class: Pay::Stripe::Webhooks::CustomerUpdated

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

Instance Method Summary collapse

Instance Method Details

#call(event) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pay/stripe/webhooks/customer_updated.rb', line 5

def call(event)
  object = event.data.object
  pay_customer = Pay::Customer.find_by(processor: :stripe, processor_id: object.id)

  # Couldn't find user, we can skip
  return unless pay_customer.present?

  stripe_customer = pay_customer.api_record

  # Sync default card
  if (payment_method_id = stripe_customer.invoice_settings.default_payment_method)
    Pay::Stripe::PaymentMethod.sync(payment_method_id, stripe_account: event.try(:account))

  else
    # No default payment method set
    pay_customer.payment_methods.update_all(default: false)
  end

  # Sync invoice credit balance and currency
  if stripe_customer.invoice_credit_balance.present?
    pay_customer.update(
      invoice_credit_balance: stripe_customer.invoice_credit_balance,
      currency: stripe_customer.currency
    )
  end
end