Skip to content

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

WIP fix refunds

Mateusz Grzyb30d326ba6a0a
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index ec29fcdb3f..9c78a9b2e6 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -1448,6 +1448,8 @@ def _process_refund(
refund_data = RefundData(
order_lines_to_refund=order_lines_to_refund,
fulfillment_lines_to_refund=fulfillment_lines_to_refund,
+ refund_shipping_costs=refund_shipping_costs,
+ amount=amount,
)
if amount is None:
amount = _calculate_refund_amount(
diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.py
index e81e2405b0..b278244e09 100644
--- a/saleor/payment/gateways/np_atobarai/api.py
+++ b/saleor/payment/gateways/np_atobarai/api.py
@@ -100,14 +100,14 @@ def 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 = get_goods_with_refunds(config, payment, payment_information)
+ goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)
data = {
"transactions": [
{
"np_transaction_id": payment.psp_reference,
"billed_amount": format_price(
- payment.captured_amount - payment_information.amount,
+ billed_amount,
payment_information.currency,
),
"goods": goods,
@@ -175,17 +175,12 @@ def reregister_transaction_for_partial_return(
)
return errors_payment_result(error_messages)
- goods = get_goods_with_refunds(config, payment, payment_information)
-
- billed_amount = format_price(
- payment.captured_amount - payment_information.amount,
- payment_information.currency,
- )
+ goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)
result, error_codes = register(
config,
payment_information,
- billed_amount,
+ format_price(billed_amount, payment_information.currency),
goods,
)
diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.py
index 242b09974c..c872734288 100644
--- a/saleor/payment/gateways/np_atobarai/api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py
@@ -1,6 +1,6 @@
import logging
from decimal import Decimal
-from typing import TYPE_CHECKING, Iterable, List, Optional
+from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple
import requests
from django.utils import timezone
@@ -175,40 +175,55 @@ def get_goods_with_refunds(
config: "ApiConfig",
payment: Payment,
payment_information: PaymentData,
-) -> List[dict]:
+) -> Tuple[List[dict], Decimal]:
"""Combine PaymentLinesData and RefundData into NP Atobarai's goods list.
Used for payment updates.
Returns current state of order lines after refunds and total order amount.
"""
goods_lines = []
- refund_data = payment_information.refund_data or RefundData(
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=[],
- )
+ if not payment_information.refund_data:
+ payment_information.refund_data = RefundData(
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=[],
+ )
order = payment.order
if not order:
raise PaymentError("Cannot refund payment without order.")
- refunded_lines = create_refunded_lines(order, refund_data)
+ print(payment_information.amount)
+
+ refunded_lines = create_refunded_lines(order, payment_information)
refunded_manual_amount = calculate_manual_refund_amount(order, payment_information)
+ billed_amount = Decimal("0.00")
for line in payment_information.lines_data.lines:
quantity = line.quantity
refunded_quantity = refunded_lines.get(line.variant_id)
if refunded_quantity:
quantity -= refunded_quantity
- goods_lines.append(
- {
- "goods_name": _get_goods_name(line, config),
- "goods_price": format_price(line.amount, payment_information.currency),
- "quantity": quantity,
- }
- )
+ if quantity:
+ goods_lines.append(
+ {
+ "goods_name": _get_goods_name(line, config),
+ "goods_price": format_price(
+ line.amount, payment_information.currency
+ ),
+ "quantity": quantity,
+ }
+ )
+ billed_amount += line.amount * quantity
+
+ # if there are no products in goods lines,
+ # do not calculate shipping/voucher/manual refund
+ if not goods_lines:
+ return [], Decimal("0.00")
goods_lines.extend(_get_voucher_and_shipping_goods(config, payment_information))
+ billed_amount += payment_information.lines_data.shipping_amount
+ billed_amount += payment_information.lines_data.voucher_amount
if refunded_manual_amount:
goods_lines.append(
@@ -220,8 +235,13 @@ def get_goods_with_refunds(
"quantity": 1,
}
)
+ billed_amount -= refunded_manual_amount
- return goods_lines
+ for line in goods_lines:
+ print(line)
+ print(billed_amount)
+
+ return goods_lines, billed_amount
def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dict]:
diff --git a/saleor/payment/gateways/np_atobarai/plugin.py b/saleor/payment/gateways/np_atobarai/plugin.py
index 62bd5a05cb..f898cecc02 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_ID
PLUGIN_NAME = GATEWAY_NAME
CONFIGURATION_PER_CHANNEL = True
- SUPPORTED_CURRENCIES = "JPY"
+ SUPPORTED_CURRENCIES = "PLN"
DEFAULT_CONFIGURATION = [
{"name": MERCHANT_CODE, "value": None},
diff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.py
index c04cbf5f21..7a55e66175 100644
--- a/saleor/payment/gateways/np_atobarai/utils.py
+++ b/saleor/payment/gateways/np_atobarai/utils.py
@@ -11,7 +11,7 @@ from ....order import FulfillmentStatus
from ....order.events import external_notification_event
from ....order.models import Fulfillment, FulfillmentLine, Order
from ... import PaymentError
-from ...interface import PaymentData, RefundData
+from ...interface import PaymentData
from .api_types import ApiConfig
from .const import SHIPPING_COMPANY_CODE_METADATA_KEY, SHIPPING_COMPANY_CODES
@@ -70,13 +70,23 @@ def np_atobarai_opentracing_trace(span_name: str):
def create_refunded_lines(
order: Order,
- refund_data: RefundData,
+ payment_information: PaymentData,
) -> 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:
+ 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
+ )
+
previous_fulfillment_lines = FulfillmentLine.objects.prefetch_related(
"order_line"
).filter(
@@ -95,12 +105,12 @@ def create_refunded_lines(
)
current_order_refund_lines = (
(variant.id, line1.quantity)
- for line1 in refund_data.order_lines_to_refund
+ for line1 in order_lines_to_refund
if (variant := line1.variant)
)
current_fulfillment_refund_lines = (
(f_variant_id, line1.quantity)
- for line1 in refund_data.fulfillment_lines_to_refund
+ for line1 in fulfillment_lines_to_refund
if (f_variant_id := line1.line.order_line.variant_id)
)
@@ -125,7 +135,21 @@ def calculate_manual_refund_amount(
Takes into account previous refunds and current refund mutation amount.
"""
- manual_amount_to_refund = payment_information.amount or Decimal("0.00")
+ assert payment_information.refund_data
+ if (
+ not payment_information.refund_data.amount
+ and payment_information.refund_data.refund_shipping_costs
+ ):
+ manual_amount_to_refund = payment_information.lines_data.shipping_amount
+ else:
+ 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 = }")
previous_manual_amount_to_refund = (
order.fulfillments.filter(
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index 4aa9de8953..e31d16436a 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -88,8 +88,10 @@ class PaymentLinesData:
@dataclass
class RefundData:
- order_lines_to_refund: List[OrderLineInfo]
- fulfillment_lines_to_refund: List[FulfillmentLineData]
+ order_lines_to_refund: List[OrderLineInfo] = field(default_factory=list)
+ fulfillment_lines_to_refund: List[FulfillmentLineData] = field(default_factory=list)
+ refund_shipping_costs: bool = False
+ amount: Optional[Decimal] = None
@dataclass