Skip to content

Instantly share code, notes, and snippets.

@rubymerchant
Created June 26, 2009 19:58
Show Gist options
  • Save rubymerchant/136698 to your computer and use it in GitHub Desktop.
Save rubymerchant/136698 to your computer and use it in GitHub Desktop.
class SubscriptionCharge < ActiveRecord::Base
belongs_to :subscription
belongs_to :user
validate_on_create :payment
private
def payment
if user.charge(amount)
subscription.update_attribute(:next_charge_at, 1.month.from_now)
else
errors.add(:credit_card, "payment failed")
#Following is rolled back since validation failed and save runs inside a transaction.
subscription.update_attributes(:status => "failed", :next_charge_at => 3.days.from_now)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment