Skip to content

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

Clean up code

Maciej Korycinski4aace2a1e0f8
diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.py
index 70146027f5..86fce35be9 100644
--- a/saleor/payment/gateways/adyen/plugin.py
+++ b/saleor/payment/gateways/adyen/plugin.py
@@ -403,6 +403,8 @@ class AdyenGatewayPlugin(BasePlugin):
currency=payment_information.currency,
).first()
is_success = True
+
+ # confirm that we should proceed the capture action
if (
not transaction_already_processed
and config.auto_capture
diff --git a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_additional_actions.py b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_additional_actions.py
index 96f34f7a3a..449121f91d 100644
--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_additional_actions.py
+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_additional_actions.py
@@ -8,6 +8,11 @@ import graphene
from ..... import PaymentError, TransactionKind
from ...webhooks import handle_additional_actions
+ERROR_MSG_MISSING_PAYMENT = "Cannot perform payment.There is no active adyen payment."
+ERROR_MSG_MISSING_CHECKOUT = (
+ "Cannot perform payment.There is no checkout with this payment."
+)
+
@mock.patch("saleor.payment.gateways.adyen.webhooks.api_call")
def test_handle_additional_actions_post(
@@ -180,9 +185,7 @@ def test_handle_additional_actions_payment_does_not_exist(payment_adyen_for_chec
# then
assert response.status_code == 404
- assert response.content.decode() == (
- "Cannot perform payment.There is no active adyen payment."
- )
+ assert response.content.decode() == ERROR_MSG_MISSING_PAYMENT
def test_handle_additional_actions_payment_lack_of_return_url(
@@ -269,9 +272,7 @@ def test_handle_additional_actions_checkout_not_related_to_payment(
# then
assert response.status_code == 404
- assert response.content.decode() == (
- "Cannot perform payment.There is no checkout with this payment."
- )
+ assert response.content.decode() == ERROR_MSG_MISSING_CHECKOUT
def test_handle_additional_actions_payment_does_not_have_checkout(
@@ -301,9 +302,7 @@ def test_handle_additional_actions_payment_does_not_have_checkout(
# then
assert response.status_code == 404
- assert response.content.decode() == (
- "Cannot perform payment.There is no checkout with this payment."
- )
+ assert response.content.decode() == ERROR_MSG_MISSING_CHECKOUT
@mock.patch("saleor.payment.gateways.adyen.webhooks.api_call")
@@ -368,9 +367,7 @@ def test_handle_additional_actions_payment_not_active(payment_adyen_for_checkout
# then
assert response.status_code == 404
- assert response.content.decode() == (
- "Cannot perform payment.There is no active adyen payment."
- )
+ assert response.content.decode() == ERROR_MSG_MISSING_PAYMENT
def test_handle_additional_actions_payment_with_no_adyen_gateway(
@@ -401,9 +398,7 @@ def test_handle_additional_actions_payment_with_no_adyen_gateway(
# then
assert response.status_code == 404
- assert response.content.decode() == (
- "Cannot perform payment.There is no active adyen payment."
- )
+ assert response.content.decode() == ERROR_MSG_MISSING_PAYMENT
@mock.patch("saleor.payment.gateways.adyen.webhooks.api_call")
diff --git a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
index e7f0b61bd2..38135a2dd0 100644
--- a/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
+++ b/saleor/payment/gateways/adyen/tests/webhooks/test_handle_notifications.py
@@ -39,12 +39,10 @@ def test_handle_authorization_for_order(
transaction = payment.transactions.last()
assert transaction.is_success is True
assert transaction.kind == TransactionKind.AUTH
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_multiple_authorization_notification(
@@ -67,12 +65,10 @@ def test_handle_multiple_authorization_notification(
assert payment.charge_status == ChargeStatus.NOT_CHARGED
capture_transaction = payment.transactions.get(kind=TransactionKind.CAPTURE)
assert capture_transaction.is_success is False
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
second_notification = notification(
merchant_reference=payment_id,
@@ -86,12 +82,10 @@ def test_handle_multiple_authorization_notification(
kind=TransactionKind.CAPTURE
).last()
assert capture_transaction.is_success is True
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 2
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 2
def test_handle_authorization_for_pending_order(
@@ -114,12 +108,10 @@ def test_handle_authorization_for_pending_order(
assert transaction.kind == TransactionKind.CAPTURE
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_authorization_for_checkout(
@@ -159,12 +151,10 @@ def test_handle_authorization_for_checkout(
assert payment.checkout is None
assert payment.order
assert payment.order.checkout_token == checkout_token
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_authorization_with_adyen_auto_capture(
@@ -199,12 +189,10 @@ def test_handle_authorization_with_adyen_auto_capture(
assert payment.transactions.count() == 2
transaction = payment.transactions.filter(kind=TransactionKind.CAPTURE).get()
assert transaction.is_success is True
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
@pytest.mark.vcr
@@ -230,12 +218,10 @@ def test_handle_authorization_with_auto_capture(
transaction = payment.transactions.filter(kind=TransactionKind.CAPTURE).get()
assert transaction.is_success is True
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_authorization_with_adyen_auto_capture_and_payment_charged(
@@ -256,12 +242,10 @@ def test_handle_authorization_with_adyen_auto_capture_and_payment_charged(
payment.refresh_from_db()
assert payment.transactions.count() == 2
assert payment.transactions.filter(kind=TransactionKind.CAPTURE).exists()
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_cancel(notification, adyen_plugin, payment_adyen_for_order):
@@ -282,12 +266,10 @@ def test_handle_cancel(notification, adyen_plugin, payment_adyen_for_order):
assert transaction.is_success is True
assert payment.order.status == OrderStatus.CANCELED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_cancel_already_canceled(
@@ -323,12 +305,10 @@ def test_handle_capture_for_order(notification, adyen_plugin, payment_adyen_for_
transaction = payment.transactions.filter(kind=TransactionKind.CAPTURE).get()
assert transaction.is_success is True
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_capture_for_checkout(
@@ -368,12 +348,10 @@ def test_handle_capture_for_checkout(
assert payment.checkout is None
assert payment.order
assert payment.order.checkout_token == checkout_token
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_capture_with_payment_already_charged(
@@ -395,12 +373,10 @@ def test_handle_capture_with_payment_already_charged(
# Payment is already captured so no need to save capture transaction
payment.refresh_from_db()
assert payment.transactions.count() == 2
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
@pytest.mark.parametrize(
@@ -427,12 +403,10 @@ def test_handle_failed_capture(
assert transaction.is_success is True
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.NOT_CHARGED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_failed_capture_partial_charge(
@@ -456,12 +430,10 @@ def test_handle_failed_capture_partial_charge(
assert transaction.is_success is True
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_pending(notification, adyen_plugin, payment_adyen_for_order):
@@ -480,12 +452,10 @@ def test_handle_pending(notification, adyen_plugin, payment_adyen_for_order):
assert transaction.is_success is True
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.PENDING
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_pending_with_adyen_auto_capture(
@@ -508,12 +478,10 @@ def test_handle_pending_with_adyen_auto_capture(
assert payment.transactions.filter(kind=TransactionKind.PENDING).first()
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.PENDING
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_pending_already_pending(
@@ -561,12 +529,10 @@ def test_handle_refund(
mock_order_refunded.assert_called_once_with(
payment.order, None, transaction.amount, payment
)
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
@mock.patch("saleor.payment.gateways.adyen.webhooks.order_refunded")
@@ -608,12 +574,10 @@ def test_handle_failed_refund_missing_transaction(
handle_failed_refund(notification, config)
assert payment.transactions.count() == 1
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_failed_refund_with_transaction_refund_ongoing(
@@ -632,17 +596,14 @@ def test_handle_failed_refund_with_transaction_refund_ongoing(
create_new_transaction(notification, payment, TransactionKind.REFUND_ONGOING)
handle_failed_refund(notification, config)
- assert (
- payment.transactions.count() == 4
- ) # ACTION_TO_CONFIRM, REFUND_ONGOING, REFUND_FAILED, FULLY_CHARGED
+ # ACTION_TO_CONFIRM, REFUND_ONGOING, REFUND_FAILED, FULLY_CHARGED
+ assert payment.transactions.count() == 4
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.total == payment.captured_amount
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_failed_refund_with_transaction_refund(
@@ -665,12 +626,10 @@ def test_handle_failed_refund_with_transaction_refund(
assert payment.transactions.count() == 4 # REFUND, REFUND_FAILED, FULLY_CHARGED
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.total == payment.captured_amount
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_reversed_refund(notification, adyen_plugin, payment_adyen_for_order):
@@ -690,12 +649,10 @@ def test_handle_reversed_refund(notification, adyen_plugin, payment_adyen_for_or
assert payment.transactions.filter(kind=TransactionKind.REFUND_REVERSED).exists()
assert payment.charge_status == ChargeStatus.FULLY_CHARGED
assert payment.total == payment.captured_amount
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
def test_handle_reversed_refund_already_processed(
@@ -731,12 +688,10 @@ def test_webhook_not_implemented(notification, adyen_plugin, payment_adyen_for_o
webhook_not_implemented(notification, config)
- assert (
- payment.order.events.filter(
- type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- ).count()
- == 1
+ external_events = payment.order.events.filter(
+ type=OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
)
+ assert external_events.count() == 1
@mock.patch("saleor.payment.gateways.adyen.webhooks.handle_refund")
diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.py
index 842f661f88..3bdd0989cc 100644
--- a/saleor/payment/gateways/adyen/webhooks.py
+++ b/saleor/payment/gateways/adyen/webhooks.py
@@ -148,7 +148,8 @@ def create_order(payment, checkout):
except ValidationError:
payment_refund_or_void(payment)
return None
- payment.order = order
+ # Refresh the payment to assign the newly created order
+ payment.refresh_from_db()
return order