Project

sat_rfc

0.0
The project is in a healthy, maintained state
Ruby library that generates RFC tax identifiers for Mexican natural persons and legal entities using the SAT algorithm documented by the IFAI (INFOMEX folio 0610100135506).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 1.0
 Project Readme

sat_rfc

Ruby gem to generate Mexican RFC tax identifiers for natural persons (personas físicas), following the SAT algorithm documented by the IFAI (Instituto Federal de Acceso a la Información).

Legal entity support (personas morales) is planned for a future release.

Installation

Add this line to your application's Gemfile:

gem "sat_rfc"

Or install from the repository:

gem "sat_rfc", git: "https://github.com/elosnaya/rfc.git"

Local development install:

bundle exec rake install

Usage

require "sat_rfc"

rfc = Rfc::Generator.new.for_natural_person(
  name: "Juan Carlos",
  first_last_name: "Perez",
  second_last_name: "Garcia",
  date_of_birth: "15/03/1990"
)

puts rfc # => "PEGJ900315PE9"

To inspect individual RFC parts, use the calculators directly:

name = "Juan Carlos"
first_last_name = "Perez"
second_last_name = "Garcia"

ten_digits = Rfc::NaturalTenDigitsCodeCalculator.new(
  name: name,
  first_last_name: first_last_name,
  second_last_name: second_last_name,
  day: 15, month: 3, year: 1990
).calculate
# => "PEGJ900315"

homoclave = Rfc::HomoclaveCalculator.new(
  name: name,
  first_last_name: first_last_name,
  second_last_name: second_last_name
).calculate
# => "PE"

verification_digit = Rfc::VerificationDigitCalculator.new("#{ten_digits}#{homoclave}").calculate
# => "9"

You can also pass the birth date as separate values:

Rfc::Generator.new.for_natural_person(
  name: "Juan Carlos",
  first_last_name: "Perez",
  second_last_name: "Garcia",
  day: 15,
  month: 3,
  year: 1990
)

date_of_birth accepts DD/MM/YYYY format or any string parseable by Ruby's Date.

Errors

begin
  Rfc::Generator.new.for_natural_person(name: "Ana", first_last_name: "Lopez", second_last_name: "Garcia")
rescue Rfc::Generator::InvalidDateError => e
  puts e.message # => "Date information missing"
end

Development

bin/setup          # install dependencies
bundle exec rspec  # run tests
bin/console        # interactive console

Example in the console:

Rfc::Generator.new.for_natural_person(
  name: "Juan Carlos",
  first_last_name: "Perez",
  second_last_name: "Garcia",
  date_of_birth: "15/03/1990"
)

Algorithm reference

This gem implements the RFC generation algorithm with homoclave for natural persons and legal entities, based on official documentation obtained through the IFAI.

Acknowledgments

This Ruby implementation is also inspired by rfc-facil, the original Java library by josketres, and the Ruby port rfc_facil by acrogenesis.

Contributing

Bug reports and pull requests are welcome on GitHub at elosnaya/rfc.

License

The gem is available as open source under the terms of the MIT License.