Skip to content

Instantly share code, notes, and snippets.

@goldnuggets24
Created May 26, 2014 17:19
Show Gist options
  • Save goldnuggets24/068295e02d560527775a to your computer and use it in GitHub Desktop.
Save goldnuggets24/068295e02d560527775a to your computer and use it in GitHub Desktop.
class Subscription < ActiveRecord::Base
belongs_to :deal
belongs_to :organization
validates_presence_of :email
attr_accessor :stripe_card_token, :email, :card_number, :card_code, :card_month, :card_year , :card
attr_accessible :organization_id, :deal_id, :stripe_card_token, :email, :name, :description, :card
def save_with_payment
if valid?
customer = Stripe::Customer.create(email: email, description: name, 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
def update_card(subscription, card_info)
token = Stripe::Token.create(
card: {
number: card_info[:number],
exp_month: card_info[:exp_month],
exp_year: card_info[:exp_year],
cvc: card_info[:cvc]
}
)
customer = Stripe::Customer.retrieve(subscription.stripe_customer_token)
card = customer.cards.create(card: token.id)
card.save
customer.default_card = card.id
customer.save
rescue Stripe::CardError => e
logger.error "Stripe error while updating card info: #{e.message}"
errors.add :base, "#{e.message}"
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment