-
-
Save sahidursuman/0c21cce8fc0639b328432055183c63f0 to your computer and use it in GitHub Desktop.
Stripe Customer rails concern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# _ _ | |
# __| |_ _ _(_)_ __ ___ | |
# (_-< _| '_| | '_ \/ -_) | |
# /__/\__|_| |_| .__/\___| | |
# |_| | |
# __ _ _ __| |_ ___ _ __ ___ _ _ | |
# / _| || (_-< _/ _ \ ' \/ -_) '_| | |
# \__|\_,_/__/\__\___/_|_|_\___|_| | |
# | |
# (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