Skip to content

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

Allow order created from draft to be marked as refunded (#6728)

Maciej Korycinskic8e009d2b06d
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 281b4e476c..a5ab28cdac 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -18,7 +18,7 @@ from ....order.actions import (
)
from ....order.error_codes import OrderErrorCode
from ....order.utils import get_valid_shipping_methods_for_order, update_order_prices
-from ....payment import CustomPaymentChoices, PaymentError, TransactionKind, gateway
+from ....payment import PaymentError, TransactionKind, gateway
from ....shipping import models as shipping_models
from ...account.types import AddressInput
from ...core.mutations import BaseMutation, ModelMutation
@@ -109,11 +109,11 @@ def clean_void_payment(payment):
def clean_refund_payment(payment):
clean_payment(payment)
- if payment.gateway == CustomPaymentChoices.MANUAL:
+ if not payment.can_refund():
raise ValidationError(
{
"payment": ValidationError(
- "Manual payments can not be refunded.",
+ "Payment cannot be refunded.",
code=OrderErrorCode.CANNOT_REFUND,
)
}
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 71fbc0a738..e89c71c21b 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -18,7 +18,7 @@ from ....order import events as order_events
from ....order.error_codes import OrderErrorCode
from ....order.events import order_replacement_created
from ....order.models import Order, OrderEvent
-from ....payment import ChargeStatus, CustomPaymentChoices, PaymentError
+from ....payment import ChargeStatus, PaymentError
from ....payment.models import Payment
from ....plugins.manager import PluginsManager
from ....shipping.models import ShippingMethod
@@ -3396,12 +3396,10 @@ def test_try_payment_action_generates_event(order, staff_user, payment_dummy):
def test_clean_order_refund_payment():
payment = MagicMock(spec=Payment)
- payment.gateway = CustomPaymentChoices.MANUAL
- Mock(spec="string")
+ payment.can_refund.return_value = False
with pytest.raises(ValidationError) as e:
clean_refund_payment(payment)
- msg = "Manual payments can not be refunded."
- assert e.value.error_dict["payment"][0].message == msg
+ assert e.value.error_dict["payment"][0].code == OrderErrorCode.CANNOT_REFUND
def test_clean_order_capture():
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 6d956b2df5..340e234c8c 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -12,6 +12,7 @@ from .utils import (
clean_authorize,
clean_capture,
create_payment_information,
+ create_transaction,
gateway_postprocess,
get_already_processed_transaction_or_create_new_transaction,
update_payment_method_details,
@@ -169,10 +170,22 @@ def refund(payment: Payment, amount: Decimal = None) -> Transaction:
_validate_refund_amount(payment, amount)
if not payment.can_refund():
raise PaymentError("This payment cannot be refunded.")
- token = _get_past_transaction_token(payment, TransactionKind.CAPTURE)
+
+ kind = TransactionKind.EXTERNAL if payment.is_manual() else TransactionKind.CAPTURE
+
+ token = _get_past_transaction_token(payment, kind)
payment_data = create_payment_information(
payment=payment, payment_token=token, amount=amount
)
+ if payment.is_manual():
+ # for manual payment we just need to mark payment as a refunded
+ return create_transaction(
+ payment,
+ TransactionKind.REFUND,
+ payment_information=payment_data,
+ is_success=True,
+ )
+
response, error = _fetch_gateway_response(
plugin_manager.refund_payment, payment.gateway, payment_data
)
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index a26b881704..becdb4466b 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -177,15 +177,14 @@ class Payment(models.Model):
ChargeStatus.FULLY_CHARGED,
ChargeStatus.PARTIALLY_REFUNDED,
)
- return (
- self.is_active
- and self.charge_status in can_refund_charge_status
- and self.gateway != CustomPaymentChoices.MANUAL
- )
+ return self.is_active and self.charge_status in can_refund_charge_status
def can_confirm(self):
return self.is_active and self.not_charged
+ def is_manual(self):
+ return self.gateway == CustomPaymentChoices.MANUAL
+
class Transaction(models.Model):
"""Represents a single payment operation.
diff --git a/saleor/payment/tests/test_gateway.py b/saleor/payment/tests/test_gateway.py
index c7a587a7b0..ee8cf3ad99 100644
--- a/saleor/payment/tests/test_gateway.py
+++ b/saleor/payment/tests/test_gateway.py
@@ -4,7 +4,7 @@ import pytest
import saleor.payment.gateway as gateway
-from .. import ChargeStatus, TransactionKind
+from .. import ChargeStatus, CustomPaymentChoices, TransactionKind
from ..interface import GatewayResponse
from ..utils import create_payment_information
@@ -164,6 +164,18 @@ def test_capture_payment(mock_payment_interface, payment_txn_preauth):
assert transaction.gateway_response == RAW_RESPONSE
+def test_refund_for_manual_payment(payment_txn_captured):
+ payment_txn_captured.gateway = CustomPaymentChoices.MANUAL
+ transaction = gateway.refund(
+ payment=payment_txn_captured, amount=PARTIAL_REFUND_AMOUNT
+ )
+ payment_txn_captured.refresh_from_db()
+ assert payment_txn_captured.charge_status == ChargeStatus.PARTIALLY_REFUNDED
+ assert transaction.amount == PARTIAL_REFUND_AMOUNT
+ assert transaction.kind == TransactionKind.REFUND
+ assert transaction.currency == "USD"
+
+
def test_partial_refund_payment(mock_payment_interface, payment_txn_captured):
capture_transaction = payment_txn_captured.transactions.get()
PAYMENT_DATA = create_payment_information(
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 12d2ef76d8..f42589e914 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -152,6 +152,7 @@ def create_transaction(
action_required: bool = False,
gateway_response: GatewayResponse = None,
error_msg=None,
+ is_success=False,
) -> Transaction:
"""Create a transaction based on transaction kind and gateway response."""
# Default values for token, amount, currency are only used in cases where
@@ -161,7 +162,7 @@ def create_transaction(
kind=kind,
action_required=False,
transaction_id=payment_information.token,
- is_success=False,
+ is_success=is_success,
amount=payment_information.amount,
currency=payment_information.currency,
error=error_msg,