Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use gateway_process_payment method in API
Marcin Gębalaf3ed9fb73da3
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.pyindex dc6d1c012d..e47bbebd16 100644--- a/saleor/checkout/models.py+++ b/saleor/checkout/models.py@@ -1,4 +1,6 @@"""Cart-related ORM models."""+from operator import attrgetter+from decimal import Decimalfrom uuid import uuid4@@ -111,6 +113,11 @@ class Cart(models.Model):matching_lines = (line for line in self if line.variant == variant)return next(matching_lines, None)+ def get_last_active_payment(self):+ payments = [+ payment for payment in self.payments.all() if payment.is_active]+ return max(payments, default=None, key=attrgetter('pk'))+class CartLine(models.Model):"""A single cart line.diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex f79898bc12..36f1c676a2 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -10,7 +10,7 @@ from ...core import analyticsfrom ...core.exceptions import InsufficientStockfrom ...core.utils.taxes import get_taxes_for_addressfrom ...payment import PaymentError-from ...payment.utils import gateway_authorize, gateway_capture, gateway_void+from ...payment.utils import gateway_process_paymentfrom ...shipping.models import ShippingMethod as ShippingMethodModelfrom ..account.i18n import I18nMixinfrom ..account.types import AddressInput, User@@ -468,38 +468,23 @@ class CheckoutComplete(BaseMutation):if not ready:cls.add_error(field=None, message=checkout_error, errors=errors)- if not errors:- try:- order = create_order(- cart=checkout,- tracking_code=analytics.get_client_id(info.context),- discounts=info.context.discounts, taxes=taxes)- except InsufficientStock:- order = None- cls.add_error(- field=None, message='Insufficient product stock.',- errors=errors)- if errors:- return CheckoutComplete(errors=errors)+ return CheckoutComplete(errors=errors)- payment = checkout.payments.filter(is_active=True).first()- # FIXME there could be a situation where order was created but payment- # failed. we should cancel/delete the order at this moment I think-- # authorize paymenttry:- gateway_authorize(payment, payment.token)- except PaymentError as exc:- msg = str(exc)- cls.add_error(field=None, message=msg, errors=errors)- return CheckoutComplete(order=order, errors=errors)+ order = create_order(+ cart=checkout,+ tracking_code=analytics.get_client_id(info.context),+ discounts=info.context.discounts, taxes=taxes)+ except InsufficientStock:+ cls.add_error(+ field=None, message='Insufficient product stock.',+ errors=errors)+ return CheckoutComplete(errors=errors)- # capture payment+ payment = checkout.get_last_active_payment()try:- gateway_capture(payment, payment.total)- except PaymentError as exc:- msg = str(exc)- cls.add_error(field=None, message=msg, errors=errors)- # Void payment if the capture failed- gateway_void(payment)+ gateway_process_payment(+ payment=payment, payment_token=payment.token)+ except PaymentError as e:+ cls.add_error(errors=errors, field=None, message=str(e))return CheckoutComplete(order=order, errors=errors)