Class: Pay::PaddleClassic::Billable

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pay_customer) ⇒ Billable

Returns a new instance of Billable.



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

def initialize(pay_customer)
  @pay_customer = pay_customer
end

Instance Attribute Details

#pay_customerObject (readonly)

Returns the value of attribute pay_customer.



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

def pay_customer
  @pay_customer
end

Instance Method Details

#add_payment_method(token = nil, default: true) ⇒ Object

Paddle does not use payment method tokens. The method signature has it here to have a uniform API with the other payment processors.



54
55
56
# File 'lib/pay/paddle_classic/billable.rb', line 54

def add_payment_method(token = nil, default: true)
  Pay::PaddleClassic::PaymentMethod.sync(pay_customer: pay_customer)
end

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



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

def charge(amount, options = {})
  subscription = pay_customer.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 = PaddleClassic.client.charges.create(subscription_id: subscription.processor_id, amount: amount.to_f / 100, charge_name: options[:charge_name])

  attributes = {
    amount: (response[:amount].to_f * 100).to_i,
    paddle_receipt_url: response[:receipt_url],
    created_at: Time.zone.parse(response[:payment_date])
  }

  # Lookup subscription payment method details
  attributes.merge! Pay::PaddleClassic::PaymentMethod.payment_method_details_for(subscription_id: subscription.processor_id)

  charge = pay_customer.charges.find_or_initialize_by(processor_id: response[:invoice_id])
  charge.update(attributes)
  charge
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

#customerObject



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

def customer
  # pass
end

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



63
64
65
66
67
# File 'lib/pay/paddle_classic/billable.rb', line 63

def processor_subscription(subscription_id, options = {})
  PaddleClassic.client.users.list(subscription_id: subscription_id).data.try(:first)
rescue ::Paddle::Error => e
  raise Pay::PaddleClassic::Error, e
end

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



48
49
50
# File 'lib/pay/paddle_classic/billable.rb', line 48

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

#trial_end_date(subscription) ⇒ Object



58
59
60
61
# File 'lib/pay/paddle_classic/billable.rb', line 58

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

#update_customer!Object



21
22
23
# File 'lib/pay/paddle_classic/billable.rb', line 21

def update_customer!
  # pass
end