如何在 Rails 中使用带有 xml 的 SOAP 服务(欧盟增值税号检查) [英] How to use SOAP service with xml in Rails (EU VAT number check)

查看:58
本文介绍了如何在 Rails 中使用带有 xml 的 SOAP 服务(欧盟增值税号检查)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to add a method in my Rails application that checks the validity of a VAT number using the EU's VIES system: http://ec.europa.eu/taxation_customs/vies/technicalInformation.html

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

I'm already pretty new to programming in Rails and the instructions here use xml. So I have trouble figuring this out. How should I include the code mentioned on the mentioned website in my Rails application?

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

In other words, what should the validate_vat(country, vatnumber) method below look like and how to process the response received from the SOAP service?

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 中.在我的控制器中,我有:


Update: I've added gem 'savon', '2.11.1' to my gemfile. In my controller I have:

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

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

And I have added the following model method:

  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 行失败并显示错误消息:参数数量错误(1 代表 2).我检查了 debuggerself.country 以及 self.varnr 确实有值.我做错了什么?

Error: The line message country_code: self.country, vat_number: self.vatnr fails with the error message: wrong number of arguments (1 for 2). I checked with the debugger and self.country as well as self.varnr do have values. What am I doing wrong?

推荐答案

为了使用来自 Ruby 的 SOAP,我使用了 excelent Savon 宝石.

For working with SOAP from Ruby I used excelent Savon gem.

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

With Savon v2, working code looks like this:

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 仍在准备中.

Note Savon v3 is still in preparation.

这篇关于如何在 Rails 中使用带有 xml 的 SOAP 服务(欧盟增值税号检查)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆