Skip to content

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

Add order refund mutation

Adam Janik0d34feb358ea
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 71b0029d88..2259277c8d 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -32,7 +32,7 @@ from .order.types import Order
from .order.mutations import (
DraftOrderCreate, OrderAddNote, OrderCancel, OrderCapture,
DraftOrderComplete, DraftOrderDelete, OrderMarkAsPaid, DraftOrderUpdate,
- OrderRelease, OrderUpdate)
+ OrderRefund, OrderRelease, OrderUpdate)
from .page.resolvers import resolve_pages, resolve_page
from .page.types import Page
from .page.mutations import PageCreate, PageDelete, PageUpdate
@@ -288,6 +288,7 @@ class Mutations(graphene.ObjectType):
order_cancel = OrderCancel.Field()
order_capture = OrderCapture.Field()
order_mark_as_paid = OrderMarkAsPaid.Field()
+ order_refund = OrderRefund.Field()
order_release = OrderRelease.Field()
order_update = OrderUpdate.Field()
diff --git a/saleor/graphql/order/mutations.py b/saleor/graphql/order/mutations.py
index ae25696a41..438aa90368 100644
--- a/saleor/graphql/order/mutations.py
+++ b/saleor/graphql/order/mutations.py
@@ -19,6 +19,17 @@ from ..utils import get_node
from .types import Order
+def try_payment_action(action, money, errors):
+ try:
+ action(money.amount)
+ except (PaymentError, ValueError) as e:
+ message_dict = e.message_dict
+ for field in message_dict:
+ for message in message_dict[field]:
+ errors.append(Error(field=field, message=message))
+
+
+
class AddressInput(graphene.InputObjectType):
first_name = graphene.String(description='Given name.')
last_name = graphene.String(description='Family name.')
@@ -332,18 +343,14 @@ class OrderCapture(BaseMutation):
order = get_node(info, order_id, only_type=Order)
payment = order.get_last_payment()
amount = order.total.quantize('0.01').gross
- try:
- payment.capture(amount.amount)
- msg = 'Captured %(amount)s' % {
- 'amount': prices_i18n.amount(amount)}
- order.history.create(content=msg, user=info.context.user)
- except (PaymentError, ValueError) as e:
- errors = []
- message_dict = e.message_dict
- for field in message_dict:
- for message in message_dict[field]:
- errors.append(Error(field=field, message=message))
+ errors = []
+ try_payment_action(payment.capture, amount, errors)
+ if errors:
return cls(errors=errors)
+
+ msg = 'Captured %(amount)s' % {
+ 'amount': prices_i18n.amount(amount)}
+ order.history.create(content=msg, user=info.context.user)
return OrderCapture(order=order)
@@ -378,3 +385,32 @@ class OrderRelease(BaseMutation):
msg = 'Released payment'
order.history.create(content=msg, user=info.context.user)
return OrderRelease(order=order)
+
+
+class OrderRefund(BaseMutation):
+ class Arguments:
+ order_id = graphene.ID(
+ required=True, description='ID of the order to refund.')
+
+ order = graphene.Field(
+ Order, description='released order.')
+
+ @classmethod
+ @permission_required('order.edit_order')
+ def mutate(cls, root, info, order_id):
+ order = get_node(info, order_id, only_type=Order)
+ payment = order.get_last_payment()
+ amount = payment.captured_amount
+ errors = []
+ if payment.variant == CustomPaymentChoices.MANUAL:
+ errors.append(
+ Error(field='payment',
+ message='Manual payments can not be refunded.'))
+ try_payment_action(payment.refund, amount, errors)
+ if errors:
+ return cls(errors=errors)
+
+ msg = 'Refunded %(amount)s' % {
+ 'amount': prices_i18n.amount(amount)}
+ order.history.create(content=msg, user=info.context.user)
+ return OrderRefund(order=order)