Created
September 8, 2012 21:00
-
-
Save coderforhire/3679700 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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