【发布时间】: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)。我检查了debugger 和self.country 以及self.varnr 确实有值。我做错了什么?
【问题讨论】:
-
尝试关注这个 railscast railscasts.com/episodes/290-soap-with-savon?view=asciicast
标签: ruby-on-rails ruby xml soap