Instantly share code, notes, and snippets.

Embed
What would you like to do?
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