Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Perform confirmation instead of capture on checkout with payment to confirm
Jakub Draganek17ba8378c2ee
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 78b87949ce..fc77758660 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -628,6 +628,8 @@ class CheckoutShippingMethodUpdate(BaseMutation):class CheckoutComplete(BaseMutation):order = graphene.Field(Order, description="Placed order.")+ confirmation_needed = graphene.Boolean(required=False, description=("Set to true if payment needs to be confirmed"+ " before checkout is complete."))class Arguments:checkout_id = graphene.ID(description="Checkout ID.", required=True)@@ -709,10 +711,14 @@ class CheckoutComplete(BaseMutation):if shipping_address is not None:shipping_address = AddressData(**shipping_address.as_data())+ payment_confirmation = payment.to_confirmtry:- txn = gateway.process_payment(- payment=payment, token=payment.token, store_source=store_source- )+ if payment_confirmation:+ txn = gateway.confirm(payment)+ else:+ txn = gateway.process_payment(+ payment=payment, token=payment.token, store_source=store_source+ )if not txn.is_success:raise PaymentError(txn.error)@@ -746,8 +752,10 @@ class CheckoutComplete(BaseMutation):# remove checkout after order is successfully paidcheckout.delete()- # return the success response with the newly created order data- return CheckoutComplete(order=order)+ # return the success response with the newly created order data+ return CheckoutComplete(order=order)++ return CheckoutComplete(order=None, confirmation_needed=True)class CheckoutAddPromoCode(BaseMutation):diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.pyindex f5a7f4ca9f..d484902949 100644--- a/saleor/payment/gateway.py+++ b/saleor/payment/gateway.py@@ -178,7 +178,7 @@ def void(payment: Payment) -> Transaction:@require_active_paymentdef confirm(payment: Payment) -> Transaction:plugin_manager = get_extensions_manager()- token = _get_past_transaction_token(payment, TransactionKind.AUTH)+ token = _get_past_transaction_token(payment, TransactionKind.CAPTURE)payment_data = create_payment_information(payment=payment, payment_token=token)response, error = _fetch_gateway_response(plugin_manager.confirm_payment, payment.gateway, payment_datadiff --git a/saleor/payment/migrations/0016_payment_to_confirm.py b/saleor/payment/migrations/0016_payment_to_confirm.pynew file mode 100644index 0000000000..189fdc1c08--- /dev/null+++ b/saleor/payment/migrations/0016_payment_to_confirm.py@@ -0,0 +1,18 @@+# Generated by Django 2.2.9 on 2020-01-20 17:30++from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('payment', '0015_transaction_action_required'),+ ]++ operations = [+ migrations.AddField(+ model_name='payment',+ name='to_confirm',+ field=models.BooleanField(default=False),+ ),+ ]diff --git a/saleor/payment/models.py b/saleor/payment/models.pyindex 1e95c191ff..d28df4bead 100644--- a/saleor/payment/models.py+++ b/saleor/payment/models.py@@ -31,6 +31,7 @@ class Payment(models.Model):gateway = models.CharField(max_length=255)is_active = models.BooleanField(default=True)+ to_confirm = models.BooleanField(default=False)created = models.DateTimeField(auto_now_add=True)modified = models.DateTimeField(auto_now=True)charge_status = models.CharField(diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 0a54a76fd7..d807c1b219 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -192,6 +192,11 @@ def gateway_postprocess(transaction, payment):if not transaction.is_success:return+ if transaction.action_required:+ payment.to_confirm = True+ payment.save(update_fields=["to_confirm"])+ return+transaction_kind = transaction.kindif transaction_kind in {TransactionKind.CAPTURE, TransactionKind.CONFIRM}:diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex b34174ec69..500bafbbff 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -1274,8 +1274,7 @@ def mock_get_manager(mocker, fake_manager):autospec=True,return_value=fake_manager,)- yield fake_manager- mgr.assert_called_once()+ return fake_managerdef test_checkout_complete_does_not_delete_checkout_after_unsuccessful_payment(@@ -1367,7 +1366,17 @@ ACTION_REQUIRED_GATEWAY_RESPONSE = GatewayResponse(amount=Decimal(3.0),currency="usd",transaction_id="1234",- error="ERROR",+ error=None,+)++TRANSACTION_CONFIRM_GATEWAY_RESPONSE = GatewayResponse(+ is_success=False,+ action_required=False,+ kind=TransactionKind.CONFIRM,+ amount=Decimal(3.0),+ currency="usd",+ transaction_id="1234",+ error=None,)@@ -1409,6 +1418,18 @@ def test_checkout_complete_confirmation_needed(checkout.refresh_from_db()payment_dummy.refresh_from_db()assert payment_dummy.is_active+ assert payment_dummy.to_confirm++ mock_get_manager.confirm_payment.return_value = ACTION_REQUIRED_GATEWAY_RESPONSE+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkoutComplete"]+ assert not data["errors"]++ mock_get_manager.confirm_payment.assert_called_once()++ new_orders_count = Order.objects.count()+ assert new_orders_count == orders_count + 1def test_checkout_complete_insufficient_stock(diff --git a/tests/conftest.py b/tests/conftest.pyindex 7de30ceddb..0b3860f5e9 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -92,7 +92,7 @@ class CaptureQueriesContext(BaseCaptureQueriesContext):def captured_queries(self):# flake8: noqabase_queries = self.connection.queries[- self.initial_queries : self.final_queries+ self.initial_queries: self.final_queries]new_queries = []