Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add configuration field to enable the native 3d secure support (#6298)
Maciej Korycinski90b28873b941
diff --git a/saleor/graphql/checkout/tests/test_checkout_complete.py b/saleor/graphql/checkout/tests/test_checkout_complete.pyindex 9872115647..d0f2b663fa 100644--- a/saleor/graphql/checkout/tests/test_checkout_complete.py+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py@@ -492,7 +492,9 @@ def test_checkout_confirm(address,shipping_method,):- mock_get_manager.confirm_payment.return_value = ACTION_REQUIRED_GATEWAY_RESPONSE+ response = ACTION_REQUIRED_GATEWAY_RESPONSE+ response.action_required = False+ mock_get_manager.confirm_payment.return_value = responsecheckout = checkout_with_itemcheckout.shipping_address = addressdiff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex 9a3b29553a..ab04079b05 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -208,17 +208,24 @@ def void(payment: Payment) -> Transaction:@payment_postprocessdef confirm(payment: Payment, additional_data: Optional[dict] = None) -> Transaction:plugin_manager = get_plugins_manager()- token = _get_past_transaction_token(payment, TransactionKind.ACTION_TO_CONFIRM)+ txn = payment.transactions.filter(+ kind=TransactionKind.ACTION_TO_CONFIRM, is_success=True+ ).last()+ token = txn.token if txn else ""payment_data = create_payment_information(payment=payment, payment_token=token, additional_data=additional_data)response, error = _fetch_gateway_response(plugin_manager.confirm_payment, payment.gateway, payment_data)+ action_required = response is not None and response.action_required+ if response and response.payment_method_info:+ update_payment_method_details(payment, response)return get_already_processed_transaction_or_create_new_transaction(payment=payment,kind=TransactionKind.CONFIRM,payment_information=payment_data,+ action_required=action_required,error_msg=error,gateway_response=response,)diff --git a/saleor/payment/gateways/adyen/plugin.py b/saleor/payment/gateways/adyen/plugin.pyindex c94adab6f6..435528ad00 100644--- a/saleor/payment/gateways/adyen/plugin.py+++ b/saleor/payment/gateways/adyen/plugin.py@@ -61,6 +61,7 @@ class AdyenGatewayPlugin(BasePlugin):{"name": "hmac-secret-key", "value": ""},{"name": "notification-user", "value": ""},{"name": "notification-password", "value": ""},+ {"name": "enable-native-3d-secure", "value": False},]CONFIG_STRUCTURE = {@@ -169,6 +170,17 @@ class AdyenGatewayPlugin(BasePlugin):),"label": "Notification password",},+ "enable-native-3d-secure": {+ "type": ConfigurationTypeField.BOOLEAN,+ "help_text": (+ "Saleor uses 3D Secure redirect authentication by default. If you want"+ " to use native 3D Secure authentication, enable this option. For more"+ " details see Adyen documentation: native - "+ "https://docs.adyen.com/checkout/3d-secure/redirect-3ds2-3ds1, redirect"+ " - https://docs.adyen.com/checkout/3d-secure/redirect-3ds2-3ds1"+ ),+ "label": "Enable native 3d secure",+ },}def __init__(self, *args, **kwargs):@@ -188,6 +200,7 @@ class AdyenGatewayPlugin(BasePlugin):"webhook_user": configuration["notification-user"],"webhook_user_password": configuration["notification-password"],"adyen_auto_capture": configuration["adyen-auto-capture"],+ "enable_native_3d_secure": configuration["enable-native-3d-secure"],},)api_key = self.config.connection_params["api_key"]@@ -267,6 +280,7 @@ class AdyenGatewayPlugin(BasePlugin):return_url=return_url,merchant_account=self.config.connection_params["merchant_account"],origin_url=self.config.connection_params["origin_url"],+ native_3d_secure=self.config.connection_params["enable_native_3d_secure"],)result = api_call(request_data, self.adyen.checkout.payments)result_code = result.message["resultCode"].strip().lower()@@ -293,9 +307,7 @@ class AdyenGatewayPlugin(BasePlugin):token=result.message.get("pspReference"),adyen_client=self.adyen,)-payment_method_info = get_payment_method_info(payment_information, result)-return GatewayResponse(is_success=is_success,action_required="action" in result.message,@@ -334,7 +346,7 @@ class AdyenGatewayPlugin(BasePlugin):if not additional_data:raise PaymentError("Unable to finish the payment.")- result = api_call(additional_data, self.adyen.checkout.payments)+ result = api_call(additional_data, self.adyen.checkout.payments_details)result_code = result.message["resultCode"].strip().lower()is_success = result_code not in FAILED_STATUSES@@ -346,9 +358,12 @@ class AdyenGatewayPlugin(BasePlugin):response = self.capture_payment(payment_information, None)is_success = response.is_success+ payment_method_info = get_payment_method_info(payment_information, result)+ action = result.message.get("action")return GatewayResponse(is_success=is_success,action_required="action" in result.message,+ action_required_data=action,kind=kind,amount=payment_information.amount,currency=payment_information.currency,@@ -356,6 +371,7 @@ class AdyenGatewayPlugin(BasePlugin):error=result.message.get("refusalReason"),raw_response=result.message,searchable_key=result.message.get("pspReference", ""),+ payment_method_info=payment_method_info,)@require_active_plugindiff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.pyindex 4cbaee3a58..c0eca80146 100644--- a/saleor/payment/gateways/adyen/tests/test_utils.py+++ b/saleor/payment/gateways/adyen/tests/test_utils.py@@ -113,6 +113,7 @@ def test_request_data_for_payment_payment_not_valid(dummy_payment_data):dummy_payment_data.data = {"is_valid": False,}+ native_3d_secure = False# whenwith pytest.raises(PaymentError) as e:@@ -121,6 +122,7 @@ def test_request_data_for_payment_payment_not_valid(dummy_payment_data):"https://www.example.com","MerchantTestAccount","https://www.example.com",+ native_3d_secure,)# then@@ -140,10 +142,11 @@ def test_request_data_for_payment(dummy_payment_data):"shopperIP": "123",}dummy_payment_data.data = data+ native_3d_secure = False# whenresult = request_data_for_payment(- dummy_payment_data, return_url, merchant_account, return_url,+ dummy_payment_data, return_url, merchant_account, return_url, native_3d_secure)# then@@ -162,6 +165,78 @@ def test_request_data_for_payment(dummy_payment_data):"shopperIP": data["shopperIP"],"billingAddress": data["billingAddress"],"browserInfo": data["browserInfo"],+ "channel": "web",+ }+++def test_request_data_for_payment_native_3d_secure(dummy_payment_data):+ # given+ return_url = "https://www.example.com"+ merchant_account = "MerchantTestAccount"+ data = {+ "is_valid": True,+ "riskData": {"clientData": "test_client_data"},+ "paymentMethod": {"type": "scheme"},+ "browserInfo": {"acceptHeader": "*/*", "colorDepth": 30, "language": "pl"},+ "billingAddress": {"address": "test_address"},+ "shopperIP": "123",+ }+ dummy_payment_data.data = data+ native_3d_secure = True++ # when+ result = request_data_for_payment(+ dummy_payment_data, return_url, merchant_account, return_url, native_3d_secure+ )++ # then+ assert result == {+ "amount": {+ "value": to_adyen_price(+ dummy_payment_data.amount, dummy_payment_data.currency+ ),+ "currency": dummy_payment_data.currency,+ },+ "reference": dummy_payment_data.graphql_payment_id,+ "paymentMethod": {"type": "scheme"},+ "returnUrl": return_url,+ "merchantAccount": merchant_account,+ "origin": return_url,+ "shopperIP": data["shopperIP"],+ "billingAddress": data["billingAddress"],+ "browserInfo": data["browserInfo"],+ "channel": "web",+ "additionalData": {"allow3DS2": "true"},+ }+++def test_request_data_for_payment_channel_different_than_web(dummy_payment_data):+ # given+ return_url = "https://www.example.com"+ merchant_account = "MerchantTestAccount"+ data = {"is_valid": True, "paymentMethod": {"type": "scheme"}, "channel": "iOS"}+ dummy_payment_data.data = data+ native_3d_secure = True++ # when+ result = request_data_for_payment(+ dummy_payment_data, return_url, merchant_account, return_url, native_3d_secure+ )++ # then+ assert result == {+ "amount": {+ "value": to_adyen_price(+ dummy_payment_data.amount, dummy_payment_data.currency+ ),+ "currency": dummy_payment_data.currency,+ },+ "reference": dummy_payment_data.graphql_payment_id,+ "paymentMethod": {"type": "scheme"},+ "returnUrl": return_url,+ "merchantAccount": merchant_account,+ "channel": "iOS",+ "additionalData": {"allow3DS2": "true"},}@@ -200,10 +275,10 @@ def test_request_data_for_payment_append_klarna_data("shopperEmail": "test_email",}append_klarna_data_mock.return_value = klarna_result-+ native_3d_secure = False# whenresult = request_data_for_payment(- dummy_payment_data, return_url, merchant_account, return_url,+ dummy_payment_data, return_url, merchant_account, return_url, native_3d_secure)# then@@ -417,4 +492,4 @@ def test_get_payment_method_info_no_additional_data(dummy_payment_data):)# then- assert payment_method_info is None+ assert payment_method_info == PaymentMethodInfo(type="card")diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.pyindex 42c485076f..21b5718f99 100644--- a/saleor/payment/gateways/adyen/utils.py+++ b/saleor/payment/gateways/adyen/utils.py@@ -58,6 +58,7 @@ def request_data_for_payment(return_url: str,merchant_account: str,origin_url: str,+ native_3d_secure: bool,) -> Dict[str, Any]:payment_data = payment_information.data or {}@@ -65,19 +66,21 @@ def request_data_for_payment(raise PaymentError("Payment data are not valid.")extra_request_params = {}+ channel = payment_data.get("channel", "web")if "browserInfo" in payment_data:extra_request_params["browserInfo"] = payment_data["browserInfo"]if "billingAddress" in payment_data:extra_request_params["billingAddress"] = payment_data["billingAddress"]if "shopperIP" in payment_data:extra_request_params["shopperIP"] = payment_data["shopperIP"]- if (- "browserInfo" in extra_request_params- and "billingAddress" in extra_request_params- and origin_url- ):++ if channel.lower() == "web" and origin_url:extra_request_params["origin"] = origin_url+ extra_request_params["channel"] = channel+ if native_3d_secure:+ extra_request_params["additionalData"] = {"allow3DS2": "true"}+payment_method = payment_data.get("paymentMethod")if not payment_method:raise PaymentError("Unable to find the paymentMethod section.")@@ -280,13 +283,13 @@ def request_for_payment_cancel(def get_payment_method_info(payment_information: "PaymentData", api_call_result: Adyen.Adyen):- payment_method_info = Noneadditional_data = api_call_result.message.get("additionalData")payment_data = payment_information.data or {}payment_method = payment_data.get("paymentMethod", {}).get("type")+ brand = Noneif additional_data:- payment_method_info = PaymentMethodInfo(- brand=additional_data.get("paymentMethod"),- type="card" if payment_method == "scheme" else payment_method,- )+ brand = additional_data.get("paymentMethod")+ payment_method_info = PaymentMethodInfo(+ brand=brand, type="card" if payment_method == "scheme" else payment_method,+ )return payment_method_infodiff --git a/saleor/payment/gateways/adyen/webhooks.py b/saleor/payment/gateways/adyen/webhooks.pyindex a79cb8846b..5a2e226bbc 100644--- a/saleor/payment/gateways/adyen/webhooks.py+++ b/saleor/payment/gateways/adyen/webhooks.py@@ -623,7 +623,6 @@ def handle_additional_actions(try:request_data = prepare_api_request_data(request, data)except KeyError as e:-return HttpResponseBadRequest(e.args[0])try:result = api_call(request_data, payment_details)@@ -689,6 +688,10 @@ def handle_api_response(payment_data = create_payment_information(payment=payment, payment_token=payment.token,)+ payment_brand = response.message.get("additionalData", {}).get("paymentMethod")+ if payment_brand:+ payment.cc_brand = payment_brand+ payment.save(update_fields=["cc_brand"])error_message = response.message.get("refusalReason")