Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move payment checks to seprate function in handle_additional_actions for adyen
IKarbowiakf7c5938891b8
diff --git a/saleor/payment/gateways/adyen/tests/test_webhook.py b/saleor/payment/gateways/adyen/tests/test_webhook.pyindex 400aa976dd..fc40167982 100644--- a/saleor/payment/gateways/adyen/tests/test_webhook.py+++ b/saleor/payment/gateways/adyen/tests/test_webhook.py@@ -866,3 +866,63 @@ def test_handle_additional_actions_api_call_error(# thenassert response.status_code == 400assert response.content.decode() == error_message+++def test_handle_additional_actions_payment_not_active(payment_adyen_for_checkout):+ # given+ payment_adyen_for_checkout.extra_data = json.dumps(+ {"payment_data": "test_data", "parameters": ["payload"]}+ )+ payment_adyen_for_checkout.is_active = False+ payment_adyen_for_checkout.save(update_fields=["extra_data", "is_active"])++ checkout = payment_adyen_for_checkout.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": checkout.pk}+ request_mock.POST = {"payload": "test"}++ payment_details_mock = mock.Mock()+ message = {+ "resultCode": "Test",+ }+ payment_details_mock.return_value.message = message++ # when+ response = handle_additional_actions(request_mock, payment_details_mock)++ # then+ assert response.status_code == 400+ assert response.content.decode() == "Payment is not active."+++def test_handle_additional_actions_payment_with_no_adyen_gateway(+ payment_adyen_for_checkout,+):+ # given+ payment_adyen_for_checkout.extra_data = json.dumps(+ {"payment_data": "test_data", "parameters": ["payload"]}+ )+ payment_adyen_for_checkout.gateway = "test"+ payment_adyen_for_checkout.save(update_fields=["extra_data", "gateway"])++ checkout = payment_adyen_for_checkout.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": checkout.pk}+ request_mock.POST = {"payload": "test"}++ payment_details_mock = mock.Mock()+ message = {+ "resultCode": "Test",+ }+ payment_details_mock.return_value.message = message++ # when+ response = handle_additional_actions(request_mock, payment_details_mock)++ # then+ assert response.status_code == 400+ assert response.content.decode() == "Cannot perform not adyen payment."diff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex b2e7d1b128..7d6ae948ad 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -496,6 +496,7 @@ def handle_webhook(request: WSGIRequest, gateway_config: "GatewayConfig"):def handle_additional_actions(request: WSGIRequest, payment_details: Callable):payment_id = request.GET.get("payment")checkout_pk = request.GET.get("checkout")+if not payment_id or not checkout_pk:return HttpResponseNotFound()@@ -505,15 +506,9 @@ def handle_additional_actions(request: WSGIRequest, payment_details: Callable):except ObjectDoesNotExist:return HttpResponseNotFound("Cannot perform payment. Payment does not exists.")- 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"- )+ response = validate_payment(payment, checkout_pk)+ if response:+ return responsedata = json.loads(payment.extra_data)return_url = payment.return_url@@ -548,3 +543,21 @@ def handle_additional_actions(request: WSGIRequest, payment_details: Callable):params.update(result.message["action"])return redirect(prepare_url(urlencode(params), return_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.")