module Reji::ManagesCustomer
Public Instance Methods
Apply a coupon to the billable entity.
# 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
Get the Stripe customer for the model.
# File lib/reji/concerns/manages_customer.rb, line 45 def as_stripe_customer assert_customer_exists Stripe::Customer.retrieve(stripe_id, stripe_options) end
Get the Stripe billing portal for this customer.
# 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 a Stripe customer for the given model.
# 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
Get the Stripe customer instance for the current user or create one.
# 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
Determine if the customer is not exempted from taxes.
# File lib/reji/concerns/manages_customer.rb, line 85 def not_tax_exempt? as_stripe_customer.tax_exempt == 'none' end
Get the Stripe supported currency used by the entity.
# File lib/reji/concerns/manages_customer.rb, line 68 def preferred_currency Reji.configuration.currency end
Determine if reverse charge applies to the customer.
# File lib/reji/concerns/manages_customer.rb, line 95 def reverse_charge_applies as_stripe_customer.tax_exempt == 'reverse' end
Get the email address used to create the customer in Stripe.
# File lib/reji/concerns/manages_customer.rb, line 52 def stripe_email email end
Determine if the entity has a Stripe customer ID.
# File lib/reji/concerns/manages_customer.rb, line 8 def stripe_id? !stripe_id.nil? end
Get the default Stripe API options for the current Billable
model.
# File lib/reji/concerns/manages_customer.rb, line 100 def stripe_options(options = {}) Reji.stripe_options(options) end
Determine if the customer is exempted from taxes.
# File lib/reji/concerns/manages_customer.rb, line 90 def tax_exempt? as_stripe_customer.tax_exempt == 'exempt' end
Update the underlying Stripe customer information for the model.
# File lib/reji/concerns/manages_customer.rb, line 31 def update_stripe_customer(options = {}) Stripe::Customer.update( stripe_id, options, stripe_options ) end
Protected Instance Methods
Determine if the entity has a Stripe customer ID and throw an exception if not.
# File lib/reji/concerns/manages_customer.rb, line 105 def assert_customer_exists raise Reji::InvalidCustomerError.not_yet_created(self) unless stripe_id? end