如何在Rails中使用带有xml的SOAP服务(EU增值税号检查)

我想在我的Rails应用程序中添加一个方法,该方法使用EU的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 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的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仍在准备中。

我刚刚开始使用ValVatgem ,它到目前为止工作得非常漂亮!