Repository navigation
[PG-3218] Adiciona exceção do Luhn check para Softnex #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e3bd688
Adiciona exceção do Luhn check para a Softnex
felipetrz-maistodos 4be2aac
Remove flake8 do teste
felipetrz-maistodos 9e0ce88
Simplifica implementação do Luhn, e adiciona suporte à regra divergen…
felipetrz-maistodos 2e6e9da
Adiciona test case para cartão terminado em 1 que é válido tanto no L…
felipetrz-maistodos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,31 @@ | ||
| class Luhn: | ||
| @classmethod | ||
| def checkdigit(cls, card_number): | ||
| """ checks to make sure that the card passes a luhn mod-10 checksum """ | ||
| _sum = 0 | ||
| num_digits = len(card_number) | ||
| oddeven = num_digits & 1 | ||
| for i in range(0, num_digits): | ||
| try: | ||
| digit = int(card_number[i]) | ||
| except ValueError: | ||
| return False | ||
| if not ((i & 1) ^ oddeven): | ||
| digit = digit * 2 | ||
| if digit > 9: | ||
| digit = digit - 9 | ||
| _sum = _sum + digit | ||
| return (_sum % 10) == 0 | ||
| def sum_digits(digits): | ||
| total = 0 | ||
| for i, digit in enumerate(digits, start=1): | ||
| # Se o índice (iniciando em 1) for ímpar, o valor do dígito é o dígito | ||
| # original multiplicado por dois, com os dígitos resultantes somados, | ||
| # o que equivale a subtrair 9 se o valor for maior que 9. | ||
| # Se for par, o próprio dígito é utilizado, que já está entre 0 e 9. | ||
| d = int(digit) | ||
| if i % 2 == 1: | ||
| d *= 2 | ||
| if d > 9: | ||
| d -= 9 | ||
| total += d | ||
| return total | ||
|
|
||
|
|
||
| def check(digits): | ||
| total = sum_digits(digits) | ||
| return total % 10 == 0 | ||
|
|
||
|
|
||
| def check_softnex(digits): | ||
| total = sum_digits(digits[:15]) | ||
| # A Softnex diverge do Luhn tradicional no cálculo do último dígito. | ||
| # Enquanto o Luhn apenas requer que a soma em módulo 10 seja 0, a Softnex | ||
| # modifica o dígito para 1 se o mesmo for 0, o que faz com que o Luhn recuse | ||
| # o número resultante. | ||
| dv = 10 - total % 10 | ||
| if dv > 9: | ||
| dv = 1 | ||
| return int(digits[-1]) == dv |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,31 @@ | ||
| from creditcard.luhn import Luhn | ||
| import pytest | ||
|
|
||
| from creditcard import luhn | ||
|
|
||
|
|
||
| class TestLuhn: | ||
| def test_checkdigit(self): | ||
| assert Luhn.checkdigit('4539578763621486') is True | ||
| assert Luhn.checkdigit('5369835519963014') is True | ||
| assert Luhn.checkdigit('30346836403940') is True | ||
| assert Luhn.checkdigit('3034683640394a') is False | ||
| @pytest.mark.parametrize( | ||
| "number,expected", | ||
| [ | ||
| ("4539578763621486", True), | ||
| ("5369835519963014", True), | ||
| ("5369835519963013", False), | ||
| ("30346836403940", True), | ||
| ("6108000158537041", True), | ||
| ("6108000000000041", False), | ||
| ("6108000000000040", True), | ||
| ], | ||
| ) | ||
| def test_check(self, number, expected): | ||
| assert luhn.check(number) is expected | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "number,expected", | ||
| [ | ||
| ("6108000158537041", True), | ||
| ("6108000000000041", True), | ||
| ("6108000000000040", False), | ||
| ], | ||
| ) | ||
| def test_check_softnex(self, number, expected): | ||
| assert luhn.check_softnex(number) is expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.