Skip to content

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

Add tests for capture and cancel orders

akjanik60069cb3ead8
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 9455a0ca67..8626a18ef8 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -135,7 +135,7 @@ class OrderMarkAsPaid(BaseMutation):
class OrderCapture(BaseMutation):
class Arguments:
- order_id = graphene.ID(
+ id = graphene.ID(
required=True, description='ID of the order to capture.')
amount = Decimal(
required=True, description='Amount of money to capture.')
@@ -145,8 +145,8 @@ class OrderCapture(BaseMutation):
@classmethod
@permission_required('order.edit_order')
- def mutate(cls, root, info, order_id, amount):
- order = get_node(info, order_id, only_type=Order)
+ def mutate(cls, root, info, id, amount):
+ order = get_node(info, id, only_type=Order)
payment = order.get_last_payment()
errors = []
try_payment_action(payment.capture, amount, errors)
@@ -160,7 +160,7 @@ class OrderCapture(BaseMutation):
class OrderRelease(BaseMutation):
class Arguments:
- order_id = graphene.ID(
+ id = graphene.ID(
required=True, description='ID of the order to release.')
order = graphene.Field(
@@ -168,8 +168,8 @@ class OrderRelease(BaseMutation):
@classmethod
@permission_required('order.edit_order')
- def mutate(cls, root, info, order_id):
- order = get_node(info, order_id, only_type=Order)
+ def mutate(cls, root, info, id):
+ order = get_node(info, id, only_type=Order)
payment = order.get_last_payment()
errors = []
if payment.status != PaymentStatus.PREAUTH:
@@ -190,7 +190,7 @@ class OrderRelease(BaseMutation):
class OrderRefund(BaseMutation):
class Arguments:
- order_id = graphene.ID(
+ id = graphene.ID(
required=True, description='ID of the order to refund.')
amount = Decimal(
required=True, description='Amount of money to refund.')
@@ -200,8 +200,8 @@ class OrderRefund(BaseMutation):
@classmethod
@permission_required('order.edit_order')
- def mutate(cls, root, info, order_id, amount):
- order = get_node(info, order_id, only_type=Order)
+ def mutate(cls, root, info, id, amount):
+ order = get_node(info, id, only_type=Order)
payment = order.get_last_payment()
errors = []
if payment.variant == CustomPaymentChoices.MANUAL:
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index bb9038d127..cdb05fbefc 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -3,7 +3,7 @@ from graphene import relay
from graphene_django import DjangoObjectType
from ...order import models
-from ..core.types import CountableDjangoObjectType
+from ..core.types import CountableDjangoObjectType, Money, TaxedMoney
class Order(CountableDjangoObjectType):
@@ -16,6 +16,10 @@ class Order(CountableDjangoObjectType):
description='User-friendly payment status.')
status_display = graphene.String(
description='User-friendly order status.')
+ captured_amount = graphene.Field(
+ Money, description='Amount captured by payment.')
+ total_prize = graphene.Field(
+ TaxedMoney, description='Total price of the order.')
class Meta:
description = 'Represents an order in the shop.'
@@ -25,6 +29,11 @@ class Order(CountableDjangoObjectType):
'shipping_price_gross', 'shipping_price_net', 'total_gross',
'total_net']
+ def resolve_captured_amount(self, info):
+ payment = self.get_last_payment()
+ if payment:
+ return payment.get_captured_price()
+
def resolve_is_paid(self, info):
return self.is_fully_paid()
@@ -40,6 +49,11 @@ class Order(CountableDjangoObjectType):
def resolve_status_display(self, info):
return self.get_status_display()
+ def resolve_total_prize(self, info):
+ payment = self.get_last_payment()
+ if payment:
+ return payment.get_total_price()
+
class OrderHistoryEntry(CountableDjangoObjectType):
class Meta:
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index ec463ef8b2..acc301fafc 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -329,3 +329,57 @@ def test_order_add_note(admin_api_client, order_with_lines, admin_user):
assert data['content'] == note
assert data['isPublic'] == is_public
assert data['user']['email'] == admin_user.email
+
+
+def test_order_cancel(admin_api_client, order_with_lines):
+ order = order_with_lines
+ query = """
+ mutation cancelOrder($id: ID!, $restock: Boolean!) {
+ orderCancel(id: $id, restock: $restock) {
+ order {
+ status
+ }
+ }
+ }
+ """
+ order_id = graphene.Node.to_global_id('Order', order.id)
+ restock = True
+ quantity = order.get_total_quantity()
+ variables = json.dumps({'id': order_id, 'restock': restock})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ data = content['data']['orderCancel']['order']
+ order.refresh_from_db()
+ history_entry = 'Restocked %d items' % quantity
+ assert order.history.last().content == history_entry
+ assert data['status'] == order.status.upper()
+
+
+def test_order_capture(admin_api_client, order_with_lines, payment_preauth):
+ order = order_with_lines
+ query = """
+ mutation captureOrder($id: ID!, $amount: Decimal!) {
+ orderCapture(id: $id, amount: $amount) {
+ order {
+ paymentStatus
+ isPaid
+ capturedAmount {
+ amount
+ }
+ }
+ }
+ }
+ """
+ order_id = graphene.Node.to_global_id('Order', order.id)
+ amount = str(payment_preauth.get_total_price().gross.amount)
+ variables = json.dumps({'id': order_id, 'amount': amount})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ data = content['data']['orderCapture']['order']
+ order.refresh_from_db()
+ assert data['paymentStatus'] == order.get_last_payment_status()
+ assert data['isPaid'] == True
+ assert data['capturedAmount']['amount'] == float(amount)
+