Reply
Highlighted
Contributor
Posts: 11
Registered: ‎02-27-2015
Accepted Solution

Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

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.


Accepted Solutions
Highlighted
Solution
Accepted by topic author Rockster160
‎08-21-2015 01:58 AM
Contributor
Posts: 11
Registered: ‎02-27-2015

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

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!

View solution in original post


All Replies
Highlighted
Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

Is that single quote in the xml or it just your masked?

Highlighted
Contributor
Posts: 11
Registered: ‎02-27-2015

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

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. 

Highlighted
Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

[ Edited ]

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?

Highlighted
Contributor
Posts: 11
Registered: ‎02-27-2015

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

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?

Highlighted
Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

The ARB - Ruby sample code here use xml

http://developer.authorize.net/downloads/samplecode/

Highlighted
Solution
Accepted by topic author Rockster160
‎08-21-2015 01:58 AM
Contributor
Posts: 11
Registered: ‎02-27-2015

Re: Trying to getHostedProfile Token for CIM via XML request returning E00002 with Ruby on Rails

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!