Skip to content

Commit 1d6057d

Browse files
committed
Process transactions other than Subscription payments
1 parent 5215fa7 commit 1d6057d

File tree

3 files changed

+114
-5
lines changed

3 files changed

+114
-5
lines changed

thebook/webhooks/paypal/services.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ def fetch_transactions(start_date: datetime.date, end_date: datetime.date):
9797
"transaction_info.transaction_subject", transaction
9898
)
9999

100-
paypal_reference_id = jmespath.search(
101-
"transaction_info.paypal_reference_id", transaction
102-
)
103-
if paypal_reference_id:
104-
# This flow only applies to membership subscriptions
100+
if transaction_type == "T0002":
101+
# This flow only applies to Subscription payment
102+
# https://developer.paypal.com/docs/transaction-search/transaction-event-codes/
103+
paypal_reference_id = jmespath.search(
104+
"transaction_info.paypal_reference_id", transaction
105+
)
105106
response = requests.get(
106107
f"{settings.PAYPAL_API_BASE_URL}/v1/billing/subscriptions/{paypal_reference_id}",
107108
headers={"Authorization": f"Bearer {access_token}"},
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"transaction_details": [
3+
{
4+
"transaction_info": {
5+
"paypal_account_id": "XPZJRN6WX8Q7Y",
6+
"transaction_id": "3DJ715755L433650N",
7+
"paypal_reference_id": "11J12750B64965920",
8+
"paypal_reference_id_type": "TXN",
9+
"transaction_event_code": "T0006",
10+
"transaction_initiation_date": "2026-03-09T18:41:22Z",
11+
"transaction_updated_date": "2026-03-09T18:41:22Z",
12+
"transaction_amount": {
13+
"currency_code": "BRL",
14+
"value": "51.00"
15+
},
16+
"fee_amount": {
17+
"currency_code": "BRL",
18+
"value": "-3.04"
19+
},
20+
"transaction_status": "S",
21+
"transaction_subject": "Order 97XKB for Tosconf[6]",
22+
"ending_balance": {
23+
"currency_code": "BRL",
24+
"value": "47.96"
25+
},
26+
"available_balance": {
27+
"currency_code": "BRL",
28+
"value": "47.96"
29+
},
30+
"custom_field": "Order 2026-97XKB",
31+
"protection_eligibility": "01",
32+
"instrument_type": "PayPal",
33+
"instrument_sub_type": "PayPal Wallet"
34+
}
35+
}
36+
],
37+
"account_number": "2LBBYEY3XL7P8",
38+
"start_date": "2026-03-09T00:00:00Z",
39+
"end_date": "2026-03-09T20:29:59Z",
40+
"last_refreshed_datetime": "2026-03-09T20:29:59Z",
41+
"page": 1,
42+
"total_items": 1,
43+
"total_pages": 1,
44+
"links": [
45+
{
46+
"href": "https://api.paypal.com/v1/reporting/transactions?start_date=2026-03-09T00%3A00%3A00-00%3A00&end_date=2026-03-10T00%3A00%3A00-00%3A00&page_size=100&page=1",
47+
"rel": "self",
48+
"method": "GET"
49+
}
50+
]
51+
}

thebook/webhooks/tests/paypal/test_services__fetch_transactions.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ def paypal__oauth2_token():
4646
return {"access_token": "test-access-token"}
4747

4848

49+
@pytest.fixture
50+
def reporting_transactions__one_common_transaction():
51+
with open(
52+
Path(
53+
SAMPLE_PAYLOADS_DIR / "reporting_transactions__one_common_transaction.json"
54+
),
55+
"r",
56+
) as payload:
57+
return payload.read()
58+
59+
4960
@pytest.fixture
5061
def paypal__brl_payload__subscription():
5162
with open(
@@ -148,6 +159,52 @@ def test_fetch_one_transaction(
148159
assert transactions[1].created_by == user
149160

150161

162+
@responses.activate
163+
def test_fetch_one_common_transaction(
164+
db,
165+
paypal_bank_account,
166+
user,
167+
bank_fee_category,
168+
paypal__oauth2_token,
169+
reporting_transactions__one_common_transaction,
170+
):
171+
responses.add(
172+
responses.POST,
173+
f"{settings.PAYPAL_API_BASE_URL}/v1/oauth2/token",
174+
json=paypal__oauth2_token,
175+
)
176+
responses.add(
177+
responses.GET,
178+
f"{settings.PAYPAL_API_BASE_URL}/v1/reporting/transactions",
179+
body=reporting_transactions__one_common_transaction,
180+
content_type="application/json",
181+
)
182+
183+
transactions = fetch_transactions(
184+
start_date=datetime.date(2026, 3, 9), end_date=datetime.date(2026, 3, 10)
185+
)
186+
187+
assert len(transactions) == 2
188+
189+
assert transactions[0].id is None
190+
assert transactions[0].reference == "3DJ715755L433650N"
191+
assert transactions[0].date == datetime.date(2026, 3, 9)
192+
assert transactions[0].description == "Order 97XKB for Tosconf[6]"
193+
assert transactions[0].amount == Decimal("51")
194+
assert transactions[0].bank_account == paypal_bank_account
195+
assert transactions[0].category is None
196+
assert transactions[0].created_by == user
197+
198+
assert transactions[1].id is None
199+
assert transactions[1].reference == "3DJ715755L433650N-T"
200+
assert transactions[1].date == datetime.date(2026, 3, 9)
201+
assert transactions[1].description == "Taxa Paypal - Order 97XKB for Tosconf[6]"
202+
assert transactions[1].amount == Decimal("-3.04")
203+
assert transactions[1].bank_account == paypal_bank_account
204+
assert transactions[1].category == bank_fee_category
205+
assert transactions[1].created_by == user
206+
207+
151208
@responses.activate
152209
def test_fetch_multiple_transactions(
153210
db,

0 commit comments

Comments
 (0)