Skip to content

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

Fix billing amount for partial refunds

Mateusz Grzyb537b2594c907
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index eab24c039c..2988b48930 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -1446,6 +1446,12 @@ def _process_refund(
manager: "PluginsManager",
):
lines_to_refund: Dict[OrderLineIDType, Tuple[QuantityType, OrderLine]] = dict()
+ 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(
order_lines_to_refund, fulfillment_lines_to_refund, lines_to_refund
@@ -1462,11 +1468,7 @@ def _process_refund(
manager,
amount=amount,
channel_slug=order.channel.slug,
- 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,
- ),
+ refund_data=refund_data,
)
except PaymentError:
raise ValidationError(
diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.py
index e81e2405b0..0340e65c78 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,12 +175,7 @@ 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,
diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.py
index 435f7e78d1..fe1041ab2c 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
@@ -8,6 +8,7 @@ from posuto import Posuto
from requests.auth import HTTPBasicAuth
from ....order.models import Order
+from ... import PaymentError
from ...interface import AddressData, PaymentData, PaymentLineData, RefundData
from ...models import Payment
from ...utils import price_to_minor_unit
@@ -176,20 +177,23 @@ 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.
+ 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=[],
refund_shipping_costs=False,
+ amount=None,
)
+
order = payment.order
- assert order
+ if not order:
+ raise PaymentError("Cannot refund payment without order.")
refunded_lines = create_refunded_lines(order, refund_data)
refunded_shipping = calculate_refunded_shipping(order, refund_data)
@@ -197,6 +201,8 @@ def get_goods_with_refunds(
order, refund_data, payment_information
)
+ total_order_amount = Decimal("0.00")
+
for line in payment_information.lines_data.lines:
quantity = line.quantity
refunded_quantity = refunded_lines.get(line.variant_id)
@@ -211,6 +217,8 @@ def get_goods_with_refunds(
}
)
+ total_order_amount += line.amount * quantity
+
if refunded_manual_amount:
goods_lines.append(
{
@@ -221,12 +229,17 @@ def get_goods_with_refunds(
"quantity": 1,
}
)
+ total_order_amount -= refunded_manual_amount
goods_lines.extend(
_get_voucher_and_shipping_goods(config, payment_information, refunded_shipping)
)
- return goods_lines
+ total_order_amount += payment_information.lines_data.voucher_amount
+ if not refunded_shipping:
+ total_order_amount += payment_information.lines_data.shipping_amount
+
+ return goods_lines, total_order_amount
def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dict]:
@@ -252,13 +265,12 @@ def cancel(config: "ApiConfig", transaction_id: str) -> NPResponse:
def register(
config: "ApiConfig",
payment_information: "PaymentData",
- billed_amount: Optional[int] = None,
+ billed_amount: Optional[Decimal] = None,
goods: Optional[List[dict]] = None,
) -> NPResponse:
if billed_amount is None:
- billed_amount = format_price(
- payment_information.amount, payment_information.currency
- )
+ billed_amount = payment_information.amount
+
if goods is None:
goods = get_goods(config, payment_information)
@@ -286,7 +298,9 @@ def register(
"shop_transaction_id": payment_information.payment_id,
"shop_order_date": order_date,
"settlement_type": NP_ATOBARAI,
- "billed_amount": billed_amount,
+ "billed_amount": format_price(
+ billed_amount, payment_information.currency
+ ),
"customer": {
"customer_name": format_name(billing),
"company_name": billing.company_name,
diff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.py
index da0fc52dfc..0b2b09c682 100644
--- a/saleor/payment/gateways/np_atobarai/utils.py
+++ b/saleor/payment/gateways/np_atobarai/utils.py
@@ -121,21 +121,20 @@ def calculate_manual_refund_amount(
order: Order,
refund_data: RefundData,
payment_information: PaymentData,
- # manual_amount_to_refund: Optional[Decimal],
) -> Decimal:
"""Return sum of all manual refunds for specified order.
Takes into account previous refunds and current refund mutation parameters.
"""
-
if (
- refund_data.fulfillment_lines_to_refund
- or refund_data.order_lines_to_refund
- or refund_data.refund_shipping_costs
+ not refund_data.order_lines_to_refund
+ and not refund_data.fulfillment_lines_to_refund
+ and not refund_data.refund_shipping_costs
+ and not refund_data.amount
):
- manual_amount_to_refund = Decimal("0.00")
- else:
manual_amount_to_refund = payment_information.amount
+ else:
+ manual_amount_to_refund = refund_data.amount or Decimal("0.00")
previous_manual_amount_to_refund = order.fulfillments.filter(
status__in=[
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index 368527fd65..115747cd67 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -91,6 +91,7 @@ class RefundData:
order_lines_to_refund: List[OrderLineInfo]
fulfillment_lines_to_refund: List[FulfillmentLineData]
refund_shipping_costs: bool
+ amount: Optional[Decimal]
@dataclass