Skip to content

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

Merge branch 'adyen_integration' into add-klarna-support

IKarbowiak09ee4d45b845
diff --cc saleor/payment/gateways/adyen/tests/test_utils.py
index c64c0ce136,4fed932842..7c3d4f9cfd
--- a/saleor/payment/gateways/adyen/tests/test_utils.py
+++ b/saleor/payment/gateways/adyen/tests/test_utils.py
@@@ -1,269 -1,87 +1,355 @@@
+ import json
+from decimal import Decimal
+from unittest import mock
-from ..utils import update_payment_with_action_required_data
+import pytest
+from prices import Money, TaxedMoney
+
+from .....core.prices import quantize_price
+from .... import PaymentError
+from ..utils import (
+ append_klarna_data,
+ convert_adyen_price_format,
+ get_price_amount,
+ get_shopper_locale_value,
+ request_data_for_gateway_config,
+ request_data_for_payment,
++ update_payment_with_action_required_data,
+)
+
+
+ "country_code, shopper_locale", [("JP", "ja_JP"), ("ZZ", "en_US"), ("US", "en_US")]
+)
+def test_get_shopper_locale_value(country_code, shopper_locale, settings):
+ # when
+ result = get_shopper_locale_value(country_code)
+
+ # then
+ assert result == shopper_locale
+
+
+def test_append_klarna_data(dummy_payment_data, payment_dummy, checkout_with_item):
+ # given
+ checkout_with_item.payments.add(payment_dummy)
+ line = checkout_with_item.lines.first()
+ payment_data = {
+ "reference": "test",
+ }
+
+ # when
+ result = append_klarna_data(dummy_payment_data, payment_data)
+
+ # then
+ total = get_price_amount(
+ line.variant.price_amount * line.quantity, line.variant.currency
+ )
+ assert result == {
+ "reference": "test",
+ "shopperLocale": "en_US",
+ "shopperReference": dummy_payment_data.customer_email,
+ "countryCode": str(checkout_with_item.country),
+ "shopperEmail": dummy_payment_data.customer_email,
+ "lineItems": [
+ {
+ "description": line.variant.product.description,
+ "quantity": line.quantity,
+ "id": line.variant.sku,
+ "taxAmount": "0",
+ "taxPercentage": 0,
+ "amountExcludingTax": total,
+ "amountIncludingTax": total,
+ }
+ ],
+ }
+
+
[email protected]("saleor.payment.gateways.adyen.utils.checkout_line_total")
+def test_append_klarna_data_tax_included(
+ mocked_checkout_line_total, dummy_payment_data, payment_dummy, checkout_with_item
+):
+ # given
+ tax_percent = 5
+ net = Money(10, "USD")
+ gross = tax_percent * net / 100 + net
+ mocked_checkout_line_total.return_value = quantize_price(
+ TaxedMoney(net=net, gross=gross), "USD"
+ )
+
+ checkout_with_item.payments.add(payment_dummy)
+ line = checkout_with_item.lines.first()
+ payment_data = {
+ "reference": "test",
+ }
+
+ # when
+ result = append_klarna_data(dummy_payment_data, payment_data)
+
+ # then
+ assert result == {
+ "reference": "test",
+ "shopperLocale": "en_US",
+ "shopperReference": dummy_payment_data.customer_email,
+ "countryCode": str(checkout_with_item.country),
+ "shopperEmail": dummy_payment_data.customer_email,
+ "lineItems": [
+ {
+ "description": line.variant.product.description,
+ "quantity": line.quantity,
+ "id": line.variant.sku,
+ "taxAmount": get_price_amount((gross - net).amount, "USD"),
+ "taxPercentage": tax_percent,
+ "amountExcludingTax": get_price_amount(net.amount, "USD"),
+ "amountIncludingTax": get_price_amount(gross.amount, "USD"),
+ }
+ ],
+ }
+
+
+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
+
+
+ "value, currency, expected_result",
+ [
+ (Decimal(1000), "EUR", Decimal(10)),
+ (Decimal(1), "PLN", Decimal("0.01")),
+ (Decimal(51), "US", Decimal("0.51")),
+ ],
+)
+def test_convert_adyen_price_format(value, currency, expected_result):
+ # when
+ result = convert_adyen_price_format(value, currency)
+
+ # then
+ assert result == expected_result
+
+
+ "value, currency, expected_result",
+ [
+ (Decimal(10), "EUR", "1000"),
+ (Decimal(1), "PLN", "100"),
+ (Decimal(100), "US", "10000"),
+ ],
+)
+def test_get_price_amount(value, currency, expected_result):
+ # when
+ result = get_price_amount(value, currency)
+
+ # then
+ assert result == expected_result
+
+
+def test_request_data_for_gateway_config(checkout, address):
+ # given
+ checkout.billing_address = address
+ merchant_account = "test_account"
+
+ # when
+ response_config = request_data_for_gateway_config(checkout, merchant_account)
+
+ # then
+ assert response_config == {
+ "merchantAccount": merchant_account,
+ "countryCode": checkout.billing_address.country,
+ "channel": "web",
+ }
+
+
+def test_request_data_for_gateway_config_no_country(checkout, address, settings):
+ # given
+ merchant_account = "test_account"
+
+ # when
+ response_config = request_data_for_gateway_config(checkout, merchant_account)
+
+ # then
+ assert response_config == {
+ "merchantAccount": merchant_account,
+ "countryCode": settings.DEFAULT_COUNTRY,
+ "channel": "web",
+ }
+
+
+ def test_update_payment_with_action_required_data_empty_extra_data(
+ payment_adyen_for_checkout,
+ ):
+ # given
+ payment_adyen_for_checkout.extra_data = ""
+ payment_adyen_for_checkout.save(update_fields=["extra_data"])
+
+ action = {
+ "paymentData": "test_data",
+ }
+ details = [
+ {"key": "payload", "type": "text"},
+ {"key": "secondParam", "type": "text"},
+ ]
+
+ # when
+ update_payment_with_action_required_data(
+ payment_adyen_for_checkout, action, details
+ )
+
+ # then
+ payment_adyen_for_checkout.refresh_from_db()
+ extra_data = json.loads(payment_adyen_for_checkout.extra_data)
+ assert len(extra_data) == 1
+ assert extra_data[0]["payment_data"] == action["paymentData"]
+ assert set(extra_data[0]["parameters"]) == {"payload", "secondParam"}
+
+
+ def test_update_payment_with_action_required_data_extra_data_as_list(
+ payment_adyen_for_checkout,
+ ):
+ # given
+ payment_adyen_for_checkout.extra_data = json.dumps([{"test_data": "test"}])
+ payment_adyen_for_checkout.save(update_fields=["extra_data"])
+
+ action = {
+ "paymentData": "test_data",
+ }
+ details = [
+ {"key": "payload", "type": "text"},
+ {"key": "secondParam", "type": "text"},
+ ]
+
+ # when
+ update_payment_with_action_required_data(
+ payment_adyen_for_checkout, action, details
+ )
+
+ # then
+ payment_adyen_for_checkout.refresh_from_db()
+ extra_data = json.loads(payment_adyen_for_checkout.extra_data)
+ assert len(extra_data) == 2
+ assert extra_data[1]["payment_data"] == action["paymentData"]
+ assert set(extra_data[1]["parameters"]) == {"payload", "secondParam"}
+
+
+ def test_update_payment_with_action_required_data_extra_data_as_dict(
+ payment_adyen_for_checkout,
+ ):
+ # given
+ payment_adyen_for_checkout.extra_data = json.dumps({"test_data": "test"})
+ payment_adyen_for_checkout.save(update_fields=["extra_data"])
+
+ action = {
+ "paymentData": "test_data",
+ }
+ details = [
+ {"key": "payload", "type": "text"},
+ {"key": "secondParam", "type": "text"},
+ ]
+
+ # when
+ update_payment_with_action_required_data(
+ payment_adyen_for_checkout, action, details
+ )
+
+ # then
+ payment_adyen_for_checkout.refresh_from_db()
+ extra_data = json.loads(payment_adyen_for_checkout.extra_data)
+ assert len(extra_data) == 2
+ assert extra_data[1]["payment_data"] == action["paymentData"]
+ assert set(extra_data[1]["parameters"]) == {"payload", "secondParam"}
diff --cc saleor/payment/gateways/adyen/utils.py
index e2b0cfee0a,f6a4a63891..b9e00975eb
--- a/saleor/payment/gateways/adyen/utils.py
+++ b/saleor/payment/gateways/adyen/utils.py
@@@ -8,10 -8,9 +9,11 @@@ from babel.numbers import get_currency_
from django.conf import settings
from django_countries.fields import Country
+from ....checkout.calculations import checkout_line_total
from ....checkout.models import Checkout
from ....core.prices import quantize_price
+from ....discount.utils import fetch_active_discounts
+ from ....payment.models import Payment
from ... import PaymentError
from ...interface import PaymentData