Skip to content

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

Pass variables as kwargs in payment gateway utils

Pacu2db56b30b32fe
diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.py
index 91d66fa6ae..bb8a621bdd 100644
--- a/saleor/graphql/payment/types.py
+++ b/saleor/graphql/payment/types.py
@@ -145,7 +145,7 @@ class Payment(CountableDjangoObjectType):
# FIXME TESTME
if not self.can_capture():
return None
- return self.get_total() - self.get_captured_amount()
+ return self.get_charge_amount()
def resolve_credit_card(self, info):
data = {
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index fb2f60bd97..102129bdf6 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -89,6 +89,8 @@ def charge(
payment: Payment, payment_token: str, amount: Decimal,
**connection_params):
"""Performs Authorize and Capture transactions in a single run."""
- authorize(payment_token)
- txn, error = capture(payment, amount)
- return txn, error
+ txn, error = authorize(payment, payment_token)
+ if error:
+ return txn, error
+ capture_txn, capture_error = capture(payment, amount)
+ return capture_txn, capture_error
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ce561a7835..a9fe565b98 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -97,7 +97,7 @@ class Payment(models.Model):
return Money(
self.captured_amount, self.currency or settings.DEFAULT_CURRENCY)
- def get_amount_to_charge(self):
+ def get_charge_amount(self):
"""Retrieve the maximum capture possible."""
return self.total - self.captured_amount
@@ -109,7 +109,7 @@ class Payment(models.Model):
def charge(self, payment_token, amount=None):
from . import utils
if amount is None:
- amount = self.get_amount_to_charge()
+ amount = self.get_charge_amount()
return utils.gateway_charge(
payment=self, payment_token=payment_token, amount=amount)
@@ -120,7 +120,7 @@ class Payment(models.Model):
def capture(self, amount=None):
from . import utils
if amount is None:
- amount = self.get_amount_to_charge()
+ amount = self.get_charge_amount()
return utils.gateway_capture(payment=self, amount=amount)
def refund(self, amount=None):
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index ba568f67e0..bb68de04ab 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -116,9 +116,7 @@ def clean_authorize(payment: Payment):
@validate_payment
def gateway_charge(
- payment: Payment,
- payment_token: str,
- amount: Decimal) -> Transaction:
+ payment: Payment, payment_token: str, amount: Decimal) -> Transaction:
"""Performs authorization and capture in a single run.
For gateways not supporting the authorization it should be a
@@ -134,7 +132,10 @@ def gateway_charge(
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
txn, error = gateway.charge(
- payment, payment_token, **gateway_params)
+ payment=payment,
+ payment_token=payment_token,
+ amount=amount,
+ **gateway_params)
if txn.is_success:
payment.charge_status = ChargeStatus.CHARGED
payment.captured_amount += txn.amount
@@ -159,7 +160,7 @@ def gateway_authorize(payment: Payment, payment_token: str) -> Transaction:
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
txn, error = gateway.authorize(
- payment, payment_token, **gateway_params)
+ payment=payment, payment_token=payment_token, **gateway_params)
if not txn.is_success:
raise PaymentError(error)
return txn
@@ -173,7 +174,7 @@ def gateway_capture(payment: Payment, amount: Decimal) -> Transaction:
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
txn, error = gateway.capture(
- payment, amount=amount, **gateway_params)
+ payment=payment, amount=amount, **gateway_params)
if txn.is_success:
payment.charge_status = ChargeStatus.CHARGED
payment.captured_amount += txn.amount
@@ -192,7 +193,7 @@ def gateway_void(payment) -> Transaction:
raise PaymentError('Only pre-authorized transactions can be voided.')
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
- txn, error = gateway.void(payment, **gateway_params)
+ txn, error = gateway.void(payment=payment, **gateway_params)
if txn.is_success:
payment.is_active = False
payment.save(update_fields=['is_active'])
@@ -215,7 +216,8 @@ def gateway_refund(payment, amount: Decimal) -> Transaction:
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
- txn, error = gateway.refund(payment, amount, **gateway_params)
+ txn, error = gateway.refund(
+ payment=payment, amount=amount, **gateway_params)
if txn.is_success:
changed_fields = ['captured_amount']
payment.captured_amount -= txn.amount
diff --git a/tests/gateway/test_dummy.py b/tests/gateway/test_dummy.py
index c90078b050..96ce0acf05 100644
--- a/tests/gateway/test_dummy.py
+++ b/tests/gateway/test_dummy.py
@@ -1,5 +1,5 @@
from decimal import Decimal
-from unittest.mock import patch
+from unittest.mock import patch, Mock
import pytest
from saleor.payment import (
@@ -7,19 +7,25 @@ from saleor.payment import (
from saleor.payment.gateways.dummy.forms import DummyPaymentForm
-@patch('saleor.payment.gateways.dummy.authorize')
-@patch('saleor.payment.gateways.dummy.capture')
-def test_charge(mock_capture, mock_authorize, payment_dummy):
- token = 'fake-token'
- amount = payment_dummy.total
+def test_charge(payment_dummy):
+ payment_dummy.charge(
+ payment_token='fake-token', amount=payment_dummy.total)
+ capture_txn, auth_txn = payment_dummy.transactions.all()
+
+ assert auth_txn.is_success
+ assert auth_txn.kind == TransactionKind.AUTH
+ assert auth_txn.payment == payment_dummy
+
+ assert capture_txn.is_success
+ assert capture_txn.kind == TransactionKind.CAPTURE
+ assert capture_txn.payment == payment_dummy
+ assert capture_txn.amount == payment_dummy.total
+
+ payment_dummy.refresh_from_db()
+ assert payment_dummy.charge_status == ChargeStatus.CHARGED
+ assert payment_dummy.captured_amount == payment_dummy.total
+ assert payment_dummy.is_active
- expected_result = ['txn', 'error']
- mock_capture.return_value = expected_result
- result = payment_dummy.charge(
- payment_token=token, amount=amount)
- assert result == expected_result
- mock_capture.assert_called_once_with(payment, amount)
- mock_authorize.assert_called_once_with(token)
def test_authorize(payment_dummy):
txn = payment_dummy.authorize(payment_token='Fake')
@@ -79,7 +85,7 @@ def test_void_gateway_error(payment_dummy, monkeypatch):
@pytest.mark.parametrize('amount', [80, 70])
-def test_charge_success(amount, payment_dummy):
+def test_capture_success(amount, payment_dummy):
txn = payment_dummy.capture(amount=amount)
assert txn.is_success
assert txn.payment == payment_dummy
@@ -94,7 +100,7 @@ def test_charge_success(amount, payment_dummy):
(80, 0, ChargeStatus.FULLY_REFUNDED, True),
(80, 80, ChargeStatus.CHARGED, True),
(120, 0, ChargeStatus.NOT_CHARGED, True), ])
-def test_charge_failed(
+def test_capture_failed(
amount, captured_amount, charge_status, is_active,
payment_dummy):
payment = payment_dummy
@@ -107,7 +113,7 @@ def test_charge_failed(
assert txn is None
-def test_charge_gateway_error(payment_dummy, monkeypatch):
+def test_capture_gateway_error(payment_dummy, monkeypatch):
monkeypatch.setattr(
'saleor.payment.gateways.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 0573e62a5d..d083fb1e09 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -209,7 +209,7 @@ def test_gateway_authorize(
gateway_authorize(payment, payment_token)
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_authorize.assert_called_once_with(
- payment, payment_token, **gateway_params)
+ payment=payment, payment_token=payment_token, **gateway_params)
@patch('saleor.payment.utils.get_payment_gateway')
@@ -246,7 +246,7 @@ def test_gateway_capture(
gateway_capture(payment, amount)
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_capture.assert_called_once_with(
- payment, amount=amount, **gateway_params)
+ payment=payment, amount=amount, **gateway_params)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.CHARGED
@@ -329,7 +329,7 @@ def test_gateway_void(mock_get_payment_gateway, payment_txn_preauth, gateway_par
gateway_void(payment)
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
- mock_void.assert_called_once_with(payment, **gateway_params)
+ mock_void.assert_called_once_with(payment=payment, **gateway_params)
payment.refresh_from_db()
assert payment.is_active == False
@@ -372,7 +372,8 @@ def test_gateway_refund(
gateway_refund(payment, amount)
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
- mock_refund.assert_called_once_with(payment, amount, **gateway_params)
+ mock_refund.assert_called_once_with(
+ payment=payment, amount=amount, **gateway_params)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.FULLY_REFUNDED