Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add klarna data to request
IKarbowiak29d4dc339180
diff --git a/saleor/payment/gateways/adyen/tests/test_utils.py b/saleor/payment/gateways/adyen/tests/test_utils.pynew file mode 100644index 0000000000..3b2ba58b74--- /dev/null+++ b/saleor/payment/gateways/adyen/tests/test_utils.py@@ -0,0 +1,52 @@+import pytest++from ..utils import append_klarna_data, get_price_amount, get_shopper_locale_value+++ "language_code, shopper_locale", [("ja", "ja_JP"), ("zz", "en_US"), ("en", "en_US")]+)+def test_get_shopper_locale_value(language_code, shopper_locale, settings):+ # given+ settings.LANGUAGE_CODE = language_code++ # when+ result = get_shopper_locale_value()++ # 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+ 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 == {+ "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,+ }+ ],+ }diff --git a/saleor/payment/gateways/adyen/utils.py b/saleor/payment/gateways/adyen/utils.pyindex 4a54f45b37..47761bc2e8 100644--- a/saleor/payment/gateways/adyen/utils.py+++ b/saleor/payment/gateways/adyen/utils.py@@ -3,12 +3,15 @@ from decimal import Decimalfrom typing import Any, Callable, Dict, Optionalimport Adyen+import graphenefrom babel.numbers import get_currency_precisionfrom django.conf import settingsfrom django_countries.fields import Country+from ....checkout.calculations import checkout_line_totalfrom ....checkout.models import Checkoutfrom ....core.prices import quantize_price+from ....discount.utils import fetch_active_discountsfrom ... import PaymentErrorfrom ...interface import PaymentData@@ -43,51 +46,6 @@ def api_call(request_data: Optional[Dict[str, Any]], method: Callable) -> Adyen.raise PaymentError("Unable to process the payment request.")-def append_klarna_data(payment_information: "PaymentData", payment_data):- # FIXME Add klarna data- pass- # _type, payment_id = from_global_id(payment_information.payment_id)- # checkout = Checkout.objects.filter(payments__id=payment_data).first()- # if not checkout:- # raise PaymentError("Unable to calculate products for klarna")- # discounts = fetch_active_discounts()- # checkout.lines.prefetch_related("variant").all()- # def resolve_total_price(self, info):- # def calculate_total_price(discounts):- # return info.context.plugins.calculate_checkout_line_total(- # checkout_line=self, discounts=discounts- # )- #- # return (- # DiscountsByDateTimeLoader(info.context)- # .load(info.context.request_time)- # .then(calculate_total_price)- # )-- # payment_data["shopperEmail"] = payment_information.customer_email- # payment_data["lineItems"] = [- # {- # "quantity": "1",- # "amountExcludingTax": "450",- # "taxPercentage": "1111",- # "description": "Sunglasses",- # "id": "Item #1",- # "taxAmount": "50",- # "amountIncludingTax": "500",- # "taxCategory": "High"- # },- # {- # "quantity": "1",- # "amountExcludingTax": "450",- # "taxPercentage": "1111",- # "description": "Headphones",- # "id": "Item #2",- # "taxAmount": "50",- # "amountIncludingTax": "500",- # "taxCategory": "High"- # }]--def request_data_for_payment(payment_information: "PaymentData", return_url, merchant_account, origin_url) -> Dict[str, Any]:@@ -112,7 +70,7 @@ def request_data_for_payment(# web1:https://shop.com, web2:https://shop1.comextra_request_params["origin"] = origin_url- method = payment_data["paymentMethod"].get("type", [])+ payment_method = payment_data["paymentMethod"]request_data = {"amount": {@@ -122,19 +80,79 @@ def request_data_for_payment("currency": payment_information.currency,},"reference": payment_information.payment_id,- "paymentMethod": payment_data.get("paymentMethod"),+ "paymentMethod": payment_method,"returnUrl": return_url,"merchantAccount": merchant_account,**extra_request_params,}+ method = payment_method.get("type", [])if "klarna" in method:- # TODOappend_klarna_data(payment_information, request_data)return request_data+def append_klarna_data(payment_information: "PaymentData", payment_data: dict):+ _type, payment_pk = graphene.Node.from_global_id(payment_information.payment_id)+ checkout = Checkout.objects.filter(payments__id=payment_pk).first()++ if not checkout:+ raise PaymentError("Unable to calculate products for klarna")++ lines = checkout.lines.prefetch_related("variant").all()+ discounts = fetch_active_discounts()+ currency = payment_information.currency++ payment_data["shopperLocale"] = get_shopper_locale_value()+ payment_data["shopperReference"] = payment_information.customer_email+ payment_data["countryCode"] = checkout.get_country()+ payment_data["shopperEmail"] = payment_information.customer_email+ line_items = []+ for line in lines:+ total = checkout_line_total(line=line, discounts=discounts)+ total_gross = total.gross.amount+ total_net = total.net.amount+ tax_amount = total.tax.amount+ line_data = {+ "quantity": line.quantity,+ "amountExcludingTax": get_price_amount(total_net, currency),+ "taxPercentage": round(tax_amount / total_gross * 100),+ "description": line.variant.product.description,+ "id": line.variant.sku,+ "taxAmount": get_price_amount(tax_amount, currency),+ "amountIncludingTax": get_price_amount(total_gross, currency),+ }+ line_items.append(line_data)+ payment_data["lineItems"] = line_items+++def get_shopper_locale_value():+ # Remove this function when "shopperLocale" will come from frontend site+ language_code_to_shopper_locale_value = {+ # https://docs.adyen.com/checkout/components-web/+ # localization-components#change-language+ "zh": "zh_CN",+ "da": "da_DK",+ "nl": "nl_NL",+ "en": "en_US",+ "fi": "fi_FI",+ "fr": "fr_FR",+ "de": "de_DE",+ "it": "it_IT",+ "ja": "ja_JP",+ "ko": "ko_KR",+ "no": "no_NO",+ "pl": "pl_PL",+ "pt": "pt_BR",+ "ru": "ru_RU",+ "es": "es_ES",+ "sv": "sv_SE",+ }+ default_language_code = settings.LANGUAGE_CODE+ return language_code_to_shopper_locale_value.get(default_language_code, "en_US")++def request_data_for_gateway_config(checkout: "Checkout", merchant_account) -> Dict[str, str]: