Skip to content

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

Handle action_required from gateway response

Jakub Draganekcd6227de9825
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index 21fe03e371..78b87949ce 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -733,16 +733,18 @@ class CheckoutComplete(BaseMutation):
{"redirect_url": error}, code=AccountErrorCode.INVALID
)
- # create the order into the database
- order = create_order(
- checkout=checkout,
- order_data=order_data,
- user=user,
- redirect_url=redirect_url,
- )
+ order = None
+ if not txn.action_required:
+ # create the order into the database
+ order = create_order(
+ checkout=checkout,
+ order_data=order_data,
+ user=user,
+ redirect_url=redirect_url,
+ )
- # remove checkout after order is successfully paid
- checkout.delete()
+ # remove checkout after order is successfully paid
+ checkout.delete()
# return the success response with the newly created order data
return CheckoutComplete(order=order)
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index eebfb88de9..f5a7f4ca9f 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -72,6 +72,7 @@ def process_payment(
return create_transaction(
payment=payment,
kind=TransactionKind.CAPTURE,
+ action_required=response.action_required,
payment_information=payment_data,
error_msg=error,
gateway_response=response,
diff --git a/saleor/payment/migrations/0015_transaction_action_required.py b/saleor/payment/migrations/0015_transaction_action_required.py
new file mode 100644
index 0000000000..8dee2504dc
--- /dev/null
+++ b/saleor/payment/migrations/0015_transaction_action_required.py
@@ -0,0 +1,18 @@
+# Generated by Django 2.2.9 on 2020-01-20 14:37
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('payment', '0014_django_price_2'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='transaction',
+ name='action_required',
+ field=models.BooleanField(default=False),
+ ),
+ ]
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ca7d39dcf3..1e95c191ff 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -193,6 +193,7 @@ class Transaction(models.Model):
token = models.CharField(max_length=128, blank=True, default="")
kind = models.CharField(max_length=10, choices=TransactionKind.CHOICES)
is_success = models.BooleanField(default=False)
+ action_required = models.BooleanField(default=False)
currency = models.CharField(max_length=settings.DEFAULT_CURRENCY_CODE_LENGTH)
amount = models.DecimalField(
max_digits=settings.DEFAULT_MAX_DIGITS,
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 10703d0db1..0a54a76fd7 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -116,6 +116,7 @@ def create_transaction(
payment: Payment,
kind: str,
payment_information: PaymentData,
+ action_required: bool = False,
gateway_response: GatewayResponse = None,
error_msg=None,
) -> Transaction:
@@ -136,6 +137,7 @@ def create_transaction(
txn = Transaction.objects.create(
payment=payment,
+ action_required=action_required,
kind=gateway_response.kind,
token=gateway_response.transaction_id,
is_success=gateway_response.is_success,
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index d31704908d..b34174ec69 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -1,3 +1,4 @@
+from decimal import Decimal
import uuid
from decimal import Decimal
from unittest import mock
@@ -1359,6 +1360,57 @@ def test_checkout_complete_no_payment(
assert orders_count == Order.objects.count()
+ACTION_REQUIRED_GATEWAY_RESPONSE = GatewayResponse(
+ is_success=True,
+ action_required=True,
+ kind=TransactionKind.CAPTURE,
+ amount=Decimal(3.0),
+ currency="usd",
+ transaction_id="1234",
+ error="ERROR",
+)
+
+
+def test_checkout_complete_confirmation_needed(
+ mock_get_manager,
+ user_api_client,
+ checkout_with_item,
+ address,
+ payment_dummy,
+ shipping_method,
+):
+ mock_get_manager.process_payment.return_value = ACTION_REQUIRED_GATEWAY_RESPONSE
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save()
+
+ total = calculations.checkout_total(checkout)
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
+ orders_count = Order.objects.count()
+
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ new_orders_count = Order.objects.count()
+ assert new_orders_count == orders_count
+ checkout.refresh_from_db()
+ payment_dummy.refresh_from_db()
+ assert payment_dummy.is_active
+
+
def test_checkout_complete_insufficient_stock(
user_api_client, checkout_with_item, address, payment_dummy, shipping_method
):