Skip to content

Commit

Permalink
Skip in calculations request event, when the pspReference is missing …
Browse files Browse the repository at this point in the history
…in the response from payment app (saleor#17002)
  • Loading branch information
IKarbowiak authored Nov 15, 2024
1 parent aa814bd commit f89b030
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 9 additions & 0 deletions saleor/payment/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def test_create_transaction_event_from_request_and_webhook_response_with_psp_ref
# then
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert TransactionEvent.objects.count() == 1


Expand Down Expand Up @@ -543,6 +544,7 @@ def test_create_transaction_event_from_request_and_webhook_response_with_no_psp_
request_event.refresh_from_db()
transaction.refresh_from_db()
assert request_event.psp_reference is None
assert request_event.include_in_calculations is False
assert transaction.events.count() == event_count + 1
assert event.psp_reference is None
assert event.type == result_event_type
Expand Down Expand Up @@ -601,6 +603,7 @@ def test_create_transaction_event_from_request_and_webhook_response_with_no_psp_
request_event.refresh_from_db()
transaction.refresh_from_db()
assert request_event.psp_reference is None
assert request_event.include_in_calculations is False
assert transaction.events.count() == event_count + 1
assert event.psp_reference is None
assert event.transaction_id == transaction.id
Expand Down Expand Up @@ -640,6 +643,7 @@ def test_create_transaction_event_from_request_and_webhook_response_part_event(
assert TransactionEvent.objects.count() == 2
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert event
assert event.psp_reference == expected_psp_reference
assert event.amount_value == amount
Expand Down Expand Up @@ -1003,6 +1007,7 @@ def test_create_transaction_event_from_request_and_webhook_response_full_event(
assert transaction.events.count() == 2
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert event
assert event.psp_reference == expected_psp_reference
assert event.amount_value == event_amount
Expand Down Expand Up @@ -1287,6 +1292,7 @@ def test_create_transaction_event_from_request_and_webhook_response_twice_auth(
assert TransactionEvent.objects.count() == 3
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert failed_event
assert failed_event.psp_reference == expected_psp_reference
assert failed_event.type == TransactionEventType.AUTHORIZATION_FAILURE
Expand Down Expand Up @@ -1342,6 +1348,7 @@ def test_create_transaction_event_from_request_and_webhook_response_same_event(
assert TransactionEvent.objects.count() == 2
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert event
assert event.pk == existing_authorize_success.pk

Expand Down Expand Up @@ -1391,6 +1398,7 @@ def test_create_transaction_event_from_request_handle_incorrect_values(
assert TransactionEvent.objects.count() == 2
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is False


@freeze_time("2018-05-31 12:00:01")
Expand Down Expand Up @@ -1438,6 +1446,7 @@ def test_create_transaction_event_from_request_and_webhook_response_different_am
assert TransactionEvent.objects.count() == 3
request_event.refresh_from_db()
assert request_event.psp_reference == expected_psp_reference
assert request_event.include_in_calculations is True
assert failed_event
assert failed_event.psp_reference == expected_psp_reference
assert failed_event.type == TransactionEventType.AUTHORIZATION_FAILURE
Expand Down
7 changes: 4 additions & 3 deletions saleor/payment/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,10 @@ def create_transaction_event_from_request_and_webhook_response(
return failure_event

psp_reference = transaction_request_response.psp_reference
request_event.psp_reference = psp_reference
request_event.include_in_calculations = True
request_event.save(update_fields=["psp_reference", "include_in_calculations"])
if psp_reference is not None:
request_event.psp_reference = psp_reference
request_event.include_in_calculations = True
request_event.save(update_fields=["psp_reference", "include_in_calculations"])
event = None
if response_event := transaction_request_response.event:
event, error_msg = _create_event_from_response(
Expand Down

0 comments on commit f89b030

Please sign in to comment.