Class: Pay::PaddleBilling::PaymentMethod

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sync

find_pay_customer, pay_processor, sync_with_retries

Class Method Details

.sync(pay_customer:, attributes:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/pay/paddle_billing/payment_method.rb', line 13

def self.sync(pay_customer:, attributes:)
  sync_with_retries do
    details = attributes.method_details
    attrs = {
      payment_method_type: details.type.downcase
    }

    case details.type.downcase
    when "card"
      attrs[:brand] = details.card.type
      attrs[:last4] = details.card.last4
      attrs[:exp_month] = details.card.expiry_month
      attrs[:exp_year] = details.card.expiry_year
    end

    payment_method = pay_customer.payment_methods.find_or_initialize_by(processor_id: attributes.payment_method_id)
    payment_method.update!(attrs)
    payment_method
  end
end

.sync_from_transaction(pay_customer:, transaction:) ⇒ Object



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

def self.sync_from_transaction(pay_customer:, transaction:)
  transaction = ::Paddle::Transaction.retrieve(id: transaction)
  return unless transaction.status == "completed"
  return if transaction.payments.empty?
  sync(pay_customer: pay_customer, attributes: transaction.payments.first)
end

Instance Method Details

#detachObject



41
42
# File 'app/models/pay/paddle_billing/payment_method.rb', line 41

def detach
end

#make_default!Object



34
35
36
37
38
39
# File 'app/models/pay/paddle_billing/payment_method.rb', line 34

def make_default!
  return if default?

  customer.payment_methods.update_all(default: false)
  update!(default: true)
end