Skip to content

Instantly share code, notes, and snippets.

@coderforhire
Created September 8, 2012 20:41
Show Gist options
  • Save coderforhire/3679557 to your computer and use it in GitHub Desktop.
Save coderforhire/3679557 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
if valid?
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("1", @user)
@subscription.user_id = @user.id
@subscription.save
format.html { redirect_to pages_thankyou_url }
else
format.html { render action: "new" }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment