#tax #eu #vat #localization #api-bindings #vies

tax_ids

一个用于验证和验证税号的库。处理欧洲、英国、挪威或瑞士的增值税号。

1 个不稳定版本

0.1.0 2024年5月17日

#81 in 国际化 (i18n)

MIT/Apache

86KB
2K SLoC

税号

这个 crate 为欧盟、英国、瑞士和挪威境内运营的企业提供验证税号(增值税/GST)的解决方案。

目前,该库提供以下功能

  • 验证税号的语法是否符合其特定类型的正则表达式模式。
  • 验证税号在相关政府数据库中的有效性(基于税号类型)。

该库受到了 Ruby 的 valvat 库的启发。

可用功能/税号类型

功能 描述 默认
eu_vat 欧盟增值税
gb_vat 英国增值税
ch_vat 瑞士增值税
no_vat 挪威增值税

更多信息请参阅 税号类型

安装

使用默认功能 eu_vat

[dependencies]
tax_ids = "0.1.0"

启用 eu_vatgb_vat 功能

[dependencies]
tax_ids = { version = "0.1.0", features = ["eu_vat", "gb_vat"] }

使用方法

use tax_ids::TaxId;
use tax_ids::VerificationStatus::{Verified, Unverified, Unavailable};
use tax_ids::UnavailableReason::{ServiceUnavailable, Timeout, Block, RateLimit};

fn main() {
  // Instantiate a new TaxId object. This can raise a ValidationError.
  let tax_id = match TaxId::new("SE556703748501") {
    Ok(tax_id) => tax_id,
    Err(e) => {
      println!("ValidationError: {}", e);
      return;
    }
  };

  assert_eq!(tax_id.value(), "SE556703748501");
  assert_eq!(tax_id.country_code(), "SE");
  assert_eq!(tax_id.tax_country_code(), "SE");
  assert_eq!(tax_id.local_value(), "556703748501");
  assert_eq!(tax_id.tax_id_type(), "eu_vat");

  // The country code is the 2-char ISO code of the country.
  // It's often the same as the tax country code, but not always.
  // For example, the country code for Greece is GR, but EL for the Greek VAT number.

  // The United Kingdom has a country code GB and tax country code GB.
  // However, due to Brexit, businesses in Northern Ireland
  // have a country code GB but use VAT number/tax country code XI when trading
  // with the EU.

  // Verification

  // Perform a verification request against the country's tax ID database.
  // This can raise a VerificationError.
  let verification = match tax_id.verify() {
    Ok(verification) => verification,
    Err(e) => {
      println!("VerificationError: {}", e);
      return;
    }
  };

  assert_eq!(verification.status(), &Verified);

  // VerificationStatus can take one out of three different statuses:
  // - Verified - The tax ID is legitimate.
  // - Unverified - The tax ID is not legitimate.
  // - Unavailable(UnavailableReason) - The verification couldn't be performed due to some reason.

  // These statuses are what you want to act upon.
  match verification.status() {
    Verified => {
      // Proceed with payment
    }
    Unverified => {
      // Ask the customer to provide a proper tax ID
    }
    Unavailable(reason) => {
      // Process payment and verify the tax ID later?
      
      match reason {
        ServiceUnavailable | Timeout => {},
        Block => {
          // Adapt to your IP / VAT being blocked
        }
        RateLimit => {
          // Consider how to avoid rate limiting
        }
      }
    }
  }

  // The full verification object:

  println!("{:?}", verification);

  // The data field is experimental and subject to change or removal.
  // It will contain different data depending on what tax ID type is being verified.
  // And what response the verification service provides.

  // Verification status: Verified
  // Verification {
  //    performed_at: 2024-05-15T14:38:31.388914+02:00,
  //    status: Verified,
  //    data: Object {
  //        "address": String("REGERINGSGATAN 19 \n111 53 STOCKHOLM"),
  //        "countryCode": String("SE"),
  //        "name": String("Spotify AB"),
  //        "requestDate": String("2024-05-15+02:00"),
  //        "valid": String("true"),
  //        "vatNumber": String("556703748501"
  //    )}
  // }
}

税号类型

税号类型 机构 手动查询 文档
eu_vat VIES 🔍 📖 + 可用性
gb_vat HMRC 🔍 📖
ch_vat BFS 🔍 📖
no_vat Brønnøysundregistrene 🔍 📖

许可

许可方式为以下之一

任选其一。

贡献

除非您明确声明,否则任何有意提交以包含在作品中的贡献,根据 Apache-2.0 许可证的定义,应双重许可如上所述,无需任何附加条款或条件。

依赖项

~7–19MB
~282K SLoC