Skip to content

Instantly share code, notes, and snippets.

@jcoletaylor
Created October 3, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcoletaylor/6815673 to your computer and use it in GitHub Desktop.
Save jcoletaylor/6815673 to your computer and use it in GitHub Desktop.
balanced account adding a card
def add_card(card_uri, reuse=false)
card = self.balanced_cards.where(:uri => card_uri).first
unless card
BalancedAccount.transaction do
begin
card = BalancedCard.new(:uri => card_uri, :reuse => reuse, :balanced_accout => self)
self.balanced_cards << card
card.save!
save!
# remote_object is an instance of Balanced::Account
remote_object.add_card(card_uri)
rescue Balanced::Error => e
# NOTE: balanceable.email is safe because we only add cards to User objects
logger.error "ERROR: unable to add card #{card_uri} for #{balanceable.email}: #{e.message}"
card = nil
raise ActiveRecord::Rollback
rescue Exception => e
# NOTE: balanceable.email is safe because we only add cards to User objects
logger.error "ERROR: unable to add card #{card_uri} for #{balanceable.email}: #{e.message}"
card = nil
raise ActiveRecord::Rollback
end # end begin / rescue
end # end transaction
end # end unless
card
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment