Skip to content

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

Too many fields from payment object (#8225)

L3str4nge310a994822ca
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index 32d40ca1a8..bcf571783f 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -49,6 +49,7 @@ from ...tests.utils import (
get_graphql_content_from_response,
)
from ..utils import validate_draft_order
+from .utils import assert_order_and_payment_ids
@pytest.fixture
@@ -7114,3 +7115,119 @@ def test_order_shipping_update_mutation_properly_recalculate_total(
content = get_graphql_content(response)
data = content["data"]["orderUpdateShipping"]
assert data["order"]["shippingMethod"] is None
+
+
+QUERY_ORDER_BY_TOKEN_WITH_PAYMENT = """
+ query OrderByToken($token: UUID!) {
+ orderByToken(token: $token){
+ id
+ payments{
+ id
+ gateway
+ isActive
+ created
+ modified
+ paymentMethodType
+ transactions{
+ gatewayResponse
+
+ }
+ actions
+ capturedAmount{
+ amount
+ }
+ availableCaptureAmount{
+ amount
+ }
+ availableRefundAmount{
+ amount
+ }
+
+ creditCard{
+ brand
+ firstDigits
+ lastDigits
+ expMonth
+ expYear
+ }
+ }
+ }
+ }
+"""
+
+QUERY_ORDER_WITH_PAYMENT_AVAILABLE_FIELDS = """
+ query OrderByToken($token: UUID!) {
+ orderByToken(token: $token){
+ id
+ payments{
+ id
+ gateway
+ isActive
+ created
+ modified
+ paymentMethodType
+ capturedAmount{
+ amount
+ }
+ chargeStatus
+ creditCard{
+ brand
+ firstDigits
+ lastDigits
+ expMonth
+ expYear
+ }
+ }
+ }
+ }
+"""
+
+
+def test_order_by_token_query_for_payment_details_without_permissions(
+ api_client, payment_txn_captured
+):
+ response = api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_WITH_PAYMENT, {"token": payment_txn_captured.order.token}
+ )
+ assert_no_permission(response)
+
+
+def test_order_by_token_query_for_payment_details_with_permissions(
+ staff_api_client, payment_txn_captured, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_BY_TOKEN_WITH_PAYMENT,
+ {"token": payment_txn_captured.order.token},
+ )
+
+ content = get_graphql_content(response)
+
+ assert_order_and_payment_ids(content, payment_txn_captured)
+
+
+def test_order_by_token_query_payment_details_available_fields_without_permissions(
+ api_client, payment_txn_captured
+):
+ response = api_client.post_graphql(
+ QUERY_ORDER_WITH_PAYMENT_AVAILABLE_FIELDS,
+ {"token": payment_txn_captured.order.token},
+ )
+
+ content = get_graphql_content(response)
+
+ assert_order_and_payment_ids(content, payment_txn_captured)
+
+
+def test_order_by_token_query_payment_details_available_fields_with_permissions(
+ staff_api_client, payment_txn_captured, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_ORDER_WITH_PAYMENT_AVAILABLE_FIELDS,
+ {"token": payment_txn_captured.order.token},
+ )
+
+ content = get_graphql_content(response)
+
+ assert_order_and_payment_ids(content, payment_txn_captured)
diff --git a/saleor/graphql/order/tests/utils.py b/saleor/graphql/order/tests/utils.py
new file mode 100644
index 0000000000..2bca2638bc
--- /dev/null
+++ b/saleor/graphql/order/tests/utils.py
@@ -0,0 +1,10 @@
+import graphene
+
+
+def assert_order_and_payment_ids(content, payment):
+ data = content["data"]["orderByToken"]
+ expected_order_id = graphene.Node.to_global_id("Order", payment.order.pk)
+ assert data["id"] == expected_order_id
+
+ expected_payment_id = graphene.Node.to_global_id("Payment", payment.pk)
+ assert data["payments"][0]["id"] == expected_payment_id
diff --git a/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py b/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py
index f253d173ce..c23de72700 100644
--- a/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py
+++ b/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py
@@ -15,10 +15,6 @@ CREATE_PAYMENT_MUTATION = """
) {
checkoutPaymentCreate(checkoutId: $checkoutId, token: $token, input: $input) {
payment {
- transactions {
- kind,
- token
- }
chargeStatus
}
errors {
@@ -56,8 +52,6 @@ def test_checkout_add_payment_by_checkout_id(
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
- transactions = data["payment"]["transactions"]
- assert not transactions
payment = Payment.objects.get()
assert payment.checkout == checkout
assert payment.is_active
diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.py
index 2e8f721e80..3591f05176 100644
--- a/saleor/graphql/payment/tests/test_payment.py
+++ b/saleor/graphql/payment/tests/test_payment.py
@@ -101,10 +101,6 @@ CREATE_PAYMENT_MUTATION = """
input: $input,
) {
payment {
- transactions {
- kind,
- token
- }
chargeStatus
}
errors {
@@ -141,8 +137,6 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
- transactions = data["payment"]["transactions"]
- assert not transactions
payment = Payment.objects.get()
assert payment.checkout == checkout
assert payment.is_active
@@ -213,8 +207,6 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required(
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
- transactions = data["payment"]["transactions"]
- assert not transactions
payment = Payment.objects.get()
assert payment.checkout == checkout
assert payment.is_active
@@ -257,8 +249,6 @@ def test_checkout_add_payment(
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
- transactions = data["payment"]["transactions"]
- assert not transactions
payment = Payment.objects.get()
assert payment.checkout == checkout
assert payment.is_active
@@ -295,8 +285,6 @@ def test_checkout_add_payment_default_amount(
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
- transactions = data["payment"]["transactions"]
- assert not transactions
payment = Payment.objects.get()
assert payment.checkout == checkout
assert payment.is_active
@@ -1165,3 +1153,81 @@ def test_payment_initialize_plugin_raises_error(
assert len(errors) == 1
assert errors[0]["field"] == "paymentData"
assert errors[0]["message"] == error_msg
+
+
+QUERY_PAYMENT_REFUND_AMOUNT = """
+ query payment($id: ID!) {
+ payment(id: $id) {
+ id,
+ availableRefundAmount{
+ amount
+ }
+ availableCaptureAmount{
+ amount
+ }
+ }
+ }
+"""
+
+QUERY_PAYMENT_CAPTURE_AMOUNT = """
+ query payment($id: ID!) {
+ payment(id: $id) {
+ id,
+ availableCaptureAmount{
+ amount
+ }
+ }
+ }
+"""
+
+
+def test_resolve_available_refund_amount_cannot_refund(
+ staff_api_client, payment_cancelled, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_PAYMENT_REFUND_AMOUNT,
+ {"id": graphene.Node.to_global_id("Payment", payment_cancelled.pk)},
+ )
+ content = get_graphql_content(response)
+
+ assert not content["data"]["payment"]["availableRefundAmount"]
+
+
+def test_resolve_available_refund_amount(
+ staff_api_client, payment_dummy_fully_charged, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_PAYMENT_REFUND_AMOUNT,
+ {"id": graphene.Node.to_global_id("Payment", payment_dummy_fully_charged.pk)},
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["payment"]["availableRefundAmount"]["amount"] == 98.4
+
+
+def test_resolve_available_capture_amount_cannot_capture(
+ staff_api_client, payment_cancelled, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_PAYMENT_CAPTURE_AMOUNT,
+ {"id": graphene.Node.to_global_id("Payment", payment_cancelled.pk)},
+ )
+ content = get_graphql_content(response)
+
+ assert not content["data"]["payment"]["availableCaptureAmount"]
+
+
+def test_resolve_available_capture_amount(
+ staff_api_client, payment_dummy, permission_manage_orders
+):
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(
+ QUERY_PAYMENT_CAPTURE_AMOUNT,
+ {"id": graphene.Node.to_global_id("Payment", payment_dummy.pk)},
+ )
+ content = get_graphql_content(response)
+
+ assert content["data"]["payment"]["availableCaptureAmount"]["amount"] == 98.4
diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.py
index 3be64b4766..1660df79d3 100644
--- a/saleor/graphql/payment/types.py
+++ b/saleor/graphql/payment/types.py
@@ -2,11 +2,13 @@ import graphene
from graphene import relay
from ...core.exceptions import PermissionDenied
+from ...core.permissions import OrderPermissions
from ...core.tracing import traced_resolver
from ...payment import models
from ..core.connection import CountableDjangoObjectType
from ..core.descriptions import ADDED_IN_31
from ..core.types import Money
+from ..decorators import permission_required
from ..meta.permissions import public_payment_permissions
from ..meta.resolvers import resolve_metadata
from ..meta.types import MetadataItem, ObjectWithMetadata
@@ -125,6 +127,12 @@ class Payment(CountableDjangoObjectType):
]
@staticmethod
+ @permission_required(OrderPermissions.MANAGE_ORDERS)
+ def resolve_customer_ip_address(root: models.Payment, _info):
+ return root.customer_ip_address
+
+ @staticmethod
+ @permission_required(OrderPermissions.MANAGE_ORDERS)
def resolve_actions(root: models.Payment, _info):
actions = []
if root.can_capture():
@@ -145,22 +153,23 @@ class Payment(CountableDjangoObjectType):
return root.get_captured_amount()
@staticmethod
+ @permission_required(OrderPermissions.MANAGE_ORDERS)
def resolve_transactions(root: models.Payment, _info):
return root.transactions.all()
@staticmethod
+ @permission_required(OrderPermissions.MANAGE_ORDERS)
def resolve_available_refund_amount(root: models.Payment, _info):
- # FIXME TESTME
if not root.can_refund():
return None
return root.get_captured_amount()
@staticmethod
+ @permission_required(OrderPermissions.MANAGE_ORDERS)
def resolve_available_capture_amount(root: models.Payment, _info):
- # FIXME TESTME
if not root.can_capture():
return None
- return root.get_charge_amount()
+ return Money(amount=root.get_charge_amount(), currency=root.currency)
@staticmethod
def resolve_credit_card(root: models.Payment, _info):
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index a503c0660c..984f637ae9 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -4174,6 +4174,13 @@ def payment(payment_dummy, payment_app):
return payment_dummy
+def payment_cancelled(payment_dummy):
+ payment_dummy.charge_status = ChargeStatus.CANCELLED
+ payment_dummy.save()
+ return payment_dummy
+
+
@pytest.fixture
def payment_dummy_fully_charged(payment_dummy):
payment_dummy.captured_amount = payment_dummy.total