Skip to content

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

Add test for request_data_for_payment in adyen

IKarbowiakdc13c4b0a751
diff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.py
index 3b2ba58b74..014eecd846 100644
--- a/saleor/payment/gateways/adyen/tests/test_utils.py
+++ b/saleor/payment/gateways/adyen/tests/test_utils.py
@@ -1,6 +1,14 @@
+from unittest import mock
+
import pytest
-from ..utils import append_klarna_data, get_price_amount, get_shopper_locale_value
+from .... import PaymentError
+from ..utils import (
+ append_klarna_data,
+ get_price_amount,
+ get_shopper_locale_value,
+ request_data_for_payment,
+)
@pytest.mark.parametrize(
@@ -26,13 +34,13 @@ def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_ite
}
# when
- append_klarna_data(dummy_payment_data, payment_data)
+ result = append_klarna_data(dummy_payment_data, payment_data)
# then
total = get_price_amount(
line.variant.price_amount * line.quantity, line.variant.currency
)
- assert payment_data == {
+ assert result == {
"reference": "test",
"shopperLocale": "en_US",
"shopperReference": dummy_payment_data.customer_email,
@@ -50,3 +58,105 @@ def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_ite
}
],
}
+
+
+def test_request_data_for_payment_payment_not_valid(dummy_payment_data):
+ # given
+ dummy_payment_data.data = {
+ "is_valid": False,
+ }
+
+ # when
+ with pytest.raises(PaymentError) as e:
+ request_data_for_payment(
+ dummy_payment_data,
+ "https://www.example.com",
+ "MerchantTestAccount",
+ "https://www.example.com",
+ )
+
+ # then
+ assert str(e._excinfo[1]) == "Payment data are not valid"
+
+
+def test_request_data_for_payment(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
+
+ # when
+ result = request_data_for_payment(
+ dummy_payment_data, return_url, merchant_account, return_url,
+ )
+
+ # then
+ assert result == {
+ "amount": {
+ "value": get_price_amount(
+ dummy_payment_data.amount, dummy_payment_data.currency
+ ),
+ "currency": dummy_payment_data.currency,
+ },
+ "reference": dummy_payment_data.payment_id,
+ "paymentMethod": {"type": "scheme"},
+ "returnUrl": return_url,
+ "merchantAccount": merchant_account,
+ "origin": return_url,
+ "shopperIP": data["shopperIP"],
+ "billingAddress": data["billingAddress"],
+ "browserInfo": data["browserInfo"],
+ }
+
+
[email protected]("saleor.payment.gateways.adyen.utils.append_klarna_data")
+def test_request_data_for_payment_append_klarna_data(
+ append_klarna_data_mock, dummy_payment_data
+):
+ # given
+ return_url = "https://www.example.com"
+ merchant_account = "MerchantTestAccount"
+ data = {
+ "is_valid": True,
+ "riskData": {"clientData": "test_client_data"},
+ "paymentMethod": {"type": "klarna"},
+ "browserInfo": {"acceptHeader": "*/*", "colorDepth": 30, "language": "pl"},
+ "billingAddress": {"address": "test_address"},
+ "shopperIP": "123",
+ }
+ dummy_payment_data.data = data
+ klarna_result = {
+ "amount": {
+ "value": get_price_amount(
+ dummy_payment_data.amount, dummy_payment_data.currency
+ ),
+ "currency": dummy_payment_data.currency,
+ },
+ "reference": dummy_payment_data.payment_id,
+ "paymentMethod": {"type": "scheme"},
+ "returnUrl": return_url,
+ "merchantAccount": merchant_account,
+ "origin": return_url,
+ "shopperIP": data["shopperIP"],
+ "billingAddress": data["billingAddress"],
+ "browserInfo": data["browserInfo"],
+ "shopperLocale": "test_shopper",
+ "shopperEmail": "test_email",
+ }
+ append_klarna_data_mock.return_value = klarna_result
+
+ # when
+ result = request_data_for_payment(
+ dummy_payment_data, return_url, merchant_account, return_url,
+ )
+
+ # then
+ assert result == klarna_result
diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.py
index 47761bc2e8..669cadf6da 100644
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@ -47,7 +47,10 @@ def api_call(request_data: Optional[Dict[str, Any]], method: Callable) -> Adyen.
def request_data_for_payment(
- payment_information: "PaymentData", return_url, merchant_account, origin_url
+ payment_information: "PaymentData",
+ return_url: str,
+ merchant_account: str,
+ origin_url: str,
) -> Dict[str, Any]:
payment_data = payment_information.data or {}
@@ -88,7 +91,7 @@ def request_data_for_payment(
method = payment_method.get("type", [])
if "klarna" in method:
- append_klarna_data(payment_information, request_data)
+ request_data = append_klarna_data(payment_information, request_data)
return request_data
@@ -125,6 +128,7 @@ def append_klarna_data(payment_information: "PaymentData", payment_data: dict):
}
line_items.append(line_data)
payment_data["lineItems"] = line_items
+ return payment_data
def get_shopper_locale_value():
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index e4f4df7ef4..5478a2f593 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1572,7 +1572,7 @@ def dummy_gateway_config():
@pytest.fixture
def dummy_payment_data(payment_dummy):
return PaymentData(
- amount=10,
+ amount=Decimal(10),
currency="USD",
payment_id=graphene.Node.to_global_id("Payment", payment_dummy.pk),
billing=None,