Class: Pay::Paddle::Billable

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle/billable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(billable) ⇒ Billable

Returns a new instance of Billable.



13
14
15
# File 'lib/pay/paddle/billable.rb', line 13

def initialize(billable)
  @billable = billable
end

Instance Attribute Details

#billableObject (readonly)

Returns the value of attribute billable.



4
5
6
# File 'lib/pay/paddle/billable.rb', line 4

def billable
  @billable
end

Instance Method Details

#charge(amount, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pay/paddle/billable.rb', line 21

def charge(amount, options = {})
  subscription = billable.subscription
  return unless subscription.processor_id
  raise Pay::Error, "A charge_name is required to create a one-time charge" if options[:charge_name].nil?
  response = PaddlePay::Subscription::Charge.create(subscription.processor_id, amount.to_f / 100, options[:charge_name], options)
  charge = billable.charges.find_or_initialize_by(processor: :paddle, processor_id: response[:invoice_id])
  charge.update(
    amount: Integer(response[:amount].to_f * 100),
    card_type: processor_subscription(subscription.processor_id).payment_information[:payment_method],
    paddle_receipt_url: response[:receipt_url],
    created_at: Time.zone.parse(response[:payment_date])
  )
  charge
rescue ::PaddlePay::PaddlePayError => e
  raise Pay::Paddle::Error, e
end

#customerObject



17
18
19
# File 'lib/pay/paddle/billable.rb', line 17

def customer
  # pass
end

#invoice!(options = {}) ⇒ Object



62
63
64
# File 'lib/pay/paddle/billable.rb', line 62

def invoice!(options = {})
  # pass
end

#payment_information(subscription_id) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pay/paddle/billable.rb', line 76

def payment_information(subscription_id)
  subscription_user = PaddlePay::Subscription::User.list({subscription_id: subscription_id}).try(:first)
  payment_information = subscription_user ? subscription_user[:payment_information] : nil
  return {} if payment_information.nil?

  case payment_information[:payment_method]
  when "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"
    {
      card_type: "PayPal"
    }
  else
    {}
  end
end

#processor_subscription(subscription_id, options = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/pay/paddle/billable.rb', line 55

def processor_subscription(subscription_id, options = {})
  hash = PaddlePay::Subscription::User.list({subscription_id: subscription_id}, options).try(:first)
  OpenStruct.new(hash)
rescue ::PaddlePay::PaddlePayError => e
  raise Pay::Paddle::Error, e
end

#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object



38
39
40
# File 'lib/pay/paddle/billable.rb', line 38

def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options)
  # pass
end

#sync_payment_informationObject



70
71
72
73
74
# File 'lib/pay/paddle/billable.rb', line 70

def sync_payment_information
  billable.update!(payment_information(billable.subscription.processor_id))
rescue ::PaddlePay::PaddlePayError => e
  raise Pay::Paddle::Error, e
end

#trial_end_date(subscription) ⇒ Object



50
51
52
53
# File 'lib/pay/paddle/billable.rb', line 50

def trial_end_date(subscription)
  return unless subscription.state == "trialing"
  Time.zone.parse(subscription.next_payment[:date]).end_of_day
end

#upcoming_invoiceObject



66
67
68
# File 'lib/pay/paddle/billable.rb', line 66

def upcoming_invoice
  # pass
end

#update_card(token) ⇒ Object



42
43
44
# File 'lib/pay/paddle/billable.rb', line 42

def update_card(token)
  sync_payment_information_from_paddle
end

#update_email!Object



46
47
48
# File 'lib/pay/paddle/billable.rb', line 46

def update_email!
  # pass
end