lib/reji/concerns/manages_customer.rb in reji-1.0.0 vs lib/reji/concerns/manages_customer.rb in reji-1.1.0
- old
+ new
@@ -3,65 +3,63 @@
module Reji
module ManagesCustomer
extend ActiveSupport::Concern
# Determine if the entity has a Stripe customer ID.
- def has_stripe_id
- ! self.stripe_id.nil?
+ def stripe_id?
+ !stripe_id.nil?
end
# Create a Stripe customer for the given model.
def create_as_stripe_customer(options = {})
- raise Reji::CustomerAlreadyCreatedError.exists(self) if self.has_stripe_id
+ raise Reji::CustomerAlreadyCreatedError.exists(self) if stripe_id?
- if ! options.key?('email') && self.stripe_email
- options[:email] = self.stripe_email
- end
+ 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, self.stripe_options
+ options, stripe_options
)
- self.update({:stripe_id => customer.id})
+ update({ stripe_id: customer.id })
customer
end
# Update the underlying Stripe customer information for the model.
def update_stripe_customer(options = {})
Stripe::Customer.update(
- self.stripe_id, options, self.stripe_options
+ stripe_id, options, stripe_options
)
end
# Get the Stripe customer instance for the current user or create one.
def create_or_get_stripe_customer(options = {})
- return self.as_stripe_customer if self.has_stripe_id
+ return as_stripe_customer if stripe_id?
- self.create_as_stripe_customer(options)
+ create_as_stripe_customer(options)
end
# Get the Stripe customer for the model.
def as_stripe_customer
- self.assert_customer_exists
+ assert_customer_exists
- Stripe::Customer.retrieve(self.stripe_id, self.stripe_options)
+ Stripe::Customer.retrieve(stripe_id, stripe_options)
end
# Get the email address used to create the customer in Stripe.
def stripe_email
- self.email
+ email
end
# Apply a coupon to the billable entity.
def apply_coupon(coupon)
- self.assert_customer_exists
+ assert_customer_exists
- customer = self.as_stripe_customer
+ customer = as_stripe_customer
customer.coupon = coupon
customer.save
end
@@ -71,43 +69,41 @@
Reji.configuration.currency
end
# Get the Stripe billing portal for this customer.
def billing_portal_url(return_url = nil)
- self.assert_customer_exists
+ assert_customer_exists
session = Stripe::BillingPortal::Session.create({
- :customer => self.stripe_id,
- :return_url => return_url || '/',
- }, self.stripe_options)
+ customer: stripe_id,
+ return_url: return_url || '/',
+ }, stripe_options)
session.url
end
# Determine if the customer is not exempted from taxes.
- def is_not_tax_exempt
- self.as_stripe_customer.tax_exempt == 'none'
+ def not_tax_exempt?
+ as_stripe_customer.tax_exempt == 'none'
end
# Determine if the customer is exempted from taxes.
- def is_tax_exempt
- self.as_stripe_customer.tax_exempt == 'exempt'
+ def tax_exempt?
+ as_stripe_customer.tax_exempt == 'exempt'
end
# Determine if reverse charge applies to the customer.
def reverse_charge_applies
- self.as_stripe_customer.tax_exempt == 'reverse'
+ as_stripe_customer.tax_exempt == 'reverse'
end
# Get the default Stripe API options for the current Billable model.
def stripe_options(options = {})
Reji.stripe_options(options)
end
- protected
-
# Determine if the entity has a Stripe customer ID and throw an exception if not.
- def assert_customer_exists
- raise Reji::InvalidCustomerError.not_yet_created(self) unless self.has_stripe_id
+ protected def assert_customer_exists
+ raise Reji::InvalidCustomerError.not_yet_created(self) unless stripe_id?
end
end
end