Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move validation_payment to handle_additional_actions in adyen webhooks.
IKarbowiak5dc25ffed1f6
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.pyindex 07df1631b0..28821f53f7 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@@ -1,4 +1,5 @@import json+import uuidfrom unittest import mockfrom urllib.parse import quote_plus@@ -271,8 +272,9 @@ def test_handle_additional_actions_payment_does_not_exist(payment_adyen_for_chec# thenassert response.status_code == 404- assert (- response.content.decode() == "Cannot perform payment. Payment does not exists."+ assert response.content.decode() == (+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout.")@@ -350,7 +352,7 @@ def test_handle_additional_actions_checkout_not_related_to_payment(payment_id = graphene.Node.to_global_id("Payment", payment_adyen_for_checkout.pk)request_mock = mock.Mock()- request_mock.GET = {"payment": payment_id, "checkout": "123"}+ request_mock.GET = {"payment": payment_id, "checkout": uuid.uuid4()}request_mock.POST = {"payload": "test"}payment_details_mock = mock.Mock()@@ -365,10 +367,10 @@ def test_handle_additional_actions_checkout_not_related_to_payment()# then- assert response.status_code == 400- assert (- response.content.decode()- == "The given checkout is not related to the specified payment"+ assert response.status_code == 404+ assert response.content.decode() == (+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout.")@@ -385,7 +387,7 @@ def test_handle_additional_actions_payment_does_not_have_checkout(payment_id = graphene.Node.to_global_id("Payment", payment_adyen_for_checkout.pk)request_mock = mock.Mock()- request_mock.GET = {"payment": payment_id, "checkout": "123"}+ request_mock.GET = {"payment": payment_id, "checkout": uuid.uuid4()}request_mock.POST = {"payload": "test"}payment_details_mock = mock.Mock()@@ -400,10 +402,10 @@ def test_handle_additional_actions_payment_does_not_have_checkout()# then- assert response.status_code == 400- assert (- response.content.decode()- == "The given payment does not have the corresponding checkout."+ assert response.status_code == 404+ assert response.content.decode() == (+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout.")@@ -472,8 +474,11 @@ def test_handle_additional_actions_payment_not_active(payment_adyen_for_checkout)# then- assert response.status_code == 400- assert response.content.decode() == "Payment is not active."+ assert response.status_code == 404+ assert response.content.decode() == (+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout."+ )def test_handle_additional_actions_payment_with_no_adyen_gateway(@@ -505,8 +510,11 @@ def test_handle_additional_actions_payment_with_no_adyen_gateway()# then- assert response.status_code == 400- assert response.content.decode() == "Cannot perform not adyen payment."+ assert response.status_code == 404+ assert response.content.decode() == (+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout."+ )@mock.patch("saleor.payment.gateways.adyen.webhooks.api_call")diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex a477c67d1f..a98b5b0a40 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -523,13 +523,17 @@ def handle_additional_actions(_type, payment_pk = from_global_id(payment_id)try:- payment = Payment.objects.get(pk=payment_pk)+ payment = Payment.objects.get(+ pk=payment_pk,+ checkout__pk=checkout_pk,+ is_active=True,+ gateway="mirumee.payments.adyen",+ )except ObjectDoesNotExist:- return HttpResponseNotFound("Cannot perform payment. Payment does not exists.")-- response = validate_payment(payment, checkout_pk)- if response:- return response+ return HttpResponseNotFound(+ "Cannot perform payment. "+ "There is no active adyen payment with specified checkout."+ )extra_data = json.loads(payment.extra_data)data = extra_data[-1] if isinstance(extra_data, list) else extra_data@@ -558,24 +562,6 @@ def handle_additional_actions(return redirect(redirect_url)-def validate_payment(payment: "Payment", checkout_pk: str):- if payment.checkout is None:- return HttpResponseBadRequest(- "The given payment does not have the corresponding checkout."- )-- if payment.checkout.pk != checkout_pk:- return HttpResponseBadRequest(- "The given checkout is not related to the specified payment"- )-- if not payment.is_active:- return HttpResponseBadRequest("Payment is not active.")-- if payment.gateway != "mirumee.payments.adyen":- return HttpResponseBadRequest("Cannot perform not adyen payment.")--def prepare_api_request_data(request: WSGIRequest, data: dict):if "parameters" not in data or "payment_data" not in data:raise KeyError(