Repository: andreroggeri/pynubank

Issue:
QRCode Para Efetivar Transferência
Olá!

Venho através deste, solicitar/opinar sobre um sistema interessante.
Seria basicamente um QRCode para ser escaneado e efetivado uma transferência de Nubank para Nubank.

Seria algo como:
Gerar um QRCode com os dados da pessoa que irá receber o valor da transferência.
Para fazer a transferência, seria direto pelo App > Transferir > QRCode, e definir o valor a ser transferido.

Grato!

Comments:

Patch:
diff --git a/pynubank/nubank.py b/pynubank/nubank.py
index 553440c..c73ee2a 100644
--- a/pynubank/nubank.py
+++ b/pynubank/nubank.py
@@ -152,3 +152,16 @@ class Nubank:
         barcode = boleto_response['data']['createTransferInBoleto']['boleto']['readableBarcode']
 
         return barcode
+
+    def create_money_request(self, amount: float) -> str:
+        account_data = self._make_graphql_request('account_feed')
+        account_id = account_data['data']['viewer']['savingsAccount']['id']
+        payload = {
+            'input': {
+                'amount': amount, 'savingsAccountId': account_id
+            }
+        }
+
+        money_request_response = self._make_graphql_request('create_money_request', payload)
+
+        return money_request_response['data']['createMoneyRequest']['moneyRequest']['url']
diff --git a/pynubank/queries/account_feed.gql b/pynubank/queries/account_feed.gql
index 7e8fd90..5236319 100644
--- a/pynubank/queries/account_feed.gql
+++ b/pynubank/queries/account_feed.gql
@@ -1,6 +1,7 @@
 {
     viewer {
         savingsAccount {
+            id
             feed {
                 id
                 __typename
diff --git a/pynubank/queries/create_money_request.gql b/pynubank/queries/create_money_request.gql
new file mode 100644
index 0000000..f23ef4c
--- /dev/null
+++ b/pynubank/queries/create_money_request.gql
@@ -0,0 +1,12 @@
+mutation createMoneyRequest_createMoneyRequestMutation(
+  $input: CreateMoneyRequestInput!
+) {
+  createMoneyRequest(input: $input) {
+    moneyRequest {
+      amount
+      message
+      url
+      id
+    }
+  }
+}


Test patch:
diff --git a/tests/fixtures/__init__.py b/tests/fixtures/__init__.py
index 20b8897..07c467e 100644
--- a/tests/fixtures/__init__.py
+++ b/tests/fixtures/__init__.py
@@ -4,6 +4,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 .create_money_request import create_money_request_return
 from .events import events_return
 from .gen_certificate import gen_certificate_return
 from .proxy import proxy_return
diff --git a/tests/fixtures/account_statements.py b/tests/fixtures/account_statements.py
index 4315b35..5c400a4 100644
--- a/tests/fixtures/account_statements.py
+++ b/tests/fixtures/account_statements.py
@@ -3,53 +3,62 @@ import pytest
 
 @pytest.fixture
 def account_statements_return():
-    return {'data': {'viewer': {'savingsAccount': {'feed': [
-        {
-            'id': 'abcde-fghi-jklmn-opqrst-uvxw',
-            '__typename': 'BillPaymentEvent',
-            'title': 'Pagamento da fatura',
-            'detail': 'Cartão Nubank - R$ 50,00',
-            'postDate': '2018-03-07'
-        },
-        {
-            'id': 'abcde-fghi-jklmn-opqrst-uvxy',
-            '__typename': 'TransferOutReversalEvent',
-            'title': 'Transferência devolvida',
-            'detail': 'Juquinha da Silva Sauro - R$ 20,00',
-            'postDate': '2018-03-06'
-        },
-        {
-            'id': 'abcde-fghi-jklmn-opqrst-uvxz',
-            '__typename': 'TransferOutEvent',
-            'title': 'Transferência enviada',
-            'detail': 'Juquinha da Silva Sauro - R$ 20,00',
-            'postDate': '2018-03-06',
-            'amount': 20.0,
-            'destinationAccount': {
-                'name': 'Juquinha da Silva Sauro'
+    return {
+        'data': {
+            'viewer': {
+                'savingsAccount': {
+                    'id': 'abc123123',
+                    'feed': [
+                        {
+                            'id': 'abcde-fghi-jklmn-opqrst-uvxw',
+                            '__typename': 'BillPaymentEvent',
+                            'title': 'Pagamento da fatura',
+                            'detail': 'Cartão Nubank - R$ 50,00',
+                            'postDate': '2018-03-07'
+                        },
+                        {
+                            'id': 'abcde-fghi-jklmn-opqrst-uvxy',
+                            '__typename': 'TransferOutReversalEvent',
+                            'title': 'Transferência devolvida',
+                            'detail': 'Juquinha da Silva Sauro - R$ 20,00',
+                            'postDate': '2018-03-06'
+                        },
+                        {
+                            'id': 'abcde-fghi-jklmn-opqrst-uvxz',
+                            '__typename': 'TransferOutEvent',
+                            'title': 'Transferência enviada',
+                            'detail': 'Juquinha da Silva Sauro - R$ 20,00',
+                            'postDate': '2018-03-06',
+                            'amount': 20.0,
+                            'destinationAccount': {
+                                'name': 'Juquinha da Silva Sauro'
+                            }
+                        },
+                        {
+                            'id': 'abcde-fghi-jklmn-opqrst-uvx1',
+                            '__typename': 'TransferInEvent',
+                            'title': 'Transferência recebida',
+                            'detail': 'R$127.33',
+                            'postDate': '2018-03-06',
+                            'amount': 127.33
+                        },
+                        {
+                            "id": "abcdefgh-ijkl-mnop-qrst-uvwxyz0123",
+                            "__typename": "BarcodePaymentEvent",
+                            "title": "Pagamento efetuado",
+                            "detail": "AES ELETROPAULO",
+                            "postDate": "2018-02-05",
+                            "amount": 169.2
+                        },
+                        {
+                            'id': 'abcde-fghi-jklmn-opqrst-uvx2',
+                            '__typename': 'WelcomeEvent',
+                            'title': 'Bem vindo à sua conta!',
+                            'detail': 'Waldisney Santos\nBanco 260 - Nu Pagamentos S.A.\nAgência 0001\nConta 000000-1',
+                            'postDate': '2017-12-18'
+                        }
+                    ]
+                }
             }
-        },
-        {
-            'id': 'abcde-fghi-jklmn-opqrst-uvx1',
-            '__typename': 'TransferInEvent',
-            'title': 'Transferência recebida',
-            'detail': 'R$127.33',
-            'postDate': '2018-03-06',
-            'amount': 127.33
-        },
-        {
-            "id": "abcdefgh-ijkl-mnop-qrst-uvwxyz0123",
-            "__typename": "BarcodePaymentEvent",
-            "title": "Pagamento efetuado",
-            "detail": "AES ELETROPAULO",
-            "postDate": "2018-02-05",
-            "amount": 169.2
-        },
-        {
-            'id': 'abcde-fghi-jklmn-opqrst-uvx2',
-            '__typename': 'WelcomeEvent',
-            'title': 'Bem vindo à sua conta!',
-            'detail': 'Waldisney Santos\nBanco 260 - Nu Pagamentos S.A.\nAgência 0001\nConta 000000-1',
-            'postDate': '2017-12-18'
         }
-    ]}}}}
+    }
diff --git a/tests/fixtures/create_money_request.py b/tests/fixtures/create_money_request.py
new file mode 100644
index 0000000..36851b4
--- /dev/null
+++ b/tests/fixtures/create_money_request.py
@@ -0,0 +1,17 @@
+import pytest
+
+
+@pytest.fixture()
+def create_money_request_return():
+    return {
+        "data": {
+            "createMoneyRequest": {
+                "moneyRequest": {
+                    "amount": 550.0,
+                    "message": None,
+                    "url": "https://some.tld/path1/path2",
+                    "id": "123123123123"
+                }
+            }
+        }
+    }
diff --git a/tests/test_nubank_client.py b/tests/test_nubank_client.py
index 860c8b5..271ab93 100644
--- a/tests/test_nubank_client.py
+++ b/tests/test_nubank_client.py
@@ -1,4 +1,4 @@
-from unittest.mock import MagicMock
+from unittest.mock import MagicMock, Mock
 
 from qrcode import QRCode
 
@@ -288,3 +288,15 @@ def test_should_generate_boleto(monkeypatch, create_boleto_return):
     boleto = client.create_boleto(200.50)
 
     assert boleto == create_boleto_return['data']['createTransferInBoleto']['boleto']['readableBarcode']
+
+
+def test_should_create_money_request(monkeypatch, create_money_request_return, account_statements_return):
+    monkeypatch.setattr(Discovery, '_update_proxy_urls', fake_update_proxy)
+    post_mock = Mock()
+    post_mock.side_effect = [account_statements_return, create_money_request_return]
+    monkeypatch.setattr(HttpClient, 'post', post_mock)
+    client = Nubank()
+
+    url = client.create_money_request(200)
+
+    assert url == create_money_request_return['data']['createMoneyRequest']['moneyRequest']['url']