Class: Pay::PaddleBilling::Billable

Inherits:
Object
  • Object
show all
Defined in:
lib/pay/paddle_billing/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_billing/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_billing/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.



74
75
76
# File 'lib/pay/paddle_billing/billable.rb', line 74

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pay/paddle_billing/billable.rb', line 47

def charge(amount, options = {})
  return Pay::Error unless options

  items = options[:items]
  opts = options.except(:items).merge(customer_id: processor_id)
  transaction = ::Paddle::Transaction.create(items: items, **opts)

  attrs = {
    amount: transaction.details.totals.grand_total,
    created_at: transaction.created_at,
    currency: transaction.currency_code,
    metadata: transaction.details.line_items&.first&.id
  }

  charge = pay_customer.charges.find_or_initialize_by(processor_id: transaction.id)
  charge.update(attrs)
  charge
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#customerObject

Retrieves a Paddle::Customer object

Finds an existing Paddle::Customer if processor_id exists Creates a new Paddle::Customer using ‘email` and `customer_name` if empty processor_id

Returns a Paddle::Customer object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pay/paddle_billing/billable.rb', line 27

def customer
  if processor_id?
    ::Paddle::Customer.retrieve(id: processor_id)
  else
    sc = ::Paddle::Customer.create(email: email, name: customer_name)
    pay_customer.update!(processor_id: sc.id)
    sc
  end
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#customer_attributesObject



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

def customer_attributes
  {email: email, name: customer_name}
end

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



83
84
85
86
87
# File 'lib/pay/paddle_billing/billable.rb', line 83

def processor_subscription(subscription_id, options = {})
  ::Paddle::Subscription.retrieve(id: subscription_id, **options)
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

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



68
69
70
# File 'lib/pay/paddle_billing/billable.rb', line 68

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

#trial_end_date(subscription) ⇒ Object



78
79
80
81
# File 'lib/pay/paddle_billing/billable.rb', line 78

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

#update_customer!(**attributes) ⇒ Object

Syncs name and email to Paddle::Customer You can also pass in other attributes that will be merged into the default attributes



41
42
43
44
45
# File 'lib/pay/paddle_billing/billable.rb', line 41

def update_customer!(**attributes)
  customer unless processor_id?
  attrs = customer_attributes.merge(attributes)
  ::Paddle::Customer.update(id: processor_id, **attrs)
end