Saturday, September 8, 2007

Stuff I learned getting ActiveMerchant to work on Rails

I'm an intermediate or less level user of Ruby on Rails, and I have been recently trying to get ActiveMerchant to work so that I can charge people's credit cards over the internet. The funny thing about software writing is that there really is no central repository of information on how to do it...well there is, it's called google, and when you run into a problem you just hope and pray that when you cut and paste the error message into google it will find some blog, bulletin board, or chat archive where someone had the same problem as you did, AND at the same time, one of these magical uber uber uber geeks will come out of the ether and arrogantly berate some idiot peon like me while reluctantly providing the "simple" solution thick with sarcasm. Yes these are the ranks one must bear as you climb your way through the muck of software mediocrity, but just think, once you get it figured out you'll be able to show your boss the cool app you built and hear gratifying accolades like "Um, ok, but can you make the letters green?"

Well just in case your trying to get ActiveMerchant to work on rails with authorize.net here are the main problems I ran into that took me a day or two to figure out because I nothing came up on google.

1.) when you install the gem make sure you go "sudo gem install activemerchant -y"... the "-y" makes sure all the dependencies get installed.

2.) in addition to putting "require 'active_merchant'" in your config/environment.rb file (right at the bottom of the file), you must also put "include ActiveMerchant::Billing" at the top of your controller within the class.

example your_controller.rb:


require 'money'
class YourController < ApplicationController
include ActiveMerchant::Billing


If you don't do this you will get an error that says something like "uninitialized constant ActiveMerchant::Billing::AuthorizeNetGateway" and you will pull your hair out trying to figure out how to fix it because apparently everyone else on the internet natively knows that you are supposed to do this and no one has ever needed to ask this question or no uber uber uber geek has felt generous enough to answer it when they did, so when you google the above mentioned error nothing really comes up (hopefully now it will though thanks to me).

3.) Finally, if you want the transactions on your app to run in test mode, but you don't want to set your entire Authorize.net account to test mode (like if you have another app using it and setting it to test mode might make you lose money), then what you need to do is NOT use the " ActiveMerchant::Billing::Base.gateway_mode = :test if RAILS_ENV != "production"" setting in your config/environment.rb like everyone else says to. If you do it won't work because apparently this is for some kind of "test" authorize.net account, and won't even work on your real authorize.net account (You'll get a response code 103 back from authorize.net which says something like your account is inactive or your username and password is wrong). To put the transactions from your app in test mode its easy (once you spend 3 hours googling it and finally figure it out), when you build the gateway in the action that charges the card just do this:

gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new({:login => user, :password => pass, :test => 'true'})


And then just remove the ":test => 'true'" parameter, when you are ready to really get people's money.

So a typical payment submission action might look something like this in it's early stages:


def purchase

amount = Money.new('1000', 'USD')

creditcard = ActiveMerchant::Billing::CreditCard.new(
:first_name => Dax,
:last_name => Ewbank,
:number => 1234123412341234,
:month => 01,
:year => 2010,
:verification_value => 123,
:type => master
)

if creditcard.valid?
gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new({:login => user, :password => pass, :test => 'true'})
response = gateway.purchase(amount, creditcard)

if response.success?
##do something if it all works out
else

raise StandardError.new( response.message )

end
end

end


So anyway, if your as dumb as I am about all this maybe what I figured out here will save you the 2 solid days of hair pulling frustration in making this all work.

I got it all working now, but I sure wished that I could have run along this post when I was doing it, it would have saved me alot of time. So here it is for your reference. uber uber uber geeks leave me alone, I know I probably screwed something up in here and your all laughing at me, but hey, I got it to work.

Glad I could help...

5 comments:

Anonymous said...

Hey everyone, I'm going to hijack this post because I can't imagine anyone would respond to it.

I need some advice and this seemed like a great place to get it. I have an uncle who passed away a few months back. He died unexpectedly at age 44, leaving behind a wife and two young boys (8 and 11).

I live in Colorado and am the only family that they have out here (we're all from Oklahoma originally). Brad, my uncle's, birthday would've been on Saturday and his wife called me asking if I would take them out to dinner on that night because they don't want to be alone. I imagine that this will be a very hard time for them and I will do my best to be a listener/consoler, but that is not really something that I would consider one of my spiritual gifts.

I am planning to pray hard about it this evening, but I was wondering if someone could offer me some scripture references that either I could use to reflect on, or that I might be able to share with them. We're all strong in our faith, but I've never really been in this type of situation before. Anyone have any good advice for me?

Thanks,
Chad

fiodax said...

This would be a situation where there is going to be a big elephant in the room right when you show up.

Lately I've been on a kick to kill elephants that are in my rooms. I think they are like the "clanging gongs" that prevent us from loving properly in 1 Corinthians 13 "If I speak in the tongues of men and of angels, but have not love, I am a noisy gong or a clanging cymbal."

When we have a conflict with someone, or when there is a sensitive issue that everyone is aware of I think it is better just to approach it and deal with it. Even though it might be painful, humiliating, and scary. Only by these kinds of risks will you be able to properly participate in the healing process of Brad's family as they obviously want you to be.

Basically what I am saying is don't avoid the obvious subject (Brad), don't avoid their pain, even their anger with God. But assure them that God is good, and he has in mind their well being through all of this. Only by confronting their pain directly and truthfully, and relying on God to do the healing, will they be able to properly and healthily heal.

Romans 8:28 "And we know that for those who love God all things work together for good, for those who are called according to his purpose."

Jeremiah 29:11-13: "11 For I know the plans I have for you, declares the Lord, plans for welfare and not for evil, to give you a future and a hope. 12 Then you will call upon me and come and pray to me, and I will hear you. 13 You will seek me and find me, when you seek me with all your heart. 14 I will be found by you, declares the Lord, and I will restore your fortunes and gather you from all the nations and all the places where I have driven you, declares the Lord, and I will bring you back to the place from which I sent you into exile."

Psalm 62:5-8:
"5 For God alone, O my soul, wait in silence,
for my hope is from him.
6 He only is my rock and my salvation,
my fortress; I shall not be shaken.
7 On God rests my salvation and my glory;
my mighty rock, my refuge is God.

8 Trust in him at all times, O people;
pour out your heart before him;
God is a refuge for us. "

I like the fact that "God is a refuge for us". It is a unique thing we have as Christians. We can experience the full meaning of pain in the world because we can take refuge in God. We don't have to construct false realities in order to deal with or hide from the pain, we can deal with it truthfully because we have a place in truth to be sheltered from it. We have a place to go for peace and safety when it seems like there is no peace and safety. I think that is awesome and it is a promise of the Bible.

When we allow elephants to creep into our rooms we are effectively constructing false realities and not directly dealing with the pain of our lives truthfully. We are denying that God is an adequate refuge from what the world throws at us and we are instead erecting a false reality to take God's place. Then when that reality comes crumbling down, we get mad at God, when in all actuality, we were the god that created the reality that failed us, the reality of God is still there and will effectively work out all things to the good of those who love Him.

Good, luck, you'll do fine. You willingness to help them, your humor, and your kind heart and your preparation in prayer will really be all you need. Let them do the talking and just listen. But you may need to first chase out the elephant in order to get them to open up.

Anonymous said...

Hey Chad - Blow off!

Some of us actually care about this post you jerk!

Paul

Anonymous said...

Do we need to pass API LOGIN and Transaction key for ActiveMerchant::Billing::AuthorizeNetGateway.new


Thanks,
Biju

Anonymous said...

Extremely extremely spotty documentation on ActiveMerchant. Thanks for posting this, it was exactly what I was trying to find.