Skip to content

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

Switch to use new gateway capture method

Jakub Draganek559a49ad4bf4
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 59d4451bca..5f96a66339 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -32,12 +32,7 @@ from ...order.models import Fulfillment, Order, OrderLine
from ...order.utils import update_order_status
from ...page.models import Page
from ...payment import gateway
-from ...payment.utils import (
- create_payment,
- gateway_authorize,
- gateway_capture,
- gateway_refund,
-)
+from ...payment.utils import create_payment, gateway_authorize, gateway_refund
from ...product.models import (
AssignedProductAttribute,
AssignedVariantAttribute,
@@ -398,7 +393,7 @@ def create_fake_payment(mock_email_confirmation, order):
if not random.choice([1, 1, 1, 0]):
return payment
# Create capture transaction
- gateway_capture(payment)
+ gateway.capture(payment)
# 25% to refund the payment
if random.choice([0, 0, 0, 1]):
gateway_refund(payment)
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 99055804f1..c96a98506a 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -28,7 +28,6 @@ from ...order.utils import (
from ...payment import ChargeStatus, CustomPaymentChoices, PaymentError, gateway
from ...payment.utils import (
clean_mark_order_as_paid,
- gateway_capture,
gateway_refund,
mark_order_as_paid,
)
@@ -342,7 +341,7 @@ class CapturePaymentForm(BasePaymentForm):
def capture(self, user):
return self.try_payment_action(
- user, gateway_capture, self.cleaned_data["amount"]
+ user, gateway.capture, self.cleaned_data["amount"]
)
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index 2ccd7e796c..9954871379 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -6,12 +6,7 @@ from ...core.taxes import zero_taxed_money
from ...core.utils import get_client_ip
from ...payment import PaymentError, gateway, models
from ...payment.error_codes import PaymentErrorCode
-from ...payment.utils import (
- create_payment,
- gateway_capture,
- gateway_confirm,
- gateway_refund,
-)
+from ...payment.utils import create_payment, gateway_confirm, gateway_refund
from ..account.i18n import I18nMixin
from ..account.types import AddressInput
from ..checkout.types import Checkout
@@ -142,7 +137,7 @@ class PaymentCapture(BaseMutation):
info, payment_id, field="payment_id", only_type=Payment
)
try:
- gateway_capture(payment, amount)
+ gateway.capture(payment, amount)
except PaymentError as e:
raise ValidationError(str(e), code=PaymentErrorCode.PAYMENT_ERROR)
return PaymentCapture(payment=payment)
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 2f18049f34..b042716d71 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -13,6 +13,7 @@ from .utils import (
clean_capture,
create_payment_information,
create_transaction,
+ update_card_details,
validate_gateway_response,
)
@@ -95,13 +96,14 @@ def authorize(payment: Payment, token: str, store_source: bool = False) -> Trans
@payment_postprocess
@raise_payment_error
+@require_active_payment
def capture(
payment: Payment, amount: Decimal = None, store_source: bool = False
) -> Transaction:
plugin_manager = get_extensions_manager()
if amount is None:
amount = payment.get_charge_amount()
- clean_capture(payment, amount)
+ clean_capture(payment, Decimal(amount))
gateway = _get_gateway(payment)
token = _get_past_transaction_token(payment, TransactionKind.AUTH)
payment_data = create_payment_information(
@@ -110,6 +112,8 @@ def capture(
response, error = _fetch_gateway_response(
plugin_manager.capture_payment, gateway, payment_data
)
+ if response.card_info:
+ update_card_details(payment, response)
return create_transaction(
payment=payment,
kind=TransactionKind.CAPTURE,
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index e8a532c4dd..080f6f1cd8 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -441,31 +441,6 @@ def gateway_authorize(payment: Payment, payment_token: str) -> Transaction:
)
-@require_active_payment
-def gateway_capture(payment: Payment, amount: Decimal = None) -> Transaction:
- """Capture the money that was reserved during the authorization stage."""
- if amount is None:
- amount = payment.get_charge_amount()
- clean_capture(payment, amount)
-
- auth_transaction = payment.transactions.filter(
- kind=TransactionKind.AUTH, is_success=True
- ).first()
- if auth_transaction is None:
- raise PaymentError("Cannot capture unauthorized transaction")
- payment_token = auth_transaction.token
-
- transaction = call_gateway(
- operation_type=OperationType.CAPTURE,
- payment=payment,
- payment_token=payment_token,
- amount=amount,
- )
-
- _gateway_postprocess(transaction, payment)
- return transaction
-
-
@require_active_payment
def gateway_confirm(payment) -> Transaction:
if not payment.can_confirm():
diff --git a/tests/test_payment.py b/tests/test_payment.py
index f3117847ff..93c84844ef 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -33,7 +33,6 @@ from saleor.payment.utils import (
create_payment_information,
create_transaction,
gateway_authorize,
- gateway_capture,
gateway_get_client_token,
gateway_process_payment,
gateway_refund,
@@ -318,7 +317,7 @@ def test_gateway_get_client_token_not_existing_gateway(settings):
@pytest.mark.parametrize(
- "func", [gateway_authorize, gateway_capture, gateway_refund, gateway.void]
+ "func", [gateway_authorize, gateway.capture, gateway_refund, gateway.void]
)
def test_payment_needs_to_be_active_for_any_action(func, payment_dummy):
payment_dummy.is_active = False
@@ -385,154 +384,8 @@ def test_gateway_authorize_errors(payment_dummy):
@patch("saleor.payment.utils.handle_fully_paid_order")
-@patch("saleor.payment.utils.get_payment_gateway")
-def test_gateway_capture(
- mock_get_payment_gateway,
- mock_handle_fully_paid_order,
- payment_txn_preauth,
- gateway_config,
- dummy_response,
-):
- payment = payment_txn_preauth
- gateway_config.auto_capture = True
- assert not payment.captured_amount
- amount = payment.total
-
- dummy_response.kind = TransactionKind.CAPTURE
- mock_capture = Mock(return_value=dummy_response)
- mock_get_payment_gateway.return_value = (Mock(capture=mock_capture), gateway_config)
-
- payment_info = create_payment_information(payment, "", amount)
- gateway_capture(payment, amount)
-
- mock_capture.assert_called_once_with(
- payment_information=payment_info, config=gateway_config
- )
-
- payment.refresh_from_db()
- assert payment.charge_status == ChargeStatus.FULLY_CHARGED
- assert payment.captured_amount == payment.total
- assert payment.cc_brand == dummy_response.card_info.brand
- assert payment.cc_exp_month == dummy_response.card_info.exp_month
- assert payment.cc_exp_year == dummy_response.card_info.exp_year
- assert payment.cc_last_digits == dummy_response.card_info.last_4
- mock_handle_fully_paid_order.assert_called_once_with(payment.order)
-
-
-@patch("saleor.payment.utils.handle_fully_paid_order")
-@patch("saleor.payment.utils.get_payment_gateway")
-def test_gateway_capture_partial_capture(
- mock_get_payment_gateway,
- mock_handle_fully_paid_order,
- payment_txn_preauth,
- gateway_config,
- settings,
- dummy_response,
-):
- payment = payment_txn_preauth
- amount = payment.total * Decimal("0.5")
- txn = payment.transactions.first()
- txn.amount = amount
- txn.currency = settings.DEFAULT_CURRENCY
-
- dummy_response.kind = TransactionKind.CAPTURE
- dummy_response.amount = amount
- mock_capture = Mock(return_value=dummy_response)
- mock_get_payment_gateway.return_value = (Mock(capture=mock_capture), gateway_config)
-
- gateway_capture(payment, amount)
-
- payment.refresh_from_db()
- assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
- assert payment.captured_amount == amount
- assert payment.currency == settings.DEFAULT_CURRENCY
- assert not mock_handle_fully_paid_order.called
-
-
-@patch("saleor.payment.utils.handle_fully_paid_order")
-@patch("saleor.payment.utils.get_payment_gateway")
-def test_gateway_capture_failed(
- mock_get_payment_gateway,
- mock_handle_fully_paid_order,
- payment_txn_preauth,
- gateway_config,
- dummy_response,
-):
- txn = payment_txn_preauth.transactions.first()
- txn.is_success = False
-
- payment = payment_txn_preauth
- amount = payment.total
-
- dummy_response.is_success = False
- dummy_response.kind = TransactionKind.CAPTURE
- mock_capture = Mock(return_value=dummy_response)
- mock_get_payment_gateway.return_value = (Mock(capture=mock_capture), gateway_config)
- with pytest.raises(PaymentError) as exc:
- gateway_capture(payment, amount)
- assert exc.value.message == EXAMPLE_ERROR
- payment.refresh_from_db()
- assert payment.charge_status == ChargeStatus.NOT_CHARGED
- assert not payment.captured_amount
- assert not mock_handle_fully_paid_order.called
-
-
-def test_gateway_capture_errors(payment_txn_preauth):
- with pytest.raises(PaymentError) as exc:
- gateway_capture(payment_txn_preauth, Decimal("0"))
- assert exc.value.message == "Amount should be a positive number."
-
- payment_txn_preauth.charge_status = ChargeStatus.FULLY_REFUNDED
- with pytest.raises(PaymentError) as exc:
- gateway_capture(payment_txn_preauth, Decimal("10"))
- assert exc.value.message == "This payment cannot be captured."
-
- payment_txn_preauth.charge_status = ChargeStatus.NOT_CHARGED
- with pytest.raises(PaymentError) as exc:
- gateway_capture(payment_txn_preauth, Decimal("1000000"))
- assert exc.value.message == ("Unable to charge more than un-captured amount.")
-
-
-@patch("saleor.payment.utils.handle_fully_paid_order")
-@patch("saleor.payment.utils.get_payment_gateway")
-def test_gateway_capture_partial_charge(
- mock_get_payment_gateway,
- mock_handle_fully_paid_order,
- payment_txn_preauth,
- gateway_config,
- transaction_token,
- settings,
- dummy_response,
-):
- payment = payment_txn_preauth
- amount = payment.total * Decimal("0.5")
- txn = payment.transactions.first()
- txn.amount = amount
- txn.currency = settings.DEFAULT_CURRENCY
-
- dummy_response.kind = TransactionKind.CAPTURE
- dummy_response.amount = amount
- mock_charge = Mock(return_value=dummy_response)
- mock_get_payment_gateway.return_value = (Mock(capture=mock_charge), gateway_config)
-
- gateway_capture(payment, amount)
-
- payment.refresh_from_db()
- assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
- assert payment.captured_amount == amount
- assert payment.currency == settings.DEFAULT_CURRENCY
- assert not mock_handle_fully_paid_order.called
-
-
-@patch("saleor.payment.utils.handle_fully_paid_order")
-@patch("saleor.payment.utils.get_payment_gateway")
def test_gateway_charge_failed(
- mock_get_payment_gateway,
- mock_handle_fully_paid_order,
- payment_txn_preauth,
- gateway_config,
- transaction_token,
- dummy_response,
+ mock_handle_fully_paid_order, mock_get_manager, payment_txn_preauth, dummy_response
):
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
@@ -542,11 +395,10 @@ def test_gateway_charge_failed(
dummy_response.is_success = False
dummy_response.kind = TransactionKind.CAPTURE
- mock_charge = Mock(return_value=dummy_response)
- mock_get_payment_gateway.return_value = (Mock(capture=mock_charge), gateway_config)
- with pytest.raises(PaymentError) as exc:
- gateway_capture(payment, amount)
- assert exc.value.message == EXAMPLE_ERROR
+ mock_get_manager.capture_payment.return_value = dummy_response
+ with pytest.raises(PaymentError):
+ gateway.capture(payment, amount)
+ mock_get_manager.capture_payment.assert_called_once()
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.NOT_CHARGED
assert not payment.captured_amount
@@ -557,17 +409,17 @@ def test_gateway_charge_errors(payment_dummy, transaction_token, settings):
payment = payment_dummy
gateway_authorize(payment, transaction_token)
with pytest.raises(PaymentError) as exc:
- gateway_capture(payment, Decimal("0"))
+ gateway.capture(payment, Decimal("0"))
assert exc.value.message == "Amount should be a positive number."
payment.charge_status = ChargeStatus.FULLY_REFUNDED
with pytest.raises(PaymentError) as exc:
- gateway_capture(payment, Decimal("10"))
+ gateway.capture(payment, Decimal("10"))
assert exc.value.message == "This payment cannot be captured."
payment.charge_status = ChargeStatus.NOT_CHARGED
with pytest.raises(PaymentError) as exc:
- gateway_capture(payment, Decimal("1000000"))
+ gateway.capture(payment, Decimal("1000000"))
assert exc.value.message == ("Unable to charge more than un-captured amount.")