I'm making this into an actualy post since it is a separate question, but I'm having some trouble using the API. The Ruby on Rails authorize-net gem has methods to easily call most actions such as creating/updating a profile, transaction, etc. However, it doesn't have a built-in method to retrieve a token. I tried the Fivell branch of the authorize-net gem to no avail- it doesn't seem to be communicating to the rest of the app.
The problem is that I am creating an XML request, it is posting to the authorize.net's server, and then returning with an OK 200, but the response is an E00002 error that says the content is not accepted. I don't really know how to specify the content any more clearly with the way that I am doing it, so I am hoping you guys may be able to help.
Here is the relevant code that I have in the user model that will be creating the request:
xml = "<?xml version='1.0' encoding='utf-8'?> <getHostedProfilePageRequest xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'> <merchantAuthentication> <name>'-----------'</name> <transactionKey>'----------------'</transactionKey> </merchantAuthentication> <customerProfileId>'--------'</customerProfileId> </getHostedProfilePageRequest>" uri = URI('https://apitest.authorize.net/xml/v1/request.api') res = Net::HTTP.post_form(uri, xml: xml)
And the response is what is returning the E00002.
Solved! Go to Solution.
03-02-2015 08:23 AM
For those in the future having the same trouble, I'll post the answer here:
Turns out one of the main problems I was having was not passing in any of the optional parameters in the XML. Make sure you have at least the hostedProfileSettings and the validationMode.
I then used the HTTParty gem to post the xml to the uri, and ruby's built in #.from_xml method to parse the token out.
Here is the code:
transaction = AuthorizeNet::CIM::Transaction.new( 'YOUR_API_LOGIN_KEY', 'YOUR_TRANSACTION_KEY', gateway: :sandbox ) xml = "<?xml version='1.0' encoding='utf-8'?> <getHostedProfilePageRequest xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'> <merchantAuthentication> <name>YOUR_API_LOGIN_KEY</name> <transactionKey>YOUR_TRANSACTION_KEY</transactionKey> </merchantAuthentication> <customerProfileId>YOUR_USER_PROFILE_ID</customerProfileId> <hostedProfileSettings> <setting> <settingName>hostedProfileValidationMode</settingName> <settingValue>testMode</settingValue> </setting> </hostedProfileSettings> </getHostedProfilePageRequest>" uri = URI('https://apitest.authorize.net/xml/v1/request.api') req = Net::HTTP::Post.new(uri.path) res = HTTParty.post(uri, body: xml, headers: { 'Content-Type' => 'application/xml' }) token = Hash.from_xml(res.body)["getHostedProfilePageResponse"]["token"]
Keep in mind- this is for a test environment. You'll have to use sandbox keys to run it. To do it in production, you'll have to change the settingValue to liveMode, change the gateway to :live, use your merchant keys, and post to api.authorize instead of apitest.authorize in the URI.
Good luck!
03-04-2015 09:27 AM
Is that single quote in the xml or it just your masked?
03-02-2015 08:35 AM
The single quotes are part of it, but I have tried with and without them. They just happened to be in there last time I tried.
03-02-2015 09:17 AM
http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html
Shouldn't you use post() and did you set the content type?
03-02-2015 09:37 AM - edited 03-02-2015 09:37 AM
I've tried a variety of different types of POST requests and have been pretty unsuccessul so far. Post_form has been the only one that actually returns any useful info. The others lead to a variety of different errors. Changing the code to:
xml = "<?xml version='1.0' encoding='utf-8'?> <getHostedProfilePageRequest xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'> <merchantAuthentication> <name>-------</name> <transactionKey>-------</transactionKey> </merchantAuthentication> <customerProfileId>-----</customerProfileId> </getHostedProfilePageRequest>" uri = URI('https://apitest.authorize.net/xml/v1/request.api') req = Net::HTTP::Post.new(uri.path) # req. Net::HTTP.start(uri.hostname, uri.port) do |http| res = http.request(xml) end
results in a No Method Error for set_body_internal. I'm sure I'm doing something wrong here, I'm just not sure what. How can I set the content type?
03-02-2015 12:50 PM
The ARB - Ruby sample code here use xml
03-02-2015 01:12 PM
For those in the future having the same trouble, I'll post the answer here:
Turns out one of the main problems I was having was not passing in any of the optional parameters in the XML. Make sure you have at least the hostedProfileSettings and the validationMode.
I then used the HTTParty gem to post the xml to the uri, and ruby's built in #.from_xml method to parse the token out.
Here is the code:
transaction = AuthorizeNet::CIM::Transaction.new( 'YOUR_API_LOGIN_KEY', 'YOUR_TRANSACTION_KEY', gateway: :sandbox ) xml = "<?xml version='1.0' encoding='utf-8'?> <getHostedProfilePageRequest xmlns='AnetApi/xml/v1/schema/AnetApiSchema.xsd'> <merchantAuthentication> <name>YOUR_API_LOGIN_KEY</name> <transactionKey>YOUR_TRANSACTION_KEY</transactionKey> </merchantAuthentication> <customerProfileId>YOUR_USER_PROFILE_ID</customerProfileId> <hostedProfileSettings> <setting> <settingName>hostedProfileValidationMode</settingName> <settingValue>testMode</settingValue> </setting> </hostedProfileSettings> </getHostedProfilePageRequest>" uri = URI('https://apitest.authorize.net/xml/v1/request.api') req = Net::HTTP::Post.new(uri.path) res = HTTParty.post(uri, body: xml, headers: { 'Content-Type' => 'application/xml' }) token = Hash.from_xml(res.body)["getHostedProfilePageResponse"]["token"]
Keep in mind- this is for a test environment. You'll have to use sandbox keys to run it. To do it in production, you'll have to change the settingValue to liveMode, change the gateway to :live, use your merchant keys, and post to api.authorize instead of apitest.authorize in the URI.
Good luck!
03-04-2015 09:27 AM