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 installUsage
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"
endDevelopment
bin/setup # install dependencies
bundle exec rspec # run tests
bin/console # interactive consoleExample 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.
-
INFOMEX folio:
0610100135506 - Document: Algoritmo para generar el RFC con homoclave para personas físicas y morales (PDF)
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.