From 72c66df7b526f29fb91308305c46d5c27a2bc976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Armando=20Rodr=C3=ADguez?= <127134616+armando-rodriguez-cko@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:35:34 +0200 Subject: [PATCH] Add Klarna Payment Context Support --- checkout_sdk/payments/contexts/contexts.py | 18 ++++++- requirements-dev.txt | 1 + .../payment_contexts_integration_test.py | 49 ++++++++++++++++--- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/checkout_sdk/payments/contexts/contexts.py b/checkout_sdk/payments/contexts/contexts.py index 84f363d1..06668bc0 100644 --- a/checkout_sdk/payments/contexts/contexts.py +++ b/checkout_sdk/payments/contexts/contexts.py @@ -1,6 +1,8 @@ from datetime import datetime -from checkout_sdk.common.common import Address, CustomerRequest +from deprecated import deprecated + +from checkout_sdk.common.common import Address, CustomerRequest, AccountHolder from checkout_sdk.common.enums import Currency, PaymentSourceType from checkout_sdk.payments.payments import PaymentRequestSource, PaymentType, ShippingDetails, BillingPlan, \ ShippingPreference, UserAction @@ -87,7 +89,21 @@ class PaymentContextsRequest: items: list # payments.contexts.PaymentContextsItems +@deprecated("This class will be removed in the future. Use PaymentContextPaypalSource instead") class PaymentContextPayPalSource(PaymentRequestSource): def __init__(self): super().__init__(PaymentSourceType.PAYPAL) + + +class PaymentContextPaypalSource(PaymentRequestSource): + + def __init__(self): + super().__init__(PaymentSourceType.PAYPAL) + + +class PaymentContextKlarnaSource(PaymentRequestSource): + account_holder: AccountHolder + + def __init__(self): + super().__init__(PaymentSourceType.KLARNA) diff --git a/requirements-dev.txt b/requirements-dev.txt index f83a9c99..c1cc2ee3 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -5,3 +5,4 @@ flake8>=4.0.1 python-dateutil>=2.8.2 pre-commit>=2.17.0 pylint>=2.12.2 +Deprecated>=1.2.14 \ No newline at end of file diff --git a/tests/payments/contexts/payment_contexts_integration_test.py b/tests/payments/contexts/payment_contexts_integration_test.py index 72f82daa..5f045a6c 100644 --- a/tests/payments/contexts/payment_contexts_integration_test.py +++ b/tests/payments/contexts/payment_contexts_integration_test.py @@ -2,11 +2,12 @@ import os -from checkout_sdk.common.enums import Currency -from checkout_sdk.payments.contexts.contexts import PaymentContextsRequest, PaymentContextPayPalSource, \ - PaymentContextsItems +from checkout_sdk.common.common import AccountHolder, Address +from checkout_sdk.common.enums import Currency, Country +from checkout_sdk.payments.contexts.contexts import PaymentContextsRequest, PaymentContextsItems, \ + PaymentContextPaypalSource, PaymentContextKlarnaSource, PaymentContextsProcessing from checkout_sdk.payments.payments import PaymentType -from tests.checkout_test_utils import assert_response +from tests.checkout_test_utils import assert_response, APM_SERVICE_UNAVAILABLE, check_error_item def test_should_create_and_get_payment_context_details(default_api): @@ -36,17 +37,51 @@ def test_should_create_and_get_payment_context_details(default_api): 'partner_metadata.order_id') +def test_create_payment_contexts_klarna_request(default_api): + processing = PaymentContextsProcessing() + processing.locale = "en-GB" + + billing_address = Address() + billing_address.country = Country.DE + + account_holder = AccountHolder() + account_holder.billing_address = billing_address + + source = PaymentContextKlarnaSource() + source.account_holder = account_holder + + items = PaymentContextsItems() + items.name = 'mask' + items.unit_price = 1000 + items.quantity = 1 + items.total_amount = 1000 + + request = PaymentContextsRequest() + request.source = source + request.amount = 1000 + request.currency = Currency.EUR + request.payment_type = PaymentType.REGULAR + request.processing_channel_id = os.environ.get('CHECKOUT_PROCESSING_CHANNEL_ID') + request.items = [items] + request.processing = processing + + check_error_item(callback=default_api.contexts.create_payment_contexts, + error_item=APM_SERVICE_UNAVAILABLE, + payment_contexts_request=request) + + def create_payment_contexts_request(): - source = PaymentContextPayPalSource() + source = PaymentContextPaypalSource() items = PaymentContextsItems() items.name = 'mask' - items.unit_price = 2000 + items.unit_price = 1000 items.quantity = 1 + items.total_amount = 1000 request = PaymentContextsRequest() request.source = source - request.amount = 2000 + request.amount = 1000 request.currency = Currency.EUR request.payment_type = PaymentType.REGULAR request.capture = True