Skip to content

Commit b4b4306

Browse files
committed
Mark transaction that already exist as duplicated
1 parent 1c859b6 commit b4b4306

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

thebook/webhooks/paypal/services.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,22 @@ def process_webhook_payload(webhook):
211211
id=webhook.id,
212212
)
213213
webhook.status = ProcessingStatus.UNPARSABLE
214-
webhook.internal_notes = "webhooks.paypal.jsondecodeerror"
214+
webhook.internal_notes = (
215+
"webhooks.paypal.services.process_webhook_payload.unparsable_event"
216+
)
217+
webhook.save()
218+
return
219+
220+
reference = jmespath.search("resource.id", payload)
221+
if Transaction.objects.filter(reference=reference).exists():
222+
logger.info(
223+
"webhooks.paypal.services.process_webhook_payload.duplicated_transaction",
224+
id=webhook.id,
225+
)
226+
webhook.status = ProcessingStatus.DUPLICATED
227+
webhook.internal_notes = (
228+
"webhooks.paypal.services.process_webhook_payload.duplicated_transaction",
229+
)
215230
webhook.save()
216231
return
217232

@@ -278,8 +293,6 @@ def process_webhook_payload(webhook):
278293

279294
description = " - ".join([part for part in description_parts if part])
280295

281-
reference = jmespath.search("resource.id", payload)
282-
283296
with transaction.atomic():
284297
bank_fee_category, _ = Category.objects.get_or_create(name="Tarifas Bancárias")
285298

thebook/webhooks/tests/paypal/test_paypal_process.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,20 @@ def test_do_not_process_duplicated_webhook_twice(
152152
duplicated_webhook.refresh_from_db()
153153

154154
assert duplicated_webhook.status == ProcessingStatus.DUPLICATED
155+
156+
157+
@responses.activate
158+
def test_do_not_reprocess_existing_transaction(db, webhook__brl_payload):
159+
transaction = baker.make(
160+
Transaction,
161+
reference="35R35201NA4015510",
162+
)
163+
164+
webhook_with_duplicated_transaction = baker.make(
165+
PaypalWebhookPayload,
166+
payload=webhook__brl_payload,
167+
)
168+
webhook_with_duplicated_transaction.process()
169+
webhook_with_duplicated_transaction.refresh_from_db()
170+
171+
assert webhook_with_duplicated_transaction.status == ProcessingStatus.DUPLICATED

0 commit comments

Comments
 (0)