Tom Harrison
Tom Harrison

Reputation: 14018

Rails 3: wrapping ActiveRecord operations and external credit card in a single transaction

I'm using ActiveMerchant to process a credit card transaction which may succeed or fail, which is part of a bigger transaction involving some internal accounting using ActiveRecord processing. I am not clear on how to roll back the entire transaction if a part fails.

I have a Payment model, and a Points model -- a user's points are converted to dollars to be used to reduce their payment. So I need to do these things:

I see how ActiveRecord::Transactions work, and assume I can use the after_commit (and after_rollback) callbacks to handle stuff like emailing the user.

But how do I make a failed credit card transaction cause the same rollback that a failed AR save would cause? Is it as easy as calling raise ActiveRecord::Rollback if my non-AR transaction fails? There's a section in the doc linked above that makes me nervous about this (relating to transactions not working across multiple database connections).

Rails 3.2.5, MySQL 5.1 (InnoDB), Ruby 1.9.3

Upvotes: 2

Views: 382

Answers (1)

Tanzeeb Khalili
Tanzeeb Khalili

Reputation: 7344

Your ActiveMerchant calls are to an external server so it will not affect your ActiveRecord transactions. Feel free to raise ActiveRecord::Rollback.

Upvotes: 2

Related Questions