Class: Pay::PaddleClassic::PaymentMethod

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

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.



47
48
49
# File 'lib/pay/paddle_classic/payment_method.rb', line 47

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.



4
5
6
# File 'lib/pay/paddle_classic/payment_method.rb', line 4

def pay_payment_method
  @pay_payment_method
end

Class Method Details

.payment_method_details_for(subscription_id:) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pay/paddle_classic/payment_method.rb', line 24

def self.payment_method_details_for(subscription_id:)
  subscription_user = PaddleClassic.client.users.list(subscription_id: subscription_id).data.try(:first)
  payment_information = subscription_user ? subscription_user[:payment_information] : {}

  case payment_information[:payment_method]&.downcase
  when "card"
    {
      payment_method_type: :card,
      brand: payment_information[:card_type],
      last4: payment_information[:last_four_digits],
      exp_month: payment_information[:expiry_date].split("/").first,
      exp_year: payment_information[:expiry_date].split("/").last
    }
  when "paypal"
    {
      payment_method_type: :paypal,
      brand: "PayPal"
    }
  else
    {}
  end
end

.sync(pay_customer:, attributes: nil) ⇒ Object

Paddle doesn’t provide PaymentMethod IDs, so we have to lookup via the Customer



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pay/paddle_classic/payment_method.rb', line 9

def self.sync(pay_customer:, attributes: nil)
  return unless pay_customer.subscription

  payment_method = pay_customer.default_payment_method || pay_customer.build_default_payment_method
  payment_method.processor_id ||= NanoId.generate

  # Lookup payment method from API unless passed in
  attributes ||= payment_method_details_for(subscription_id: pay_customer.subscription.processor_id)

  payment_method.update!(attributes)
  payment_method
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

Instance Method Details

#detachObject

Remove payment method



56
57
# File 'lib/pay/paddle_classic/payment_method.rb', line 56

def detach
end

#make_default!Object

Sets payment method as default



52
53
# File 'lib/pay/paddle_classic/payment_method.rb', line 52

def make_default!
end