Class: Pay::LemonSqueezy::Customer

Inherits:
Customer show all
Includes:
Routing
Defined in:
app/models/pay/lemon_squeezy/customer.rb

Instance Method Summary collapse

Methods included from Routing

#default_url_options

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



50
51
# File 'app/models/pay/lemon_squeezy/customer.rb', line 50

def add_payment_method(token = nil, default: true)
end

#api_recordObject

Retrieves a LemonSqueezy::Customer object

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

Returns a LemonSqueezy::Customer object



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

def api_record
  if processor_id?
    ::LemonSqueezy::Customer.retrieve(id: processor_id)
  elsif (lsc = ::LemonSqueezy::Customer.list(store_id: Pay::LemonSqueezy.store_id, email: email).data.first)
    update!(processor_id: lsc.id)
    lsc
  else
    lsc = ::LemonSqueezy::Customer.create(store_id: Pay::LemonSqueezy.store_id, **api_record_attributes)
    update!(processor_id: lsc.id)
    lsc
  end
rescue ::LemonSqueezy::Error => e
  raise Pay::LemonSqueezy::Error, e
end

#api_record_attributesObject



11
12
13
# File 'app/models/pay/lemon_squeezy/customer.rb', line 11

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

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

Raises:



43
44
45
# File 'app/models/pay/lemon_squeezy/customer.rb', line 43

def charge(amount, options = {})
  raise Pay::Error, "LemonSqueezy does not support one-off charges"
end

#checkout(**options) ⇒ Object

checkout(variant_id: “1234”) checkout(variant_id: “1234”, product_options: redirect_url)



55
56
57
58
59
60
61
62
63
# File 'app/models/pay/lemon_squeezy/customer.rb', line 55

def checkout(**options)
  api_record unless processor_id?

  options[:store_id] = Pay::LemonSqueezy.store_id
  options[:product_options] ||= {}
  options[:product_options][:redirect_url] = merge_order_id_param(options.dig(:product_options, :redirect_url) || root_url)

  ::LemonSqueezy::Checkout.create(**options)
end

#portal_urlObject



65
66
67
# File 'app/models/pay/lemon_squeezy/customer.rb', line 65

def portal_url
  api_record.urls.customer_portal
end

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



47
48
# File 'app/models/pay/lemon_squeezy/customer.rb', line 47

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

#update_api_record(**attributes) ⇒ Object

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



38
39
40
41
# File 'app/models/pay/lemon_squeezy/customer.rb', line 38

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