Skip to content

Commit

Permalink
feat: Add get_pix_message method to extract more information from pix…
Browse files Browse the repository at this point in the history
… transactions
  • Loading branch information
roarena authored Jul 7, 2022
1 parent 1db33f1 commit cc7b844
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
50 changes: 46 additions & 4 deletions pynubank/nubank.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,19 +286,61 @@ def create_pix_payment_qrcode(self, account_id: str, amount: float, pix_key: dic

@requires_auth_mode(AuthMode.APP)
def get_pix_identifier(self, transaction_id: str):
def find_pix_identifier(table_item: dict):
return table_item.get('label') == 'Identificador'

response = self._make_graphql_request('pix_receipt_screen', {'type': 'TRANSFER_IN', 'id': transaction_id})

if 'errors' in response.keys():
return

screen_pieces = response['data']['viewer']['savingsAccount']['getGenericReceiptScreen']['screenPieces']

return self._get_pix_id(screen_pieces)

def _get_pix_value(self, screen_pieces: dict):
def find_pix_value(table_item: dict):
return table_item.get('label') == 'Valor'

table_items = list(itertools.chain(*[table_item.get('tableItems', []) for table_item in screen_pieces]))
value_data = next(filter(find_pix_value, table_items), None)

if value_data is None:
return

return value_data['value']

identifier_data = next(filter(find_pix_identifier, table_items), None)
def _get_pix_id(self, screen_pieces: dict):
def find_pix_id(table_item: dict):
return table_item.get('label') == 'Identificador'

table_items = list(itertools.chain(*[table_item.get('tableItems', []) for table_item in screen_pieces]))
identifier_data = next(filter(find_pix_id, table_items), None)

if identifier_data is None:
return

return identifier_data['value']

def _get_pix_message(self, screen_pieces: dict):
message_content = list(itertools.chain(*[table_item.get('messageContent', []) for table_item in screen_pieces]))

return ''.join(message_content)

def _get_pix_date(self, screen_pieces: dict):
transaction_date = list(itertools.chain(*[table_item.get('headerSubtitle', []) for table_item in screen_pieces]))

return ''.join(transaction_date)

@requires_auth_mode(AuthMode.APP)
def get_pix_details(self, transaction_id: str):
response = self._make_graphql_request('pix_receipt_screen', {'type': 'TRANSFER_IN', 'id': transaction_id})

if 'errors' in response.keys():
return

screen_pieces = response['data']['viewer']['savingsAccount']['getGenericReceiptScreen']['screenPieces']

return {
"id": self._get_pix_id(screen_pieces),
"value": self._get_pix_value(screen_pieces),
"message": self._get_pix_message(screen_pieces),
"date": self._get_pix_date(screen_pieces),
}
6 changes: 6 additions & 0 deletions pynubank/utils/mocked_responses/pix_receipt_screen.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
}
]
},
{
"__typename": "ReceiptMessagePiece",
"fallbackMessage": "Atualize seu app para ver todas as informações!",
"messageTitle": "Descrição",
"messageContent": "Mensagem enviada via PIX"
},
{
"__typename": "ReceiptTablePiece",
"fallbackMessage": "Atualize seu app para ver todas as informações!",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def read(fname):

setup(
name='pynubank',
version='2.18.0',
version='2.18.1',
url='https://github.com/andreroggeri/pynubank',
author='André Roggeri Campos',
author_email='[email protected]',
Expand Down
13 changes: 13 additions & 0 deletions tests/test_nubank_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,16 @@ def test_creditcard_methods_should_allow_web_authentication(method_name, method_

def test_creditcard_methods_should_allow_app_authentication():
pass


def test_get_pix_details():
nubank_client = Nubank(client=MockHttpClient())
nubank_client.authenticate_with_cert('1234', 'hunter12', 'some-file.p12')

pix_details = nubank_client.get_pix_details('IdentificadorPixAqui')

assert pix_details is not None
assert pix_details['message'] == 'Mensagem enviada via PIX'
assert pix_details['id'] == 'IdentificadorPixAqui'
assert pix_details['value'] == 'R$ 2,00'
assert pix_details['date'] == '09 AGO 2021 - 17:05:08'

0 comments on commit cc7b844

Please sign in to comment.