File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import base64
2+ import re
3+ import uuid
24import qrcode
35from collections import OrderedDict
46from django import forms
911from pretix .base .payment import BasePaymentProvider
1012from 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
1939class 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 )
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments