Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Clean up code
Mateusz Grzyb7cc2983b1ae2
diff --git a/saleor/payment/gateways/np_atobarai/__init__.py b/saleor/payment/gateways/np_atobarai/__init__.pyindex 2490692705..466481010b 100644--- a/saleor/payment/gateways/np_atobarai/__init__.py+++ b/saleor/payment/gateways/np_atobarai/__init__.py@@ -9,6 +9,7 @@ from ... import PaymentError, TransactionKindfrom ...interface import GatewayResponse, PaymentDatafrom ...models import Paymentfrom . import api+from .api_helpers import get_goods_with_refundsfrom .api_types import ApiConfig, PaymentStatusfrom .const import NP_PLUGIN_IDfrom .utils import (@@ -112,9 +113,12 @@ def refund(payment_information: PaymentData, config: ApiConfig) -> GatewayResponif not payment:raise PaymentError(f"Payment with id {payment_id} does not exist.")+ goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)+# in case of refunding less than already captured+ # and not all lines are being refunded,# the transaction needs to be re-registered in NP Atobarai- if payment_information.amount < payment.captured_amount:+ if payment_information.amount < payment.captured_amount and goods:order = payment.orderif not order:@@ -126,6 +130,8 @@ def refund(payment_information: PaymentData, config: ApiConfig) -> GatewayResponconfig,payment,payment_information,+ goods,+ billed_amount,)if result.status == PaymentStatus.FOR_REREGISTRATION:@@ -138,6 +144,8 @@ def refund(payment_information: PaymentData, config: ApiConfig) -> GatewayResponpayment_information,shipping_company_code,tracking_number,+ goods,+ billed_amount,)else:diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.pyindex b278244e09..5f06a8fb01 100644--- a/saleor/payment/gateways/np_atobarai/api.py+++ b/saleor/payment/gateways/np_atobarai/api.py@@ -1,4 +1,5 @@import logging+from decimal import Decimalfrom typing import List, Optional, Tuplefrom ....order.models import Fulfillment@@ -7,7 +8,6 @@ from ...models import Paymentfrom .api_helpers import (cancel,format_price,- get_goods_with_refunds,handle_unrecoverable_state,np_request,register,@@ -94,14 +94,14 @@ def change_transaction(config: ApiConfig,payment: Payment,payment_information: PaymentData,+ goods: List[dict],+ billed_amount: Decimal,) -> PaymentResult:"""Change transaction.If the fulfillment was reported prior to changing given transaction,then no change is applied and payment status is set to FOR_REREGISTRATION."""- goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)-data = {"transactions": [{@@ -152,6 +152,8 @@ def reregister_transaction_for_partial_return(payment_information: PaymentData,shipping_company_code: Optional[str],tracking_number: Optional[str],+ goods: List[dict],+ billed_amount: Decimal,) -> PaymentResult:"""Change transaction.@@ -175,8 +177,6 @@ def reregister_transaction_for_partial_return()return errors_payment_result(error_messages)- goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)-result, error_codes = register(config,payment_information,diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.pyindex c872734288..09d5e08081 100644--- a/saleor/payment/gateways/np_atobarai/api_helpers.py+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py@@ -182,20 +182,16 @@ def get_goods_with_refunds(Returns current state of order lines after refunds and total order amount."""goods_lines = []- if not payment_information.refund_data:- payment_information.refund_data = RefundData(- order_lines_to_refund=[],- fulfillment_lines_to_refund=[],- )+ refund_data = payment_information.refund_data or RefundData()order = payment.orderif not order:raise PaymentError("Cannot refund payment without order.")- print(payment_information.amount)-- refunded_lines = create_refunded_lines(order, payment_information)- refunded_manual_amount = calculate_manual_refund_amount(order, payment_information)+ refunded_lines = create_refunded_lines(order, refund_data)+ refunded_manual_amount = calculate_manual_refund_amount(+ order, payment_information, refund_data+ )billed_amount = Decimal("0.00")for line in payment_information.lines_data.lines:@@ -237,10 +233,6 @@ def get_goods_with_refunds()billed_amount -= refunded_manual_amount- for line in goods_lines:- print(line)- print(billed_amount)-return goods_lines, billed_amountdiff --git a/saleor/payment/gateways/np_atobarai/plugin.py b/saleor/payment/gateways/np_atobarai/plugin.pyindex f898cecc02..62bd5a05cb 100644--- a/saleor/payment/gateways/np_atobarai/plugin.py+++ b/saleor/payment/gateways/np_atobarai/plugin.py@@ -36,7 +36,7 @@ class NPAtobaraiGatewayPlugin(BasePlugin):PLUGIN_ID = NP_PLUGIN_IDPLUGIN_NAME = GATEWAY_NAMECONFIGURATION_PER_CHANNEL = True- SUPPORTED_CURRENCIES = "PLN"+ SUPPORTED_CURRENCIES = "JPY"DEFAULT_CONFIGURATION = [{"name": MERCHANT_CODE, "value": None},diff --git a/saleor/payment/gateways/np_atobarai/tests/test_utils.py b/saleor/payment/gateways/np_atobarai/tests/test_utils.pyindex c044b1812e..ee3aa08361 100644--- a/saleor/payment/gateways/np_atobarai/tests/test_utils.py+++ b/saleor/payment/gateways/np_atobarai/tests/test_utils.py@@ -399,6 +399,5 @@ def test_calculate_manual_refund_amount_previously_refunded(refund_amount = calculate_manual_refund_amount(order, Mock(amount=current_refund_amount))- print(refund_amount, order.shipping_price_gross_amount)# thenassert refund_amount == expected_refund_amountdiff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.pyindex 7a55e66175..610591b41b 100644--- a/saleor/payment/gateways/np_atobarai/utils.py+++ b/saleor/payment/gateways/np_atobarai/utils.py@@ -11,7 +11,7 @@ from ....order import FulfillmentStatusfrom ....order.events import external_notification_eventfrom ....order.models import Fulfillment, FulfillmentLine, Orderfrom ... import PaymentError-from ...interface import PaymentData+from ...interface import PaymentData, RefundDatafrom .api_types import ApiConfigfrom .const import SHIPPING_COMPANY_CODE_METADATA_KEY, SHIPPING_COMPANY_CODES@@ -70,22 +70,19 @@ def np_atobarai_opentracing_trace(span_name: str):def create_refunded_lines(order: Order,- payment_information: PaymentData,+ refund_data: RefundData,) -> Dict[int, int]:"""Return all refunded product variants for specified order.Takes into account previous refunds and current refund mutation parameters.:return: Dictionary of variant ids and refunded quantities."""- assert payment_information.refund_data- if payment_information.refund_data.amount:+ if refund_data.amount:order_lines_to_refund = []fulfillment_lines_to_refund = []else:- order_lines_to_refund = payment_information.refund_data.order_lines_to_refund- fulfillment_lines_to_refund = (- payment_information.refund_data.fulfillment_lines_to_refund- )+ order_lines_to_refund = refund_data.order_lines_to_refund+ fulfillment_lines_to_refund = refund_data.fulfillment_lines_to_refundprevious_fulfillment_lines = FulfillmentLine.objects.prefetch_related("order_line"@@ -130,26 +127,16 @@ def create_refunded_lines(def calculate_manual_refund_amount(order: Order,payment_information: PaymentData,+ refund_data: RefundData,) -> Decimal:"""Return sum of all manual refunds for specified order.Takes into account previous refunds and current refund mutation amount."""- assert payment_information.refund_data- if (- not payment_information.refund_data.amount- and payment_information.refund_data.refund_shipping_costs- ):+ if not refund_data.amount and refund_data.refund_shipping_costs:manual_amount_to_refund = payment_information.lines_data.shipping_amountelse:- manual_amount_to_refund = payment_information.refund_data.amount or Decimal(- "0.00"- )-- print(f"{payment_information.refund_data.amount = }")- print(f"{payment_information.refund_data.refund_shipping_costs = }")- print(f"{payment_information.lines_data.shipping_amount = }")- print(f"{manual_amount_to_refund = }")+ manual_amount_to_refund = refund_data.amount or Decimal("0.00")previous_manual_amount_to_refund = (order.fulfillments.filter(