Skip to content

Commit

Permalink
feat: Add boleto feature
Browse files Browse the repository at this point in the history
andreroggeri authored and André Roggeri Campos committed Jun 16, 2020
1 parent 29ac8d0 commit e5a5f7f
Showing 6 changed files with 77 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pynubank/nubank.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions pynubank/queries/account_id.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query RequestMoney_Query {
viewer {
...RequestMoney_viewer
id
}
}

fragment RequestMoney_viewer on Customer {
savingsAccount {
id
}
}
13 changes: 13 additions & 0 deletions pynubank/queries/create_boleto.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation withCreateBoleto_Mutation(
$input: CreateTransferInBoletoInput!
) {
createTransferInBoleto(input: $input) {
boleto {
id
dueDate
barcode
readableBarcode
amount
}
}
}
1 change: 1 addition & 0 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/fixtures/create_boleto.py
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
10 changes: 10 additions & 0 deletions tests/test_nubank_client.py
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit e5a5f7f

Please sign in to comment.