Skip to content

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

Use gateway_process_payment method in API

Marcin Gębalaf3ed9fb73da3
diff --git a/saleor/checkout/models.py b/saleor/checkout/models.py
index 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 Decimal
from 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.py
index f79898bc12..36f1c676a2 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -10,7 +10,7 @@ from ...core import analytics
from ...core.exceptions import InsufficientStock
from ...core.utils.taxes import get_taxes_for_address
from ...payment import PaymentError
-from ...payment.utils import gateway_authorize, gateway_capture, gateway_void
+from ...payment.utils import gateway_process_payment
from ...shipping.models import ShippingMethod as ShippingMethodModel
from ..account.i18n import I18nMixin
from ..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 payment
try:
- 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)