Skip to content

Instantly share code, notes, and snippets.

@coderforhire
Created September 8, 2012 21:00
Show Gist options
  • Save coderforhire/3679700 to your computer and use it in GitHub Desktop.
Save coderforhire/3679700 to your computer and use it in GitHub Desktop.
class Subscription < ActiveRecord::Base
belongs_to :user
attr_accessible :user_id, :stripe_card_token
def save_with_payment(plan, email)
if valid?
customer = Stripe::Customer.create(description: 'ta@gmail.com', plan: '1', card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
def create
@subscription = Subscription.new(params[:subscription])
@user = User.new(params[:user])
respond_to do |format|
if @user.save && @subscription.save_with_payment(plan, @user.email)
@subscription.user_id = @user.id
@subscription.save
format.html { redirect_to pages_thankyou_url }
else
format.html { render action: "new" }
end
end
end
class User < ActiveRecord::Base
has_many :subscriptions
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment