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(pay_customer) ⇒ Billable

Returns a new instance of Billable.


13
14
15
# File 'lib/pay/paddle/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/billable.rb', line 4

def pay_customer
  @pay_customer
end

Instance Method Details

#add_payment_method(token, default: true) ⇒ Object


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

def add_payment_method(token, default: true)
  Pay::Paddle::PaymentMethod.sync(self)
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/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 = PaddlePay::Subscription::Charge.create(subscription.processor_id, amount.to_f / 100, options[:charge_name], options)

  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::Paddle::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 ::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

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


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

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


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

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

#trial_end_date(subscription) ⇒ Object


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

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/billable.rb', line 21

def update_customer!
  # pass
end