【问题标题】:How to use SOAP service with xml in Rails (EU VAT number check)如何在 Rails 中使用带有 xml 的 SOAP 服务(欧盟增值税号检查)
【发布时间】:2015-09-17 17:39:47
【问题描述】:

我想在我的 Rails 应用程序中添加一个使用欧盟 VIES 系统检查增值税号有效性的方法:http://ec.europa.eu/taxation_customs/vies/technicalInformation.html

我对 Rails 编程已经很陌生了,这里的说明使用xml。所以我很难弄清楚这一点。我应该如何在我的 Rails 应用程序中包含上述网站上提到的代码?

也就是说,下面的validate_vat(country, vatnumber) 方法应该是什么样子以及如何处理从 SOAP 服务接收到的响应?

def vatsubmission
  @organization = Organization.find(params[:id])
  @organization.update_attributes(vat_params)

  @organization.validate_vat(@organization.country, @organization.vatnumber) if (@organization.vatnumber? && @organization.vatnumber?)

  # Process response
  if valid == false
    @organization.update_attributes(valid_vat: false)
    flash.now[:danger] = "False VAT number"
    render ...
  elsif valid == true
    @organization.update_attributes(valid_vat: true)
    flash.now[:success] = "VAT number validated"
    render ...
  else
    flash.now[:danger] = "VAT number could not be validated"
    render  ...
  end
end

def validate_vat(country, vatnumber)
  ??
end

更新:我已将 gem 'savon', '2.11.1' 添加到我的 gemfile 中。在我的控制器中,我有:

def update
  @organization = Organization.find(params[:id])
  if @organization.check_valid == true
    @organization.update_attributes(validnr: true)
  else
    @organization.update_attributes(validnr: false)
  end
end

并且我添加了以下模型方法:

  require 'savon'

  def check_valid
    debugger
    if ["DK", "CY", "etc"].include? self.country
      client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
      resp = client.call :check_vat do
        message country_code: self.country, vat_number: self.vatnr
      end
      data = resp.to_hash[:check_vat_response]
      data[:valid]
    end
  end

错误:message country_code: self.country, vat_number: self.vatnr 行失败并显示错误消息:wrong number of arguments (1 for 2)。我检查了debuggerself.country 以及self.varnr 确实有值。我做错了什么?

【问题讨论】:

标签: ruby-on-rails ruby xml soap


【解决方案1】:

为了使用来自 Ruby 的 SOAP,我使用了出色的 Savon gem。

使用Savon v2,工作代码如下所示:

require 'savon'

client = Savon.client(wsdl: 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')
resp = client.call :check_vat do
  message country_code: 'AT', vat_number: '123'
end
data = resp.to_hash[:check_vat_response]
data[:valid] #=> false :)

注意 Savon v3 仍在准备中。

【讨论】:

  • 谢谢@dimakura。我正在尝试按照您的建议实施,但收到一条错误消息。我会把它添加到我原来的帖子中。
  • @Marty 你先在本地测试了吗?您可以从 irb 调用它。
  • 我在开发服务器上测试过,irb 对我来说是新的。我应该在 irb 中做什么来测试它?如果在终端中我选择:irb 然后client = Savon.client('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')。这会产生一个错误:NameError: uninitialized constant Savon 我认为必须这样做我需要在某处require savon?
  • 哦,对不起。我错过了Savon.client(wsdl: '...')。它现在应该可以工作了。请参阅上面的更新
  • 我用最新的错误信息更新了 OP。 message country_code: self.country, vat_number: self.vatnr 行失败并显示错误消息:wrong number of arguments (1 for 2)
【解决方案2】:

我刚刚开始为此使用ValVat gem,到目前为止效果很好!

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    相关资源
    最近更新 更多