Class: Pay::PaddleBilling::Customer

Inherits:
Customer show all
Defined in:
app/models/pay/paddle_billing/customer.rb

Instance Method Summary collapse

Methods inherited from Customer

#active?, #customer_name, #deleted?, #has_incomplete_payment?, #on_generic_trial?, #on_trial?, #on_trial_or_subscribed?, #retry_past_due_subscriptions!, #subscribed?, #subscription, #update_payment_method

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.



68
69
70
# File 'app/models/pay/paddle_billing/customer.rb', line 68

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

#api_recordObject

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/pay/paddle_billing/customer.rb', line 19

def api_record
  if processor_id?
    ::Paddle::Customer.retrieve(id: processor_id)
  elsif (pc = ::Paddle::Customer.list(email: email).data&.first)
    update!(processor_id: pc.id)
    pc
  else
    pc = ::Paddle::Customer.create(email: email, name: customer_name)
    update!(processor_id: pc.id)
    pc
  end
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

#api_record_attributesObject



9
10
11
# File 'app/models/pay/paddle_billing/customer.rb', line 9

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/pay/paddle_billing/customer.rb', line 41

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 = charges.find_or_initialize_by(processor_id: transaction.id)
  charge.update(attrs)
  charge
rescue ::Paddle::Error => e
  raise Pay::PaddleBilling::Error, e
end

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



62
63
64
# File 'app/models/pay/paddle_billing/customer.rb', line 62

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

#update_api_record(**attributes) ⇒ Object

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



36
37
38
39
# File 'app/models/pay/paddle_billing/customer.rb', line 36

def update_api_record(**attributes)
  api_record unless processor_id?
  ::Paddle::Customer.update(id: processor_id, **api_record_attributes.merge(attributes))
end