Skip to content

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

Move stored payment sources to User graphql object

Jakub Draganekbd5c2294aae7
diff --git a/saleor/graphql/account/resolvers.py b/saleor/graphql/account/resolvers.py
index 9a58f4cecd..776475b215 100644
--- a/saleor/graphql/account/resolvers.py
+++ b/saleor/graphql/account/resolvers.py
@@ -1,9 +1,15 @@
+from itertools import chain
import graphene_django_optimizer as gql_optimizer
from django.db.models import Q
from i18naddress import get_validation_rules
from ...account import models
from ...core.utils import get_client_ip, get_country_by_ip
+from ...payment.utils import (
+ fetch_customer_id,
+ list_enabled_gateways,
+ retrieve_customer_sources,
+)
from ..utils import filter_by_query_param
from .types import AddressValidationData, ChoiceValue
@@ -79,3 +85,38 @@ def resolve_address_validator(info, country_code, country_area, city_area):
postal_code_examples=rules.postal_code_examples,
postal_code_prefix=rules.postal_code_prefix,
)
+
+
+def resolve_payment_sources(user: models.User):
+ stored_customer_accounts = {
+ gateway: fetch_customer_id(user, gateway) for gateway in list_enabled_gateways()
+ }
+ return list(
+ chain(
+ *[
+ prepare_graphql_payment_sources_type(
+ retrieve_customer_sources(gateway, customer_id)
+ )
+ for gateway, customer_id in stored_customer_accounts.items()
+ if customer_id is not None
+ ]
+ )
+ )
+
+
+def prepare_graphql_payment_sources_type(payment_sources):
+ sources = []
+ for src in payment_sources:
+ sources.append(
+ {
+ "gateway": src.gateway,
+ "credit_card_info": {
+ "last_digits": src.credit_card_info.last_4,
+ "exp_year": src.credit_card_info.exp_year,
+ "exp_month": src.credit_card_info.exp_month,
+ "brand": "",
+ "first_digits": "",
+ },
+ }
+ )
+ return sources
diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.py
index 4e2d23ba93..604af8fa3a 100644
--- a/saleor/graphql/account/types.py
+++ b/saleor/graphql/account/types.py
@@ -2,7 +2,7 @@ import graphene
import graphene_django_optimizer as gql_optimizer
from django.contrib.auth import get_user_model
from graphene import relay
-from graphql_jwt.decorators import permission_required
+from graphql_jwt.decorators import login_required, permission_required
from ...account import models
from ...checkout.utils import get_user_checkout
@@ -182,6 +182,10 @@ class User(CountableDjangoObjectType):
),
model_field="events",
)
+ stored_payment_sources = graphene.List(
+ "saleor.graphql.payment.types.PaymentSource",
+ description="List of stored payment sources",
+ )
class Meta:
description = "Represents user data."
@@ -252,6 +256,13 @@ class User(CountableDjangoObjectType):
info=info,
)
+ @staticmethod
+ @login_required
+ def resolve_stored_payment_sources(root: models.User, _):
+ from .resolvers import resolve_payment_sources
+
+ return resolve_payment_sources(root)
+
class ChoiceValue(graphene.ObjectType):
raw = graphene.String()
diff --git a/saleor/graphql/payment/resolvers.py b/saleor/graphql/payment/resolvers.py
index f7a91739f6..d680770411 100644
--- a/saleor/graphql/payment/resolvers.py
+++ b/saleor/graphql/payment/resolvers.py
@@ -1,15 +1,8 @@
-from itertools import chain
-
import graphene_django_optimizer as gql_optimizer
from ...payment import models
from ...payment.interface import TokenConfig
-from ...payment.utils import (
- fetch_customer_id,
- gateway_get_client_token,
- list_enabled_gateways,
- retrieve_customer_sources,
-)
+from ...payment.utils import fetch_customer_id, gateway_get_client_token
from ..utils import filter_by_query_param
PAYMENT_SEARCH_FIELDS = ["id"]
@@ -28,38 +21,3 @@ def resolve_payment_client_token(gateway=None, user=None):
token_config = TokenConfig(customer_id=customer_id)
return gateway_get_client_token(gateway, token_config)
return gateway_get_client_token(gateway)
-
-
-def resolve_payment_sources(user):
- stored_customer_accounts = {
- gateway: fetch_customer_id(user, gateway) for gateway in list_enabled_gateways()
- }
- return list(
- chain(
- *[
- prepare_graphql_payment_sources_type(
- retrieve_customer_sources(gateway, customer_id)
- )
- for gateway, customer_id in stored_customer_accounts.items()
- if customer_id is not None
- ]
- )
- )
-
-
-def prepare_graphql_payment_sources_type(payment_sources):
- sources = []
- for src in payment_sources:
- sources.append(
- {
- "gateway": src.gateway,
- "credit_card_info": {
- "last_digits": src.credit_card_info.last_4,
- "exp_year": src.credit_card_info.exp_year,
- "exp_month": src.credit_card_info.exp_month,
- "brand": "",
- "first_digits": "",
- },
- }
- )
- return sources
diff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.py
index b1fbd9a513..e38fd7ec78 100644
--- a/saleor/graphql/payment/schema.py
+++ b/saleor/graphql/payment/schema.py
@@ -1,23 +1,16 @@
import graphene
-from graphql_jwt.decorators import login_required, permission_required
+from graphql_jwt.decorators import permission_required
from ..core.fields import PrefetchingConnectionField
from .enums import PaymentGatewayEnum
from .mutations import PaymentCapture, PaymentRefund, PaymentVoid
-from .resolvers import (
- resolve_payment_client_token,
- resolve_payment_sources,
- resolve_payments,
-)
-from .types import Payment, PaymentSource
+from .resolvers import resolve_payment_client_token, resolve_payments
+from .types import Payment
class PaymentQueries(graphene.ObjectType):
payment = graphene.Field(Payment, id=graphene.Argument(graphene.ID))
payments = PrefetchingConnectionField(Payment, description="List of payments")
- payment_stored_sources = graphene.List(
- PaymentSource, description="List of stored payment sources"
- )
payment_client_token = graphene.Field(
graphene.String, args={"gateway": PaymentGatewayEnum()}
)
@@ -30,10 +23,6 @@ class PaymentQueries(graphene.ObjectType):
def resolve_payments(self, info, query=None, **_kwargs):
return resolve_payments(info, query)
- @login_required
- def resolve_payment_stored_sources(self, info):
- return resolve_payment_sources(info.context.user)
-
def resolve_payment_client_token(self, info, gateway=None):
user = info.context.user if info.context.user.is_authenticated else None
return resolve_payment_client_token(gateway, user=user)
diff --git a/tests/api/test_payment.py b/tests/api/test_payment.py
index 658628cd6d..072740835c 100644
--- a/tests/api/test_payment.py
+++ b/tests/api/test_payment.py
@@ -550,10 +550,12 @@ def test_list_payment_sources(
):
query = """
{
- paymentStoredSources {
- gateway
- creditCardInfo {
- lastDigits
+ me {
+ storedPaymentSources {
+ gateway
+ creditCardInfo {
+ lastDigits
+ }
}
}
}
@@ -563,14 +565,14 @@ def test_list_payment_sources(
)
source = CustomerSource(id="test1", gateway="braintree", credit_card_info=card)
mock_get_source_list = mocker.patch(
- "saleor.graphql.payment.resolvers.retrieve_customer_sources",
+ "saleor.graphql.account.resolvers.retrieve_customer_sources",
return_value=[source],
autospec=True,
)
response = user_api_client.post_graphql(query)
mock_get_source_list.assert_called_once_with("braintree", braintree_customer_id)
- content = get_graphql_content(response)["data"]["paymentStoredSources"]
+ content = get_graphql_content(response)["data"]["me"]["storedPaymentSources"]
assert content is not None and len(content) == 1
assert content[0] == {
"gateway": "braintree",