Class: Pay::Lago::PaymentMethod

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

Constant Summary collapse

VALID_PROVIDERS =
%i[stripe adyen gocardless]

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.



43
44
45
# File 'lib/pay/lago/payment_method.rb', line 43

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.



5
6
7
# File 'lib/pay/lago/payment_method.rb', line 5

def pay_payment_method
  @pay_payment_method
end

Class Method Details

.sync(lago_customer) ⇒ Object

Lago doesn’t provide PaymentMethod IDs, so we have to lookup via the Customer. The most recently synced payment method is always the default.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pay/lago/payment_method.rb', line 15

def self.sync(lago_customer)
  pay_customer = Pay::Customer.find_by(processor_id: lago_customer.external_id)
  if pay_customer.blank?
    Rails.logger.debug "Pay::Customer #{lago_customer.external_id} is not in the database while syncing Payment Method
    #{lago_customer.provider_customer_id}"
    return
  end

  payment_data = lago_customer.billing_configuration
  provider_id = payment_data.provider_customer_id

  payment_method = pay_customer.payment_methods.find_by(processor_id: provider_id)
  payment_method ||= pay_customer.build_default_payment_method

  payment_method_attr = {
    processor_id: provider_id,
    type: "card",
    data: Lago.openstruct_to_h(payment_data)
  }

  payment_method.update!(payment_method_attr)
  payment_method.make_default!
  
  payment_method
rescue ::Lago::Api::HttpError => e
  raise Pay::Lago::Error, e
end

.valid_provider?(provider) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/pay/lago/payment_method.rb', line 9

def self.valid_provider?(provider)
  VALID_PROVIDERS.include?(provider.to_s.to_sym)
end

Instance Method Details

#detach!Object



63
64
65
# File 'lib/pay/lago/payment_method.rb', line 63

def detach!
  customer.clear_payment_method!
end

#make_default!Object



59
60
61
# File 'lib/pay/lago/payment_method.rb', line 59

def make_default!
  update_customer!
end

#pull!Object



47
48
49
50
51
# File 'lib/pay/lago/payment_method.rb', line 47

def pull!
  return false unless pay_payment_method.default?
  customer.pull!
  pay_payment_method.update!(data: customer.data["billing_configuration"])
end

#push!Object



53
54
55
56
57
# File 'lib/pay/lago/payment_method.rb', line 53

def push!
  return false unless pay_payment_method.default?
  update_customer!
  customer.push!
end