Skip to content

Commit 77d470c

Browse files
committed
Process OpenPix refund transaction
1 parent 380b90f commit 77d470c

File tree

4 files changed

+135
-11
lines changed

4 files changed

+135
-11
lines changed

thebook/webhooks/openpix/services.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def calculate_openpix_fee(amount, transaction_type):
1616
on the amount received, the active account plan and the type of the transaction
1717
"""
1818
fee = 0.0
19+
if transaction_type == "REFUND":
20+
return fee
1921

2022
if transaction_type == "WITHDRAW":
2123
fee = -1
@@ -68,12 +70,25 @@ def fetch_transactions(start_date: datetime.date, end_date: datetime.date):
6870

6971
transaction_type = jmespath.search("type", transaction)
7072
is_bank_account_transfer = transaction_type == "WITHDRAW"
73+
is_refund = transaction_type == "REFUND"
7174
if is_bank_account_transfer:
7275
transaction_category = bank_account_transfer_category
7376
transaction_description = (
7477
f"Transferência entre contas bancárias - {transaction_id}"
7578
)
7679
transaction_amount = -1 * transaction_amount
80+
elif is_refund:
81+
transaction_category = None
82+
credit_party_name = (
83+
jmespath.search("creditParty.holder.name", transaction) or ""
84+
)
85+
credit_party_tax_id = jmespath.search(
86+
"creditParty.holder.taxID.taxID", transaction
87+
)
88+
transaction_description = (
89+
f"Reembolso - {credit_party_name} - {credit_party_tax_id}"
90+
)
91+
transaction_amount = -1 * transaction_amount
7792
else:
7893
transaction_category = None
7994
payer_name = jmespath.search("payer.name", transaction) or ""
@@ -97,17 +112,18 @@ def fetch_transactions(start_date: datetime.date, end_date: datetime.date):
97112
transaction_amount, transaction_type
98113
)
99114

100-
results.append(
101-
Transaction(
102-
reference=f"{transaction_id}-T",
103-
date=transaction_date,
104-
description=f"Taxa OpenPix - {transaction_description}",
105-
amount=transaction_fee_amount,
106-
bank_account=bank_account,
107-
category=bank_fee_category,
108-
source="openpix-fetch-transactions",
109-
created_by=user,
115+
if transaction_fee_amount != 0.0:
116+
results.append(
117+
Transaction(
118+
reference=f"{transaction_id}-T",
119+
date=transaction_date,
120+
description=f"Taxa OpenPix - {transaction_description}",
121+
amount=transaction_fee_amount,
122+
bank_account=bank_account,
123+
category=bank_fee_category,
124+
source="openpix-fetch-transactions",
125+
created_by=user,
126+
)
110127
)
111-
)
112128

113129
return results
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"pageInfo": {
3+
"skip": 0,
4+
"limit": 100,
5+
"hasPreviousPage": false,
6+
"hasNextPage": false
7+
},
8+
"status": "OK",
9+
"transactions": [
10+
{
11+
"value": 10200,
12+
"time": "2026-03-24T23:36:42.136Z",
13+
"endToEndId": "D54811417202603242336EmE2DpJUiYc",
14+
"status": "CONFIRMED",
15+
"type": "REFUND",
16+
"pixQrCode": {
17+
"name": "MSVG9",
18+
"value": 10200,
19+
"comment": "good",
20+
"identifier": "MSVG9",
21+
"correlationID": "ABCD9",
22+
"paymentLinkID": "4da5a308-cf3b-430b-8f71-66a565857703",
23+
"createdAt": "2026-03-16T23:53:19.385Z",
24+
"updatedAt": "2026-03-16T23:53:19.385Z"
25+
},
26+
"subType": "REFUND_SENT",
27+
"debitParty": {
28+
"pixKey": {
29+
"pixKey": "e77996eb-d15e-46ea-99a7-3190af4e293e",
30+
"type": "RANDOM"
31+
},
32+
"account": {
33+
"branch": "0001",
34+
"account": "987654",
35+
"accountType": "TRAN"
36+
},
37+
"psp": {
38+
"id": "11111111",
39+
"name": "WOOVI IP LTDA."
40+
},
41+
"holder": {
42+
"taxID": {
43+
"taxID": "12345678000110",
44+
"type": "BR:CNPJ"
45+
}
46+
}
47+
},
48+
"creditParty": {
49+
"account": {
50+
"branch": "0001",
51+
"account": "12345678",
52+
"accountType": "TRAN"
53+
},
54+
"psp": {
55+
"id": "12345678",
56+
"name": "NU PAGAMENTOS - IP"
57+
},
58+
"holder": {
59+
"name": "John Doe",
60+
"taxID": {
61+
"taxID": "12345678910",
62+
"type": "BR:CPF"
63+
}
64+
}
65+
},
66+
"createdAt": "2026-03-24T23:36:42.225Z"
67+
}
68+
]
69+
}

thebook/webhooks/tests/openpix/test_calculate_openpix_fee.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
("FIXO", "PAYMENT", 630, -0.85),
2323
("PERCENTUAL", "WITHDRAW", 630, -1),
2424
("FIXO", "WITHDRAW", 630, -1),
25+
("FIXO", "REFUND", 630, 0.0),
26+
("PERCENTUAL", "REFUND", 630, 0.0),
2527
],
2628
)
2729
def test_calculate_openpix_fee_based_on_selected_plan(

thebook/webhooks/tests/openpix/test_services__fetch_transactions.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ def openpix_fetch_transactions__withdraw_transaction():
7777
return payload.read()
7878

7979

80+
@pytest.fixture
81+
def openpix__refund_transaction():
82+
with open(
83+
Path(SAMPLE_PAYLOADS_DIR / "openpix__refund_transaction.json"), "r"
84+
) as payload:
85+
return payload.read()
86+
87+
8088
@responses.activate
8189
def test_fetch_multiple_transactions(
8290
db,
@@ -267,3 +275,32 @@ def test_fetch_withdraw_transaction(
267275
assert transactions[1].bank_account == openpix_bank_account
268276
assert transactions[1].category == bank_fee_category
269277
assert transactions[1].created_by == user
278+
279+
280+
@responses.activate
281+
def test_fetch_refund_transaction(
282+
db,
283+
openpix__refund_transaction,
284+
openpix_bank_account,
285+
user,
286+
):
287+
responses.add(
288+
responses.GET,
289+
f"{settings.OPENPIX_API_BASE_URL}/api/v1/transaction",
290+
body=openpix__refund_transaction,
291+
content_type="application/json",
292+
)
293+
294+
transactions = fetch_transactions(
295+
start_date=datetime.date(2026, 3, 24), end_date=datetime.date(2026, 3, 26)
296+
)
297+
298+
assert len(transactions) == 1
299+
300+
assert transactions[0].reference == "D54811417202603242336EmE2DpJUiYc"
301+
assert transactions[0].date == datetime.date(2026, 3, 24)
302+
assert transactions[0].description == "Reembolso - John Doe - 12345678910"
303+
assert transactions[0].amount == Decimal("-102")
304+
assert transactions[0].bank_account == openpix_bank_account
305+
assert transactions[0].source == "openpix-fetch-transactions"
306+
assert transactions[0].created_by == user

0 commit comments

Comments
 (0)