Skip to content

Instantly share code, notes, and snippets.

@stephancom
Created March 21, 2014 03:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephancom/9679213 to your computer and use it in GitHub Desktop.
Save stephancom/9679213 to your computer and use it in GitHub Desktop.
Stripe Customer rails concern
# _ _
# __| |_ _ _(_)_ __ ___
# (_-< _| '_| | '_ \/ -_)
# /__/\__|_| |_| .__/\___|
# |_|
# __ _ _ __| |_ ___ _ __ ___ _ _
# / _| || (_-< _/ _ \ ' \/ -_) '_|
# \__|\_,_/__/\__\___/_|_|_\___|_|
#
# (c) 2013 stephan.com
module StripeCustomer
extend ActiveSupport::Concern
included do
before_destroy :remove_stripe_customer
end
def remove_stripe_customer
stripe_customer.delete
self.stripe_customer_id = nil
self.save
end
def stripe_customer
if self.stripe_customer_id
begin
customer = Stripe::Customer.retrieve(self.stripe_customer_id) # TODO make this configurable
return customer
rescue
logger.info "There was a problem fetching the customer from Stripe"
end
end
begin
customer = self.class.create_stripe_customer( :email => self.email )
rescue
logger.info 'There was error creating the Stripe customer'
end
self.stripe_customer_id = customer.id
self.save
customer
end
module ClassMethods
def create_stripe_customer(params = {})
begin
Stripe::Customer.create(
:email => params[:email]
)
rescue
'There was an error adding a customer'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment