Class: Billy::PaymentMethod

Inherits:
ApplicationRecord show all
Defined in:
app/models/billy/payment_method.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.paddle_details(processor_id:) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/billy/payment_method.rb', line 39

def self.paddle_details(processor_id:)
  subscription_user = Billy::Paddle.client.users.list(subscription_id: processor_id).data.first
  payment_information = subscription_user ? subscription_user.payment_information : nil

  case payment_information.payment_method
  when "card"
    {
      method: "card",
      card_type: payment_information.card_type,
      card_last4: payment_information.last_four_digits,
      card_exp_month: payment_information.expiry_date.split("/").first,
      card_exp_year: payment_information.expiry_date.split("/").last
    }
  when "paypal"
    {
      method: "paypal"
    }
  end
end

.sync(billy_account:, attributes:) ⇒ Object

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



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/billy/payment_method.rb', line 26

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

  payment_method = .default_payment_method || .build_default_payment_method

  attributes[:processor] = "paddle"
  payment_method.update!(attributes)
  payment_method.make_default!
  payment_method
rescue ::Paddle::Error => e
  raise Billy::Errors::PaddleError, e
end

Instance Method Details

#card_type_nameObject



6
7
8
9
10
11
12
13
14
15
# File 'app/models/billy/payment_method.rb', line 6

def card_type_name
  case card_type
  when "visa"
    "Visa"
  when "master", "mastercard"
    "MasterCard"
  when "paypal"
    "PayPal"
  end
end

#make_default!Object



17
18
19
20
21
22
# File 'app/models/billy/payment_method.rb', line 17

def make_default!
  return if default?

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