Skip to content

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

Implement SALEOR-4229

Mateusz Grzyb86410e08fb3f
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index d69b4c933a..cc46ac1ac4 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -9,16 +9,18 @@ from ...checkout.utils import cancel_active_payments
from ...core.permissions import OrderPermissions
from ...core.utils import get_client_ip
from ...core.utils.url import validate_storefront_url
-from ...payment import PaymentError, gateway
+from ...payment import PaymentError, Store, gateway
from ...payment.error_codes import PaymentErrorCode
from ...payment.utils import create_payment, is_currency_supported
from ..account.i18n import I18nMixin
from ..checkout.mutations import get_checkout_by_token
from ..checkout.types import Checkout
+from ..core.enums import to_enum
from ..core.mutations import BaseMutation
from ..core.scalars import UUID, PositiveDecimal
from ..core.types import common as common_types
from ..core.validators import validate_one_of_args_is_in_mutation
+from ..meta.mutations import BaseMetadataMutation, MetadataInput
from .types import Payment, PaymentInitialized
@@ -53,6 +55,9 @@ class PaymentInput(graphene.InputObjectType):
)
+StoreEnum = to_enum(Store)
+
+
class CheckoutPaymentCreate(BaseMutation, I18nMixin):
checkout = graphene.Field(Checkout, description="Related checkout object.")
payment = graphene.Field(Payment, description="A newly created payment.")
@@ -69,6 +74,16 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
input = PaymentInput(
description="Data required to create a new payment.", required=True
)
+ store = StoreEnum(
+ description="Payment store type.",
+ required=False,
+ default_value=Store.NONE,
+ )
+ metadata = graphene.List(
+ graphene.NonNull(MetadataInput),
+ description="User public metadata.",
+ required=False,
+ )
class Meta:
description = "Create a new payment for given checkout."
@@ -133,7 +148,16 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
)
@classmethod
- def perform_mutation(cls, _root, info, checkout_id=None, token=None, **data):
+ def perform_mutation(
+ cls,
+ _root,
+ info,
+ checkout_id=None,
+ token=None,
+ store=Store.NONE,
+ metadata=None,
+ **data
+ ):
# DEPRECATED
validate_one_of_args_is_in_mutation(
PaymentErrorCode, "checkout_id", checkout_id, "token", token
@@ -183,6 +207,10 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
cancel_active_payments(checkout)
+ if metadata is not None:
+ BaseMetadataMutation.validate_metadata_keys(metadata)
+ metadata = {data.key: data.value for data in metadata}
+
payment = create_payment(
gateway=gateway,
payment_token=data.get("token", ""),
@@ -194,6 +222,8 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
customer_ip_address=get_client_ip(info.context),
checkout=checkout,
return_url=data.get("return_url"),
+ store=store,
+ metadata=metadata,
)
return CheckoutPaymentCreate(payment=payment, checkout=checkout)
diff --git a/saleor/graphql/payment/tests/test_payment.py b/saleor/graphql/payment/tests/test_payment.py
index 40a4fe0cad..fa0d9c8a5e 100644
--- a/saleor/graphql/payment/tests/test_payment.py
+++ b/saleor/graphql/payment/tests/test_payment.py
@@ -91,9 +91,20 @@ def test_payment_void_gateway_error(
CREATE_PAYMENT_MUTATION = """
- mutation CheckoutPaymentCreate($token: UUID, $input: PaymentInput!) {
- checkoutPaymentCreate(token: $token, input: $input) {
+ mutation CheckoutPaymentCreate(
+ $token: UUID,
+ $input: PaymentInput!,
+ $store: StoreEnum,
+ $metadata: [MetadataInput!]
+ ) {
+ checkoutPaymentCreate(
+ token: $token,
+ input: $input,
+ store: $store,
+ metadata: $metadata
+ ) {
payment {
+ id
transactions {
kind,
token
@@ -435,6 +446,78 @@ def test_create_payment_for_checkout_with_active_payments(
assert active_payments.first().pk not in previous_active_payments_ids
[email protected]("store", [None, "NONE", "ON_SESSION", "OFF_SESSION"])
+def test_create_payment_with_store(
+ user_api_client, checkout_without_shipping_required, address, store
+):
+ # given
+ checkout = checkout_without_shipping_required
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.checkout_total(
+ manager=manager, checkout_info=checkout_info, lines=lines, address=address
+ )
+
+ variables = {
+ "token": checkout.token,
+ "input": {
+ "gateway": DUMMY_GATEWAY,
+ "token": "sample-token",
+ "amount": total.gross.amount,
+ },
+ "store": store,
+ }
+
+ # when
+ user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
+ checkout.refresh_from_db()
+ payment = checkout.payments.first()
+ assert payment.store == (store or "NONE").lower()
+
+
+ "metadata", [[{"key": f"key{i}", "value": f"value{i}"} for i in range(5)], [], None]
+)
+def test_create_payment_with_metadata(
+ user_api_client, checkout_without_shipping_required, address, metadata
+):
+ # given
+ checkout = checkout_without_shipping_required
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.checkout_total(
+ manager=manager, checkout_info=checkout_info, lines=lines, address=address
+ )
+
+ variables = {
+ "token": checkout.token,
+ "input": {
+ "gateway": DUMMY_GATEWAY,
+ "token": "sample-token",
+ "amount": total.gross.amount,
+ },
+ "metadata": metadata,
+ }
+
+ # when
+ user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
+ checkout.refresh_from_db()
+ payment = checkout.payments.first()
+ assert payment.metadata == {m["key"]: m["value"] for m in metadata or {}}
+
+
CAPTURE_QUERY = """
mutation PaymentCapture($paymentId: ID!, $amount: PositiveDecimal!) {
paymentCapture(paymentId: $paymentId, amount: $amount) {
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 4d4a6f06a5..d2ce879e35 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3777,7 +3777,7 @@ type Mutation {
checkoutLinesAdd(checkoutId: ID, lines: [CheckoutLineInput]!, token: UUID): CheckoutLinesAdd
checkoutLinesUpdate(checkoutId: ID, lines: [CheckoutLineInput]!, token: UUID): CheckoutLinesUpdate
checkoutRemovePromoCode(checkoutId: ID, promoCode: String!, token: UUID): CheckoutRemovePromoCode
- checkoutPaymentCreate(checkoutId: ID, input: PaymentInput!, token: UUID): CheckoutPaymentCreate
+ checkoutPaymentCreate(checkoutId: ID, input: PaymentInput!, metadata: [MetadataInput!], store: StoreEnum = none, token: UUID): CheckoutPaymentCreate
checkoutShippingAddressUpdate(checkoutId: ID, shippingAddress: AddressInput!, token: UUID): CheckoutShippingAddressUpdate
checkoutShippingMethodUpdate(checkoutId: ID, shippingMethodId: ID!, token: UUID): CheckoutShippingMethodUpdate
checkoutLanguageCodeUpdate(checkoutId: ID, languageCode: LanguageCodeEnum!, token: UUID): CheckoutLanguageCodeUpdate
@@ -6402,6 +6402,12 @@ input StockInput {
quantity: Int!
}
+enum StoreEnum {
+ ON_SESSION
+ OFF_SESSION
+ NONE
+}
+
type TaxType {
description: String
taxCode: String
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index c456a92531..6e142a95a8 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -12,7 +12,7 @@ from ..checkout.models import Checkout
from ..core.prices import quantize_price
from ..core.tracing import traced_atomic_transaction
from ..order.models import Order
-from . import ChargeStatus, GatewayError, PaymentError, TransactionKind
+from . import ChargeStatus, GatewayError, PaymentError, Store, TransactionKind
from .error_codes import PaymentErrorCode
from .interface import AddressData, GatewayResponse, PaymentData, PaymentMethodInfo
from .models import Payment, Transaction
@@ -94,6 +94,8 @@ def create_payment(
order: Order = None,
return_url: str = None,
external_reference: Optional[str] = None,
+ store: str = Store.NONE,
+ metadata: Optional[Dict[str, str]] = None,
) -> Payment:
"""Create a payment instance.
@@ -142,6 +144,8 @@ def create_payment(
"total": total,
"return_url": return_url,
"psp_reference": external_reference or "",
+ "store": store,
+ "metadata": {} if metadata is None else metadata,
}
payment, _ = Payment.objects.get_or_create(defaults=defaults, **data)