Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Update pspReference when recieving different event than success (#15110)

Maciej Korycinskic0a655f8ed5e
diff --git a/saleor/checkout/actions.py b/saleor/checkout/actions.py
index 8f2f4e055d..5c39045c89 100644
--- a/saleor/checkout/actions.py
+++ b/saleor/checkout/actions.py
@@ -11,6 +11,17 @@ from .models import Checkout
from .payment_utils import update_refundable_for_checkout
+def update_last_transaction_modified_at_for_checkout(
+ checkout: Checkout, transaction: TransactionItem
+):
+ if (
+ not checkout.last_transaction_modified_at
+ or checkout.last_transaction_modified_at < transaction.modified_at
+ ):
+ checkout.last_transaction_modified_at = transaction.modified_at
+ checkout.save(update_fields=["last_transaction_modified_at"])
+
+
def transaction_amounts_for_checkout_updated(
transaction: TransactionItem,
manager: "PluginsManager",
@@ -31,12 +42,7 @@ def transaction_amounts_for_checkout_updated(
CheckoutChargeStatus.OVERCHARGED,
]
- if (
- not checkout.last_transaction_modified_at
- or checkout.last_transaction_modified_at < transaction.modified_at
- ):
- checkout.last_transaction_modified_at = transaction.modified_at
- checkout.save(update_fields=["last_transaction_modified_at"])
+ update_last_transaction_modified_at_for_checkout(checkout, transaction)
refundable = transaction.last_refund_success and (
transaction.authorized_value > Decimal(0)
diff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.py
index 870b2cecfd..8aabaf4b06 100644
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@ -1927,6 +1927,117 @@ def test_create_transaction_event_updates_transaction_modified_at(
assert checkout.last_transaction_modified_at == calculation_time
+def test_create_transaction_event_for_transaction_session_failure_set_psp_reference(
+ transaction_item_generator,
+ transaction_session_response,
+ webhook_app,
+ plugins_manager,
+):
+ # given
+ expected_psp_reference = "ABC"
+ expected_amount = Decimal("15")
+ response = transaction_session_response.copy()
+ response["result"] = TransactionEventType.CHARGE_FAILURE.upper()
+ response["amount"] = expected_amount
+ response["pspReference"] = expected_psp_reference
+
+ transaction = transaction_item_generator(available_actions=["charge"])
+ request_event = TransactionEvent.objects.create(
+ transaction=transaction,
+ include_in_calculations=False,
+ amount_value=expected_amount,
+ type=TransactionEventType.CHARGE_REQUEST,
+ )
+
+ # when
+ create_transaction_event_for_transaction_session(
+ request_event,
+ webhook_app,
+ manager=plugins_manager,
+ transaction_webhook_response=response,
+ )
+
+ # then
+ transaction.refresh_from_db()
+ assert transaction.events.count() == 2
+ failure_event = transaction.events.last()
+ assert failure_event.psp_reference == expected_psp_reference
+ assert failure_event.type == TransactionEventType.CHARGE_FAILURE
+ assert transaction.psp_reference == expected_psp_reference
+
+
+def test_create_transaction_event_for_transaction_session_when_psp_ref_missing(
+ transaction_item_generator,
+ transaction_session_response,
+ webhook_app,
+ plugins_manager,
+):
+ # given
+ expected_amount = Decimal("15")
+ response = transaction_session_response.copy()
+ response["result"] = TransactionEventType.CHARGE_ACTION_REQUIRED.upper()
+ response["amount"] = expected_amount
+ response["pspReference"] = None
+
+ transaction = transaction_item_generator(available_actions=["charge"])
+ current_psp_reference = transaction.psp_reference
+ request_event = TransactionEvent.objects.create(
+ transaction=transaction,
+ include_in_calculations=False,
+ amount_value=expected_amount,
+ type=TransactionEventType.CHARGE_REQUEST,
+ )
+
+ # when
+ create_transaction_event_for_transaction_session(
+ request_event,
+ webhook_app,
+ manager=plugins_manager,
+ transaction_webhook_response=response,
+ )
+
+ # then
+ transaction.refresh_from_db()
+ assert transaction.events.count() == 2
+ assert transaction.psp_reference == current_psp_reference
+
+
+@freeze_time("2018-05-31 12:00:01")
+def test_create_transaction_event_updates_transaction_modified_at_for_failure(
+ transaction_item_generator,
+ transaction_session_response,
+ webhook_app,
+ plugins_manager,
+ checkout,
+):
+ # given
+ expected_amount = Decimal("15")
+ response = transaction_session_response.copy()
+ response["amount"] = expected_amount
+ response["result"] = TransactionEventType.CHARGE_FAILURE.upper()
+
+ transaction = transaction_item_generator(checkout_id=checkout.pk)
+ request_event = TransactionEvent.objects.create(
+ transaction=transaction, include_in_calculations=False
+ )
+
+ # when
+ with freeze_time("2023-03-18 12:00:00"):
+ calculation_time = datetime.now(pytz.UTC)
+ create_transaction_event_for_transaction_session(
+ request_event,
+ webhook_app,
+ manager=plugins_manager,
+ transaction_webhook_response=response,
+ )
+
+ # then
+ transaction.refresh_from_db()
+ checkout.refresh_from_db()
+ assert transaction.modified_at == calculation_time
+ assert checkout.last_transaction_modified_at == calculation_time
+
+
def test_recalculate_refundable_for_checkout_with_request_refund(
transaction_item_generator, checkout
):
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 668484ee4b..d9a772ca67 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -17,7 +17,10 @@ from django.utils import timezone
from ..account.models import User
from ..app.models import App
from ..channel import TransactionFlowStrategy
-from ..checkout.actions import transaction_amounts_for_checkout_updated
+from ..checkout.actions import (
+ transaction_amounts_for_checkout_updated,
+ update_last_transaction_modified_at_for_checkout,
+)
from ..checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ..checkout.models import Checkout
from ..checkout.payment_utils import update_refundable_for_checkout
@@ -1229,13 +1232,13 @@ def create_transaction_event_for_transaction_session(
if request_event_update_fields:
request_event.save(update_fields=request_event_update_fields)
+ transaction_item = event.transaction
if event.type in [
TransactionEventType.AUTHORIZATION_REQUEST,
TransactionEventType.AUTHORIZATION_SUCCESS,
TransactionEventType.CHARGE_REQUEST,
TransactionEventType.CHARGE_SUCCESS,
]:
- transaction_item = event.transaction
previous_authorized_value = transaction_item.authorized_value
previous_charged_value = transaction_item.charged_value
previous_refunded_value = transaction_item.refunded_value
@@ -1280,6 +1283,13 @@ def create_transaction_event_for_transaction_session(
)
elif transaction_item.checkout_id:
transaction_amounts_for_checkout_updated(transaction_item, manager)
+ elif event.psp_reference and transaction_item.psp_reference != event.psp_reference:
+ transaction_item.psp_reference = event.psp_reference
+ transaction_item.save(update_fields=["psp_reference", "modified_at"])
+ if transaction_item.checkout_id:
+ checkout = cast(Checkout, transaction_item.checkout)
+ update_last_transaction_modified_at_for_checkout(checkout, transaction_item)
+
return event