Module: Reji::ManagesCustomer

Extended by:
ActiveSupport::Concern
Included in:
Billable
Defined in:
lib/reji/concerns/manages_customer.rb

Instance Method Summary collapse

Instance Method Details

#apply_coupon(coupon) ⇒ Object

Apply a coupon to the billable entity.



57
58
59
60
61
62
63
64
65
# File 'lib/reji/concerns/manages_customer.rb', line 57

def apply_coupon(coupon)
  assert_customer_exists

  customer = as_stripe_customer

  customer.coupon = coupon

  customer.save
end

#as_stripe_customerObject

Get the Stripe customer for the model.



45
46
47
48
49
# File 'lib/reji/concerns/manages_customer.rb', line 45

def as_stripe_customer
  assert_customer_exists

  Stripe::Customer.retrieve(stripe_id, stripe_options)
end

#billing_portal_url(return_url = nil) ⇒ Object

Get the Stripe billing portal for this customer.



73
74
75
76
77
78
79
80
81
82
# File 'lib/reji/concerns/manages_customer.rb', line 73

def billing_portal_url(return_url = nil)
  assert_customer_exists

  session = Stripe::BillingPortal::Session.create({
    customer: stripe_id,
    return_url: return_url || '/',
  }, stripe_options)

  session.url
end

#create_as_stripe_customer(options = {}) ⇒ Object

Create a Stripe customer for the given model.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/reji/concerns/manages_customer.rb', line 13

def create_as_stripe_customer(options = {})
  raise Reji::CustomerAlreadyCreatedError.exists(self) if stripe_id?

  options[:email] = stripe_email if !options.key?('email') && stripe_email

  # Here we will create the customer instance on Stripe and store the ID of the
  # user from Stripe. This ID will correspond with the Stripe user instances
  # and allow us to retrieve users from Stripe later when we need to work.
  customer = Stripe::Customer.create(
    options, stripe_options
  )

  update({ stripe_id: customer.id })

  customer
end

#create_or_get_stripe_customer(options = {}) ⇒ Object

Get the Stripe customer instance for the current user or create one.



38
39
40
41
42
# File 'lib/reji/concerns/manages_customer.rb', line 38

def create_or_get_stripe_customer(options = {})
  return as_stripe_customer if stripe_id?

  create_as_stripe_customer(options)
end

#not_tax_exempt?Boolean

Determine if the customer is not exempted from taxes.

Returns:

  • (Boolean)


85
86
87
# File 'lib/reji/concerns/manages_customer.rb', line 85

def not_tax_exempt?
  as_stripe_customer.tax_exempt == 'none'
end

#preferred_currencyObject

Get the Stripe supported currency used by the entity.



68
69
70
# File 'lib/reji/concerns/manages_customer.rb', line 68

def preferred_currency
  Reji.configuration.currency
end

#reverse_charge_appliesObject

Determine if reverse charge applies to the customer.



95
96
97
# File 'lib/reji/concerns/manages_customer.rb', line 95

def reverse_charge_applies
  as_stripe_customer.tax_exempt == 'reverse'
end

#stripe_emailObject

Get the email address used to create the customer in Stripe.



52
53
54
# File 'lib/reji/concerns/manages_customer.rb', line 52

def stripe_email
  email
end

#stripe_id?Boolean

Determine if the entity has a Stripe customer ID.

Returns:

  • (Boolean)


8
9
10
# File 'lib/reji/concerns/manages_customer.rb', line 8

def stripe_id?
  !stripe_id.nil?
end

#stripe_options(options = {}) ⇒ Object

Get the default Stripe API options for the current Billable model.



100
101
102
# File 'lib/reji/concerns/manages_customer.rb', line 100

def stripe_options(options = {})
  Reji.stripe_options(options)
end

#tax_exempt?Boolean

Determine if the customer is exempted from taxes.

Returns:

  • (Boolean)


90
91
92
# File 'lib/reji/concerns/manages_customer.rb', line 90

def tax_exempt?
  as_stripe_customer.tax_exempt == 'exempt'
end

#update_stripe_customer(options = {}) ⇒ Object

Update the underlying Stripe customer information for the model.



31
32
33
34
35
# File 'lib/reji/concerns/manages_customer.rb', line 31

def update_stripe_customer(options = {})
  Stripe::Customer.update(
    stripe_id, options, stripe_options
  )
end