lib/banktools-se/utils.rb in banktools-se-0.4.0 vs lib/banktools-se/utils.rb in banktools-se-0.5.0
- old
+ new
@@ -11,8 +11,18 @@
}
sum = digits.inject(0) { |m, x| m + x }
sum % 10 == 0
end
+ def self.luhn_checksum(number)
+ digits = number.to_s.scan(/\d/).reverse.map { |x| x.to_i }
+ digits = digits.each_with_index.map { |d, i|
+ d *= 2 if i.even?
+ d > 9 ? d - 9 : d
+ }
+ sum = digits.inject(0) { |m, x| m + x }
+ mod = 10 - sum % 10
+ mod == 10 ? 0 : mod
+ end
end
end
end