Skip to content

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

Split payment module (#13160)

Iga Karbowiak062b574e7eb8
diff --git a/saleor/graphql/order/bulk_mutations/order_bulk_create.py b/saleor/graphql/order/bulk_mutations/order_bulk_create.py
index 51fc61fdc2..f7a37f2a55 100644
--- a/saleor/graphql/order/bulk_mutations/order_bulk_create.py
+++ b/saleor/graphql/order/bulk_mutations/order_bulk_create.py
@@ -57,7 +57,10 @@ from ...core.types import BaseInputObjectType, BaseObjectType, NonNullList
from ...core.types.common import OrderBulkCreateError
from ...core.utils import from_global_id_or_error
from ...meta.mutations import MetadataInput
-from ...payment.mutations import TransactionCreate, TransactionCreateInput
+from ...payment.mutations.transaction.transaction_create import (
+ TransactionCreate,
+ TransactionCreateInput,
+)
from ...payment.utils import metadata_contains_empty_key
from ...plugins.dataloaders import get_plugin_manager_promise
from ..enums import OrderStatusEnum, StockUpdatePolicyEnum
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
deleted file mode 100644
index f071ead360..0000000000
--- a/saleor/graphql/payment/mutations.py
+++ /dev/null
@@ -1,2207 +0,0 @@
-import uuid
-from decimal import Decimal
-from typing import TYPE_CHECKING, Dict, List, Optional, Union, cast
-
-import graphene
-from django.core.exceptions import ValidationError
-from django.core.validators import URLValidator
-from django.db import transaction
-from django.db.models import Model, QuerySet
-from django.utils import timezone
-from graphql import GraphQLError
-
-from ...app.models import App
-from ...channel import TransactionFlowStrategy
-from ...channel.models import Channel
-from ...checkout import models as checkout_models
-from ...checkout.actions import transaction_amounts_for_checkout_updated
-from ...checkout.calculations import (
- calculate_checkout_total_with_gift_cards,
- fetch_checkout_data,
-)
-from ...checkout.checkout_cleaner import clean_billing_address, clean_checkout_shipping
-from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from ...checkout.utils import cancel_active_payments
-from ...core.error_codes import MetadataErrorCode
-from ...core.exceptions import PermissionDenied
-from ...core.tracing import traced_atomic_transaction
-from ...core.utils import get_client_ip
-from ...core.utils.url import validate_storefront_url
-from ...order import OrderStatus
-from ...order import models as order_models
-from ...order.actions import order_refunded, order_transaction_updated
-from ...order.events import transaction_event as order_transaction_event
-from ...order.fetch import fetch_order_info
-from ...order.models import Order
-from ...order.search import update_order_search_vector
-from ...order.utils import updates_amounts_for_order
-from ...payment import (
- PaymentError,
- StorePaymentMethod,
- TransactionAction,
- TransactionEventType,
- TransactionKind,
- gateway,
-)
-from ...payment import models as payment_models
-from ...payment.error_codes import (
- PaymentErrorCode,
- TransactionCreateErrorCode,
- TransactionProcessErrorCode,
- TransactionRequestActionErrorCode,
- TransactionUpdateErrorCode,
-)
-from ...payment.gateway import (
- request_cancelation_action,
- request_charge_action,
- request_refund_action,
-)
-from ...payment.interface import PaymentGatewayData
-from ...payment.transaction_item_calculations import (
- calculate_transaction_amount_based_on_events,
- recalculate_transaction_amounts,
-)
-from ...payment.utils import (
- authorization_success_already_exists,
- create_failed_transaction_event,
- create_manual_adjustment_events,
- create_payment,
- get_already_existing_event,
- get_final_session_statuses,
- handle_transaction_initialize_session,
- handle_transaction_process_session,
- is_currency_supported,
-)
-from ...permission.auth_filters import AuthorizationFilters
-from ...permission.enums import OrderPermissions, PaymentPermissions
-from ..account.i18n import I18nMixin
-from ..app.dataloaders import get_app_promise
-from ..channel.enums import TransactionFlowStrategyEnum
-from ..channel.utils import validate_channel
-from ..checkout.mutations.utils import get_checkout
-from ..checkout.types import Checkout
-from ..core import ResolveInfo
-from ..core.descriptions import (
- ADDED_IN_31,
- ADDED_IN_34,
- ADDED_IN_313,
- DEPRECATED_IN_3X_INPUT,
- PREVIEW_FEATURE,
- PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT,
-)
-from ..core.doc_category import DOC_CATEGORY_CHECKOUT, DOC_CATEGORY_PAYMENTS
-from ..core.enums import (
- PaymentGatewayInitializeErrorCode,
- TransactionEventReportErrorCode,
- TransactionInitializeErrorCode,
-)
-from ..core.fields import JSONString
-from ..core.mutations import BaseMutation, ModelMutation
-from ..core.scalars import JSON, UUID, PositiveDecimal
-from ..core.types import BaseInputObjectType, BaseObjectType
-from ..core.types import common as common_types
-from ..core.utils import from_global_id_or_error
-from ..meta.mutations import MetadataInput
-from ..plugins.dataloaders import get_plugin_manager_promise
-from ..utils import get_user_or_app_from_context
-from .enums import (
- StorePaymentMethodEnum,
- TransactionActionEnum,
- TransactionEventStatusEnum,
- TransactionEventTypeEnum,
-)
-from .types import Payment, PaymentInitialized, TransactionEvent, TransactionItem
-from .utils import check_if_requestor_has_access, metadata_contains_empty_key
-
-if TYPE_CHECKING:
- from ...account.models import User
- from ...plugins.manager import PluginsManager
-
-
-class PaymentInput(BaseInputObjectType):
- gateway = graphene.Field(
- graphene.String,
- description="A gateway to use with that payment.",
- required=True,
- )
- token = graphene.String(
- required=False,
- description=(
- "Client-side generated payment token, representing customer's "
- "billing data in a secure manner."
- ),
- )
- amount = PositiveDecimal(
- required=False,
- description=(
- "Total amount of the transaction, including "
- "all taxes and discounts. If no amount is provided, "
- "the checkout total will be used."
- ),
- )
- return_url = graphene.String(
- required=False,
- description=(
- "URL of a storefront view where user should be redirected after "
- "requiring additional actions. Payment with additional actions will not be "
- "finished if this field is not provided."
- ),
- )
- store_payment_method = StorePaymentMethodEnum(
- description="Payment store type." + ADDED_IN_31,
- required=False,
- default_value=StorePaymentMethodEnum.NONE.name,
- )
- metadata = common_types.NonNullList(
- MetadataInput,
- description="User public metadata." + ADDED_IN_31,
- required=False,
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class CheckoutPaymentCreate(BaseMutation, I18nMixin):
- checkout = graphene.Field(Checkout, description="Related checkout object.")
- payment = graphene.Field(Payment, description="A newly created payment.")
-
- class Arguments:
- id = graphene.ID(
- description="The checkout's ID." + ADDED_IN_34,
- required=False,
- )
- token = UUID(
- description=f"Checkout token.{DEPRECATED_IN_3X_INPUT} Use `id` instead.",
- required=False,
- )
- checkout_id = graphene.ID(
- required=False,
- description=(
- f"The ID of the checkout. {DEPRECATED_IN_3X_INPUT} Use `id` instead."
- ),
- )
- input = PaymentInput(
- description="Data required to create a new payment.", required=True
- )
-
- class Meta:
- description = "Create a new payment for given checkout."
- doc_category = DOC_CATEGORY_CHECKOUT
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def clean_payment_amount(cls, info: ResolveInfo, checkout_total, amount):
- if amount != checkout_total.gross.amount:
- raise ValidationError(
- {
- "amount": ValidationError(
- "Partial payments are not allowed, amount should be "
- "equal checkout's total.",
- code=PaymentErrorCode.PARTIAL_PAYMENT_NOT_ALLOWED.value,
- )
- }
- )
-
- @classmethod
- def validate_gateway(cls, manager, gateway_id, checkout):
- """Validate if given gateway can be used for this checkout.
-
- Check if provided gateway is active and CONFIGURATION_PER_CHANNEL is True.
- If CONFIGURATION_PER_CHANNEL is False then check if gateway has
- defined currency.
- """
- payment_gateway = manager.get_plugin(gateway_id, checkout.channel.slug)
-
- if not payment_gateway or not payment_gateway.active:
- cls.raise_not_supported_gateway_error(gateway_id)
-
- if not payment_gateway.CONFIGURATION_PER_CHANNEL:
- if not is_currency_supported(checkout.currency, gateway_id, manager):
- cls.raise_not_supported_gateway_error(gateway_id)
-
- @classmethod
- def raise_not_supported_gateway_error(cls, gateway_id: str):
- raise ValidationError(
- {
- "gateway": ValidationError(
- f"The gateway {gateway_id} is not available for this checkout.",
- code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
- )
- }
- )
-
- @classmethod
- def validate_token(cls, manager, gateway: str, input_data: dict, channel_slug: str):
- token = input_data.get("token")
- is_required = manager.token_is_required_as_payment_input(gateway, channel_slug)
- if not token and is_required:
- raise ValidationError(
- {
- "token": ValidationError(
- f"Token is required for {gateway}.",
- code=PaymentErrorCode.REQUIRED.value,
- ),
- }
- )
-
- @classmethod
- def validate_return_url(cls, input_data):
- return_url = input_data.get("return_url")
- if not return_url:
- return
- try:
- validate_storefront_url(return_url)
- except ValidationError as error:
- raise ValidationError(
- {"redirect_url": error}, code=PaymentErrorCode.INVALID.value
- )
-
- @classmethod
- def validate_metadata_keys(cls, metadata_list: List[dict]):
- if metadata_contains_empty_key(metadata_list):
- raise ValidationError(
- {
- "input": ValidationError(
- {
- "metadata": ValidationError(
- "Metadata key cannot be empty.",
- code=MetadataErrorCode.REQUIRED.value,
- )
- }
- )
- }
- )
-
- @staticmethod
- def validate_checkout_email(checkout: "checkout_models.Checkout"):
- if not checkout.email:
- raise ValidationError(
- "Checkout email must be set.",
- code=PaymentErrorCode.CHECKOUT_EMAIL_NOT_SET.value,
- )
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls,
- _root,
- info: ResolveInfo,
- /,
- *,
- checkout_id=None,
- id=None,
- input,
- token=None,
- ):
- checkout = get_checkout(cls, info, checkout_id=checkout_id, token=token, id=id)
-
- cls.validate_checkout_email(checkout)
-
- gateway = input["gateway"]
-
- manager = get_plugin_manager_promise(info.context).get()
- cls.validate_gateway(manager, gateway, checkout)
- cls.validate_return_url(input)
-
- lines, unavailable_variant_pks = fetch_checkout_lines(checkout)
- if unavailable_variant_pks:
- not_available_variants_ids = {
- graphene.Node.to_global_id("ProductVariant", pk)
- for pk in unavailable_variant_pks
- }
- raise ValidationError(
- {
- "token": ValidationError(
- "Some of the checkout lines variants are unavailable.",
- code=PaymentErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.value,
- params={"variants": not_available_variants_ids},
- )
- }
- )
- if not lines:
- raise ValidationError(
- {
- "lines": ValidationError(
- "Cannot create payment for checkout without lines.",
- code=PaymentErrorCode.NO_CHECKOUT_LINES.value,
- )
- }
- )
- checkout_info = fetch_checkout_info(checkout, lines, manager)
-
- cls.validate_token(
- manager, gateway, input, channel_slug=checkout_info.channel.slug
- )
-
- address = (
- checkout.shipping_address or checkout.billing_address
- ) # FIXME: check which address we need here
- checkout_total = calculate_checkout_total_with_gift_cards(
- manager=manager,
- checkout_info=checkout_info,
- lines=lines,
- address=address,
- )
- amount = input.get("amount", checkout_total.gross.amount)
- clean_checkout_shipping(checkout_info, lines, PaymentErrorCode)
- clean_billing_address(checkout_info, PaymentErrorCode)
- cls.clean_payment_amount(info, checkout_total, amount)
- extra_data = {
- "customer_user_agent": info.context.META.get("HTTP_USER_AGENT"),
- }
-
- metadata = input.get("metadata")
-
- if metadata is not None:
- cls.validate_metadata_keys(metadata)
- metadata = {data.key: data.value for data in metadata}
-
- # The payment creation and deactivation of old payments should happened in the
- # transaction to avoid creating multiple active payments.
- with transaction.atomic():
- # The checkout lock is used to prevent processing checkout completion
- # and new payment creation. This kind of case could result in the missing
- # payments, that were created for the checkout that was already converted
- # to an order.
- checkout = (
- checkout_models.Checkout.objects.select_for_update()
- .filter(pk=checkout_info.checkout.pk)
- .first()
- )
-
- if not checkout:
- raise ValidationError(
- "Checkout doesn't exist anymore.",
- code=PaymentErrorCode.NOT_FOUND.value,
- )
-
- cancel_active_payments(checkout)
-
- payment = None
- if amount != 0:
- store_payment_method = (
- input.get("store_payment_method") or StorePaymentMethod.NONE
- )
-
- payment = create_payment(
- gateway=gateway,
- payment_token=input.get("token", ""),
- total=amount,
- currency=checkout.currency,
- email=checkout.get_customer_email(),
- extra_data=extra_data,
- # FIXME this is not a customer IP address.
- # It is a client storefront ip
- customer_ip_address=get_client_ip(info.context),
- checkout=checkout,
- return_url=input.get("return_url"),
- store_payment_method=store_payment_method,
- metadata=metadata,
- )
-
- return CheckoutPaymentCreate(payment=payment, checkout=checkout)
-
-
-class PaymentCapture(BaseMutation):
- payment = graphene.Field(Payment, description="Updated payment.")
-
- class Arguments:
- payment_id = graphene.ID(required=True, description="Payment ID.")
- amount = PositiveDecimal(description="Transaction amount.")
-
- class Meta:
- description = "Captures the authorized payment amount."
- doc_category = DOC_CATEGORY_PAYMENTS
- permissions = (OrderPermissions.MANAGE_ORDERS,)
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, amount=None, payment_id
- ):
- payment = cls.get_node_or_error(
- info, payment_id, field="payment_id", only_type=Payment
- )
- channel = payment.order.channel if payment.order else payment.checkout.channel
- cls.check_channel_permissions(info, [channel.id])
- channel_slug = channel.slug
- manager = get_plugin_manager_promise(info.context).get()
- try:
- gateway.capture(
- payment,
- manager,
- amount=amount,
- channel_slug=channel_slug,
- )
- payment.refresh_from_db()
- except PaymentError as e:
- raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
- return PaymentCapture(payment=payment)
-
-
-class PaymentRefund(PaymentCapture):
- class Meta:
- description = "Refunds the captured payment amount."
- doc_category = DOC_CATEGORY_PAYMENTS
- permissions = (OrderPermissions.MANAGE_ORDERS,)
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, amount=None, payment_id
- ):
- app = get_app_promise(info.context).get()
- user = info.context.user
- manager = get_plugin_manager_promise(info.context).get()
-
- payment = cls.get_node_or_error(
- info, payment_id, field="payment_id", only_type=Payment
- )
- channel = payment.order.channel if payment.order else payment.checkout.channel
- cls.check_channel_permissions(info, [channel.id])
- channel_slug = channel.slug
- manager = get_plugin_manager_promise(info.context).get()
- payment_transaction = None
- try:
- payment_transaction = gateway.refund(
- payment,
- manager,
- amount=amount,
- channel_slug=channel_slug,
- )
- payment.refresh_from_db()
- except PaymentError as e:
- raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
- if (
- payment.order_id
- and payment_transaction
- and payment_transaction.kind == TransactionKind.REFUND
- ):
- order = cast(order_models.Order, payment.order)
- order_refunded(
- order=order,
- user=user,
- app=app,
- amount=amount,
- payment=payment,
- manager=manager,
- )
- return PaymentRefund(payment=payment)
-
-
-class PaymentVoid(BaseMutation):
- payment = graphene.Field(Payment, description="Updated payment.")
-
- class Arguments:
- payment_id = graphene.ID(required=True, description="Payment ID.")
-
- class Meta:
- description = "Voids the authorized payment."
- doc_category = DOC_CATEGORY_PAYMENTS
- permissions = (OrderPermissions.MANAGE_ORDERS,)
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, payment_id
- ):
- payment = cls.get_node_or_error(
- info, payment_id, field="payment_id", only_type=Payment
- )
- channel = payment.order.channel if payment.order else payment.checkout.channel
- cls.check_channel_permissions(info, [channel.id])
- channel_slug = channel.slug
- manager = get_plugin_manager_promise(info.context).get()
- try:
- gateway.void(payment, manager, channel_slug=channel_slug)
- payment.refresh_from_db()
- except PaymentError as e:
- raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
- return PaymentVoid(payment=payment)
-
-
-class PaymentInitialize(BaseMutation):
- initialized_payment = graphene.Field(PaymentInitialized, required=False)
-
- class Arguments:
- gateway = graphene.String(
- description="A gateway name used to initialize the payment.",
- required=True,
- )
- channel = graphene.String(
- description="Slug of a channel for which the data should be returned.",
- )
- payment_data = JSONString(
- required=False,
- description=(
- "Client-side generated data required to initialize the payment."
- ),
- )
-
- class Meta:
- description = "Initializes payment process when it is required by gateway."
- doc_category = DOC_CATEGORY_PAYMENTS
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def validate_channel(cls, channel_slug):
- try:
- channel = Channel.objects.get(slug=channel_slug)
- except Channel.DoesNotExist:
- raise ValidationError(
- {
- "channel": ValidationError(
- f"Channel with '{channel_slug}' slug does not exist.",
- code=PaymentErrorCode.NOT_FOUND.value,
- )
- }
- )
- if not channel.is_active:
- raise ValidationError(
- {
- "channel": ValidationError(
- f"Channel with '{channel_slug}' is inactive.",
- code=PaymentErrorCode.CHANNEL_INACTIVE.value,
- )
- }
- )
- return channel
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls, _root, info: ResolveInfo, /, *, channel, gateway, payment_data
- ):
- cls.validate_channel(channel_slug=channel)
- manager = get_plugin_manager_promise(info.context).get()
- try:
- response = manager.initialize_payment(
- gateway, payment_data, channel_slug=channel
- )
- except PaymentError as e:
- raise ValidationError(
- {
- "payment_data": ValidationError(
- str(e), code=PaymentErrorCode.INVALID.value
- )
- }
- )
- return PaymentInitialize(initialized_payment=response)
-
-
-class MoneyInput(graphene.InputObjectType):
- currency = graphene.String(description="Currency code.", required=True)
- amount = PositiveDecimal(description="Amount of money.", required=True)
-
-
-class CardInput(graphene.InputObjectType):
- code = graphene.String(
- description=(
- "Payment method nonce, a token returned "
- "by the appropriate provider's SDK."
- ),
- required=True,
- )
- cvc = graphene.String(description="Card security code.", required=False)
- money = MoneyInput(
- description="Information about currency and amount.", required=True
- )
-
-
-class PaymentCheckBalanceInput(BaseInputObjectType):
- gateway_id = graphene.types.String(
- description="An ID of a payment gateway to check.", required=True
- )
- method = graphene.types.String(description="Payment method name.", required=True)
- channel = graphene.String(
- description="Slug of a channel for which the data should be returned.",
- required=True,
- )
- card = CardInput(description="Information about card.", required=True)
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class PaymentCheckBalance(BaseMutation):
- data = JSONString(description="Response from the gateway.")
-
- class Arguments:
- input = PaymentCheckBalanceInput(
- description="Fields required to check payment balance.", required=True
- )
-
- class Meta:
- description = "Check payment balance."
- doc_category = DOC_CATEGORY_PAYMENTS
- error_type_class = common_types.PaymentError
- error_type_field = "payment_errors"
-
- @classmethod
- def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
- manager = get_plugin_manager_promise(info.context).get()
- gateway_id = data["input"]["gateway_id"]
- money = data["input"]["card"].get("money", {})
-
- cls.validate_gateway(gateway_id, manager)
- cls.validate_currency(money.currency, gateway_id, manager)
-
- channel = data["input"].pop("channel")
- validate_channel(channel, PaymentErrorCode)
-
- try:
- data = manager.check_payment_balance(data["input"], channel)
- except PaymentError as e:
- raise ValidationError(
- str(e), code=PaymentErrorCode.BALANCE_CHECK_ERROR.value
- )
-
- return PaymentCheckBalance(data=data)
-
- @classmethod
- def validate_gateway(cls, gateway_id, manager):
- gateways_id = [gateway.id for gateway in manager.list_payment_gateways()]
-
- if gateway_id not in gateways_id:
- raise ValidationError(
- {
- "gateway_id": ValidationError(
- f"The gateway_id {gateway_id} is not available.",
- code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
- )
- }
- )
-
- @classmethod
- def validate_currency(cls, currency, gateway_id, manager):
- if not is_currency_supported(currency, gateway_id, manager):
- raise ValidationError(
- {
- "currency": ValidationError(
- f"The currency {currency} is not available for {gateway_id}.",
- code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
- )
- }
- )
-
-
-class TransactionUpdateInput(BaseInputObjectType):
- status = graphene.String(
- description=(
- "Status of the transaction."
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " The `status` is not needed. The amounts can be used to define "
- "the current status of transactions."
- ),
- )
- type = graphene.String(
- description="Payment type used for this transaction."
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Use `name` and `message` instead.",
- )
- name = graphene.String(
- description="Payment name of the transaction." + ADDED_IN_313
- )
- message = graphene.String(
- description="The message of the transaction." + ADDED_IN_313
- )
-
- reference = graphene.String(
- description=(
- "Reference of the transaction. "
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Use `pspReference` instead."
- )
- )
- psp_reference = graphene.String(
- description=("PSP Reference of the transaction. " + ADDED_IN_313)
- )
- available_actions = graphene.List(
- graphene.NonNull(TransactionActionEnum),
- description="List of all possible actions for the transaction",
- )
- amount_authorized = MoneyInput(description="Amount authorized by this transaction.")
- amount_charged = MoneyInput(description="Amount charged by this transaction.")
- amount_refunded = MoneyInput(description="Amount refunded by this transaction.")
- amount_voided = MoneyInput(
- description="Amount voided by this transaction."
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Use `amountCanceled` instead."
- )
- amount_canceled = MoneyInput(
- description="Amount canceled by this transaction." + ADDED_IN_313
- )
-
- metadata = graphene.List(
- graphene.NonNull(MetadataInput),
- description="Payment public metadata.",
- required=False,
- )
- private_metadata = graphene.List(
- graphene.NonNull(MetadataInput),
- description="Payment private metadata.",
- required=False,
- )
- external_url = graphene.String(
- description=(
- "The url that will allow to redirect user to "
- "payment provider page with transaction event details." + ADDED_IN_313
- )
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class TransactionCreateInput(TransactionUpdateInput):
- ...
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class TransactionEventInput(BaseInputObjectType):
- status = graphene.Field(
- TransactionEventStatusEnum,
- required=False,
- description="Current status of the payment transaction."
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Status will be calculated by Saleor.",
- )
- reference = graphene.String(
- description=(
- "Reference of the transaction. "
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Use `pspReference` instead."
- )
- )
-
- psp_reference = graphene.String(
- description=("PSP Reference related to this action." + ADDED_IN_313)
- )
- name = graphene.String(
- description="Name of the transaction."
- + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
- + " Use `message` instead. `name` field will be added to `message`."
- )
- message = graphene.String(
- description="The message related to the event." + ADDED_IN_313
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class TransactionCreate(BaseMutation):
- transaction = graphene.Field(TransactionItem)
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the checkout or order.",
- required=True,
- )
- transaction = TransactionCreateInput(
- required=True,
- description="Input data required to create a new transaction object.",
- )
- transaction_event = TransactionEventInput(
- description="Data that defines a transaction event."
- )
-
- class Meta:
- description = (
- "Create transaction for checkout or order." + ADDED_IN_34 + PREVIEW_FEATURE
- )
- doc_category = DOC_CATEGORY_PAYMENTS
- error_type_class = common_types.TransactionCreateError
- permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
-
- @classmethod
- def validate_external_url(cls, external_url: Optional[str], error_code: str):
- if external_url is None:
- return
- validator = URLValidator()
- try:
- validator(external_url)
- except ValidationError:
- raise ValidationError(
- {
- "transaction": ValidationError(
- "Invalid format of `externalUrl`.", code=error_code
- )
- }
- )
-
- @classmethod
- def validate_metadata_keys( # type: ignore[override]
- cls, metadata_list: List[dict], field_name, error_code
- ):
- if metadata_contains_empty_key(metadata_list):
- raise ValidationError(
- {
- "transaction": ValidationError(
- f"{field_name} key cannot be empty.",
- code=error_code,
- )
- }
- )
-
- @classmethod
- def get_money_data_from_input(cls, cleaned_data: dict) -> Dict[str, Decimal]:
- money_data = {}
- if amount_authorized := cleaned_data.pop("amount_authorized", None):
- money_data["authorized_value"] = amount_authorized["amount"]
- if amount_charged := cleaned_data.pop("amount_charged", None):
- money_data["charged_value"] = amount_charged["amount"]
- if amount_refunded := cleaned_data.pop("amount_refunded", None):
- money_data["refunded_value"] = amount_refunded["amount"]
-
- if amount_canceled := cleaned_data.pop("amount_canceled", None):
- money_data["canceled_value"] = amount_canceled["amount"]
- elif amount_voided := cleaned_data.pop("amount_voided", None):
- money_data["canceled_value"] = amount_voided["amount"]
- return money_data
-
- @classmethod
- def cleanup_metadata_data(cls, cleaned_data: dict):
- if metadata := cleaned_data.pop("metadata", None):
- cleaned_data["metadata"] = {data.key: data.value for data in metadata}
- if private_metadata := cleaned_data.pop("private_metadata", None):
- cleaned_data["private_metadata"] = {
- data.key: data.value for data in private_metadata
- }
-
- @classmethod
- def validate_instance(
- cls, instance: Model, instance_id
- ) -> Union[checkout_models.Checkout, order_models.Order]:
- """Validate if provided instance is an order or checkout type."""
- if not isinstance(instance, (checkout_models.Checkout, order_models.Order)):
- raise ValidationError(
- {
- "id": ValidationError(
- f"Couldn't resolve to Checkout or Order: {instance_id}",
- code=TransactionCreateErrorCode.NOT_FOUND.value,
- )
- }
- )
- return instance
-
- @classmethod
- def validate_money_input(
- cls, transaction_data: dict, currency: str, error_code: str
- ):
- if not transaction_data:
- return
- money_input_fields = [
- "amount_authorized",
- "amount_charged",
- "amount_refunded",
- "amount_canceled",
- "amount_voided",
- ]
- errors = {}
- for money_field_name in money_input_fields:
- field = transaction_data.get(money_field_name)
- if not field:
- continue
- if field["currency"] != currency:
- errors[money_field_name] = ValidationError(
- f"Currency needs to be the same as for order: {currency}",
- code=error_code,
- )
- if errors:
- raise ValidationError(errors)
-
- @classmethod
- def validate_input(
- cls, instance: Union[checkout_models.Checkout, order_models.Order], transaction
- ) -> Union[checkout_models.Checkout, order_models.Order]:
- currency = instance.currency
-
- cls.validate_money_input(
- transaction,
- currency,
- TransactionCreateErrorCode.INCORRECT_CURRENCY.value,
- )
- cls.validate_metadata_keys(
- transaction.get("metadata", []),
- field_name="metadata",
- error_code=TransactionCreateErrorCode.METADATA_KEY_REQUIRED.value,
- )
- cls.validate_metadata_keys(
- transaction.get("private_metadata", []),
- field_name="privateMetadata",
- error_code=TransactionCreateErrorCode.METADATA_KEY_REQUIRED.value,
- )
- cls.validate_external_url(
- transaction.get("external_url"),
- error_code=TransactionCreateErrorCode.INVALID.value,
- )
- return instance
-
- @classmethod
- def create_transaction(
- cls, transaction_input: dict, user, app, save: bool = True
- ) -> payment_models.TransactionItem:
- cls.cleanup_metadata_data(transaction_input)
-
- transaction_type = transaction_input.pop("type", None)
- transaction_input["name"] = transaction_input.get("name", transaction_type)
- reference = transaction_input.pop("reference", None)
- transaction_input["psp_reference"] = transaction_input.get(
- "psp_reference", reference
- )
- app_identifier = None
- if app and app.identifier:
- app_identifier = app.identifier
- transaction = payment_models.TransactionItem(
- token=uuid.uuid4(),
- use_old_id=True,
- **transaction_input,
- user=user if user and user.is_authenticated else None,
- app_identifier=app_identifier,
- app=app,
- )
- if save:
- transaction.save()
- return transaction
-
- @classmethod
- def create_transaction_event(
- cls,
- transaction_event_input: dict,
- transaction: payment_models.TransactionItem,
- user,
- app,
- ) -> payment_models.TransactionEvent:
- reference = transaction_event_input.pop("reference", None)
- psp_reference = transaction_event_input.get("psp_reference", reference)
- app_identifier = None
- if app and app.identifier:
- app_identifier = app.identifier
- return transaction.events.create(
- status=transaction_event_input.get("status"),
- psp_reference=psp_reference,
- message=cls.create_event_message(transaction_event_input),
- transaction=transaction,
- user=user if user and user.is_authenticated else None,
- app_identifier=app_identifier,
- app=app,
- type=TransactionEventType.INFO,
- currency=transaction.currency,
- )
-
- @classmethod
- def create_event_message(cls, transaction_event: dict) -> str:
- message = transaction_event.get("message")
- name = transaction_event.get("name")
- if message and name:
- return message + " " + name
- elif message:
- return message
- elif name:
- return name
- return ""
-
- @classmethod
- def update_order(
- cls,
- order: order_models.Order,
- money_data: dict,
- update_search_vector: bool = True,
- ) -> None:
- update_fields = []
- if money_data:
- updates_amounts_for_order(order, save=False)
- update_fields.extend(
- [
- "total_authorized_amount",
- "total_charged_amount",
- "authorize_status",
- "charge_status",
- ]
- )
- if (
- order.channel.automatically_confirm_all_new_orders
- and order.status == OrderStatus.UNCONFIRMED
- ):
- order.status = OrderStatus.UNFULFILLED
- update_fields.append("status")
-
- if update_search_vector:
- update_order_search_vector(order, save=False)
- update_fields.append(
- "search_vector",
- )
-
- if update_fields:
- update_fields.append("updated_at")
- order.save(update_fields=update_fields)
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls,
- _root,
- info: ResolveInfo,
- /,
- *,
- id: str,
- transaction: Dict,
- transaction_event=None,
- ):
- order_or_checkout_instance = cls.get_node_or_error(info, id)
- order_or_checkout_instance = cls.validate_instance(
- order_or_checkout_instance, id
- )
- order_or_checkout_instance = cls.validate_input(
- order_or_checkout_instance, transaction=transaction
- )
- transaction_data = {**transaction}
- transaction_data["currency"] = order_or_checkout_instance.currency
- app = get_app_promise(info.context).get()
- user = info.context.user
- manager = get_plugin_manager_promise(info.context).get()
-
- if isinstance(order_or_checkout_instance, checkout_models.Checkout):
- transaction_data["checkout_id"] = order_or_checkout_instance.pk
- else:
- transaction_data["order_id"] = order_or_checkout_instance.pk
- if transaction_event:
- reference = transaction_event.get("reference", None)
- psp_reference = transaction_event.get("psp_reference", reference)
- order_transaction_event(
- order=order_or_checkout_instance,
- user=user,
- app=app,
- reference=psp_reference,
- status=transaction_event.get("status"),
- message=cls.create_event_message(transaction_event),
- )
- money_data = cls.get_money_data_from_input(transaction_data)
- new_transaction = cls.create_transaction(transaction_data, user=user, app=app)
- if money_data:
- create_manual_adjustment_events(
- transaction=new_transaction, money_data=money_data, user=user, app=app
- )
- recalculate_transaction_amounts(new_transaction)
- if transaction_data.get("order_id") and money_data:
- order = cast(order_models.Order, new_transaction.order)
- cls.update_order(order, money_data, update_search_vector=True)
-
- order_info = fetch_order_info(order)
- order_transaction_updated(
- order_info=order_info,
- transaction_item=new_transaction,
- manager=manager,
- user=user,
- app=app,
- previous_authorized_value=Decimal(0),
- previous_charged_value=Decimal(0),
- previous_refunded_value=Decimal(0),
- )
- if transaction_data.get("checkout_id") and money_data:
- transaction_amounts_for_checkout_updated(new_transaction, manager)
-
- if transaction_event:
- cls.create_transaction_event(transaction_event, new_transaction, user, app)
- return TransactionCreate(transaction=new_transaction)
-
-
-def get_transaction_item(id: str) -> payment_models.TransactionItem:
- """Get transaction based on global ID.
-
- The transactions created before 3.13 were using the `id` field as a graphql ID.
- From 3.13, the `token` is used as a graphql ID. All transactionItems created
- before 3.13 will use an `int` id as an identification.
- """
- _, db_id = from_global_id_or_error(
- global_id=id, only_type=TransactionItem, raise_error=True
- )
- if db_id.isdigit():
- query_params = {"id": db_id, "use_old_id": True}
- else:
- query_params = {"token": db_id}
- instance = payment_models.TransactionItem.objects.filter(**query_params).first()
- if not instance:
- raise ValidationError(
- {
- "id": ValidationError(
- f"Couldn't resolve to a node: {id}",
- code=TransactionUpdateErrorCode.NOT_FOUND.value,
- )
- }
- )
- return instance
-
-
-class TransactionUpdate(TransactionCreate):
- transaction = graphene.Field(TransactionItem)
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the transaction.",
- required=True,
- )
- transaction = TransactionUpdateInput(
- description="Input data required to create a new transaction object.",
- )
- transaction_event = TransactionEventInput(
- description="Data that defines a transaction transaction."
- )
-
- class Meta:
- auto_permission_message = False
- description = (
- "Update transaction."
- + ADDED_IN_34
- + PREVIEW_FEATURE
- + "\n\nRequires the following permissions: "
- + f"{AuthorizationFilters.OWNER.name} "
- + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
- f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
- f"Staff user cannot update a transaction that is owned by the app."
- )
- doc_category = DOC_CATEGORY_PAYMENTS
- error_type_class = common_types.TransactionUpdateError
- permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
- object_type = TransactionItem
-
- @classmethod
- def check_can_update(
- cls,
- transaction: payment_models.TransactionItem,
- user: Optional["User"],
- app: Optional["App"],
- ):
- if not check_if_requestor_has_access(
- transaction=transaction, user=user, app=app
- ):
- raise PermissionDenied(
- permissions=[
- AuthorizationFilters.OWNER,
- PaymentPermissions.HANDLE_PAYMENTS,
- ]
- )
-
- @classmethod
- def validate_transaction_input(
- cls, instance: payment_models.TransactionItem, transaction_data
- ):
- currency = instance.currency
- cls.validate_money_input(
- transaction_data,
- currency,
- TransactionUpdateErrorCode.INCORRECT_CURRENCY.value,
- )
- cls.validate_metadata_keys(
- transaction_data.get("metadata", []),
- field_name="metadata",
- error_code=TransactionUpdateErrorCode.METADATA_KEY_REQUIRED.value,
- )
- cls.validate_metadata_keys(
- transaction_data.get("private_metadata", []),
- field_name="privateMetadata",
- error_code=TransactionUpdateErrorCode.METADATA_KEY_REQUIRED.value,
- )
- cls.validate_external_url(
- transaction_data.get("external_url"),
- error_code=TransactionCreateErrorCode.INVALID.value,
- )
-
- @classmethod
- def update_transaction(
- cls,
- instance: payment_models.TransactionItem,
- transaction_data: dict,
- money_data: dict,
- user: Optional["User"],
- app: Optional["App"],
- ):
- psp_reference = transaction_data.get(
- "psp_reference", transaction_data.pop("reference", None)
- )
- if psp_reference and instance.psp_reference != psp_reference:
- if payment_models.TransactionItem.objects.filter(
- psp_reference=psp_reference
- ).exists():
- raise ValidationError(
- {
- "transaction": ValidationError(
- "Transaction with provided `pspReference` already exists.",
- code=TransactionUpdateErrorCode.UNIQUE.value,
- )
- }
- )
- transaction_data["name"] = transaction_data.get(
- "name", transaction_data.pop("type", None)
- )
- transaction_data["psp_reference"] = psp_reference
- instance = cls.construct_instance(instance, transaction_data)
- instance.save()
- if money_data:
- calculate_transaction_amount_based_on_events(transaction=instance)
- create_manual_adjustment_events(
- transaction=instance, money_data=money_data, user=user, app=app
- )
- recalculate_transaction_amounts(instance)
-
- @classmethod
- def assign_app_to_transaction_data_if_missing(
- cls,
- transaction: payment_models.TransactionItem,
- transaction_data: dict,
- app: Optional["App"],
- ):
- """Assign app to transaction if missing.
-
- TransactionItem created before 3.13, doesn't have a relation to the owner app.
- When app updates a transaction, we need to assign the app to the transaction.
- """
- transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
- if app and not transaction.user_id and not transaction_has_assigned_app:
- transaction_data["app"] = app
- transaction_data["app_identifier"] = app.identifier
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls,
- _root,
- info: ResolveInfo,
- /,
- *,
- id: str,
- transaction=None,
- transaction_event=None,
- ):
- app = get_app_promise(info.context).get()
- user = info.context.user
- instance = get_transaction_item(id)
- manager = get_plugin_manager_promise(info.context).get()
-
- cls.check_can_update(
- transaction=instance,
- user=user if user and user.is_authenticated else None,
- app=app,
- )
- money_data = {}
- previous_transaction_psp_reference = instance.psp_reference
- previous_authorized_value = instance.authorized_value
- previous_charged_value = instance.charged_value
- previous_refunded_value = instance.refunded_value
-
- if transaction:
- cls.validate_transaction_input(instance, transaction)
- cls.assign_app_to_transaction_data_if_missing(instance, transaction, app)
- cls.cleanup_metadata_data(transaction)
- money_data = cls.get_money_data_from_input(transaction)
- cls.update_transaction(instance, transaction, money_data, user, app)
-
- event = None
- if transaction_event:
- event = cls.create_transaction_event(transaction_event, instance, user, app)
- if instance.order:
- reference = transaction_event.pop("reference", None)
- psp_reference = transaction_event.get("psp_reference", reference)
- order_transaction_event(
- order=instance.order,
- user=user,
- app=app,
- reference=psp_reference or "",
- status=transaction_event["status"],
- message=cls.create_event_message(transaction_event),
- )
- if instance.order_id:
- order = cast(order_models.Order, instance.order)
- should_update_search_vector = bool(
- (instance.psp_reference != previous_transaction_psp_reference)
- or (event and event.psp_reference)
- )
- cls.update_order(
- order, money_data, update_search_vector=should_update_search_vector
- )
- order_info = fetch_order_info(order)
- order_transaction_updated(
- order_info=order_info,
- transaction_item=instance,
- manager=manager,
- user=user,
- app=app,
- previous_authorized_value=previous_authorized_value,
- previous_charged_value=previous_charged_value,
- previous_refunded_value=previous_refunded_value,
- )
- if instance.checkout_id and money_data:
- manager = get_plugin_manager_promise(info.context).get()
- transaction_amounts_for_checkout_updated(instance, manager)
-
- return TransactionUpdate(transaction=instance)
-
-
-class TransactionRequestAction(BaseMutation):
- transaction = graphene.Field(TransactionItem)
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the transaction.",
- required=True,
- )
- action_type = graphene.Argument(
- TransactionActionEnum,
- required=True,
- description="Determines the action type.",
- )
- amount = PositiveDecimal(
- description=(
- "Transaction request amount. If empty for refund or capture, maximal "
- "possible amount will be used."
- )
- )
-
- class Meta:
- description = (
- "Request an action for payment transaction." + ADDED_IN_34 + PREVIEW_FEATURE
- )
- doc_category = DOC_CATEGORY_PAYMENTS
- error_type_class = common_types.TransactionRequestActionError
- permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
-
- @classmethod
- def check_permissions(cls, context, permissions=None, **data):
- required_permissions = permissions or cls._meta.permissions
- requestor = get_user_or_app_from_context(context)
- for required_permission in required_permissions:
- # We want to allow to call this mutation for requestor with one of following
- # permission: manage_orders, handle_payments
- if requestor and requestor.has_perm(required_permission):
- return True
- return False
-
- @classmethod
- def handle_transaction_action(
- cls,
- action,
- action_kwargs,
- action_value: Optional[Decimal],
- user: Optional["User"],
- app: Optional[App],
- ):
- if action == TransactionAction.VOID or action == TransactionAction.CANCEL:
- transaction = action_kwargs["transaction"]
- request_event = cls.create_transaction_event_requested(
- transaction, 0, action, user=user, app=app
- )
- request_cancelation_action(
- **action_kwargs,
- cancel_value=action_value,
- request_event=request_event,
- action=action,
- )
- elif action == TransactionAction.CHARGE:
- transaction = action_kwargs["transaction"]
- action_value = action_value or transaction.authorized_value
- action_value = min(action_value, transaction.authorized_value)
- request_event = cls.create_transaction_event_requested(
- transaction, action_value, TransactionAction.CHARGE, user=user, app=app
- )
- request_charge_action(
- **action_kwargs, charge_value=action_value, request_event=request_event
- )
- elif action == TransactionAction.REFUND:
- transaction = action_kwargs["transaction"]
- action_value = action_value or transaction.charged_value
- action_value = min(action_value, transaction.charged_value)
- request_event = cls.create_transaction_event_requested(
- transaction, action_value, TransactionAction.REFUND, user=user, app=app
- )
- request_refund_action(
- **action_kwargs, refund_value=action_value, request_event=request_event
- )
-
- @classmethod
- def create_transaction_event_requested(
- cls, transaction, action_value, action, user=None, app=None
- ):
- if action in (TransactionAction.CANCEL, TransactionAction.VOID):
- type = TransactionEventType.CANCEL_REQUEST
- elif action == TransactionAction.CHARGE:
- type = TransactionEventType.CHARGE_REQUEST
- elif action == TransactionAction.REFUND:
- type = TransactionEventType.REFUND_REQUEST
- else:
- raise ValidationError(
- {
- "actionType": ValidationError(
- "Incorrect action.",
- code=TransactionRequestActionErrorCode.INVALID.value,
- )
- }
- )
- return transaction.events.create(
- amount_value=action_value,
- currency=transaction.currency,
- type=type,
- user=user,
- app=app,
- app_identifier=app.identifier if app else None,
- )
-
- @classmethod
- def perform_mutation(cls, root, info: ResolveInfo, /, **data):
- id = data["id"]
- action_type = data["action_type"]
- action_value = data.get("amount")
- transaction = get_transaction_item(id)
- if transaction.order_id:
- order = cast(Order, transaction.order)
- channel = order.channel
- else:
- checkout = cast(Checkout, transaction.checkout)
- channel = checkout.channel
- cls.check_channel_permissions(info, [channel.id])
- channel_slug = channel.slug
- user = info.context.user
- app = get_app_promise(info.context).get()
- manager = get_plugin_manager_promise(info.context).get()
- action_kwargs = {
- "channel_slug": channel_slug,
- "user": user,
- "app": app,
- "transaction": transaction,
- "manager": manager,
- }
-
- try:
- cls.handle_transaction_action(
- action_type, action_kwargs, action_value, user, app
- )
- except PaymentError as e:
- error_enum = TransactionRequestActionErrorCode
- code = error_enum.MISSING_TRANSACTION_ACTION_REQUEST_WEBHOOK.value
- raise ValidationError(str(e), code=code)
- return TransactionRequestAction(transaction=transaction)
-
-
-class TransactionEventReport(ModelMutation):
- already_processed = graphene.Boolean(
- description="Defines if the reported event hasn't been processed earlier."
- )
- transaction = graphene.Field(
- TransactionItem, description="The transaction related to the reported event."
- )
- transaction_event = graphene.Field(
- TransactionEvent,
- description=(
- "The event assigned to this report. if `alreadyProcessed` is set to `true`,"
- " the previously processed event will be returned."
- ),
- )
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the transaction.",
- required=True,
- )
- psp_reference = graphene.String(
- description="PSP Reference of the event to report.", required=True
- )
- type = graphene.Argument(
- TransactionEventTypeEnum,
- required=True,
- description="Current status of the event to report.",
- )
- amount = PositiveDecimal(
- description="The amount of the event to report.", required=True
- )
- time = graphene.DateTime(
- description=(
- "The time of the event to report. If not provide, "
- "the current time will be used."
- )
- )
- external_url = graphene.String(
- description=(
- "The url that will allow to redirect user to "
- "payment provider page with event details."
- )
- )
- message = graphene.String(description="The message related to the event.")
- available_actions = graphene.List(
- graphene.NonNull(TransactionActionEnum),
- description="List of all possible actions for the transaction",
- )
-
- class Meta:
- description = (
- "Report the event for the transaction."
- + ADDED_IN_313
- + PREVIEW_FEATURE
- + "\n\nRequires the following permissions: "
- + f"{AuthorizationFilters.OWNER.name} "
- + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
- f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
- f"Staff user cannot update a transaction that is owned by the app."
- )
- error_type_class = common_types.TransactionEventReportError
- permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
- doc_category = DOC_CATEGORY_PAYMENTS
- model = payment_models.TransactionEvent
- object_type = TransactionEvent
- auto_permission_message = False
-
- @classmethod
- def _update_mutation_arguments_and_fields(cls, arguments, fields):
- cls._meta.arguments.update(arguments)
-
- @classmethod
- def update_transaction(
- cls,
- transaction: payment_models.TransactionItem,
- available_actions: Optional[list[str]] = None,
- app: Optional["App"] = None,
- ):
- fields_to_update = [
- "authorized_value",
- "charged_value",
- "refunded_value",
- "canceled_value",
- "authorize_pending_value",
- "charge_pending_value",
- "refund_pending_value",
- "cancel_pending_value",
- ]
- if available_actions is not None:
- transaction.available_actions = available_actions
- fields_to_update.append("available_actions")
-
- recalculate_transaction_amounts(transaction, save=False)
- transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
- if app and not transaction.user_id and not transaction_has_assigned_app:
- transaction.app_id = app.pk
- transaction.app_identifier = app.identifier
- fields_to_update.append("app")
- fields_to_update.append("app_identifier")
- transaction.save(update_fields=fields_to_update)
-
- @classmethod
- def perform_mutation( # type: ignore[override]
- cls,
- root,
- info: ResolveInfo,
- /,
- *,
- id,
- psp_reference,
- type,
- amount,
- time=None,
- external_url=None,
- message=None,
- available_actions=None,
- ):
- user = info.context.user
- app = get_app_promise(info.context).get()
- manager = get_plugin_manager_promise(info.context).get()
-
- transaction = get_transaction_item(id)
-
- if not check_if_requestor_has_access(
- transaction=transaction, user=user, app=app
- ):
- raise PermissionDenied(
- permissions=[
- AuthorizationFilters.OWNER,
- PaymentPermissions.HANDLE_PAYMENTS,
- ]
- )
-
- app_identifier = None
- if app and app.identifier:
- app_identifier = app.identifier
-
- transaction_event_data = {
- "psp_reference": psp_reference,
- "type": type,
- "amount_value": amount,
- "currency": transaction.currency,
- "created_at": time or timezone.now(),
- "external_url": external_url or "",
- "message": message or "",
- "transaction": transaction,
- "app_identifier": app_identifier,
- "app": app,
- "user": user,
- "include_in_calculations": True,
- }
- transaction_event = cls.get_instance(info, **transaction_event_data)
- transaction_event = cast(payment_models.TransactionEvent, transaction_event)
- transaction_event = cls.construct_instance(
- transaction_event, transaction_event_data
- )
-
- cls.clean_instance(info, transaction_event)
- already_processed = False
- error_code = None
- error_msg = None
- error_field = None
- with traced_atomic_transaction():
- # The mutation can be called multiple times by the app. That can cause a
- # thread race. We need to be sure, that we will always create a single event
- # on our side for specific action.
- existing_event = get_already_existing_event(transaction_event)
- if existing_event and existing_event.amount != transaction_event.amount:
- error_code = TransactionEventReportErrorCode.INCORRECT_DETAILS.value
- error_msg = (
- "The transaction with provided `pspReference` and "
- "`type` already exists with different amount."
- )
- error_field = "pspReference"
- elif existing_event:
- already_processed = True
- transaction_event = existing_event
- elif (
- transaction_event.type == TransactionEventType.AUTHORIZATION_SUCCESS
- and authorization_success_already_exists(transaction.pk)
- ):
- error_code = TransactionEventReportErrorCode.ALREADY_EXISTS.value
- error_msg = (
- "Event with `AUTHORIZATION_SUCCESS` already "
- "reported for the transaction. Use "
- "`AUTHORIZATION_ADJUSTMENT` to change the "
- "authorization amount."
- )
- error_field = "type"
- else:
- transaction_event.save()
-
- if error_msg and error_code and error_field:
- create_failed_transaction_event(transaction_event, cause=error_msg)
- raise ValidationError({error_field: ValidationError(error_msg, error_code)})
- if not already_processed:
- previous_authorized_value = transaction.authorized_value
- previous_charged_value = transaction.charged_value
- previous_refunded_value = transaction.refunded_value
- cls.update_transaction(
- transaction,
- available_actions=available_actions,
- app=app,
- )
- if transaction.order_id:
- order = cast(order_models.Order, transaction.order)
- update_order_search_vector(order, save=False)
- updates_amounts_for_order(order, save=False)
- order.save(
- update_fields=[
- "total_charged_amount",
- "charge_status",
- "updated_at",
- "total_authorized_amount",
- "authorize_status",
- "search_vector",
- ]
- )
- order_info = fetch_order_info(order)
- order_transaction_updated(
- order_info=order_info,
- transaction_item=transaction,
- manager=manager,
- user=user,
- app=app,
- previous_authorized_value=previous_authorized_value,
- previous_charged_value=previous_charged_value,
- previous_refunded_value=previous_refunded_value,
- )
- if transaction.checkout_id:
- manager = get_plugin_manager_promise(info.context).get()
- transaction_amounts_for_checkout_updated(transaction, manager)
-
- return cls(
- already_processed=already_processed,
- transaction=transaction,
- transaction_event=transaction_event,
- errors=[],
- )
-
-
-class PaymentGatewayConfig(BaseObjectType):
- id = graphene.String(required=True, description="The app identifier.")
- data = graphene.Field(
- JSON, description="The JSON data required to initialize the payment gateway."
- )
- errors = common_types.NonNullList(common_types.PaymentGatewayConfigError)
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class PaymentGatewayToInitialize(BaseInputObjectType):
- id = graphene.String(
- required=True,
- description="The identifier of the payment gateway app to initialize.",
- )
- data = graphene.Field(
- JSON, description="The data that will be passed to the payment gateway."
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
-
-
-class TransactionSessionBase(BaseMutation):
- class Meta:
- abstract = True
-
- @classmethod
- def clean_source_object(
- cls,
- info,
- id,
- incorrect_type_error_code: str,
- not_found_error: str,
- manager: "PluginsManager",
- ) -> Union[checkout_models.Checkout, order_models.Order]:
- source_object_type, source_object_id = from_global_id_or_error(
- id, raise_error=False
- )
- if not source_object_type or not source_object_id:
- raise GraphQLError(f"Couldn't resolve id: {id}.")
-
- if source_object_type not in ["Checkout", "Order"]:
- raise ValidationError(
- {
- "id": ValidationError(
- "Must receive a `Checkout` or `Order` id.",
- code=incorrect_type_error_code,
- )
- }
- )
- source_object: Optional[Union[checkout_models.Checkout, order_models.Order]]
- if source_object_type == "Checkout":
- source_object = (
- checkout_models.Checkout.objects.select_related("channel")
- .prefetch_related("payment_transactions")
- .filter(pk=source_object_id)
- .first()
- )
- if source_object:
- lines, _ = fetch_checkout_lines(source_object)
- checkout_info = fetch_checkout_info(source_object, lines, manager)
- checkout_info, _ = fetch_checkout_data(checkout_info, manager, lines)
- source_object = checkout_info.checkout
- else:
- source_object = (
- order_models.Order.objects.select_related("channel")
- .prefetch_related("payment_transactions")
- .filter(pk=source_object_id)
- .first()
- )
-
- if not source_object:
- raise ValidationError(
- {
- "id": ValidationError(
- "`Order` or `Checkout` not found.",
- code=not_found_error,
- )
- }
- )
- return source_object
-
- @classmethod
- def get_amount(
- cls,
- source_object: Union[checkout_models.Checkout, order_models.Order],
- input_amount: Optional[Decimal],
- ) -> Decimal:
- if input_amount is not None:
- return input_amount
- amount: Decimal = source_object.total_gross_amount
- transactions = source_object.payment_transactions.all()
- for transaction_item in transactions:
- amount_to_reduce = transaction_item.authorized_value
- if amount_to_reduce < transaction_item.charged_value:
- amount_to_reduce = transaction_item.charged_value
- amount -= amount_to_reduce
- amount -= transaction_item.authorize_pending_value
- amount -= transaction_item.charge_pending_value
-
- return amount if amount >= Decimal(0) else Decimal(0)
-
-
-class PaymentGatewayInitialize(TransactionSessionBase):
- gateway_configs = common_types.NonNullList(PaymentGatewayConfig)
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the checkout or order.",
- required=True,
- )
- amount = graphene.Argument(
- PositiveDecimal,
- description=(
- "The amount requested for initializing the payment gateway. "
- "If not provided, the difference between checkout.total - "
- "transactions that are already processed will be send."
- ),
- )
- payment_gateways = graphene.List(
- graphene.NonNull(PaymentGatewayToInitialize),
- description="List of payment gateways to initialize.",
- required=False,
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
- description = (
- "Initializes a payment gateway session. It triggers the webhook "
- "`PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
- "If `paymentGateways` is not provided, the webhook will be send to all "
- "subscribed payment gateways." + ADDED_IN_313 + PREVIEW_FEATURE
- )
- error_type_class = common_types.PaymentGatewayInitializeError
-
- @classmethod
- def prepare_response(
- cls,
- payment_gateways_input: list[PaymentGatewayData],
- payment_gateways_response: list[PaymentGatewayData],
- ) -> list[PaymentGatewayConfig]:
- response = []
- payment_gateways_response_dict = {
- gateway.app_identifier: gateway for gateway in payment_gateways_response
- }
-
- payment_gateways_input_dict = (
- {gateway.app_identifier: gateway for gateway in payment_gateways_input}
- if payment_gateways_input
- else payment_gateways_response_dict
- )
- for identifier in payment_gateways_input_dict:
- app_identifier = identifier
- payment_gateway_response = payment_gateways_response_dict.get(identifier)
- if payment_gateway_response:
- response_data = payment_gateway_response.data
- errors = []
- if payment_gateway_response.error:
- code = common_types.PaymentGatewayConfigErrorCode.INVALID.value
- errors = [
- {
- "field": "id",
- "message": payment_gateway_response.error,
- "code": code,
- }
- ]
-
- else:
- response_data = None
- code = common_types.PaymentGatewayConfigErrorCode.NOT_FOUND.value
- msg = (
- "Active app with `HANDLE_PAYMENT` permissions or "
- "app webhook not found."
- )
- errors = [
- {
- "field": "id",
- "message": msg,
- "code": code,
- }
- ]
- data_to_return = response_data.get("data") if response_data else None
- response.append(
- PaymentGatewayConfig(
- id=app_identifier, data=data_to_return, errors=errors
- )
- )
- return response
-
- @classmethod
- def perform_mutation(cls, root, info, *, id, amount=None, payment_gateways=None):
- manager = get_plugin_manager_promise(info.context).get()
- source_object = cls.clean_source_object(
- info,
- id,
- PaymentGatewayInitializeErrorCode.INVALID.value,
- PaymentGatewayInitializeErrorCode.NOT_FOUND.value,
- manager=manager,
- )
- payment_gateways_data = []
- if payment_gateways:
- payment_gateways_data = [
- PaymentGatewayData(
- app_identifier=gateway["id"], data=gateway.get("data")
- )
- for gateway in payment_gateways
- ]
- amount = cls.get_amount(source_object, amount)
- response_data = manager.payment_gateway_initialize_session(
- amount, payment_gateways_data, source_object
- )
- return cls(
- gateway_configs=cls.prepare_response(payment_gateways_data, response_data),
- errors=[],
- )
-
-
-class TransactionInitialize(TransactionSessionBase):
- transaction = graphene.Field(
- TransactionItem, description="The initialized transaction."
- )
- transaction_event = graphene.Field(
- TransactionEvent,
- description="The event created for the initialized transaction.",
- )
- data = graphene.Field(
- JSON, description="The JSON data required to finalize the payment."
- )
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the checkout or order.",
- required=True,
- )
- amount = graphene.Argument(
- PositiveDecimal,
- description=(
- "The amount requested for initializing the payment gateway. "
- "If not provided, the difference between checkout.total - "
- "transactions that are already processed will be send."
- ),
- )
- action = graphene.Argument(
- TransactionFlowStrategyEnum,
- description=(
- "The expected action called for the transaction. By default, the "
- "`channel.defaultTransactionFlowStrategy` will be used. The field "
- "can be used only by app that has `HANDLE_PAYMENTS` permission."
- ),
- )
- payment_gateway = graphene.Argument(
- PaymentGatewayToInitialize,
- description="Payment gateway used to initialize the transaction.",
- required=True,
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
- description = (
- "Initializes a transaction session. It triggers the webhook "
- "`TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
- + ADDED_IN_313
- + PREVIEW_FEATURE
- )
- error_type_class = common_types.TransactionInitializeError
-
- @classmethod
- def clean_action(cls, info, action: Optional[str], channel: "Channel"):
- if not action:
- return channel.default_transaction_flow_strategy
- app = get_app_promise(info.context).get()
- if not app or not app.has_perm(PaymentPermissions.HANDLE_PAYMENTS):
- raise PermissionDenied(permissions=[PaymentPermissions.HANDLE_PAYMENTS])
- return action
-
- @classmethod
- def clean_app_from_payment_gateway(cls, payment_gateway: PaymentGatewayData) -> App:
- app = App.objects.filter(identifier=payment_gateway.app_identifier).first()
- if not app:
- raise ValidationError(
- {
- "payment_gateway": ValidationError(
- message="App with provided identifier not found.",
- code=TransactionInitializeErrorCode.NOT_FOUND.value,
- )
- }
- )
- return app
-
- @classmethod
- def perform_mutation(
- cls, root, info, *, id, payment_gateway, amount=None, action=None
- ):
- manager = get_plugin_manager_promise(info.context).get()
- payment_gateway_data = PaymentGatewayData(
- app_identifier=payment_gateway["id"], data=payment_gateway.get("data")
- )
- source_object = cls.clean_source_object(
- info,
- id,
- TransactionInitializeErrorCode.INVALID.value,
- TransactionInitializeErrorCode.NOT_FOUND.value,
- manager=manager,
- )
- action = cls.clean_action(info, action, source_object.channel)
-
- amount = cls.get_amount(
- source_object,
- amount,
- )
- app = cls.clean_app_from_payment_gateway(payment_gateway_data)
- transaction, event, data = handle_transaction_initialize_session(
- source_object=source_object,
- payment_gateway=payment_gateway_data,
- amount=amount,
- action=action,
- app=app,
- manager=manager,
- )
- return cls(transaction=transaction, transaction_event=event, data=data)
-
-
-class TransactionProcess(BaseMutation):
- transaction = graphene.Field(
- TransactionItem, description="The processed transaction."
- )
- transaction_event = graphene.Field(
- TransactionEvent,
- description="The event created for the processed transaction.",
- )
- data = graphene.Field(
- JSON, description="The json data required to finalize the payment."
- )
-
- class Arguments:
- id = graphene.ID(
- description="The ID of the transaction to process.",
- required=True,
- )
- data = graphene.Argument(
- JSON, description="The data that will be passed to the payment gateway."
- )
-
- class Meta:
- doc_category = DOC_CATEGORY_PAYMENTS
- description = (
- "Processes a transaction session. It triggers the webhook "
- "`TRANSACTION_PROCESS_SESSION`, to the assigned `paymentGateways`. "
- + ADDED_IN_313
- + PREVIEW_FEATURE
- )
- error_type_class = common_types.TransactionProcessError
-
- @classmethod
- def get_action(cls, event: payment_models.TransactionEvent, channel: "Channel"):
- if event.type == TransactionEventType.AUTHORIZATION_REQUEST:
- return TransactionFlowStrategy.AUTHORIZATION
- elif event.type == TransactionEventType.CHARGE_REQUEST:
- return TransactionFlowStrategy.CHARGE
-
- return channel.default_transaction_flow_strategy
-
- @classmethod
- def get_source_object(
- cls, transaction_item: payment_models.TransactionItem
- ) -> Union[checkout_models.Checkout, order_models.Order]:
- if transaction_item.checkout_id:
- checkout = cast(checkout_models.Checkout, transaction_item.checkout)
- return checkout
- if transaction_item.order_id:
- order = cast(order_models.Order, transaction_item.order)
- return order
- raise ValidationError(
- {
- "id": ValidationError(
- "Transaction doesn't have attached order or checkout.",
- code=TransactionProcessErrorCode.INVALID.value,
- )
- }
- )
-
- @classmethod
- def get_request_event(cls, events: QuerySet) -> payment_models.TransactionEvent:
- """Get event with details of requested action.
-
- This searches for a request event with the appropriate type and
- include_in_calculations set to false. Request events created from
- transactionInitialize have their include_in_calculation set to false by default.
- """
- for event in events:
- if (
- event.type
- in [
- TransactionEventType.AUTHORIZATION_REQUEST,
- TransactionEventType.CHARGE_REQUEST,
- ]
- and not event.include_in_calculations
- ):
- return event
- raise ValidationError(
- {
- "id": ValidationError(
- "Missing call of `transactionInitialize` mutation.",
- code=TransactionProcessErrorCode.INVALID.value,
- )
- }
- )
-
- @classmethod
- def get_already_processed_event(cls, events) -> Optional[TransactionEvent]:
- for event in events:
- if (
- event.type in get_final_session_statuses()
- and event.include_in_calculations
- ):
- return event
- return None
-
- @classmethod
- def clean_payment_app(cls, transaction_item: payment_models.TransactionItem) -> App:
- if not transaction_item.app_identifier:
- raise ValidationError(
- {
- "id": ValidationError(
- "Transaction doesn't have attached app that could process the "
- "request.",
- code=TransactionProcessErrorCode.MISSING_PAYMENT_APP_RELATION.value,
- )
- }
- )
- app = App.objects.filter(identifier=transaction_item.app_identifier).first()
- if not app:
- raise ValidationError(
- {
- "id": ValidationError(
- "Payment app attached to the transaction, doesn't exist.",
- code=TransactionProcessErrorCode.MISSING_PAYMENT_APP.value,
- )
- }
- )
- return app
-
- @classmethod
- def perform_mutation(cls, root, info, *, id, data=None):
- transaction_item = cls.get_node_or_error(
- info, id, only_type="TransactionItem", field="token"
- )
- transaction_item = cast(payment_models.TransactionItem, transaction_item)
- events = transaction_item.events.all()
- if processed_event := cls.get_already_processed_event(events):
- return cls(
- transaction=transaction_item,
- transaction_event=processed_event,
- data=None,
- )
- request_event = cls.get_request_event(events)
- source_object = cls.get_source_object(transaction_item)
- app = cls.clean_payment_app(transaction_item)
- app_identifier = app.identifier
- app_identifier = cast(str, app_identifier)
- action = cls.get_action(request_event, source_object.channel)
- manager = get_plugin_manager_promise(info.context).get()
- event, data = handle_transaction_process_session(
- transaction_item=transaction_item,
- source_object=source_object,
- payment_gateway=PaymentGatewayData(
- app_identifier=app_identifier, data=data
- ),
- app=app,
- action=action,
- manager=manager,
- request_event=request_event,
- )
-
- transaction_item.refresh_from_db()
- return cls(transaction=transaction_item, transaction_event=event, data=data)
diff --git a/saleor/graphql/payment/mutations/__init__.py b/saleor/graphql/payment/mutations/__init__.py
new file mode 100644
index 0000000000..5d729c1762
--- /dev/null
+++ b/saleor/graphql/payment/mutations/__init__.py
@@ -0,0 +1,33 @@
+from .payment import (
+ CheckoutPaymentCreate,
+ PaymentCapture,
+ PaymentCheckBalance,
+ PaymentGatewayInitialize,
+ PaymentInitialize,
+ PaymentRefund,
+ PaymentVoid,
+)
+from .transaction import (
+ TransactionCreate,
+ TransactionEventReport,
+ TransactionInitialize,
+ TransactionProcess,
+ TransactionRequestAction,
+ TransactionUpdate,
+)
+
+__all__ = [
+ "CheckoutPaymentCreate",
+ "PaymentCapture",
+ "PaymentCheckBalance",
+ "PaymentGatewayInitialize",
+ "PaymentInitialize",
+ "PaymentRefund",
+ "PaymentVoid",
+ "TransactionCreate",
+ "TransactionEventReport",
+ "TransactionInitialize",
+ "TransactionProcess",
+ "TransactionRequestAction",
+ "TransactionUpdate",
+]
diff --git a/saleor/graphql/payment/mutations/base.py b/saleor/graphql/payment/mutations/base.py
new file mode 100644
index 0000000000..1fcdea92fa
--- /dev/null
+++ b/saleor/graphql/payment/mutations/base.py
@@ -0,0 +1,96 @@
+from decimal import Decimal
+from typing import TYPE_CHECKING, Optional, Union
+
+from django.core.exceptions import ValidationError
+from graphql import GraphQLError
+
+from ....checkout import models as checkout_models
+from ....checkout.calculations import fetch_checkout_data
+from ....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from ....order import models as order_models
+from ...core.mutations import BaseMutation
+from ...core.utils import from_global_id_or_error
+
+if TYPE_CHECKING:
+ from ....plugins.manager import PluginsManager
+
+
+class TransactionSessionBase(BaseMutation):
+ class Meta:
+ abstract = True
+
+ @classmethod
+ def clean_source_object(
+ cls,
+ info,
+ id,
+ incorrect_type_error_code: str,
+ not_found_error: str,
+ manager: "PluginsManager",
+ ) -> Union[checkout_models.Checkout, order_models.Order]:
+ source_object_type, source_object_id = from_global_id_or_error(
+ id, raise_error=False
+ )
+ if not source_object_type or not source_object_id:
+ raise GraphQLError(f"Couldn't resolve id: {id}.")
+
+ if source_object_type not in ["Checkout", "Order"]:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Must receive a `Checkout` or `Order` id.",
+ code=incorrect_type_error_code,
+ )
+ }
+ )
+ source_object: Optional[Union[checkout_models.Checkout, order_models.Order]]
+ if source_object_type == "Checkout":
+ source_object = (
+ checkout_models.Checkout.objects.select_related("channel")
+ .prefetch_related("payment_transactions")
+ .filter(pk=source_object_id)
+ .first()
+ )
+ if source_object:
+ lines, _ = fetch_checkout_lines(source_object)
+ checkout_info = fetch_checkout_info(source_object, lines, manager)
+ checkout_info, _ = fetch_checkout_data(checkout_info, manager, lines)
+ source_object = checkout_info.checkout
+ else:
+ source_object = (
+ order_models.Order.objects.select_related("channel")
+ .prefetch_related("payment_transactions")
+ .filter(pk=source_object_id)
+ .first()
+ )
+
+ if not source_object:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "`Order` or `Checkout` not found.",
+ code=not_found_error,
+ )
+ }
+ )
+ return source_object
+
+ @classmethod
+ def get_amount(
+ cls,
+ source_object: Union[checkout_models.Checkout, order_models.Order],
+ input_amount: Optional[Decimal],
+ ) -> Decimal:
+ if input_amount is not None:
+ return input_amount
+ amount: Decimal = source_object.total_gross_amount
+ transactions = source_object.payment_transactions.all()
+ for transaction_item in transactions:
+ amount_to_reduce = transaction_item.authorized_value
+ if amount_to_reduce < transaction_item.charged_value:
+ amount_to_reduce = transaction_item.charged_value
+ amount -= amount_to_reduce
+ amount -= transaction_item.authorize_pending_value
+ amount -= transaction_item.charge_pending_value
+
+ return amount if amount >= Decimal(0) else Decimal(0)
diff --git a/saleor/graphql/payment/mutations/payment/__init__.py b/saleor/graphql/payment/mutations/payment/__init__.py
new file mode 100644
index 0000000000..e1b6d77205
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/__init__.py
@@ -0,0 +1,17 @@
+from .checkout_payment_create import CheckoutPaymentCreate
+from .payment_capture import PaymentCapture
+from .payment_check_balance import PaymentCheckBalance
+from .payment_gateway_initialize import PaymentGatewayInitialize
+from .payment_initialize import PaymentInitialize
+from .payment_refund import PaymentRefund
+from .payment_void import PaymentVoid
+
+__all__ = [
+ "CheckoutPaymentCreate",
+ "PaymentCapture",
+ "PaymentCheckBalance",
+ "PaymentGatewayInitialize",
+ "PaymentInitialize",
+ "PaymentRefund",
+ "PaymentVoid",
+]
diff --git a/saleor/graphql/payment/mutations/payment/checkout_payment_create.py b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
new file mode 100644
index 0000000000..2ed67c41a4
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/checkout_payment_create.py
@@ -0,0 +1,320 @@
+from typing import List
+
+import graphene
+from django.core.exceptions import ValidationError
+from django.db import transaction
+
+from .....checkout import models as checkout_models
+from .....checkout.calculations import calculate_checkout_total_with_gift_cards
+from .....checkout.checkout_cleaner import (
+ clean_billing_address,
+ clean_checkout_shipping,
+)
+from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
+from .....checkout.utils import cancel_active_payments
+from .....core.error_codes import MetadataErrorCode
+from .....core.utils import get_client_ip
+from .....core.utils.url import validate_storefront_url
+from .....payment import StorePaymentMethod
+from .....payment.error_codes import PaymentErrorCode
+from .....payment.utils import create_payment, is_currency_supported
+from ....account.i18n import I18nMixin
+from ....checkout.mutations.utils import get_checkout
+from ....checkout.types import Checkout
+from ....core import ResolveInfo
+from ....core.descriptions import ADDED_IN_31, ADDED_IN_34, DEPRECATED_IN_3X_INPUT
+from ....core.doc_category import DOC_CATEGORY_CHECKOUT, DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.scalars import UUID, PositiveDecimal
+from ....core.types import BaseInputObjectType
+from ....core.types import common as common_types
+from ....meta.mutations import MetadataInput
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...enums import StorePaymentMethodEnum
+from ...types import Payment
+from ...utils import metadata_contains_empty_key
+
+
+class PaymentInput(BaseInputObjectType):
+ gateway = graphene.Field(
+ graphene.String,
+ description="A gateway to use with that payment.",
+ required=True,
+ )
+ token = graphene.String(
+ required=False,
+ description=(
+ "Client-side generated payment token, representing customer's "
+ "billing data in a secure manner."
+ ),
+ )
+ amount = PositiveDecimal(
+ required=False,
+ description=(
+ "Total amount of the transaction, including "
+ "all taxes and discounts. If no amount is provided, "
+ "the checkout total will be used."
+ ),
+ )
+ return_url = graphene.String(
+ required=False,
+ description=(
+ "URL of a storefront view where user should be redirected after "
+ "requiring additional actions. Payment with additional actions will not be "
+ "finished if this field is not provided."
+ ),
+ )
+ store_payment_method = StorePaymentMethodEnum(
+ description="Payment store type." + ADDED_IN_31,
+ required=False,
+ default_value=StorePaymentMethodEnum.NONE.name,
+ )
+ metadata = common_types.NonNullList(
+ MetadataInput,
+ description="User public metadata." + ADDED_IN_31,
+ required=False,
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class CheckoutPaymentCreate(BaseMutation, I18nMixin):
+ checkout = graphene.Field(Checkout, description="Related checkout object.")
+ payment = graphene.Field(Payment, description="A newly created payment.")
+
+ class Arguments:
+ id = graphene.ID(
+ description="The checkout's ID." + ADDED_IN_34,
+ required=False,
+ )
+ token = UUID(
+ description=f"Checkout token.{DEPRECATED_IN_3X_INPUT} Use `id` instead.",
+ required=False,
+ )
+ checkout_id = graphene.ID(
+ required=False,
+ description=(
+ f"The ID of the checkout. {DEPRECATED_IN_3X_INPUT} Use `id` instead."
+ ),
+ )
+ input = PaymentInput(
+ description="Data required to create a new payment.", required=True
+ )
+
+ class Meta:
+ description = "Create a new payment for given checkout."
+ doc_category = DOC_CATEGORY_CHECKOUT
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def clean_payment_amount(cls, info: ResolveInfo, checkout_total, amount):
+ if amount != checkout_total.gross.amount:
+ raise ValidationError(
+ {
+ "amount": ValidationError(
+ "Partial payments are not allowed, amount should be "
+ "equal checkout's total.",
+ code=PaymentErrorCode.PARTIAL_PAYMENT_NOT_ALLOWED.value,
+ )
+ }
+ )
+
+ @classmethod
+ def validate_gateway(cls, manager, gateway_id, checkout):
+ """Validate if given gateway can be used for this checkout.
+
+ Check if provided gateway is active and CONFIGURATION_PER_CHANNEL is True.
+ If CONFIGURATION_PER_CHANNEL is False then check if gateway has
+ defined currency.
+ """
+ payment_gateway = manager.get_plugin(gateway_id, checkout.channel.slug)
+
+ if not payment_gateway or not payment_gateway.active:
+ cls.raise_not_supported_gateway_error(gateway_id)
+
+ if not payment_gateway.CONFIGURATION_PER_CHANNEL:
+ if not is_currency_supported(checkout.currency, gateway_id, manager):
+ cls.raise_not_supported_gateway_error(gateway_id)
+
+ @classmethod
+ def raise_not_supported_gateway_error(cls, gateway_id: str):
+ raise ValidationError(
+ {
+ "gateway": ValidationError(
+ f"The gateway {gateway_id} is not available for this checkout.",
+ code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
+ )
+ }
+ )
+
+ @classmethod
+ def validate_token(cls, manager, gateway: str, input_data: dict, channel_slug: str):
+ token = input_data.get("token")
+ is_required = manager.token_is_required_as_payment_input(gateway, channel_slug)
+ if not token and is_required:
+ raise ValidationError(
+ {
+ "token": ValidationError(
+ f"Token is required for {gateway}.",
+ code=PaymentErrorCode.REQUIRED.value,
+ ),
+ }
+ )
+
+ @classmethod
+ def validate_return_url(cls, input_data):
+ return_url = input_data.get("return_url")
+ if not return_url:
+ return
+ try:
+ validate_storefront_url(return_url)
+ except ValidationError as error:
+ raise ValidationError(
+ {"redirect_url": error}, code=PaymentErrorCode.INVALID.value
+ )
+
+ @classmethod
+ def validate_metadata_keys(cls, metadata_list: List[dict]):
+ if metadata_contains_empty_key(metadata_list):
+ raise ValidationError(
+ {
+ "input": ValidationError(
+ {
+ "metadata": ValidationError(
+ "Metadata key cannot be empty.",
+ code=MetadataErrorCode.REQUIRED.value,
+ )
+ }
+ )
+ }
+ )
+
+ @staticmethod
+ def validate_checkout_email(checkout: "checkout_models.Checkout"):
+ if not checkout.email:
+ raise ValidationError(
+ "Checkout email must be set.",
+ code=PaymentErrorCode.CHECKOUT_EMAIL_NOT_SET.value,
+ )
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls,
+ _root,
+ info: ResolveInfo,
+ /,
+ *,
+ checkout_id=None,
+ id=None,
+ input,
+ token=None,
+ ):
+ checkout = get_checkout(cls, info, checkout_id=checkout_id, token=token, id=id)
+
+ cls.validate_checkout_email(checkout)
+
+ gateway = input["gateway"]
+
+ manager = get_plugin_manager_promise(info.context).get()
+ cls.validate_gateway(manager, gateway, checkout)
+ cls.validate_return_url(input)
+
+ lines, unavailable_variant_pks = fetch_checkout_lines(checkout)
+ if unavailable_variant_pks:
+ not_available_variants_ids = {
+ graphene.Node.to_global_id("ProductVariant", pk)
+ for pk in unavailable_variant_pks
+ }
+ raise ValidationError(
+ {
+ "token": ValidationError(
+ "Some of the checkout lines variants are unavailable.",
+ code=PaymentErrorCode.UNAVAILABLE_VARIANT_IN_CHANNEL.value,
+ params={"variants": not_available_variants_ids},
+ )
+ }
+ )
+ if not lines:
+ raise ValidationError(
+ {
+ "lines": ValidationError(
+ "Cannot create payment for checkout without lines.",
+ code=PaymentErrorCode.NO_CHECKOUT_LINES.value,
+ )
+ }
+ )
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+
+ cls.validate_token(
+ manager, gateway, input, channel_slug=checkout_info.channel.slug
+ )
+
+ address = (
+ checkout.shipping_address or checkout.billing_address
+ ) # FIXME: check which address we need here
+ checkout_total = calculate_checkout_total_with_gift_cards(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=address,
+ )
+ amount = input.get("amount", checkout_total.gross.amount)
+ clean_checkout_shipping(checkout_info, lines, PaymentErrorCode)
+ clean_billing_address(checkout_info, PaymentErrorCode)
+ cls.clean_payment_amount(info, checkout_total, amount)
+ extra_data = {
+ "customer_user_agent": info.context.META.get("HTTP_USER_AGENT"),
+ }
+
+ metadata = input.get("metadata")
+
+ if metadata is not None:
+ cls.validate_metadata_keys(metadata)
+ metadata = {data.key: data.value for data in metadata}
+
+ # The payment creation and deactivation of old payments should happened in the
+ # transaction to avoid creating multiple active payments.
+ with transaction.atomic():
+ # The checkout lock is used to prevent processing checkout completion
+ # and new payment creation. This kind of case could result in the missing
+ # payments, that were created for the checkout that was already converted
+ # to an order.
+ checkout = (
+ checkout_models.Checkout.objects.select_for_update()
+ .filter(pk=checkout_info.checkout.pk)
+ .first()
+ )
+
+ if not checkout:
+ raise ValidationError(
+ "Checkout doesn't exist anymore.",
+ code=PaymentErrorCode.NOT_FOUND.value,
+ )
+
+ cancel_active_payments(checkout)
+
+ payment = None
+ if amount != 0:
+ store_payment_method = (
+ input.get("store_payment_method") or StorePaymentMethod.NONE
+ )
+
+ payment = create_payment(
+ gateway=gateway,
+ payment_token=input.get("token", ""),
+ total=amount,
+ currency=checkout.currency,
+ email=checkout.get_customer_email(),
+ extra_data=extra_data,
+ # FIXME this is not a customer IP address.
+ # It is a client storefront ip
+ customer_ip_address=get_client_ip(info.context),
+ checkout=checkout,
+ return_url=input.get("return_url"),
+ store_payment_method=store_payment_method,
+ metadata=metadata,
+ )
+
+ return CheckoutPaymentCreate(payment=payment, checkout=checkout)
diff --git a/saleor/graphql/payment/mutations/payment/payment_capture.py b/saleor/graphql/payment/mutations/payment/payment_capture.py
new file mode 100644
index 0000000000..ab114b5a10
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_capture.py
@@ -0,0 +1,51 @@
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....payment import PaymentError, gateway
+from .....payment.error_codes import PaymentErrorCode
+from .....permission.enums import OrderPermissions
+from ....core import ResolveInfo
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.scalars import PositiveDecimal
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import Payment
+
+
+class PaymentCapture(BaseMutation):
+ payment = graphene.Field(Payment, description="Updated payment.")
+
+ class Arguments:
+ payment_id = graphene.ID(required=True, description="Payment ID.")
+ amount = PositiveDecimal(description="Transaction amount.")
+
+ class Meta:
+ description = "Captures the authorized payment amount."
+ doc_category = DOC_CATEGORY_PAYMENTS
+ permissions = (OrderPermissions.MANAGE_ORDERS,)
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, amount=None, payment_id
+ ):
+ payment = cls.get_node_or_error(
+ info, payment_id, field="payment_id", only_type=Payment
+ )
+ channel = payment.order.channel if payment.order else payment.checkout.channel
+ cls.check_channel_permissions(info, [channel.id])
+ channel_slug = channel.slug
+ manager = get_plugin_manager_promise(info.context).get()
+ try:
+ gateway.capture(
+ payment,
+ manager,
+ amount=amount,
+ channel_slug=channel_slug,
+ )
+ payment.refresh_from_db()
+ except PaymentError as e:
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
+ return PaymentCapture(payment=payment)
diff --git a/saleor/graphql/payment/mutations/payment/payment_check_balance.py b/saleor/graphql/payment/mutations/payment/payment_check_balance.py
new file mode 100644
index 0000000000..5a4213f465
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_check_balance.py
@@ -0,0 +1,111 @@
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....payment import PaymentError
+from .....payment.error_codes import PaymentErrorCode
+from .....payment.utils import is_currency_supported
+from ....channel.utils import validate_channel
+from ....core import ResolveInfo
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.fields import JSONString
+from ....core.mutations import BaseMutation
+from ....core.scalars import PositiveDecimal
+from ....core.types import BaseInputObjectType
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+
+
+class MoneyInput(graphene.InputObjectType):
+ currency = graphene.String(description="Currency code.", required=True)
+ amount = PositiveDecimal(description="Amount of money.", required=True)
+
+
+class CardInput(graphene.InputObjectType):
+ code = graphene.String(
+ description=(
+ "Payment method nonce, a token returned "
+ "by the appropriate provider's SDK."
+ ),
+ required=True,
+ )
+ cvc = graphene.String(description="Card security code.", required=False)
+ money = MoneyInput(
+ description="Information about currency and amount.", required=True
+ )
+
+
+class PaymentCheckBalanceInput(BaseInputObjectType):
+ gateway_id = graphene.types.String(
+ description="An ID of a payment gateway to check.", required=True
+ )
+ method = graphene.types.String(description="Payment method name.", required=True)
+ channel = graphene.String(
+ description="Slug of a channel for which the data should be returned.",
+ required=True,
+ )
+ card = CardInput(description="Information about card.", required=True)
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class PaymentCheckBalance(BaseMutation):
+ data = JSONString(description="Response from the gateway.")
+
+ class Arguments:
+ input = PaymentCheckBalanceInput(
+ description="Fields required to check payment balance.", required=True
+ )
+
+ class Meta:
+ description = "Check payment balance."
+ doc_category = DOC_CATEGORY_PAYMENTS
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def perform_mutation(cls, _root, info: ResolveInfo, /, **data):
+ manager = get_plugin_manager_promise(info.context).get()
+ gateway_id = data["input"]["gateway_id"]
+ money = data["input"]["card"].get("money", {})
+
+ cls.validate_gateway(gateway_id, manager)
+ cls.validate_currency(money.currency, gateway_id, manager)
+
+ channel = data["input"].pop("channel")
+ validate_channel(channel, PaymentErrorCode)
+
+ try:
+ data = manager.check_payment_balance(data["input"], channel)
+ except PaymentError as e:
+ raise ValidationError(
+ str(e), code=PaymentErrorCode.BALANCE_CHECK_ERROR.value
+ )
+
+ return PaymentCheckBalance(data=data)
+
+ @classmethod
+ def validate_gateway(cls, gateway_id, manager):
+ gateways_id = [gateway.id for gateway in manager.list_payment_gateways()]
+
+ if gateway_id not in gateways_id:
+ raise ValidationError(
+ {
+ "gateway_id": ValidationError(
+ f"The gateway_id {gateway_id} is not available.",
+ code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
+ )
+ }
+ )
+
+ @classmethod
+ def validate_currency(cls, currency, gateway_id, manager):
+ if not is_currency_supported(currency, gateway_id, manager):
+ raise ValidationError(
+ {
+ "currency": ValidationError(
+ f"The currency {currency} is not available for {gateway_id}.",
+ code=PaymentErrorCode.NOT_SUPPORTED_GATEWAY.value,
+ )
+ }
+ )
diff --git a/saleor/graphql/payment/mutations/payment/payment_gateway_initialize.py b/saleor/graphql/payment/mutations/payment/payment_gateway_initialize.py
new file mode 100644
index 0000000000..1d51d9f220
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_gateway_initialize.py
@@ -0,0 +1,149 @@
+import graphene
+
+from .....payment.interface import PaymentGatewayData
+from ....core.descriptions import ADDED_IN_313, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.enums import PaymentGatewayInitializeErrorCode
+from ....core.scalars import JSON, PositiveDecimal
+from ....core.types import BaseInputObjectType, BaseObjectType
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ..base import TransactionSessionBase
+
+
+class PaymentGatewayConfig(BaseObjectType):
+ id = graphene.String(required=True, description="The app identifier.")
+ data = graphene.Field(
+ JSON, description="The JSON data required to initialize the payment gateway."
+ )
+ errors = common_types.NonNullList(common_types.PaymentGatewayConfigError)
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class PaymentGatewayToInitialize(BaseInputObjectType):
+ id = graphene.String(
+ required=True,
+ description="The identifier of the payment gateway app to initialize.",
+ )
+ data = graphene.Field(
+ JSON, description="The data that will be passed to the payment gateway."
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class PaymentGatewayInitialize(TransactionSessionBase):
+ gateway_configs = common_types.NonNullList(PaymentGatewayConfig)
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the checkout or order.",
+ required=True,
+ )
+ amount = graphene.Argument(
+ PositiveDecimal,
+ description=(
+ "The amount requested for initializing the payment gateway. "
+ "If not provided, the difference between checkout.total - "
+ "transactions that are already processed will be send."
+ ),
+ )
+ payment_gateways = graphene.List(
+ graphene.NonNull(PaymentGatewayToInitialize),
+ description="List of payment gateways to initialize.",
+ required=False,
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+ description = (
+ "Initializes a payment gateway session. It triggers the webhook "
+ "`PAYMENT_GATEWAY_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
+ "If `paymentGateways` is not provided, the webhook will be send to all "
+ "subscribed payment gateways." + ADDED_IN_313 + PREVIEW_FEATURE
+ )
+ error_type_class = common_types.PaymentGatewayInitializeError
+
+ @classmethod
+ def prepare_response(
+ cls,
+ payment_gateways_input: list[PaymentGatewayData],
+ payment_gateways_response: list[PaymentGatewayData],
+ ) -> list[PaymentGatewayConfig]:
+ response = []
+ payment_gateways_response_dict = {
+ gateway.app_identifier: gateway for gateway in payment_gateways_response
+ }
+
+ payment_gateways_input_dict = (
+ {gateway.app_identifier: gateway for gateway in payment_gateways_input}
+ if payment_gateways_input
+ else payment_gateways_response_dict
+ )
+ for identifier in payment_gateways_input_dict:
+ app_identifier = identifier
+ payment_gateway_response = payment_gateways_response_dict.get(identifier)
+ if payment_gateway_response:
+ response_data = payment_gateway_response.data
+ errors = []
+ if payment_gateway_response.error:
+ code = common_types.PaymentGatewayConfigErrorCode.INVALID.value
+ errors = [
+ {
+ "field": "id",
+ "message": payment_gateway_response.error,
+ "code": code,
+ }
+ ]
+
+ else:
+ response_data = None
+ code = common_types.PaymentGatewayConfigErrorCode.NOT_FOUND.value
+ msg = (
+ "Active app with `HANDLE_PAYMENT` permissions or "
+ "app webhook not found."
+ )
+ errors = [
+ {
+ "field": "id",
+ "message": msg,
+ "code": code,
+ }
+ ]
+ data_to_return = response_data.get("data") if response_data else None
+ response.append(
+ PaymentGatewayConfig(
+ id=app_identifier, data=data_to_return, errors=errors
+ )
+ )
+ return response
+
+ @classmethod
+ def perform_mutation(cls, root, info, *, id, amount=None, payment_gateways=None):
+ manager = get_plugin_manager_promise(info.context).get()
+ source_object = cls.clean_source_object(
+ info,
+ id,
+ PaymentGatewayInitializeErrorCode.INVALID.value,
+ PaymentGatewayInitializeErrorCode.NOT_FOUND.value,
+ manager=manager,
+ )
+ payment_gateways_data = []
+ if payment_gateways:
+ payment_gateways_data = [
+ PaymentGatewayData(
+ app_identifier=gateway["id"], data=gateway.get("data")
+ )
+ for gateway in payment_gateways
+ ]
+ amount = cls.get_amount(source_object, amount)
+ response_data = manager.payment_gateway_initialize_session(
+ amount, payment_gateways_data, source_object
+ )
+ return cls(
+ gateway_configs=cls.prepare_response(payment_gateways_data, response_data),
+ errors=[],
+ )
diff --git a/saleor/graphql/payment/mutations/payment/payment_initialize.py b/saleor/graphql/payment/mutations/payment/payment_initialize.py
new file mode 100644
index 0000000000..c05dca991c
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_initialize.py
@@ -0,0 +1,82 @@
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....channel.models import Channel
+from .....payment import PaymentError
+from .....payment.error_codes import PaymentErrorCode
+from ....core import ResolveInfo
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.fields import JSONString
+from ....core.mutations import BaseMutation
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import PaymentInitialized
+
+
+class PaymentInitialize(BaseMutation):
+ initialized_payment = graphene.Field(PaymentInitialized, required=False)
+
+ class Arguments:
+ gateway = graphene.String(
+ description="A gateway name used to initialize the payment.",
+ required=True,
+ )
+ channel = graphene.String(
+ description="Slug of a channel for which the data should be returned.",
+ )
+ payment_data = JSONString(
+ required=False,
+ description=(
+ "Client-side generated data required to initialize the payment."
+ ),
+ )
+
+ class Meta:
+ description = "Initializes payment process when it is required by gateway."
+ doc_category = DOC_CATEGORY_PAYMENTS
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def validate_channel(cls, channel_slug):
+ try:
+ channel = Channel.objects.get(slug=channel_slug)
+ except Channel.DoesNotExist:
+ raise ValidationError(
+ {
+ "channel": ValidationError(
+ f"Channel with '{channel_slug}' slug does not exist.",
+ code=PaymentErrorCode.NOT_FOUND.value,
+ )
+ }
+ )
+ if not channel.is_active:
+ raise ValidationError(
+ {
+ "channel": ValidationError(
+ f"Channel with '{channel_slug}' is inactive.",
+ code=PaymentErrorCode.CHANNEL_INACTIVE.value,
+ )
+ }
+ )
+ return channel
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, channel, gateway, payment_data
+ ):
+ cls.validate_channel(channel_slug=channel)
+ manager = get_plugin_manager_promise(info.context).get()
+ try:
+ response = manager.initialize_payment(
+ gateway, payment_data, channel_slug=channel
+ )
+ except PaymentError as e:
+ raise ValidationError(
+ {
+ "payment_data": ValidationError(
+ str(e), code=PaymentErrorCode.INVALID.value
+ )
+ }
+ )
+ return PaymentInitialize(initialized_payment=response)
diff --git a/saleor/graphql/payment/mutations/payment/payment_refund.py b/saleor/graphql/payment/mutations/payment/payment_refund.py
new file mode 100644
index 0000000000..4942af03cc
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_refund.py
@@ -0,0 +1,67 @@
+from typing import cast
+
+from django.core.exceptions import ValidationError
+
+from .....order import models as order_models
+from .....order.actions import order_refunded
+from .....payment import PaymentError, TransactionKind, gateway
+from .....payment.error_codes import PaymentErrorCode
+from .....permission.enums import OrderPermissions
+from ....app.dataloaders import get_app_promise
+from ....core import ResolveInfo
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import Payment
+from .payment_capture import PaymentCapture
+
+
+class PaymentRefund(PaymentCapture):
+ class Meta:
+ description = "Refunds the captured payment amount."
+ doc_category = DOC_CATEGORY_PAYMENTS
+ permissions = (OrderPermissions.MANAGE_ORDERS,)
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, amount=None, payment_id
+ ):
+ app = get_app_promise(info.context).get()
+ user = info.context.user
+ manager = get_plugin_manager_promise(info.context).get()
+
+ payment = cls.get_node_or_error(
+ info, payment_id, field="payment_id", only_type=Payment
+ )
+ channel = payment.order.channel if payment.order else payment.checkout.channel
+ cls.check_channel_permissions(info, [channel.id])
+ channel_slug = channel.slug
+ manager = get_plugin_manager_promise(info.context).get()
+ payment_transaction = None
+ try:
+ payment_transaction = gateway.refund(
+ payment,
+ manager,
+ amount=amount,
+ channel_slug=channel_slug,
+ )
+ payment.refresh_from_db()
+ except PaymentError as e:
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
+ if (
+ payment.order_id
+ and payment_transaction
+ and payment_transaction.kind == TransactionKind.REFUND
+ ):
+ order = cast(order_models.Order, payment.order)
+ order_refunded(
+ order=order,
+ user=user,
+ app=app,
+ amount=amount,
+ payment=payment,
+ manager=manager,
+ )
+ return PaymentRefund(payment=payment)
diff --git a/saleor/graphql/payment/mutations/payment/payment_void.py b/saleor/graphql/payment/mutations/payment/payment_void.py
new file mode 100644
index 0000000000..f87513ffe0
--- /dev/null
+++ b/saleor/graphql/payment/mutations/payment/payment_void.py
@@ -0,0 +1,44 @@
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....payment import PaymentError, gateway
+from .....payment.error_codes import PaymentErrorCode
+from .....permission.enums import OrderPermissions
+from ....core import ResolveInfo
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import Payment
+
+
+class PaymentVoid(BaseMutation):
+ payment = graphene.Field(Payment, description="Updated payment.")
+
+ class Arguments:
+ payment_id = graphene.ID(required=True, description="Payment ID.")
+
+ class Meta:
+ description = "Voids the authorized payment."
+ doc_category = DOC_CATEGORY_PAYMENTS
+ permissions = (OrderPermissions.MANAGE_ORDERS,)
+ error_type_class = common_types.PaymentError
+ error_type_field = "payment_errors"
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls, _root, info: ResolveInfo, /, *, payment_id
+ ):
+ payment = cls.get_node_or_error(
+ info, payment_id, field="payment_id", only_type=Payment
+ )
+ channel = payment.order.channel if payment.order else payment.checkout.channel
+ cls.check_channel_permissions(info, [channel.id])
+ channel_slug = channel.slug
+ manager = get_plugin_manager_promise(info.context).get()
+ try:
+ gateway.void(payment, manager, channel_slug=channel_slug)
+ payment.refresh_from_db()
+ except PaymentError as e:
+ raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR.value)
+ return PaymentVoid(payment=payment)
diff --git a/saleor/graphql/payment/mutations/transaction/__init__.py b/saleor/graphql/payment/mutations/transaction/__init__.py
new file mode 100644
index 0000000000..81f72c1f05
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/__init__.py
@@ -0,0 +1,15 @@
+from .transaction_create import TransactionCreate
+from .transaction_event_report import TransactionEventReport
+from .transaction_initialize import TransactionInitialize
+from .transaction_process import TransactionProcess
+from .transaction_request_action import TransactionRequestAction
+from .transaction_update import TransactionUpdate
+
+__all__ = [
+ "TransactionCreate",
+ "TransactionEventReport",
+ "TransactionInitialize",
+ "TransactionProcess",
+ "TransactionRequestAction",
+ "TransactionUpdate",
+]
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_create.py b/saleor/graphql/payment/mutations/transaction/transaction_create.py
new file mode 100644
index 0000000000..e9a19aff19
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_create.py
@@ -0,0 +1,458 @@
+import uuid
+from decimal import Decimal
+from typing import Dict, List, Optional, Union, cast
+
+import graphene
+from django.core.exceptions import ValidationError
+from django.core.validators import URLValidator
+from django.db.models import Model
+
+from .....checkout import models as checkout_models
+from .....checkout.actions import transaction_amounts_for_checkout_updated
+from .....order import OrderStatus
+from .....order import models as order_models
+from .....order.actions import order_transaction_updated
+from .....order.events import transaction_event as order_transaction_event
+from .....order.fetch import fetch_order_info
+from .....order.search import update_order_search_vector
+from .....order.utils import updates_amounts_for_order
+from .....payment import TransactionEventType
+from .....payment import models as payment_models
+from .....payment.error_codes import TransactionCreateErrorCode
+from .....payment.transaction_item_calculations import recalculate_transaction_amounts
+from .....payment.utils import create_manual_adjustment_events
+from .....permission.enums import PaymentPermissions
+from ....app.dataloaders import get_app_promise
+from ....core import ResolveInfo
+from ....core.descriptions import (
+ ADDED_IN_34,
+ ADDED_IN_313,
+ PREVIEW_FEATURE,
+ PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT,
+)
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.types import BaseInputObjectType
+from ....core.types import common as common_types
+from ....meta.mutations import MetadataInput
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...enums import TransactionActionEnum, TransactionEventStatusEnum
+from ...types import TransactionItem
+from ...utils import metadata_contains_empty_key
+from ..payment.payment_check_balance import MoneyInput
+
+
+class TransactionCreateInput(BaseInputObjectType):
+ status = graphene.String(
+ description=(
+ "Status of the transaction."
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " The `status` is not needed. The amounts can be used to define "
+ "the current status of transactions."
+ ),
+ )
+ type = graphene.String(
+ description="Payment type used for this transaction."
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Use `name` and `message` instead.",
+ )
+ name = graphene.String(
+ description="Payment name of the transaction." + ADDED_IN_313
+ )
+ message = graphene.String(
+ description="The message of the transaction." + ADDED_IN_313
+ )
+
+ reference = graphene.String(
+ description=(
+ "Reference of the transaction. "
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Use `pspReference` instead."
+ )
+ )
+ psp_reference = graphene.String(
+ description=("PSP Reference of the transaction. " + ADDED_IN_313)
+ )
+ available_actions = graphene.List(
+ graphene.NonNull(TransactionActionEnum),
+ description="List of all possible actions for the transaction",
+ )
+ amount_authorized = MoneyInput(description="Amount authorized by this transaction.")
+ amount_charged = MoneyInput(description="Amount charged by this transaction.")
+ amount_refunded = MoneyInput(description="Amount refunded by this transaction.")
+ amount_voided = MoneyInput(
+ description="Amount voided by this transaction."
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Use `amountCanceled` instead."
+ )
+ amount_canceled = MoneyInput(
+ description="Amount canceled by this transaction." + ADDED_IN_313
+ )
+
+ metadata = graphene.List(
+ graphene.NonNull(MetadataInput),
+ description="Payment public metadata.",
+ required=False,
+ )
+ private_metadata = graphene.List(
+ graphene.NonNull(MetadataInput),
+ description="Payment private metadata.",
+ required=False,
+ )
+ external_url = graphene.String(
+ description=(
+ "The url that will allow to redirect user to "
+ "payment provider page with transaction event details." + ADDED_IN_313
+ )
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class TransactionEventInput(BaseInputObjectType):
+ status = graphene.Field(
+ TransactionEventStatusEnum,
+ required=False,
+ description="Current status of the payment transaction."
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Status will be calculated by Saleor.",
+ )
+ reference = graphene.String(
+ description=(
+ "Reference of the transaction. "
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Use `pspReference` instead."
+ )
+ )
+
+ psp_reference = graphene.String(
+ description=("PSP Reference related to this action." + ADDED_IN_313)
+ )
+ name = graphene.String(
+ description="Name of the transaction."
+ + PREVIEW_FEATURE_DEPRECATED_IN_313_INPUT
+ + " Use `message` instead. `name` field will be added to `message`."
+ )
+ message = graphene.String(
+ description="The message related to the event." + ADDED_IN_313
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class TransactionCreate(BaseMutation):
+ transaction = graphene.Field(TransactionItem)
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the checkout or order.",
+ required=True,
+ )
+ transaction = TransactionCreateInput(
+ required=True,
+ description="Input data required to create a new transaction object.",
+ )
+ transaction_event = TransactionEventInput(
+ description="Data that defines a transaction event."
+ )
+
+ class Meta:
+ description = (
+ "Create transaction for checkout or order." + ADDED_IN_34 + PREVIEW_FEATURE
+ )
+ doc_category = DOC_CATEGORY_PAYMENTS
+ error_type_class = common_types.TransactionCreateError
+ permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
+
+ @classmethod
+ def validate_external_url(cls, external_url: Optional[str], error_code: str):
+ if external_url is None:
+ return
+ validator = URLValidator()
+ try:
+ validator(external_url)
+ except ValidationError:
+ raise ValidationError(
+ {
+ "transaction": ValidationError(
+ "Invalid format of `externalUrl`.", code=error_code
+ )
+ }
+ )
+
+ @classmethod
+ def validate_metadata_keys( # type: ignore[override]
+ cls, metadata_list: List[dict], field_name, error_code
+ ):
+ if metadata_contains_empty_key(metadata_list):
+ raise ValidationError(
+ {
+ "transaction": ValidationError(
+ f"{field_name} key cannot be empty.",
+ code=error_code,
+ )
+ }
+ )
+
+ @classmethod
+ def get_money_data_from_input(cls, cleaned_data: dict) -> Dict[str, Decimal]:
+ money_data = {}
+ if amount_authorized := cleaned_data.pop("amount_authorized", None):
+ money_data["authorized_value"] = amount_authorized["amount"]
+ if amount_charged := cleaned_data.pop("amount_charged", None):
+ money_data["charged_value"] = amount_charged["amount"]
+ if amount_refunded := cleaned_data.pop("amount_refunded", None):
+ money_data["refunded_value"] = amount_refunded["amount"]
+
+ if amount_canceled := cleaned_data.pop("amount_canceled", None):
+ money_data["canceled_value"] = amount_canceled["amount"]
+ elif amount_voided := cleaned_data.pop("amount_voided", None):
+ money_data["canceled_value"] = amount_voided["amount"]
+ return money_data
+
+ @classmethod
+ def cleanup_metadata_data(cls, cleaned_data: dict):
+ if metadata := cleaned_data.pop("metadata", None):
+ cleaned_data["metadata"] = {data.key: data.value for data in metadata}
+ if private_metadata := cleaned_data.pop("private_metadata", None):
+ cleaned_data["private_metadata"] = {
+ data.key: data.value for data in private_metadata
+ }
+
+ @classmethod
+ def validate_instance(
+ cls, instance: Model, instance_id
+ ) -> Union[checkout_models.Checkout, order_models.Order]:
+ """Validate if provided instance is an order or checkout type."""
+ if not isinstance(instance, (checkout_models.Checkout, order_models.Order)):
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ f"Couldn't resolve to Checkout or Order: {instance_id}",
+ code=TransactionCreateErrorCode.NOT_FOUND.value,
+ )
+ }
+ )
+ return instance
+
+ @classmethod
+ def validate_money_input(
+ cls, transaction_data: dict, currency: str, error_code: str
+ ):
+ if not transaction_data:
+ return
+ money_input_fields = [
+ "amount_authorized",
+ "amount_charged",
+ "amount_refunded",
+ "amount_canceled",
+ "amount_voided",
+ ]
+ errors = {}
+ for money_field_name in money_input_fields:
+ field = transaction_data.get(money_field_name)
+ if not field:
+ continue
+ if field["currency"] != currency:
+ errors[money_field_name] = ValidationError(
+ f"Currency needs to be the same as for order: {currency}",
+ code=error_code,
+ )
+ if errors:
+ raise ValidationError(errors)
+
+ @classmethod
+ def validate_input(
+ cls, instance: Union[checkout_models.Checkout, order_models.Order], transaction
+ ) -> Union[checkout_models.Checkout, order_models.Order]:
+ currency = instance.currency
+
+ cls.validate_money_input(
+ transaction,
+ currency,
+ TransactionCreateErrorCode.INCORRECT_CURRENCY.value,
+ )
+ cls.validate_metadata_keys(
+ transaction.get("metadata", []),
+ field_name="metadata",
+ error_code=TransactionCreateErrorCode.METADATA_KEY_REQUIRED.value,
+ )
+ cls.validate_metadata_keys(
+ transaction.get("private_metadata", []),
+ field_name="privateMetadata",
+ error_code=TransactionCreateErrorCode.METADATA_KEY_REQUIRED.value,
+ )
+ cls.validate_external_url(
+ transaction.get("external_url"),
+ error_code=TransactionCreateErrorCode.INVALID.value,
+ )
+ return instance
+
+ @classmethod
+ def create_transaction(
+ cls, transaction_input: dict, user, app, save: bool = True
+ ) -> payment_models.TransactionItem:
+ cls.cleanup_metadata_data(transaction_input)
+
+ transaction_type = transaction_input.pop("type", None)
+ transaction_input["name"] = transaction_input.get("name", transaction_type)
+ reference = transaction_input.pop("reference", None)
+ transaction_input["psp_reference"] = transaction_input.get(
+ "psp_reference", reference
+ )
+ app_identifier = None
+ if app and app.identifier:
+ app_identifier = app.identifier
+ transaction = payment_models.TransactionItem(
+ token=uuid.uuid4(),
+ use_old_id=True,
+ **transaction_input,
+ user=user if user and user.is_authenticated else None,
+ app_identifier=app_identifier,
+ app=app,
+ )
+ if save:
+ transaction.save()
+ return transaction
+
+ @classmethod
+ def create_transaction_event(
+ cls,
+ transaction_event_input: dict,
+ transaction: payment_models.TransactionItem,
+ user,
+ app,
+ ) -> payment_models.TransactionEvent:
+ reference = transaction_event_input.pop("reference", None)
+ psp_reference = transaction_event_input.get("psp_reference", reference)
+ app_identifier = None
+ if app and app.identifier:
+ app_identifier = app.identifier
+ return transaction.events.create(
+ status=transaction_event_input.get("status"),
+ psp_reference=psp_reference,
+ message=cls.create_event_message(transaction_event_input),
+ transaction=transaction,
+ user=user if user and user.is_authenticated else None,
+ app_identifier=app_identifier,
+ app=app,
+ type=TransactionEventType.INFO,
+ currency=transaction.currency,
+ )
+
+ @classmethod
+ def create_event_message(cls, transaction_event: dict) -> str:
+ message = transaction_event.get("message")
+ name = transaction_event.get("name")
+ if message and name:
+ return message + " " + name
+ elif message:
+ return message
+ elif name:
+ return name
+ return ""
+
+ @classmethod
+ def update_order(
+ cls,
+ order: order_models.Order,
+ money_data: dict,
+ update_search_vector: bool = True,
+ ) -> None:
+ update_fields = []
+ if money_data:
+ updates_amounts_for_order(order, save=False)
+ update_fields.extend(
+ [
+ "total_authorized_amount",
+ "total_charged_amount",
+ "authorize_status",
+ "charge_status",
+ ]
+ )
+ if (
+ order.channel.automatically_confirm_all_new_orders
+ and order.status == OrderStatus.UNCONFIRMED
+ ):
+ order.status = OrderStatus.UNFULFILLED
+ update_fields.append("status")
+
+ if update_search_vector:
+ update_order_search_vector(order, save=False)
+ update_fields.append(
+ "search_vector",
+ )
+
+ if update_fields:
+ update_fields.append("updated_at")
+ order.save(update_fields=update_fields)
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls,
+ _root,
+ info: ResolveInfo,
+ /,
+ *,
+ id: str,
+ transaction: Dict,
+ transaction_event=None,
+ ):
+ order_or_checkout_instance = cls.get_node_or_error(info, id)
+ order_or_checkout_instance = cls.validate_instance(
+ order_or_checkout_instance, id
+ )
+ order_or_checkout_instance = cls.validate_input(
+ order_or_checkout_instance, transaction=transaction
+ )
+ transaction_data = {**transaction}
+ transaction_data["currency"] = order_or_checkout_instance.currency
+ app = get_app_promise(info.context).get()
+ user = info.context.user
+ manager = get_plugin_manager_promise(info.context).get()
+
+ if isinstance(order_or_checkout_instance, checkout_models.Checkout):
+ transaction_data["checkout_id"] = order_or_checkout_instance.pk
+ else:
+ transaction_data["order_id"] = order_or_checkout_instance.pk
+ if transaction_event:
+ reference = transaction_event.get("reference", None)
+ psp_reference = transaction_event.get("psp_reference", reference)
+ order_transaction_event(
+ order=order_or_checkout_instance,
+ user=user,
+ app=app,
+ reference=psp_reference,
+ status=transaction_event.get("status"),
+ message=cls.create_event_message(transaction_event),
+ )
+ money_data = cls.get_money_data_from_input(transaction_data)
+ new_transaction = cls.create_transaction(transaction_data, user=user, app=app)
+ if money_data:
+ create_manual_adjustment_events(
+ transaction=new_transaction, money_data=money_data, user=user, app=app
+ )
+ recalculate_transaction_amounts(new_transaction)
+ if transaction_data.get("order_id") and money_data:
+ order = cast(order_models.Order, new_transaction.order)
+ cls.update_order(order, money_data, update_search_vector=True)
+
+ order_info = fetch_order_info(order)
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=new_transaction,
+ manager=manager,
+ user=user,
+ app=app,
+ previous_authorized_value=Decimal(0),
+ previous_charged_value=Decimal(0),
+ previous_refunded_value=Decimal(0),
+ )
+ if transaction_data.get("checkout_id") and money_data:
+ transaction_amounts_for_checkout_updated(new_transaction, manager)
+
+ if transaction_event:
+ cls.create_transaction_event(transaction_event, new_transaction, user, app)
+ return TransactionCreate(transaction=new_transaction)
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_event_report.py b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
new file mode 100644
index 0000000000..297d830b65
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_event_report.py
@@ -0,0 +1,279 @@
+from typing import Optional, cast
+
+import graphene
+from django.core.exceptions import ValidationError
+from django.utils import timezone
+
+from .....app.models import App
+from .....checkout.actions import transaction_amounts_for_checkout_updated
+from .....core.exceptions import PermissionDenied
+from .....core.tracing import traced_atomic_transaction
+from .....order import models as order_models
+from .....order.actions import order_transaction_updated
+from .....order.fetch import fetch_order_info
+from .....order.search import update_order_search_vector
+from .....order.utils import updates_amounts_for_order
+from .....payment import TransactionEventType
+from .....payment import models as payment_models
+from .....payment.transaction_item_calculations import recalculate_transaction_amounts
+from .....payment.utils import (
+ authorization_success_already_exists,
+ create_failed_transaction_event,
+ get_already_existing_event,
+)
+from .....permission.auth_filters import AuthorizationFilters
+from .....permission.enums import PaymentPermissions
+from ....app.dataloaders import get_app_promise
+from ....core import ResolveInfo
+from ....core.descriptions import ADDED_IN_313, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.enums import TransactionEventReportErrorCode
+from ....core.mutations import ModelMutation
+from ....core.scalars import PositiveDecimal
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...enums import TransactionActionEnum, TransactionEventTypeEnum
+from ...types import TransactionEvent, TransactionItem
+from ...utils import check_if_requestor_has_access
+from .utils import get_transaction_item
+
+
+class TransactionEventReport(ModelMutation):
+ already_processed = graphene.Boolean(
+ description="Defines if the reported event hasn't been processed earlier."
+ )
+ transaction = graphene.Field(
+ TransactionItem, description="The transaction related to the reported event."
+ )
+ transaction_event = graphene.Field(
+ TransactionEvent,
+ description=(
+ "The event assigned to this report. if `alreadyProcessed` is set to `true`,"
+ " the previously processed event will be returned."
+ ),
+ )
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the transaction.",
+ required=True,
+ )
+ psp_reference = graphene.String(
+ description="PSP Reference of the event to report.", required=True
+ )
+ type = graphene.Argument(
+ TransactionEventTypeEnum,
+ required=True,
+ description="Current status of the event to report.",
+ )
+ amount = PositiveDecimal(
+ description="The amount of the event to report.", required=True
+ )
+ time = graphene.DateTime(
+ description=(
+ "The time of the event to report. If not provide, "
+ "the current time will be used."
+ )
+ )
+ external_url = graphene.String(
+ description=(
+ "The url that will allow to redirect user to "
+ "payment provider page with event details."
+ )
+ )
+ message = graphene.String(description="The message related to the event.")
+ available_actions = graphene.List(
+ graphene.NonNull(TransactionActionEnum),
+ description="List of all possible actions for the transaction",
+ )
+
+ class Meta:
+ description = (
+ "Report the event for the transaction."
+ + ADDED_IN_313
+ + PREVIEW_FEATURE
+ + "\n\nRequires the following permissions: "
+ + f"{AuthorizationFilters.OWNER.name} "
+ + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
+ f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
+ f"Staff user cannot update a transaction that is owned by the app."
+ )
+ error_type_class = common_types.TransactionEventReportError
+ permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
+ doc_category = DOC_CATEGORY_PAYMENTS
+ model = payment_models.TransactionEvent
+ object_type = TransactionEvent
+ auto_permission_message = False
+
+ @classmethod
+ def _update_mutation_arguments_and_fields(cls, arguments, fields):
+ cls._meta.arguments.update(arguments)
+
+ @classmethod
+ def update_transaction(
+ cls,
+ transaction: payment_models.TransactionItem,
+ available_actions: Optional[list[str]] = None,
+ app: Optional["App"] = None,
+ ):
+ fields_to_update = [
+ "authorized_value",
+ "charged_value",
+ "refunded_value",
+ "canceled_value",
+ "authorize_pending_value",
+ "charge_pending_value",
+ "refund_pending_value",
+ "cancel_pending_value",
+ ]
+ if available_actions is not None:
+ transaction.available_actions = available_actions
+ fields_to_update.append("available_actions")
+
+ recalculate_transaction_amounts(transaction, save=False)
+ transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
+ if app and not transaction.user_id and not transaction_has_assigned_app:
+ transaction.app_id = app.pk
+ transaction.app_identifier = app.identifier
+ fields_to_update.append("app")
+ fields_to_update.append("app_identifier")
+ transaction.save(update_fields=fields_to_update)
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls,
+ root,
+ info: ResolveInfo,
+ /,
+ *,
+ id,
+ psp_reference,
+ type,
+ amount,
+ time=None,
+ external_url=None,
+ message=None,
+ available_actions=None,
+ ):
+ user = info.context.user
+ app = get_app_promise(info.context).get()
+ manager = get_plugin_manager_promise(info.context).get()
+
+ transaction = get_transaction_item(id)
+
+ if not check_if_requestor_has_access(
+ transaction=transaction, user=user, app=app
+ ):
+ raise PermissionDenied(
+ permissions=[
+ AuthorizationFilters.OWNER,
+ PaymentPermissions.HANDLE_PAYMENTS,
+ ]
+ )
+
+ app_identifier = None
+ if app and app.identifier:
+ app_identifier = app.identifier
+
+ transaction_event_data = {
+ "psp_reference": psp_reference,
+ "type": type,
+ "amount_value": amount,
+ "currency": transaction.currency,
+ "created_at": time or timezone.now(),
+ "external_url": external_url or "",
+ "message": message or "",
+ "transaction": transaction,
+ "app_identifier": app_identifier,
+ "app": app,
+ "user": user,
+ "include_in_calculations": True,
+ }
+ transaction_event = cls.get_instance(info, **transaction_event_data)
+ transaction_event = cast(payment_models.TransactionEvent, transaction_event)
+ transaction_event = cls.construct_instance(
+ transaction_event, transaction_event_data
+ )
+
+ cls.clean_instance(info, transaction_event)
+ already_processed = False
+ error_code = None
+ error_msg = None
+ error_field = None
+ with traced_atomic_transaction():
+ # The mutation can be called multiple times by the app. That can cause a
+ # thread race. We need to be sure, that we will always create a single event
+ # on our side for specific action.
+ existing_event = get_already_existing_event(transaction_event)
+ if existing_event and existing_event.amount != transaction_event.amount:
+ error_code = TransactionEventReportErrorCode.INCORRECT_DETAILS.value
+ error_msg = (
+ "The transaction with provided `pspReference` and "
+ "`type` already exists with different amount."
+ )
+ error_field = "pspReference"
+ elif existing_event:
+ already_processed = True
+ transaction_event = existing_event
+ elif (
+ transaction_event.type == TransactionEventType.AUTHORIZATION_SUCCESS
+ and authorization_success_already_exists(transaction.pk)
+ ):
+ error_code = TransactionEventReportErrorCode.ALREADY_EXISTS.value
+ error_msg = (
+ "Event with `AUTHORIZATION_SUCCESS` already "
+ "reported for the transaction. Use "
+ "`AUTHORIZATION_ADJUSTMENT` to change the "
+ "authorization amount."
+ )
+ error_field = "type"
+ else:
+ transaction_event.save()
+
+ if error_msg and error_code and error_field:
+ create_failed_transaction_event(transaction_event, cause=error_msg)
+ raise ValidationError({error_field: ValidationError(error_msg, error_code)})
+ if not already_processed:
+ previous_authorized_value = transaction.authorized_value
+ previous_charged_value = transaction.charged_value
+ previous_refunded_value = transaction.refunded_value
+ cls.update_transaction(
+ transaction,
+ available_actions=available_actions,
+ app=app,
+ )
+ if transaction.order_id:
+ order = cast(order_models.Order, transaction.order)
+ update_order_search_vector(order, save=False)
+ updates_amounts_for_order(order, save=False)
+ order.save(
+ update_fields=[
+ "total_charged_amount",
+ "charge_status",
+ "updated_at",
+ "total_authorized_amount",
+ "authorize_status",
+ "search_vector",
+ ]
+ )
+ order_info = fetch_order_info(order)
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=transaction,
+ manager=manager,
+ user=user,
+ app=app,
+ previous_authorized_value=previous_authorized_value,
+ previous_charged_value=previous_charged_value,
+ previous_refunded_value=previous_refunded_value,
+ )
+ if transaction.checkout_id:
+ manager = get_plugin_manager_promise(info.context).get()
+ transaction_amounts_for_checkout_updated(transaction, manager)
+
+ return cls(
+ already_processed=already_processed,
+ transaction=transaction,
+ transaction_event=transaction_event,
+ errors=[],
+ )
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_initialize.py b/saleor/graphql/payment/mutations/transaction/transaction_initialize.py
new file mode 100644
index 0000000000..4489abfc4b
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_initialize.py
@@ -0,0 +1,127 @@
+from typing import Optional
+
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....app.models import App
+from .....channel.models import Channel
+from .....core.exceptions import PermissionDenied
+from .....payment.interface import PaymentGatewayData
+from .....payment.utils import handle_transaction_initialize_session
+from .....permission.enums import PaymentPermissions
+from ....app.dataloaders import get_app_promise
+from ....channel.enums import TransactionFlowStrategyEnum
+from ....core.descriptions import ADDED_IN_313, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.enums import TransactionInitializeErrorCode
+from ....core.scalars import JSON, PositiveDecimal
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import TransactionEvent, TransactionItem
+from ..base import TransactionSessionBase
+from ..payment.payment_gateway_initialize import PaymentGatewayToInitialize
+
+
+class TransactionInitialize(TransactionSessionBase):
+ transaction = graphene.Field(
+ TransactionItem, description="The initialized transaction."
+ )
+ transaction_event = graphene.Field(
+ TransactionEvent,
+ description="The event created for the initialized transaction.",
+ )
+ data = graphene.Field(
+ JSON, description="The JSON data required to finalize the payment."
+ )
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the checkout or order.",
+ required=True,
+ )
+ amount = graphene.Argument(
+ PositiveDecimal,
+ description=(
+ "The amount requested for initializing the payment gateway. "
+ "If not provided, the difference between checkout.total - "
+ "transactions that are already processed will be send."
+ ),
+ )
+ action = graphene.Argument(
+ TransactionFlowStrategyEnum,
+ description=(
+ "The expected action called for the transaction. By default, the "
+ "`channel.defaultTransactionFlowStrategy` will be used. The field "
+ "can be used only by app that has `HANDLE_PAYMENTS` permission."
+ ),
+ )
+ payment_gateway = graphene.Argument(
+ PaymentGatewayToInitialize,
+ description="Payment gateway used to initialize the transaction.",
+ required=True,
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+ description = (
+ "Initializes a transaction session. It triggers the webhook "
+ "`TRANSACTION_INITIALIZE_SESSION`, to the requested `paymentGateways`. "
+ + ADDED_IN_313
+ + PREVIEW_FEATURE
+ )
+ error_type_class = common_types.TransactionInitializeError
+
+ @classmethod
+ def clean_action(cls, info, action: Optional[str], channel: "Channel"):
+ if not action:
+ return channel.default_transaction_flow_strategy
+ app = get_app_promise(info.context).get()
+ if not app or not app.has_perm(PaymentPermissions.HANDLE_PAYMENTS):
+ raise PermissionDenied(permissions=[PaymentPermissions.HANDLE_PAYMENTS])
+ return action
+
+ @classmethod
+ def clean_app_from_payment_gateway(cls, payment_gateway: PaymentGatewayData) -> App:
+ app = App.objects.filter(identifier=payment_gateway.app_identifier).first()
+ if not app:
+ raise ValidationError(
+ {
+ "payment_gateway": ValidationError(
+ message="App with provided identifier not found.",
+ code=TransactionInitializeErrorCode.NOT_FOUND.value,
+ )
+ }
+ )
+ return app
+
+ @classmethod
+ def perform_mutation(
+ cls, root, info, *, id, payment_gateway, amount=None, action=None
+ ):
+ manager = get_plugin_manager_promise(info.context).get()
+ payment_gateway_data = PaymentGatewayData(
+ app_identifier=payment_gateway["id"], data=payment_gateway.get("data")
+ )
+ source_object = cls.clean_source_object(
+ info,
+ id,
+ TransactionInitializeErrorCode.INVALID.value,
+ TransactionInitializeErrorCode.NOT_FOUND.value,
+ manager=manager,
+ )
+ action = cls.clean_action(info, action, source_object.channel)
+
+ amount = cls.get_amount(
+ source_object,
+ amount,
+ )
+ app = cls.clean_app_from_payment_gateway(payment_gateway_data)
+ transaction, event, data = handle_transaction_initialize_session(
+ source_object=source_object,
+ payment_gateway=payment_gateway_data,
+ amount=amount,
+ action=action,
+ app=app,
+ manager=manager,
+ )
+ return cls(transaction=transaction, transaction_event=event, data=data)
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_process.py b/saleor/graphql/payment/mutations/transaction/transaction_process.py
new file mode 100644
index 0000000000..5a6b59387d
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_process.py
@@ -0,0 +1,185 @@
+from typing import TYPE_CHECKING, Optional, Union, cast
+
+import graphene
+from django.core.exceptions import ValidationError
+from django.db.models import QuerySet
+
+from .....app.models import App
+from .....channel import TransactionFlowStrategy
+from .....channel.models import Channel
+from .....checkout import models as checkout_models
+from .....order import models as order_models
+from .....payment import TransactionEventType
+from .....payment import models as payment_models
+from .....payment.error_codes import TransactionProcessErrorCode
+from .....payment.interface import PaymentGatewayData
+from .....payment.utils import (
+ get_final_session_statuses,
+ handle_transaction_process_session,
+)
+from ....core.descriptions import ADDED_IN_313, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.scalars import JSON
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import TransactionEvent, TransactionItem
+
+if TYPE_CHECKING:
+ pass
+
+
+class TransactionProcess(BaseMutation):
+ transaction = graphene.Field(
+ TransactionItem, description="The processed transaction."
+ )
+ transaction_event = graphene.Field(
+ TransactionEvent,
+ description="The event created for the processed transaction.",
+ )
+ data = graphene.Field(
+ JSON, description="The json data required to finalize the payment."
+ )
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the transaction to process.",
+ required=True,
+ )
+ data = graphene.Argument(
+ JSON, description="The data that will be passed to the payment gateway."
+ )
+
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+ description = (
+ "Processes a transaction session. It triggers the webhook "
+ "`TRANSACTION_PROCESS_SESSION`, to the assigned `paymentGateways`. "
+ + ADDED_IN_313
+ + PREVIEW_FEATURE
+ )
+ error_type_class = common_types.TransactionProcessError
+
+ @classmethod
+ def get_action(cls, event: payment_models.TransactionEvent, channel: "Channel"):
+ if event.type == TransactionEventType.AUTHORIZATION_REQUEST:
+ return TransactionFlowStrategy.AUTHORIZATION
+ elif event.type == TransactionEventType.CHARGE_REQUEST:
+ return TransactionFlowStrategy.CHARGE
+
+ return channel.default_transaction_flow_strategy
+
+ @classmethod
+ def get_source_object(
+ cls, transaction_item: payment_models.TransactionItem
+ ) -> Union[checkout_models.Checkout, order_models.Order]:
+ if transaction_item.checkout_id:
+ checkout = cast(checkout_models.Checkout, transaction_item.checkout)
+ return checkout
+ if transaction_item.order_id:
+ order = cast(order_models.Order, transaction_item.order)
+ return order
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Transaction doesn't have attached order or checkout.",
+ code=TransactionProcessErrorCode.INVALID.value,
+ )
+ }
+ )
+
+ @classmethod
+ def get_request_event(cls, events: QuerySet) -> payment_models.TransactionEvent:
+ """Get event with details of requested action.
+
+ This searches for a request event with the appropriate type and
+ include_in_calculations set to false. Request events created from
+ transactionInitialize have their include_in_calculation set to false by default.
+ """
+ for event in events:
+ if (
+ event.type
+ in [
+ TransactionEventType.AUTHORIZATION_REQUEST,
+ TransactionEventType.CHARGE_REQUEST,
+ ]
+ and not event.include_in_calculations
+ ):
+ return event
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Missing call of `transactionInitialize` mutation.",
+ code=TransactionProcessErrorCode.INVALID.value,
+ )
+ }
+ )
+
+ @classmethod
+ def get_already_processed_event(cls, events) -> Optional[TransactionEvent]:
+ for event in events:
+ if (
+ event.type in get_final_session_statuses()
+ and event.include_in_calculations
+ ):
+ return event
+ return None
+
+ @classmethod
+ def clean_payment_app(cls, transaction_item: payment_models.TransactionItem) -> App:
+ if not transaction_item.app_identifier:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Transaction doesn't have attached app that could process the "
+ "request.",
+ code=TransactionProcessErrorCode.MISSING_PAYMENT_APP_RELATION.value,
+ )
+ }
+ )
+ app = App.objects.filter(identifier=transaction_item.app_identifier).first()
+ if not app:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ "Payment app attached to the transaction, doesn't exist.",
+ code=TransactionProcessErrorCode.MISSING_PAYMENT_APP.value,
+ )
+ }
+ )
+ return app
+
+ @classmethod
+ def perform_mutation(cls, root, info, *, id, data=None):
+ transaction_item = cls.get_node_or_error(
+ info, id, only_type="TransactionItem", field="token"
+ )
+ transaction_item = cast(payment_models.TransactionItem, transaction_item)
+ events = transaction_item.events.all()
+ if processed_event := cls.get_already_processed_event(events):
+ return cls(
+ transaction=transaction_item,
+ transaction_event=processed_event,
+ data=None,
+ )
+ request_event = cls.get_request_event(events)
+ source_object = cls.get_source_object(transaction_item)
+ app = cls.clean_payment_app(transaction_item)
+ app_identifier = app.identifier
+ app_identifier = cast(str, app_identifier)
+ action = cls.get_action(request_event, source_object.channel)
+ manager = get_plugin_manager_promise(info.context).get()
+ event, data = handle_transaction_process_session(
+ transaction_item=transaction_item,
+ source_object=source_object,
+ payment_gateway=PaymentGatewayData(
+ app_identifier=app_identifier, data=data
+ ),
+ app=app,
+ action=action,
+ manager=manager,
+ request_event=request_event,
+ )
+
+ transaction_item.refresh_from_db()
+ return cls(transaction=transaction_item, transaction_event=event, data=data)
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_request_action.py b/saleor/graphql/payment/mutations/transaction/transaction_request_action.py
new file mode 100644
index 0000000000..f96d918774
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_request_action.py
@@ -0,0 +1,176 @@
+from decimal import Decimal
+from typing import TYPE_CHECKING, Optional, cast
+
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....app.models import App
+from .....order.models import Order
+from .....payment import PaymentError, TransactionAction, TransactionEventType
+from .....payment.error_codes import TransactionRequestActionErrorCode
+from .....payment.gateway import (
+ request_cancelation_action,
+ request_charge_action,
+ request_refund_action,
+)
+from .....permission.enums import PaymentPermissions
+from ....app.dataloaders import get_app_promise
+from ....checkout.types import Checkout
+from ....core import ResolveInfo
+from ....core.descriptions import ADDED_IN_34, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.mutations import BaseMutation
+from ....core.scalars import PositiveDecimal
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ....utils import get_user_or_app_from_context
+from ...enums import TransactionActionEnum
+from ...types import TransactionItem
+from .utils import get_transaction_item
+
+if TYPE_CHECKING:
+ from .....account.models import User
+
+
+class TransactionRequestAction(BaseMutation):
+ transaction = graphene.Field(TransactionItem)
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the transaction.",
+ required=True,
+ )
+ action_type = graphene.Argument(
+ TransactionActionEnum,
+ required=True,
+ description="Determines the action type.",
+ )
+ amount = PositiveDecimal(
+ description=(
+ "Transaction request amount. If empty for refund or capture, maximal "
+ "possible amount will be used."
+ )
+ )
+
+ class Meta:
+ description = (
+ "Request an action for payment transaction." + ADDED_IN_34 + PREVIEW_FEATURE
+ )
+ doc_category = DOC_CATEGORY_PAYMENTS
+ error_type_class = common_types.TransactionRequestActionError
+ permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
+
+ @classmethod
+ def check_permissions(cls, context, permissions=None, **data):
+ required_permissions = permissions or cls._meta.permissions
+ requestor = get_user_or_app_from_context(context)
+ for required_permission in required_permissions:
+ # We want to allow to call this mutation for requestor with one of following
+ # permission: manage_orders, handle_payments
+ if requestor and requestor.has_perm(required_permission):
+ return True
+ return False
+
+ @classmethod
+ def handle_transaction_action(
+ cls,
+ action,
+ action_kwargs,
+ action_value: Optional[Decimal],
+ user: Optional["User"],
+ app: Optional[App],
+ ):
+ if action == TransactionAction.VOID or action == TransactionAction.CANCEL:
+ transaction = action_kwargs["transaction"]
+ request_event = cls.create_transaction_event_requested(
+ transaction, 0, action, user=user, app=app
+ )
+ request_cancelation_action(
+ **action_kwargs,
+ cancel_value=action_value,
+ request_event=request_event,
+ action=action,
+ )
+ elif action == TransactionAction.CHARGE:
+ transaction = action_kwargs["transaction"]
+ action_value = action_value or transaction.authorized_value
+ action_value = min(action_value, transaction.authorized_value)
+ request_event = cls.create_transaction_event_requested(
+ transaction, action_value, TransactionAction.CHARGE, user=user, app=app
+ )
+ request_charge_action(
+ **action_kwargs, charge_value=action_value, request_event=request_event
+ )
+ elif action == TransactionAction.REFUND:
+ transaction = action_kwargs["transaction"]
+ action_value = action_value or transaction.charged_value
+ action_value = min(action_value, transaction.charged_value)
+ request_event = cls.create_transaction_event_requested(
+ transaction, action_value, TransactionAction.REFUND, user=user, app=app
+ )
+ request_refund_action(
+ **action_kwargs, refund_value=action_value, request_event=request_event
+ )
+
+ @classmethod
+ def create_transaction_event_requested(
+ cls, transaction, action_value, action, user=None, app=None
+ ):
+ if action in (TransactionAction.CANCEL, TransactionAction.VOID):
+ type = TransactionEventType.CANCEL_REQUEST
+ elif action == TransactionAction.CHARGE:
+ type = TransactionEventType.CHARGE_REQUEST
+ elif action == TransactionAction.REFUND:
+ type = TransactionEventType.REFUND_REQUEST
+ else:
+ raise ValidationError(
+ {
+ "actionType": ValidationError(
+ "Incorrect action.",
+ code=TransactionRequestActionErrorCode.INVALID.value,
+ )
+ }
+ )
+ return transaction.events.create(
+ amount_value=action_value,
+ currency=transaction.currency,
+ type=type,
+ user=user,
+ app=app,
+ app_identifier=app.identifier if app else None,
+ )
+
+ @classmethod
+ def perform_mutation(cls, root, info: ResolveInfo, /, **data):
+ id = data["id"]
+ action_type = data["action_type"]
+ action_value = data.get("amount")
+ transaction = get_transaction_item(id)
+ if transaction.order_id:
+ order = cast(Order, transaction.order)
+ channel = order.channel
+ else:
+ checkout = cast(Checkout, transaction.checkout)
+ channel = checkout.channel
+ cls.check_channel_permissions(info, [channel.id])
+ channel_slug = channel.slug
+ user = info.context.user
+ app = get_app_promise(info.context).get()
+ manager = get_plugin_manager_promise(info.context).get()
+ action_kwargs = {
+ "channel_slug": channel_slug,
+ "user": user,
+ "app": app,
+ "transaction": transaction,
+ "manager": manager,
+ }
+
+ try:
+ cls.handle_transaction_action(
+ action_type, action_kwargs, action_value, user, app
+ )
+ except PaymentError as e:
+ error_enum = TransactionRequestActionErrorCode
+ code = error_enum.MISSING_TRANSACTION_ACTION_REQUEST_WEBHOOK.value
+ raise ValidationError(str(e), code=code)
+ return TransactionRequestAction(transaction=transaction)
diff --git a/saleor/graphql/payment/mutations/transaction/transaction_update.py b/saleor/graphql/payment/mutations/transaction/transaction_update.py
new file mode 100644
index 0000000000..789357cf52
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/transaction_update.py
@@ -0,0 +1,249 @@
+from typing import TYPE_CHECKING, Optional, cast
+
+import graphene
+from django.core.exceptions import ValidationError
+
+from .....app.models import App
+from .....checkout.actions import transaction_amounts_for_checkout_updated
+from .....core.exceptions import PermissionDenied
+from .....order import models as order_models
+from .....order.actions import order_transaction_updated
+from .....order.events import transaction_event as order_transaction_event
+from .....order.fetch import fetch_order_info
+from .....payment import models as payment_models
+from .....payment.error_codes import (
+ TransactionCreateErrorCode,
+ TransactionUpdateErrorCode,
+)
+from .....payment.transaction_item_calculations import (
+ calculate_transaction_amount_based_on_events,
+ recalculate_transaction_amounts,
+)
+from .....payment.utils import create_manual_adjustment_events
+from .....permission.auth_filters import AuthorizationFilters
+from .....permission.enums import PaymentPermissions
+from ....app.dataloaders import get_app_promise
+from ....core import ResolveInfo
+from ....core.descriptions import ADDED_IN_34, PREVIEW_FEATURE
+from ....core.doc_category import DOC_CATEGORY_PAYMENTS
+from ....core.types import common as common_types
+from ....plugins.dataloaders import get_plugin_manager_promise
+from ...types import TransactionItem
+from ...utils import check_if_requestor_has_access
+from .transaction_create import (
+ TransactionCreate,
+ TransactionCreateInput,
+ TransactionEventInput,
+)
+from .utils import get_transaction_item
+
+if TYPE_CHECKING:
+ from .....account.models import User
+
+
+class TransactionUpdateInput(TransactionCreateInput):
+ class Meta:
+ doc_category = DOC_CATEGORY_PAYMENTS
+
+
+class TransactionUpdate(TransactionCreate):
+ transaction = graphene.Field(TransactionItem)
+
+ class Arguments:
+ id = graphene.ID(
+ description="The ID of the transaction.",
+ required=True,
+ )
+ transaction = TransactionUpdateInput(
+ description="Input data required to create a new transaction object.",
+ )
+ transaction_event = TransactionEventInput(
+ description="Data that defines a transaction transaction."
+ )
+
+ class Meta:
+ auto_permission_message = False
+ description = (
+ "Update transaction."
+ + ADDED_IN_34
+ + PREVIEW_FEATURE
+ + "\n\nRequires the following permissions: "
+ + f"{AuthorizationFilters.OWNER.name} "
+ + f"and {PaymentPermissions.HANDLE_PAYMENTS.name} for apps, "
+ f"{PaymentPermissions.HANDLE_PAYMENTS.name} for staff users. "
+ f"Staff user cannot update a transaction that is owned by the app."
+ )
+ doc_category = DOC_CATEGORY_PAYMENTS
+ error_type_class = common_types.TransactionUpdateError
+ permissions = (PaymentPermissions.HANDLE_PAYMENTS,)
+ object_type = TransactionItem
+
+ @classmethod
+ def check_can_update(
+ cls,
+ transaction: payment_models.TransactionItem,
+ user: Optional["User"],
+ app: Optional["App"],
+ ):
+ if not check_if_requestor_has_access(
+ transaction=transaction, user=user, app=app
+ ):
+ raise PermissionDenied(
+ permissions=[
+ AuthorizationFilters.OWNER,
+ PaymentPermissions.HANDLE_PAYMENTS,
+ ]
+ )
+
+ @classmethod
+ def validate_transaction_input(
+ cls, instance: payment_models.TransactionItem, transaction_data
+ ):
+ currency = instance.currency
+ cls.validate_money_input(
+ transaction_data,
+ currency,
+ TransactionUpdateErrorCode.INCORRECT_CURRENCY.value,
+ )
+ cls.validate_metadata_keys(
+ transaction_data.get("metadata", []),
+ field_name="metadata",
+ error_code=TransactionUpdateErrorCode.METADATA_KEY_REQUIRED.value,
+ )
+ cls.validate_metadata_keys(
+ transaction_data.get("private_metadata", []),
+ field_name="privateMetadata",
+ error_code=TransactionUpdateErrorCode.METADATA_KEY_REQUIRED.value,
+ )
+ cls.validate_external_url(
+ transaction_data.get("external_url"),
+ error_code=TransactionCreateErrorCode.INVALID.value,
+ )
+
+ @classmethod
+ def update_transaction(
+ cls,
+ instance: payment_models.TransactionItem,
+ transaction_data: dict,
+ money_data: dict,
+ user: Optional["User"],
+ app: Optional["App"],
+ ):
+ psp_reference = transaction_data.get(
+ "psp_reference", transaction_data.pop("reference", None)
+ )
+ if psp_reference and instance.psp_reference != psp_reference:
+ if payment_models.TransactionItem.objects.filter(
+ psp_reference=psp_reference
+ ).exists():
+ raise ValidationError(
+ {
+ "transaction": ValidationError(
+ "Transaction with provided `pspReference` already exists.",
+ code=TransactionUpdateErrorCode.UNIQUE.value,
+ )
+ }
+ )
+ transaction_data["name"] = transaction_data.get(
+ "name", transaction_data.pop("type", None)
+ )
+ transaction_data["psp_reference"] = psp_reference
+ instance = cls.construct_instance(instance, transaction_data)
+ instance.save()
+ if money_data:
+ calculate_transaction_amount_based_on_events(transaction=instance)
+ create_manual_adjustment_events(
+ transaction=instance, money_data=money_data, user=user, app=app
+ )
+ recalculate_transaction_amounts(instance)
+
+ @classmethod
+ def assign_app_to_transaction_data_if_missing(
+ cls,
+ transaction: payment_models.TransactionItem,
+ transaction_data: dict,
+ app: Optional["App"],
+ ):
+ """Assign app to transaction if missing.
+
+ TransactionItem created before 3.13, doesn't have a relation to the owner app.
+ When app updates a transaction, we need to assign the app to the transaction.
+ """
+ transaction_has_assigned_app = transaction.app_id or transaction.app_identifier
+ if app and not transaction.user_id and not transaction_has_assigned_app:
+ transaction_data["app"] = app
+ transaction_data["app_identifier"] = app.identifier
+
+ @classmethod
+ def perform_mutation( # type: ignore[override]
+ cls,
+ _root,
+ info: ResolveInfo,
+ /,
+ *,
+ id: str,
+ transaction=None,
+ transaction_event=None,
+ ):
+ app = get_app_promise(info.context).get()
+ user = info.context.user
+ instance = get_transaction_item(id)
+ manager = get_plugin_manager_promise(info.context).get()
+
+ cls.check_can_update(
+ transaction=instance,
+ user=user if user and user.is_authenticated else None,
+ app=app,
+ )
+ money_data = {}
+ previous_transaction_psp_reference = instance.psp_reference
+ previous_authorized_value = instance.authorized_value
+ previous_charged_value = instance.charged_value
+ previous_refunded_value = instance.refunded_value
+
+ if transaction:
+ cls.validate_transaction_input(instance, transaction)
+ cls.assign_app_to_transaction_data_if_missing(instance, transaction, app)
+ cls.cleanup_metadata_data(transaction)
+ money_data = cls.get_money_data_from_input(transaction)
+ cls.update_transaction(instance, transaction, money_data, user, app)
+
+ event = None
+ if transaction_event:
+ event = cls.create_transaction_event(transaction_event, instance, user, app)
+ if instance.order:
+ reference = transaction_event.pop("reference", None)
+ psp_reference = transaction_event.get("psp_reference", reference)
+ order_transaction_event(
+ order=instance.order,
+ user=user,
+ app=app,
+ reference=psp_reference or "",
+ status=transaction_event["status"],
+ message=cls.create_event_message(transaction_event),
+ )
+ if instance.order_id:
+ order = cast(order_models.Order, instance.order)
+ should_update_search_vector = bool(
+ (instance.psp_reference != previous_transaction_psp_reference)
+ or (event and event.psp_reference)
+ )
+ cls.update_order(
+ order, money_data, update_search_vector=should_update_search_vector
+ )
+ order_info = fetch_order_info(order)
+ order_transaction_updated(
+ order_info=order_info,
+ transaction_item=instance,
+ manager=manager,
+ user=user,
+ app=app,
+ previous_authorized_value=previous_authorized_value,
+ previous_charged_value=previous_charged_value,
+ previous_refunded_value=previous_refunded_value,
+ )
+ if instance.checkout_id and money_data:
+ manager = get_plugin_manager_promise(info.context).get()
+ transaction_amounts_for_checkout_updated(instance, manager)
+
+ return TransactionUpdate(transaction=instance)
diff --git a/saleor/graphql/payment/mutations/transaction/utils.py b/saleor/graphql/payment/mutations/transaction/utils.py
new file mode 100644
index 0000000000..87a1fa6501
--- /dev/null
+++ b/saleor/graphql/payment/mutations/transaction/utils.py
@@ -0,0 +1,38 @@
+from typing import TYPE_CHECKING
+
+from django.core.exceptions import ValidationError
+
+from .....payment import models as payment_models
+from .....payment.error_codes import TransactionUpdateErrorCode
+from ....core.utils import from_global_id_or_error
+from ...types import TransactionItem
+
+if TYPE_CHECKING:
+ pass
+
+
+def get_transaction_item(id: str) -> payment_models.TransactionItem:
+ """Get transaction based on global ID.
+
+ The transactions created before 3.13 were using the `id` field as a graphql ID.
+ From 3.13, the `token` is used as a graphql ID. All transactionItems created
+ before 3.13 will use an `int` id as an identification.
+ """
+ _, db_id = from_global_id_or_error(
+ global_id=id, only_type=TransactionItem, raise_error=True
+ )
+ if db_id.isdigit():
+ query_params = {"id": db_id, "use_old_id": True}
+ else:
+ query_params = {"token": db_id}
+ instance = payment_models.TransactionItem.objects.filter(**query_params).first()
+ if not instance:
+ raise ValidationError(
+ {
+ "id": ValidationError(
+ f"Couldn't resolve to a node: {id}",
+ code=TransactionUpdateErrorCode.NOT_FOUND.value,
+ )
+ }
+ )
+ return instance
diff --git a/saleor/graphql/payment/tests/benchmark/test_payment_transactions.py b/saleor/graphql/payment/tests/benchmark/test_payment_transactions.py
index f819447c13..479407b386 100644
--- a/saleor/graphql/payment/tests/benchmark/test_payment_transactions.py
+++ b/saleor/graphql/payment/tests/benchmark/test_payment_transactions.py
@@ -57,8 +57,11 @@ def test_payment_transactions(
permission_group_manage_orders,
count_queries,
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
transactions_count = 0
+
+ # when
content = get_graphql_content(
staff_api_client.post_graphql(
PAYMENT_TRANSACTIONS_QUERY,
@@ -66,6 +69,7 @@ def test_payment_transactions(
)
)
+ # then
edges = content["data"]["orders"]["edges"]
for edge in edges:
for payment in edge["node"]["payments"]:
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 39bca5fa28..e298c463dc 100644
--- a/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py
+++ b/saleor/graphql/payment/tests/deprecated/test_checkout_payment_create.py
@@ -29,6 +29,7 @@ CREATE_PAYMENT_MUTATION = """
def test_checkout_add_payment_by_checkout_id(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -48,7 +49,11 @@ def test_checkout_add_payment_by_checkout_id(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
@@ -67,6 +72,7 @@ def test_checkout_add_payment_by_checkout_id(
def test_checkout_add_payment_neither_token_and_id_given(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -84,7 +90,11 @@ def test_checkout_add_payment_neither_token_and_id_given(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert len(data["errors"]) == 1
@@ -95,6 +105,7 @@ def test_checkout_add_payment_neither_token_and_id_given(
def test_checkout_add_payment_both_token_and_id_given(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -115,7 +126,11 @@ def test_checkout_add_payment_both_token_and_id_given(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert len(data["errors"]) == 1
diff --git a/saleor/graphql/payment/tests/mutations/test_check_payment_balance.py b/saleor/graphql/payment/tests/mutations/test_check_payment_balance.py
index 24228ce396..7729eb40a3 100644
--- a/saleor/graphql/payment/tests/mutations/test_check_payment_balance.py
+++ b/saleor/graphql/payment/tests/mutations/test_check_payment_balance.py
@@ -24,11 +24,15 @@ MUTATION_CHECK_PAYMENT_BALANCE = """
def test_payment_check_balance_mutation_validate_gateway_does_not_exist(
check_payment_balance_mock, staff_api_client, check_payment_balance_input
):
+ # given
check_payment_balance_input["gatewayId"] = "mirumee.payments.not_existing_gateway"
+
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
content = get_graphql_content(response)
errors = content["data"]["paymentCheckBalance"]["errors"]
@@ -48,11 +52,15 @@ def test_payment_check_balance_mutation_validate_gateway_does_not_exist(
def test_payment_check_balance_validate_not_supported_currency(
_, __, check_payment_balance_mock, staff_api_client, check_payment_balance_input
):
+ # given
check_payment_balance_input["card"]["money"]["currency"] = "ABSTRACT_CURRENCY"
+
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
content = get_graphql_content(response)
errors = content["data"]["paymentCheckBalance"]["errors"]
@@ -73,11 +81,15 @@ def test_payment_check_balance_validate_not_supported_currency(
def test_payment_check_balance_validate_channel_does_not_exist(
_, __, check_payment_balance_mock, staff_api_client, check_payment_balance_input
):
+ # given
check_payment_balance_input["channel"] = "not_existing_channel"
+
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
content = get_graphql_content(response)
errors = content["data"]["paymentCheckBalance"]["errors"]
@@ -102,14 +114,17 @@ def test_payment_check_balance_validate_channel_inactive(
channel_USD,
check_payment_balance_input,
):
+ # given
channel_USD.is_active = False
channel_USD.save(update_fields=["is_active"])
check_payment_balance_input["channel"] = "main"
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
content = get_graphql_content(response)
errors = content["data"]["paymentCheckBalance"]["errors"]
@@ -123,7 +138,9 @@ def test_payment_check_balance_validate_channel_inactive(
@patch.object(PluginsManager, "check_payment_balance")
@patch.object(PaymentCheckBalance, "validate_gateway")
@patch.object(PaymentCheckBalance, "validate_currency")
-@patch("saleor.graphql.payment.mutations.validate_channel")
+@patch(
+ "saleor.graphql.payment.mutations.payment.payment_check_balance.validate_channel"
+)
def test_payment_check_balance_payment(
_,
__,
@@ -132,10 +149,12 @@ def test_payment_check_balance_payment(
staff_api_client,
check_payment_balance_input,
):
+ # when
staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
check_payment_balance_mock.assert_called_once_with(
{
"gateway_id": "mirumee.payments.gateway",
@@ -153,7 +172,9 @@ def test_payment_check_balance_payment(
@patch.object(PluginsManager, "check_payment_balance")
@patch.object(PaymentCheckBalance, "validate_gateway")
@patch.object(PaymentCheckBalance, "validate_currency")
-@patch("saleor.graphql.payment.mutations.validate_channel")
+@patch(
+ "saleor.graphql.payment.mutations.payment.payment_check_balance.validate_channel"
+)
def test_payment_check_balance_balance_raises_error(
_,
__,
@@ -162,14 +183,17 @@ def test_payment_check_balance_balance_raises_error(
staff_api_client,
check_payment_balance_input,
):
+ # given
check_payment_balance_mock.side_effect = Mock(
side_effect=PaymentError("Test payment error")
)
+ # when
response = staff_api_client.post_graphql(
MUTATION_CHECK_PAYMENT_BALANCE, {"input": check_payment_balance_input}
)
+ # then
content = get_graphql_content(response)
errors = content["data"]["paymentCheckBalance"]["errors"]
diff --git a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
index b9b2523259..de95321b63 100644
--- a/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
+++ b/saleor/graphql/payment/tests/mutations/test_checkout_payment_create.py
@@ -44,6 +44,7 @@ CREATE_PAYMENT_MUTATION = """
def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -54,6 +55,8 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
total = calculations.checkout_total(
manager=manager, checkout_info=checkout_info, lines=lines, address=address
)
+
+ # when
variables = {
"id": to_global_id_or_none(checkout),
"input": {
@@ -63,6 +66,8 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
},
}
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
@@ -81,6 +86,7 @@ def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
def test_checkout_add_payment_without_shipping_method_with_shipping_required(
user_api_client, checkout_with_shipping_required, address
):
+ # given
checkout = checkout_with_shipping_required
checkout.billing_address = address
@@ -100,7 +106,11 @@ def test_checkout_add_payment_without_shipping_method_with_shipping_required(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -111,6 +121,7 @@ def test_checkout_add_payment_without_shipping_method_with_shipping_required(
def test_checkout_add_payment_with_shipping_method_and_shipping_required(
user_api_client, checkout_with_shipping_required, other_shipping_method, address
):
+ # given
checkout = checkout_with_shipping_required
checkout.billing_address = address
checkout.shipping_address = address
@@ -131,7 +142,11 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -151,6 +166,7 @@ def test_checkout_add_payment_with_shipping_method_and_shipping_required(
def test_checkout_add_payment(
user_api_client, checkout_without_shipping_required, address, customer_user
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.email = "old@example"
@@ -173,7 +189,11 @@ def test_checkout_add_payment(
"returnUrl": return_url,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -195,6 +215,7 @@ def test_checkout_add_payment(
def test_checkout_add_payment_default_amount(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -210,7 +231,11 @@ def test_checkout_add_payment_default_amount(
"id": to_global_id_or_none(checkout),
"input": {"gateway": DUMMY_GATEWAY, "token": "sample-token"},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert not data["errors"]
@@ -226,6 +251,7 @@ def test_checkout_add_payment_default_amount(
def test_checkout_add_payment_bad_amount(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save()
@@ -245,7 +271,11 @@ def test_checkout_add_payment_bad_amount(
"amount": str(total.gross.amount + Decimal(1)),
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert (
@@ -256,6 +286,7 @@ def test_checkout_add_payment_bad_amount(
def test_checkout_add_payment_no_checkout_email(
user_api_client, checkout_without_shipping_required, address, customer_user
):
+ # given
checkout = checkout_without_shipping_required
checkout.email = None
checkout.save(update_fields=["email"])
@@ -276,7 +307,11 @@ def test_checkout_add_payment_no_checkout_email(
"returnUrl": return_url,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -291,6 +326,7 @@ def test_checkout_add_payment_no_checkout_email(
def test_checkout_add_payment_not_supported_currency(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.currency = "EUR"
@@ -300,7 +336,11 @@ def test_checkout_add_payment_not_supported_currency(
"id": to_global_id_or_none(checkout),
"input": {"gateway": DUMMY_GATEWAY, "token": "sample-token", "amount": "10.0"},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert data["errors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name
@@ -310,6 +350,7 @@ def test_checkout_add_payment_not_supported_currency(
def test_checkout_add_payment_not_existing_gateway(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save(update_fields=["billing_address", "currency"])
@@ -318,7 +359,11 @@ def test_checkout_add_payment_not_existing_gateway(
"id": to_global_id_or_none(checkout),
"input": {"gateway": "not.existing", "token": "sample-token", "amount": "10.0"},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert data["errors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name
@@ -329,6 +374,7 @@ def test_checkout_add_payment_not_existing_gateway(
def test_checkout_add_payment_gateway_inactive(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.save(update_fields=["billing_address", "currency"])
@@ -337,7 +383,11 @@ def test_checkout_add_payment_gateway_inactive(
"id": to_global_id_or_none(checkout),
"input": {"gateway": DUMMY_GATEWAY, "token": "sample-token", "amount": "10.0"},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
assert data["errors"][0]["code"] == PaymentErrorCode.NOT_SUPPORTED_GATEWAY.name
@@ -347,6 +397,7 @@ def test_checkout_add_payment_gateway_inactive(
def test_use_checkout_billing_address_as_payment_billing(
user_api_client, checkout_without_shipping_required, address
):
+ # given
checkout = checkout_without_shipping_required
manager = get_plugins_manager()
lines, _ = fetch_checkout_lines(checkout)
@@ -362,7 +413,11 @@ def test_use_checkout_billing_address_as_payment_billing(
"amount": total.gross.amount,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -548,6 +603,7 @@ def test_create_payment_with_metadata(
def test_checkout_add_payment_no_variant_channel_listings(
user_api_client, checkout_without_shipping_required, address, customer_user
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.email = "old@example"
@@ -573,7 +629,11 @@ def test_checkout_add_payment_no_variant_channel_listings(
"returnUrl": return_url,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -589,6 +649,7 @@ def test_checkout_add_payment_no_variant_channel_listings(
def test_checkout_add_payment_no_product_channel_listings(
user_api_client, checkout_without_shipping_required, address, customer_user
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.email = "old@example"
@@ -614,7 +675,11 @@ def test_checkout_add_payment_no_product_channel_listings(
"returnUrl": return_url,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -630,6 +695,7 @@ def test_checkout_add_payment_no_product_channel_listings(
def test_checkout_add_payment_checkout_without_lines(
user_api_client, checkout_without_shipping_required, address, customer_user
):
+ # given
checkout = checkout_without_shipping_required
checkout.billing_address = address
checkout.email = "old@example"
@@ -654,7 +720,11 @@ def test_checkout_add_payment_checkout_without_lines(
"returnUrl": return_url,
},
}
+
+ # when
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["checkoutPaymentCreate"]
@@ -693,7 +763,8 @@ def test_checkout_add_payment_run_multiple_times(
# when
with before_after.before(
- "saleor.graphql.payment.mutations.cancel_active_payments",
+ "saleor.graphql.payment.mutations.payment."
+ "checkout_payment_create.cancel_active_payments",
call_payment_create_mutation,
):
response = user_api_client.post_graphql(CREATE_PAYMENT_MUTATION, variables)
diff --git a/saleor/graphql/payment/tests/mutations/test_payment_capture.py b/saleor/graphql/payment/tests/mutations/test_payment_capture.py
index 118f819c74..9a8c71c226 100644
--- a/saleor/graphql/payment/tests/mutations/test_payment_capture.py
+++ b/saleor/graphql/payment/tests/mutations/test_payment_capture.py
@@ -30,13 +30,18 @@ CAPTURE_QUERY = """
def test_payment_capture_success(
staff_api_client, permission_group_manage_orders, payment_txn_preauth
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
payment = payment_txn_preauth
assert payment.charge_status == ChargeStatus.NOT_CHARGED
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
variables = {"paymentId": payment_id, "amount": str(payment_txn_preauth.total)}
+
+ # when
response = staff_api_client.post_graphql(CAPTURE_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["paymentCapture"]
assert not data["errors"]
@@ -102,13 +107,18 @@ def test_payment_capture_success_by_app(
def test_payment_capture_with_invalid_argument(
staff_api_client, permission_group_manage_orders, payment_txn_preauth
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
payment = payment_txn_preauth
assert payment.charge_status == ChargeStatus.NOT_CHARGED
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
variables = {"paymentId": payment_id, "amount": 0}
+
+ # when
response = staff_api_client.post_graphql(CAPTURE_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["paymentCapture"]
assert len(data["errors"]) == 1
@@ -121,13 +131,18 @@ def test_payment_capture_with_payment_non_authorized_yet(
"""Ensure capture a payment that is set as authorized is failing with
the proper error message.
"""
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
payment = payment_dummy
assert payment.charge_status == ChargeStatus.NOT_CHARGED
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
variables = {"paymentId": payment_id, "amount": 1}
+
+ # when
response = staff_api_client.post_graphql(CAPTURE_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["paymentCapture"]
assert data["errors"] == [
diff --git a/saleor/graphql/payment/tests/mutations/test_payment_initialize.py b/saleor/graphql/payment/tests/mutations/test_payment_initialize.py
index 748b824f1a..ba36f58778 100644
--- a/saleor/graphql/payment/tests/mutations/test_payment_initialize.py
+++ b/saleor/graphql/payment/tests/mutations/test_payment_initialize.py
@@ -27,6 +27,7 @@ mutation PaymentInitialize(
@patch.object(PluginsManager, "initialize_payment")
def test_payment_initialize(mocked_initialize_payment, api_client, channel_USD):
+ # given
exected_initialize_payment_response = InitializedPaymentResponse(
gateway="gateway.id",
name="PaymentPluginName",
@@ -46,7 +47,11 @@ def test_payment_initialize(mocked_initialize_payment, api_client, channel_USD):
{"paymentMethod": "applepay", "validationUrl": "https://127.0.0.1/valid"}
),
}
+
+ # when
response = api_client.post_graphql(query, variables)
+
+ # then
content = get_graphql_content(response)
init_payment_data = content["data"]["paymentInitialize"]["initializedPayment"]
assert init_payment_data["gateway"] == exected_initialize_payment_response.gateway
@@ -58,6 +63,7 @@ def test_payment_initialize(mocked_initialize_payment, api_client, channel_USD):
def test_payment_initialize_gateway_doesnt_exist(api_client, channel_USD):
+ # given
query = PAYMENT_INITIALIZE_MUTATION
variables = {
"gateway": "wrong.gateway",
@@ -66,7 +72,11 @@ def test_payment_initialize_gateway_doesnt_exist(api_client, channel_USD):
{"paymentMethod": "applepay", "validationUrl": "https://127.0.0.1/valid"}
),
}
+
+ # when
response = api_client.post_graphql(query, variables)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["paymentInitialize"]["initializedPayment"] is None
@@ -75,6 +85,7 @@ def test_payment_initialize_gateway_doesnt_exist(api_client, channel_USD):
def test_payment_initialize_plugin_raises_error(
mocked_initialize_payment, api_client, channel_USD
):
+ # given
error_msg = "Missing paymentMethod field."
mocked_initialize_payment.side_effect = PaymentError(error_msg)
@@ -84,7 +95,11 @@ def test_payment_initialize_plugin_raises_error(
"channel": channel_USD.slug,
"paymentData": json.dumps({"validationUrl": "https://127.0.0.1/valid"}),
}
+
+ # when
response = api_client.post_graphql(query, variables)
+
+ # then
content = get_graphql_content(response)
initialized_payment_data = content["data"]["paymentInitialize"][
"initializedPayment"
diff --git a/saleor/graphql/payment/tests/mutations/test_payment_void.py b/saleor/graphql/payment/tests/mutations/test_payment_void.py
index 841e6a7429..a1a6fe7ce0 100644
--- a/saleor/graphql/payment/tests/mutations/test_payment_void.py
+++ b/saleor/graphql/payment/tests/mutations/test_payment_void.py
@@ -23,11 +23,16 @@ VOID_QUERY = """
def test_payment_void_success(
staff_api_client, permission_group_manage_orders, payment_txn_preauth
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
assert payment_txn_preauth.charge_status == ChargeStatus.NOT_CHARGED
payment_id = graphene.Node.to_global_id("Payment", payment_txn_preauth.pk)
variables = {"paymentId": payment_id}
+
+ # when
response = staff_api_client.post_graphql(VOID_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["paymentVoid"]
assert not data["errors"]
@@ -89,12 +94,17 @@ def test_payment_void_success_by_app(
def test_payment_void_gateway_error(
staff_api_client, permission_group_manage_orders, payment_txn_preauth, monkeypatch
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
assert payment_txn_preauth.charge_status == ChargeStatus.NOT_CHARGED
payment_id = graphene.Node.to_global_id("Payment", payment_txn_preauth.pk)
variables = {"paymentId": payment_id}
monkeypatch.setattr("saleor.payment.gateways.dummy.dummy_success", lambda: False)
+
+ # when
response = staff_api_client.post_graphql(VOID_QUERY, variables)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["paymentVoid"]
assert data["errors"]
diff --git a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
index 6ba1bd46e8..5b16418ffe 100644
--- a/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
+++ b/saleor/graphql/payment/tests/mutations/test_transaction_event_report.py
@@ -587,7 +587,8 @@ def test_transaction_event_report_incorrect_amount_for_already_existing(
@patch(
- "saleor.graphql.payment.mutations.recalculate_transaction_amounts",
+ "saleor.graphql.payment.mutations.transaction.transaction_event_report."
+ "recalculate_transaction_amounts",
wraps=recalculate_transaction_amounts,
)
def test_transaction_event_report_calls_amount_recalculations(
diff --git a/saleor/graphql/payment/tests/queries/test_payment_capture.py b/saleor/graphql/payment/tests/queries/test_payment_capture.py
index e1a7334538..10644463d0 100644
--- a/saleor/graphql/payment/tests/queries/test_payment_capture.py
+++ b/saleor/graphql/payment/tests/queries/test_payment_capture.py
@@ -17,11 +17,16 @@ QUERY_PAYMENT_CAPTURE_AMOUNT = """
def test_resolve_available_capture_amount_cannot_capture(
staff_api_client, payment_cancelled, permission_manage_orders
):
+ # given
staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_CAPTURE_AMOUNT,
{"id": graphene.Node.to_global_id("Payment", payment_cancelled.pk)},
)
+
+ # then
content = get_graphql_content(response)
assert not content["data"]["payment"]["availableCaptureAmount"]
@@ -30,11 +35,16 @@ def test_resolve_available_capture_amount_cannot_capture(
def test_resolve_available_capture_amount(
staff_api_client, payment_dummy, permission_manage_orders
):
+ # given
staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_CAPTURE_AMOUNT,
{"id": graphene.Node.to_global_id("Payment", payment_dummy.pk)},
)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["payment"]["availableCaptureAmount"]["amount"] == 98.4
diff --git a/saleor/graphql/payment/tests/queries/test_payment_refund.py b/saleor/graphql/payment/tests/queries/test_payment_refund.py
index ff757d6420..5fcc12a171 100644
--- a/saleor/graphql/payment/tests/queries/test_payment_refund.py
+++ b/saleor/graphql/payment/tests/queries/test_payment_refund.py
@@ -20,11 +20,16 @@ QUERY_PAYMENT_REFUND_AMOUNT = """
def test_resolve_available_refund_amount_cannot_refund(
staff_api_client, payment_cancelled, permission_manage_orders
):
+ # given
staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_REFUND_AMOUNT,
{"id": graphene.Node.to_global_id("Payment", payment_cancelled.pk)},
)
+
+ # then
content = get_graphql_content(response)
assert not content["data"]["payment"]["availableRefundAmount"]
@@ -33,11 +38,16 @@ def test_resolve_available_refund_amount_cannot_refund(
def test_resolve_available_refund_amount(
staff_api_client, payment_dummy_fully_charged, permission_manage_orders
):
+ # given
staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_REFUND_AMOUNT,
{"id": graphene.Node.to_global_id("Payment", payment_dummy_fully_charged.pk)},
)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["payment"]["availableRefundAmount"]["amount"] == 98.4
diff --git a/saleor/graphql/payment/tests/queries/test_payment_sources.py b/saleor/graphql/payment/tests/queries/test_payment_sources.py
index 8bd6684180..e3dc8117b3 100644
--- a/saleor/graphql/payment/tests/queries/test_payment_sources.py
+++ b/saleor/graphql/payment/tests/queries/test_payment_sources.py
@@ -21,9 +21,14 @@ def dummy_customer_id():
def test_store_payment_gateway_meta(customer_user, braintree_customer_id):
+ # given
gateway_name = "braintree"
meta_key = "BRAINTREE.customer_id"
+
+ # when
store_customer_id(customer_user, gateway_name, braintree_customer_id)
+
+ # then
assert customer_user.private_metadata[meta_key] == braintree_customer_id
customer_user.refresh_from_db()
assert fetch_customer_id(customer_user, gateway_name) == braintree_customer_id
@@ -51,6 +56,7 @@ def set_dummy_customer_id(customer_user, dummy_customer_id):
def test_list_payment_sources(
mocker, dummy_customer_id, set_dummy_customer_id, user_api_client, channel_USD
):
+ # given
metadata = {f"key_{i}": f"value_{i}" for i in range(5)}
gateway = DUMMY_GATEWAY
query = """
@@ -91,8 +97,11 @@ def test_list_payment_sources(
return_value=[source],
autospec=True,
)
+
+ # when
response = user_api_client.post_graphql(query)
+ # then
mock_get_source_list.assert_called_once_with(gateway, dummy_customer_id, ANY, None)
content = get_graphql_content(response)["data"]["me"]["storedPaymentSources"]
assert content is not None and len(content) == 1
@@ -112,6 +121,7 @@ def test_stored_payment_sources_restriction(
mocker, staff_api_client, customer_user, permission_manage_users
):
# Only owner of storedPaymentSources can fetch it.
+ # given
card = PaymentMethodInfo(last_4="5678", exp_year=2020, exp_month=12, name="JohnDoe")
source = CustomerSource(id="test1", gateway="dummy", credit_card_info=card)
mocker.patch(
@@ -133,7 +143,11 @@ def test_stored_payment_sources_restriction(
}
"""
variables = {"id": customer_user_id}
+
+ # when
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_users]
)
+
+ # then
assert_no_permission(response)
diff --git a/saleor/graphql/payment/tests/queries/test_payments_query.py b/saleor/graphql/payment/tests/queries/test_payments_query.py
index e595b80075..f101d2e312 100644
--- a/saleor/graphql/payment/tests/queries/test_payments_query.py
+++ b/saleor/graphql/payment/tests/queries/test_payments_query.py
@@ -65,8 +65,13 @@ PAYMENT_QUERY = """ query Payments($filter: PaymentFilterInput){
def test_payments_query(
payment_txn_captured, permission_group_manage_orders, staff_api_client
):
+ # given
permission_group_manage_orders.user_set.add(staff_api_client.user)
+
+ # when
response = staff_api_client.post_graphql(PAYMENT_QUERY)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["payments"]["edges"][0]["node"]
pay = payment_txn_captured
@@ -92,10 +97,15 @@ def test_payments_query(
def test_query_payments(
payment_dummy, permission_group_manage_orders, staff_api_client
):
+ # given
payment = payment_dummy
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
permission_group_manage_orders.user_set.add(staff_api_client.user)
+
+ # when
response = staff_api_client.post_graphql(PAYMENT_QUERY, {})
+
+ # then
content = get_graphql_content(response)
edges = content["data"]["payments"]["edges"]
payment_ids = [edge["node"]["id"] for edge in edges]
@@ -225,13 +235,18 @@ QUERY_PAYMENT_BY_ID = """
def test_query_payment(payment_dummy, user_api_client, permission_manage_orders):
+ # given
query = QUERY_PAYMENT_BY_ID
payment = payment_dummy
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
variables = {"id": payment_id}
+
+ # when
response = user_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
)
+
+ # then
content = get_graphql_content(response)
payment_data = content["data"]["payment"]
received_id = payment_data["id"]
@@ -242,6 +257,7 @@ def test_query_payment(payment_dummy, user_api_client, permission_manage_orders)
def test_query_payment_with_checkout(
payment_dummy, user_api_client, permission_manage_orders, checkout
):
+ # given
query = QUERY_PAYMENT_BY_ID
payment = payment_dummy
payment.order = None
@@ -249,9 +265,13 @@ def test_query_payment_with_checkout(
payment.save()
payment_id = graphene.Node.to_global_id("Payment", payment.pk)
variables = {"id": payment_id}
+
+ # when
response = user_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders]
)
+
+ # then
content = get_graphql_content(response)
payment_data = content["data"]["payment"]
received_id = payment_data["id"]
@@ -262,11 +282,16 @@ def test_query_payment_with_checkout(
def test_staff_query_payment_by_invalid_id(
staff_api_client, payment_dummy, permission_manage_orders
):
+ # given
id = "bh/"
variables = {"id": id}
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_BY_ID, variables, permissions=[permission_manage_orders]
)
+
+ # then
content = get_graphql_content_from_response(response)
assert len(content["errors"]) == 1
assert content["errors"][0]["message"] == f"Couldn't resolve id: {id}."
@@ -276,9 +301,14 @@ def test_staff_query_payment_by_invalid_id(
def test_staff_query_payment_with_invalid_object_type(
staff_api_client, payment_dummy, permission_manage_orders
):
+ # given
variables = {"id": graphene.Node.to_global_id("Order", payment_dummy.pk)}
+
+ # when
response = staff_api_client.post_graphql(
QUERY_PAYMENT_BY_ID, variables, permissions=[permission_manage_orders]
)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["payment"] is None