Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add payment authorize and charge mutation
Unknown71bb3a4265c9
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.pyindex 00ad0853d8..200b5cec33 100644--- a/saleor/graphql/payment/mutations.py+++ b/saleor/graphql/payment/mutations.py@@ -3,13 +3,13 @@ import decimalimport graphenefrom django.conf import settingsfrom graphql_jwt.decorators import permission_required-from prices import Money, TaxedMoneyfrom ...core.utils.taxes import get_taxes_for_addressfrom ...payment import PaymentErrorfrom ...payment.utils import (- create_payment, gateway_authorize, gateway_capture, gateway_refund,- gateway_void)+ create_payment,+ gateway_authorize, gateway_capture, gateway_charge,+ gateway_refund, gateway_void)from ..account.i18n import I18nMixinfrom ..account.types import AddressInputfrom ..checkout.types import Checkout@@ -113,6 +113,35 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):'customer_user_agent': user_agent}+class PaymentAuthorize(BaseMutation):+ payment = graphene.Field(Payment, description='Updated payment')++ class Arguments:+ payment_id = graphene.ID(required=True, description='Payment ID')+ payment_token = graphene.String(+ description='One-time-use reference to payment information')++ class Meta:+ description = 'Authorize the payment'++ @classmethod+ @permission_required('order.manage_orders')+ def mutate(cls, root, info, payment_id, payment_token):+ errors = []+ payment = cls.get_node_or_error(+ info, payment_id, errors, 'payment_id', only_type=Payment)++ if not payment:+ return PaymentAuthorize(errors=errors)++ try:+ gateway_authorize(payment, payment_token)+ except PaymentError as exc:+ msg = str(exc)+ cls.add_error(field=None, message=msg, errors=errors)+ return PaymentAuthorize(payment=payment, errors=errors)++class PaymentCapture(BaseMutation):payment = graphene.Field(Payment, description='Updated payment')@@ -141,6 +170,36 @@ class PaymentCapture(BaseMutation):return PaymentCapture(payment=payment, errors=errors)+class PaymentCharge(BaseMutation):+ payment = graphene.Field(Payment, description='Updated payment')++ class Arguments:+ payment_id = graphene.ID(required=True, description='Payment ID')+ payment_token = graphene.String(+ description='One-time-use reference to payment information')+ amount = Decimal(description='Transaction amount')++ class Meta:+ description = 'Authorize the payment'++ @classmethod+ @permission_required('order.manage_orders')+ def mutate(cls, root, info, payment_id, payment_token, amount=None):+ errors = []+ payment = cls.get_node_or_error(+ info, payment_id, errors, 'payment_id', only_type=Payment)++ if not payment:+ return PaymentCharge(errors=errors)++ try:+ gateway_charge(payment, payment_token, amount)+ except PaymentError as exc:+ msg = str(exc)+ cls.add_error(field=None, message=msg, errors=errors)+ return PaymentCharge(payment=payment, errors=errors)++class PaymentRefund(PaymentCapture):@classmethod@permission_required('order.manage_orders')diff --git a/saleor/graphql/payment/schema.py b/saleor/graphql/payment/schema.pyindex f0144111dc..0e95df091a 100644--- a/saleor/graphql/payment/schema.py+++ b/saleor/graphql/payment/schema.py@@ -2,7 +2,9 @@ import graphenefrom graphql_jwt.decorators import permission_requiredfrom ..core.fields import PrefetchingConnectionField-from .mutations import PaymentCapture, PaymentRefund, PaymentVoid+from .mutations import (+ PaymentAuthorize, PaymentCapture, PaymentCharge,+ PaymentRefund, PaymentVoid)from .resolvers import resolve_payment_client_token, resolve_paymentsfrom .types import Payment, PaymentGatewayEnum@@ -26,6 +28,8 @@ class PaymentQueries(graphene.ObjectType):class PaymentMutations(graphene.ObjectType):+ payment_authorize = PaymentAuthorize.Field()payment_capture = PaymentCapture.Field()+ payment_charge = PaymentCharge.Field()payment_refund = PaymentRefund.Field()payment_void = PaymentVoid.Field()