Skip to content

Commit e10a581

Browse files
committed
Add Pix key simple validation
1 parent 16a2155 commit e10a581

3 files changed

Lines changed: 43 additions & 7 deletions

File tree

pretix_pix_manual/payment.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import base64
2+
import re
3+
import uuid
24
import qrcode
35
from collections import OrderedDict
46
from django import forms
@@ -9,11 +11,29 @@
911
from pretix.base.payment import BasePaymentProvider
1012
from pypix import Pix
1113

12-
from pretix_pix_manual.pix import PixInfo
1314

15+
def is_valid_pix_key(key):
16+
# Email regex
17+
email_regex = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
18+
if email_regex.match(key):
19+
return True
20+
21+
# CPF: 11 digits
22+
if re.fullmatch(r"\d{11}", key):
23+
return True
24+
25+
# CNPJ: 14 digits
26+
if re.fullmatch(r"\d{14}", key):
27+
return True
28+
29+
# Random key: UUID format
30+
try:
31+
parsed_uuid = uuid.UUID(key)
32+
return True
33+
except ValueError:
34+
pass
1435

15-
def _is_valid_pix_key(pix_key):
16-
return True
36+
return False
1737

1838

1939
class PixManual(BasePaymentProvider):
@@ -69,7 +89,7 @@ def settings_form_fields(self):
6989

7090
def settings_form_clean(self, cleaned_data):
7191
pix_key = cleaned_data.get("payment_pix_manual__pix_key")
72-
if not _is_valid_pix_key(pix_key):
92+
if not is_valid_pix_key(pix_key):
7393
raise ValidationError(
7494
{"payment_pix_manual__pix_key": _("Please provide a valid Pix key.")}
7595
)

tests/test_main.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/test_validate_pix_key.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pytest
2+
3+
from pretix_pix_manual.payment import is_valid_pix_key
4+
5+
@pytest.mark.parametrize(
6+
"pix_key,is_valid",
7+
[
8+
("some random string", False),
9+
("123123123", False),
10+
("b7517c20-2d67-4a42-a41e", False),
11+
("email@example.com", True), # E-mail address
12+
("85899296000187", True), # CNPJ
13+
("01032624078", True), # CPF
14+
("b7517c20-2d67-4a42-a41e-cfa622d53ec4", True), # Random key
15+
16+
],
17+
)
18+
def test_is_valid_pix_key(pix_key, is_valid):
19+
assert is_valid_pix_key(pix_key) == is_valid

0 commit comments

Comments
 (0)