AuthorizeNet / sample-code-ruby

This repository contains working code samples which demonstrate ruby integration with the Authorize.Net API
MIT License
24 stars 58 forks source link

Element 'paymentProfiles' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text. #49

Closed ITDevMohsin closed 6 years ago

ITDevMohsin commented 6 years ago

I am try to use create customer code in my ruby on rails project but for some reasons its giving me strange error.

My another requirement is to validate credit card by putting validation_mode: 'liveMode' but can't figure out where to put it as I do not want to create customer if he will give false credit card.

I am using gem authorizenet in my Gem File.

Gem File:

source 'https://rubygems.org'

ruby '2.2.0'
gem 'rails', '4.2.0'
gem 'dotenv-rails'
gem 'pg'
gem 'fog','1.23.0'
gem 'devise'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'carrierwave'
gem 'chargify_api_ares'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'ranked-model'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
gem 'twilio-ruby'
gem "figaro"
gem 'rails_12factor', group: :production
gem 'will_paginate'
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
gem 'authorizenet'
group :development, :test do
  gem 'byebug'
  gem 'pry'
  gem 'web-console', '~> 2.0'
  gem 'spring'
end

Gemfile.lock

GIT
  remote: git://github.com/rweng/jquery-datatables-rails.git
  revision: d486b31b192a2924b1913e080ad23460e6b96d31
  specs:
    jquery-datatables-rails (3.4.0)
      actionpack (>= 3.1)
      jquery-rails
      railties (>= 3.1)
      sass-rails

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.0)
      actionview (= 4.2.0)
      activesupport (= 4.2.0)
      rack (~> 1.6.0)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    actionview (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    activejob (4.2.0)
      activesupport (= 4.2.0)
      globalid (>= 0.3.0)
    activemodel (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
    activerecord (4.2.0)
      activemodel (= 4.2.0)
      activesupport (= 4.2.0)
      arel (~> 6.0)
    activeresource (4.0.0)
      activemodel (~> 4.0)
      activesupport (~> 4.0)
      rails-observers (~> 0.1.1)
    activesupport (4.2.0)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    arel (6.0.0)
    authorizenet (1.8.9.1)
      nokogiri (~> 1.6, >= 1.6.4z)
      roxml (= 3.3.1)
    bcrypt (3.1.10)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    builder (3.2.2)
    byebug (4.0.5)
      columnize (= 0.9.0)
    carrierwave (0.10.0)
      activemodel (>= 3.2.0)
      activesupport (>= 3.2.0)
      json (>= 1.7)
      mime-types (>= 1.16)
    chargify_api_ares (1.3.3)
      activeresource (>= 3.2.16)
    coderay (1.1.0)
    coffee-rails (4.1.0)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.0)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.9.1.1)
    columnize (0.9.0)
    debug_inspector (0.0.2)
    devise (3.4.1)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 3.2.6, < 5)
      responders
      thread_safe (~> 0.1)
      warden (~> 1.2.3)
    dotenv (2.0.1)
    dotenv-rails (2.0.1)
      dotenv (= 2.0.1)
    erubis (2.7.0)
    excon (0.45.3)
    execjs (2.5.2)
    faraday (0.9.1)
      multipart-post (>= 1.2, < 3)
    figaro (1.1.1)
      thor (~> 0.14)
    fog (1.23.0)
      fog-brightbox
      fog-core (~> 1.23)
      fog-json
      fog-softlayer
      ipaddress (~> 0.5)
      nokogiri (~> 1.5, >= 1.5.11)
    fog-brightbox (0.7.1)
      fog-core (~> 1.22)
      fog-json
      inflecto (~> 0.0.2)
    fog-core (1.30.0)
      builder
      excon (~> 0.45)
      formatador (~> 0.2)
      mime-types
      net-scp (~> 1.1)
      net-ssh (>= 2.1.3)
    fog-json (1.0.1)
      fog-core (~> 1.0)
      multi_json (~> 1.0)
    fog-softlayer (0.4.5)
      fog-core
      fog-json
    formatador (0.2.5)
    globalid (0.3.5)
      activesupport (>= 4.1.0)
    hashie (3.4.1)
    i18n (0.7.0)
    inflecto (0.0.2)
    ipaddress (0.8.0)
    jbuilder (2.2.13)
      activesupport (>= 3.0.0, < 5)
      multi_json (~> 1.2)
    jquery-rails (4.0.3)
      rails-dom-testing (~> 1.0)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    jquery-ui-rails (5.0.3)
      railties (>= 3.2.16)
    json (1.8.2)
    jwt (1.5.0)
    loofah (2.0.2)
      nokogiri (>= 1.5.9)
    mail (2.6.3)
      mime-types (>= 1.16, < 3)
    method_source (0.8.2)
    mime-types (2.5)
    mini_portile (0.6.2)
    minitest (5.6.1)
    multi_json (1.11.0)
    multi_xml (0.5.5)
    multipart-post (2.0.0)
    net-scp (1.2.1)
      net-ssh (>= 2.6.5)
    net-ssh (2.9.2)
    nokogiri (1.6.6.2)
      mini_portile (~> 0.6.0)
    oauth (0.4.7)
    oauth2 (1.0.0)
      faraday (>= 0.8, < 0.10)
      jwt (~> 1.0)
      multi_json (~> 1.3)
      multi_xml (~> 0.5)
      rack (~> 1.2)
    omniauth (1.2.2)
      hashie (>= 1.2, < 4)
      rack (~> 1.0)
    omniauth-facebook (2.0.1)
      omniauth-oauth2 (~> 1.2)
    omniauth-oauth (1.1.0)
      oauth
      omniauth (~> 1.0)
    omniauth-oauth2 (1.3.0)
      oauth2 (~> 1.0)
      omniauth (~> 1.2)
    omniauth-twitter (1.2.0)
      json (~> 1.3)
      omniauth-oauth (~> 1.1)
    orm_adapter (0.5.0)
    pg (0.18.1)
    pry (0.10.1)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    rack (1.6.1)
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (4.2.0)
      actionmailer (= 4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      activemodel (= 4.2.0)
      activerecord (= 4.2.0)
      activesupport (= 4.2.0)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.0)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.6)
      activesupport (>= 4.2.0.beta, < 5.0)
      nokogiri (~> 1.6.0)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.2)
      loofah (~> 2.0)
    rails-observers (0.1.2)
      activemodel (~> 4.0)
    rails_12factor (0.0.3)
      rails_serve_static_assets
      rails_stdout_logging
    rails_serve_static_assets (0.0.4)
    rails_stdout_logging (0.0.3)
    railties (4.2.0)
      actionpack (= 4.2.0)
      activesupport (= 4.2.0)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (10.4.2)
    ranked-model (0.4.0)
      activerecord (>= 3.1.12)
    rdoc (4.2.0)
    responders (2.1.0)
      railties (>= 4.2.0, < 5)
    roxml (3.3.1)
      activesupport (>= 2.3.0)
      nokogiri (>= 1.3.3)
    sass (3.4.13)
    sass-rails (5.0.3)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (~> 1.1)
    sdoc (0.4.1)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    slop (3.6.0)
    spring (1.3.6)
    sprockets (3.1.0)
      rack (~> 1.0)
    sprockets-rails (2.3.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      sprockets (>= 2.8, < 4.0)
    thor (0.19.1)
    thread_safe (0.3.5)
    tilt (1.4.1)
    turbolinks (2.5.3)
      coffee-rails
    twilio-ruby (4.2.1)
      builder (>= 2.1.2)
      jwt (~> 1.0)
      multi_json (>= 1.3.0)
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    uglifier (2.7.1)
      execjs (>= 0.3.0)
      json (>= 1.8.0)
    warden (1.2.3)
      rack (>= 1.0)
    web-console (2.1.2)
      activemodel (>= 4.0)
      binding_of_caller (>= 0.7.2)
      railties (>= 4.0)
      sprockets-rails (>= 2.0, < 4.0)
    wicked_pdf (0.11.0)
      rails
    will_paginate (3.1.5)
    wkhtmltopdf-binary (0.9.9.3)

PLATFORMS
  ruby

DEPENDENCIES
  authorizenet
  byebug
  carrierwave
  chargify_api_ares
  coffee-rails (~> 4.1.0)
  devise
  dotenv-rails
  figaro
  fog (= 1.23.0)
  jbuilder (~> 2.0)
  jquery-datatables-rails!
  jquery-rails
  jquery-ui-rails
  omniauth
  omniauth-facebook
  omniauth-twitter
  pg
  pry
  rails (= 4.2.0)
  rails_12factor
  ranked-model
  sass-rails (~> 5.0)
  sdoc (~> 0.4.0)
  spring
  turbolinks
  twilio-ruby
  uglifier (>= 1.3.0)
  web-console (~> 2.0)
  wicked_pdf
  will_paginate
  wkhtmltopdf-binary

RUBY VERSION
   ruby 2.2.0p0

BUNDLED WITH
   1.15.4

Code:

def payment
 transaction = AuthorizeNet::API::Transaction.new(API_LOGIN, API_KEY, :gateway => :sandbox)
 request = AuthorizeNet::API::CreateCustomerProfileRequest.new(nil,nil,nil, validation_mode: 'liveMode')
 payment = AuthorizeNet::API::PaymentType.new(AuthorizeNet::API::CreditCardType.new('4111111111111111', '2020-05'))
 profile = AuthorizeNet::API::CustomerPaymentProfileType.new(nil, nil, payment, nil, nil)
 request.profile = AuthorizeNet::API::CustomerProfileType.new('jdoe'+rand(10000).to_s, 'John2 Doe', rand(10000).to_s + '@mail.com', [profile], nil)
 response = transaction.create_customer_profile(request)
 if response.messages.resultCode == AuthorizeNet::API::MessageTypeEnum::Ok
  puts "Successfully created a customer profile with id:  #{response.customerProfileId}"
  puts "Customer Payment Profile Id List:"
  response.customerPaymentProfileIdList.numericString.each do |id|
    puts id
  end
  puts "Customer Shipping Address Id List:"
  response.customerShippingAddressIdList.numericString.each do |id|
   puts id
  end
 else
  puts response.messages.messages[0].text
  puts "Failed to create a new customer profile."
 end
 puts response.inspect
 end

Error:

The element 'paymentProfiles' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain text. List of possible elements expected: 'customerType, billTo, payment, driversLicense, taxId, defaultPaymentProfile' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. Failed to create a new customer profile.

Full Trace:

#<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 @refId=nil, @messages=#
<AuthorizeNet::API::MessagesType:0x000000064ba090 @resultCode="Error", @messages=[#
<AuthorizeNet::API::MessagesType::Message:0x007f6f9819a488 @code="E00003", @text="The 
element 'paymentProfiles' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' cannot contain
 text. List of possible elements expected: 'customerType, billTo, payment, driversLicense, taxId, 
defaultPaymentProfile' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.", 
@roxml_references=[#<ROXML::XMLTextRef:0x007f6f9819a3e8 @opts=#
<ROXML::Definition:0x0000000351da58 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil,
 @required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="code", @array=false, 
@blocks=[], @sought_type=:text, @attr_name="code", @name="code">, @instance=#
<AuthorizeNet::API::MessagesType::Message:0x007f6f9819a488 ...>, @default_namespace="xmlns">, 
#<ROXML::XMLTextRef:0x007f6f9819a3c0 @opts=#<ROXML::Definition:0x0000000351cb08 
@default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, @required=nil, @frozen=nil, 
@wrapper=nil, @namespace=nil, @accessor="text", @array=false, @blocks=[], @sought_type=:text, 
@attr_name="text", @name="text">, @instance=#
<AuthorizeNet::API::MessagesType::Message:0x007f6f9819a488 ...>, 
@default_namespace="xmlns">]>], @roxml_references=[#<ROXML::XMLTextRef:0x000000064b9fa0 
@opts=#<ROXML::Definition:0x00000003517928 @default=nil, @to_xml=nil, @name_explicit=false,
@cdata=nil, @required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="resultCode", 
@array=false, @blocks=[], @sought_type=:text, @attr_name="resultCode", @name="resultCode">, 
@instance=#<AuthorizeNet::API::MessagesType:0x000000064ba090 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLObjectRef:0x000000064b9f78 @opts=#
<ROXML::Definition:0x00000003516b40 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="messages", @array=true, 
@blocks=[], @sought_type=AuthorizeNet::API::MessagesType::Message, @attr_name="messages", 
@name="message">, @instance=#<AuthorizeNet::API::MessagesType:0x000000064ba090 ...>, 
@default_namespace="xmlns">]>, @sessionToken=nil, @customerProfileId=nil, 
@customerPaymentProfileIdList=nil, @customerShippingAddressIdList=nil, 
@validationDirectResponseList=nil, @roxml_references=[#<ROXML::XMLTextRef:0x007f6f98185420 
@opts=#<ROXML::Definition:0x00000002bc2b48 @default=nil, @to_xml=nil, @name_explicit=false, 
@cdata=nil, @required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="refId", 
@array=false, @blocks=[], @sought_type=:text, @attr_name="refId", @name="refId">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLObjectRef:0x007f6f981853f8 @opts=#
<ROXML::Definition:0x00000002bc0f50 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil,
 @required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="messages", @array=false,
 @blocks=[], @sought_type=AuthorizeNet::API::MessagesType, @attr_name="messages", 
@name="messages">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLTextRef:0x007f6f981853d0 @opts=#
<ROXML::Definition:0x00000002bc2328 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="sessionToken", 
@array=false, @blocks=[], @sought_type=:text, @attr_name="sessionToken", 
@name="sessionToken">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLTextRef:0x007f6f981853a8 @opts=#
<ROXML::Definition:0x00000002bbea70 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, @accessor="customerProfileId", 
@array=false, @blocks=[], @sought_type=:text, @attr_name="customerProfileId", 
@name="customerProfileId">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLTextRef:0x007f6f98185380 @opts=#
<ROXML::Definition:0x00000002bbd030 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, 
@accessor="customerPaymentProfileIdList", @array=false, @blocks=[], @sought_type=:text, 
@attr_name="customerPaymentProfileIdList", @name="customerPaymentProfileIdList">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLTextRef:0x007f6f98185358 @opts=#
<ROXML::Definition:0x00000002bb30d0 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, 
@accessor="customerShippingAddressIdList", @array=false, @blocks=[], @sought_type=:text, 
@attr_name="customerShippingAddressIdList", @name="customerShippingAddressIdList">, 
@instance=#<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">, #<ROXML::XMLTextRef:0x007f6f98185330 @opts=#
<ROXML::Definition:0x00000002bb1b68 @default=nil, @to_xml=nil, @name_explicit=false, @cdata=nil, 
@required=nil, @frozen=nil, @wrapper=nil, @namespace=nil, 
@accessor="validationDirectResponseList", @array=false, @blocks=[], @sought_type=:text, 
@attr_name="validationDirectResponseList", @name="validationDirectResponseList">, @instance=#
<AuthorizeNet::API::CreateCustomerProfileResponse:0x007f6f981854c0 ...>, 
@default_namespace="xmlns">]>
adavidw commented 6 years ago

Hi @ITDevMohsin,

First off, as far as validation mode goes, you can specify it two ways. When you call the "new" method on CreateCustomerProfileRequest, you can just pass in "liveMode" as a parameter in the right spot, like this:

    request = AuthorizeNet::API::CreateCustomerProfileRequest.new(nil,nil,nil, 'liveMode')

Alternatively, you can call the setter method like this:

    request = AuthorizeNet::API::CreateCustomerProfileRequest.new
    request.validationMode = ValidationModeEnum::liveMode

Note that when you use "liveMode", you must provide an address for the transaction.

adavidw commented 6 years ago

With regards to the other issues, I noticed the sample code we provided wasn't that clear. I updated the sample code for create-customer-profile to show more info and construct the request in such a way that it's clearer what it's doing. Please check that out and see if that helps you any. If you have any other questions, please feel free to post them here.