diff --git a/pynubank/nubank.py b/pynubank/nubank.py index 923d54f..553440c 100644 --- a/pynubank/nubank.py +++ b/pynubank/nubank.py @@ -35,10 +35,15 @@ def _get_query(query_name): with open(path) as gql: return gql.read() - def _make_graphql_request(self, graphql_object): + def _make_graphql_request(self, graphql_object, variables=None): + if variables is None: + variables = {} + body = { + 'variables': variables, 'query': self._get_query(graphql_object) } + return self.client.post(self.query_url, json=body) def _password_auth(self, cpf: str, password: str): @@ -133,3 +138,17 @@ def get_account_statements(self): def get_account_balance(self): data = self._make_graphql_request('account_balance') return data['data']['viewer']['savingsAccount']['currentSavingsBalance']['netAmount'] + + def create_boleto(self, amount: float) -> str: + customer_id_response = self._make_graphql_request('account_id') + customer_id = customer_id_response['data']['viewer']['id'] + + payload = { + "input": {"amount": str(amount), "customerId": customer_id} + } + + boleto_response = self._make_graphql_request('create_boleto', payload) + + barcode = boleto_response['data']['createTransferInBoleto']['boleto']['readableBarcode'] + + return barcode diff --git a/pynubank/queries/account_id.gql b/pynubank/queries/account_id.gql new file mode 100644 index 0000000..0e2ce9c --- /dev/null +++ b/pynubank/queries/account_id.gql @@ -0,0 +1,12 @@ +query RequestMoney_Query { + viewer { + ...RequestMoney_viewer + id + } +} + +fragment RequestMoney_viewer on Customer { + savingsAccount { + id + } +} diff --git a/pynubank/queries/create_boleto.gql b/pynubank/queries/create_boleto.gql new file mode 100644 index 0000000..2b6f6e6 --- /dev/null +++ b/pynubank/queries/create_boleto.gql @@ -0,0 +1,13 @@ +mutation withCreateBoleto_Mutation( + $input: CreateTransferInBoletoInput! +) { + createTransferInBoleto(input: $input) { + boleto { + id + dueDate + barcode + readableBarcode + amount + } + } +} diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py index 79f1bb2..20b8897 100644 --- a/tests/fixtures/__init__.py +++ b/tests/fixtures/__init__.py @@ -3,6 +3,7 @@ from .authentication import authentication_return from .bills import bills_return from .bills_details import bill_details_return +from .create_boleto import create_boleto_return from .events import events_return from .gen_certificate import gen_certificate_return from .proxy import proxy_return diff --git a/tests/fixtures/create_boleto.py b/tests/fixtures/create_boleto.py new file mode 100644 index 0000000..461d444 --- /dev/null +++ b/tests/fixtures/create_boleto.py @@ -0,0 +1,21 @@ +import pytest + + +@pytest.fixture() +def create_boleto_return(): + return { + "data": { + "viewer": { + "id": "123123123" + }, + "createTransferInBoleto": { + "boleto": { + "id": "123123123", + "dueDate": "2020-06-16", + "barcode": "123123132123123123123", + "readableBarcode": "123131321231231.2313212312.2131231.21332123", + "amount": 1231.23 + } + } + } + } diff --git a/tests/test_nubank_client.py b/tests/test_nubank_client.py index 756a4b8..860c8b5 100644 --- a/tests/test_nubank_client.py +++ b/tests/test_nubank_client.py @@ -278,3 +278,13 @@ def test_get_qr_code(monkeypatch): assert uid != '' assert isinstance(qr, QRCode) + + +def test_should_generate_boleto(monkeypatch, create_boleto_return): + monkeypatch.setattr(Discovery, '_update_proxy_urls', fake_update_proxy) + monkeypatch.setattr(HttpClient, 'post', MagicMock(return_value=create_boleto_return)) + client = Nubank() + + boleto = client.create_boleto(200.50) + + assert boleto == create_boleto_return['data']['createTransferInBoleto']['boleto']['readableBarcode']