Skip to content

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

Refactoring

NyanKiyoshic7f93b3054fd
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index ee0a6caa6d..ef2513cf3d 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -28,7 +28,6 @@ class OperationType(Enum):
PROCESS_PAYMENT = 'process_payment'
AUTH = 'authorize'
CAPTURE = 'capture'
- CHARGE = 'charge'
VOID = 'void'
REFUND = 'refund'
@@ -54,7 +53,6 @@ class TransactionKind:
The following transactions types are possible:
- AUTH - an amount reserved against the customer's funding source. Money
does not change hands until the authorization is captured.
- - CHARGE - authorization and capture in a single step.
- VOID - a cancellation of a pending authorization or capture.
- CAPTURE - a transfer of the money that was reserved during the
authorization stage.
@@ -62,7 +60,6 @@ class TransactionKind:
"""
AUTH = 'auth'
- CHARGE = 'charge'
CAPTURE = 'capture'
VOID = 'void'
REFUND = 'refund'
@@ -70,7 +67,6 @@ class TransactionKind:
# Which were authorized, but needs to be confirmed manually by staff
# eg. Braintree with "submit_for_settlement" enabled
CHOICES = [(AUTH, pgettext_lazy('transaction kind', 'Authorization')),
- (CHARGE, pgettext_lazy('transaction kind', 'Charge')),
(REFUND, pgettext_lazy('transaction kind', 'Refund')),
(CAPTURE, pgettext_lazy('transaction kind', 'Capture')),
(VOID, pgettext_lazy('transaction kind', 'Void'))]
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index 563144a033..94ead66f85 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -1,19 +1,14 @@
import uuid
-from typing import Dict, List
+from typing import Dict
-from ... import ChargeStatus
+from ... import ChargeStatus, TransactionKind
from ...interface import GatewayResponse, PaymentData
from .forms import DummyPaymentForm
TEMPLATE_PATH = 'order/payment/dummy.html'
-
-class TransactionKind:
- AUTH = 'auth'
- CAPTURE = 'capture'
- CHARGE = 'charge'
- REFUND = 'refund'
- VOID = 'void'
+# The gateway does not support payment authorization
+SUPPORTS_AUTHORIZATION = False
def dummy_success():
@@ -61,8 +56,14 @@ def void(
def capture(
- payment_information: PaymentData, connection_params: Dict
+ payment_information: PaymentData, connection_params
) -> GatewayResponse:
+ """Performs Authorize and Capture transactions in a single run."""
+ if not SUPPORTS_AUTHORIZATION:
+ auth_resp = authorize(payment_information, connection_params)
+ if not auth_resp.is_success:
+ return auth_resp
+
error = None
success = dummy_success()
if not success:
@@ -95,16 +96,6 @@ def refund(
)
-def charge(
- payment_information: PaymentData, connection_params
-) -> GatewayResponse:
- """Performs Authorize and Capture transactions in a single run."""
- auth_resp = authorize(payment_information, connection_params)
- if not auth_resp.is_success:
- return auth_resp
- return capture(payment_information, connection_params)
-
-
def process_payment(
payment_information: PaymentData, connection_params
) -> GatewayResponse:
@@ -113,7 +104,7 @@ def process_payment(
# Process payment normally if payment token is valid
if token not in dict(ChargeStatus.CHOICES):
- return charge(payment_information, connection_params)
+ return capture(payment_information, connection_params)
# Process payment by charge status which is selected in the payment form
# Note that is for testing by dummy gateway only
diff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.py
index 500a60c206..3105277229 100644
--- a/saleor/payment/gateways/razorpay/__init__.py
+++ b/saleor/payment/gateways/razorpay/__init__.py
@@ -6,6 +6,7 @@ from typing import Dict
import razorpay
import razorpay.errors
+from ... import TransactionKind
from ...interface import GatewayResponse, PaymentData
from . import errors
from .forms import RazorPaymentForm
@@ -16,6 +17,9 @@ TEMPLATE_PATH = 'order/payment/razorpay.html'
# The list of currencies supported by razorpay
SUPPORTED_CURRENCIES = 'INR',
+# The gateway does not support payment authorization
+SUPPORTS_AUTHORIZATION = False
+
# Define what are the razorpay exceptions,
# as the razorpay provider doesn't define a base exception as of now.
RAZORPAY_EXCEPTIONS = (
@@ -28,14 +32,6 @@ RAZORPAY_EXCEPTIONS = (
logger = logging.getLogger(__name__)
-class TransactionKind:
- AUTH = 'auth'
- CAPTURE = 'capture'
- CHARGE = 'charge'
- REFUND = 'refund'
- VOID = 'void'
-
-
def _generate_response(
payment_information: PaymentData, kind: str, data: Dict
) -> GatewayResponse:
@@ -94,7 +90,7 @@ def get_client_token(**_):
return str(uuid.uuid4())
-def charge(
+def capture(
payment_information: PaymentData, connection_params: Dict
) -> GatewayResponse:
"""Charge a authorized payment using the razorpay client.
@@ -127,7 +123,7 @@ def charge(
return _generate_response(
payment_information=payment_information,
- kind=TransactionKind.CHARGE, data=response)
+ kind=TransactionKind.CAPTURE, data=response)
def refund(
@@ -168,6 +164,6 @@ def refund(
def process_payment(
payment_information: PaymentData, connection_params
) -> GatewayResponse:
- return charge(
+ return capture(
payment_information=payment_information,
connection_params=connection_params)
diff --git a/saleor/payment/gateways/stripe/__init__.py b/saleor/payment/gateways/stripe/__init__.py
index 0fcb680c3e..74d2d86971 100644
--- a/saleor/payment/gateways/stripe/__init__.py
+++ b/saleor/payment/gateways/stripe/__init__.py
@@ -2,6 +2,7 @@ from typing import Dict
import stripe
+from ... import TransactionKind
from ...interface import GatewayResponse, PaymentData
from .forms import StripePaymentModalForm
from .utils import (
@@ -12,14 +13,6 @@ from .utils import (
TEMPLATE_PATH = 'order/payment/stripe.html'
-class TransactionKind:
- AUTH = 'auth'
- CAPTURE = 'capture'
- CHARGE = 'charge'
- REFUND = 'refund'
- VOID = 'void'
-
-
def get_client_token(**_):
"""Not implemented for stripe gateway currently. The client token can be
generated by Stripe's checkout.js or stripe.js automatically.
@@ -71,26 +64,6 @@ def capture(
kind=TransactionKind.CAPTURE, response=response, error=error)
-def charge(
- payment_information: PaymentData, connection_params: Dict
-) -> GatewayResponse:
- client, error = _get_client(**connection_params), None
-
- try:
- # Charge without pre-authorize
- response = _create_stripe_charge(
- client=client, payment_information=payment_information,
- should_capture=True)
- except stripe.error.StripeError as exc:
- response = _get_error_response_from_exc(exc)
- error = exc.user_message
-
- # Create response
- return _create_response(
- payment_information=payment_information,
- kind=TransactionKind.CHARGE, response=response, error=error)
-
-
def refund(
payment_information: PaymentData, connection_params: Dict
) -> GatewayResponse:
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index a83b9dab0c..cf569b9ac2 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -139,12 +139,15 @@ class Payment(models.Model):
return self.is_active and self.not_charged
def can_capture(self):
- return self.is_active and self.not_charged and self.is_authorized
+ from .utils import get_can_gateway_authorize
+
+ if not (self.is_active and self.not_charged):
+ return False
+
+ if get_can_gateway_authorize(self.gateway):
+ return self.is_authorized
- def can_charge(self):
- not_fully_charged = (
- self.charge_status == ChargeStatus.PARTIALLY_CHARGED)
- return self.is_active and (self.not_charged or not_fully_charged)
+ return True
def can_void(self):
return self.is_active and self.not_charged and self.is_authorized
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 231412783a..dde60b22ce 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -37,14 +37,17 @@ def get_gateway_operation_func(gateway, operation_type):
return gateway.authorize
if operation_type == OperationType.CAPTURE:
return gateway.capture
- if operation_type == OperationType.CHARGE:
- return gateway.charge
if operation_type == OperationType.VOID:
return gateway.void
if operation_type == OperationType.REFUND:
return gateway.refund
+def get_can_gateway_authorize(gateway):
+ gateway, _ = get_payment_gateway(gateway)
+ return getattr(gateway, 'SUPPORTS_AUTHORIZATION', True)
+
+
def create_payment_information(
payment: Payment, payment_token: str = None,
amount: Decimal = None) -> PaymentData:
@@ -217,17 +220,6 @@ def gateway_get_client_token(gateway_name: str):
return gateway.get_client_token(connection_params=gateway_params)
-def clean_charge(payment: Payment, amount: Decimal):
- """Check if payment can be charged."""
- if amount <= 0:
- raise PaymentError('Amount should be a positive number.')
- if not payment.can_charge():
- raise PaymentError('This payment cannot be charged.')
- if amount > payment.total or amount > (
- payment.total - payment.captured_amount):
- raise PaymentError('Unable to charge more than un-captured amount.')
-
-
def clean_capture(payment: Payment, amount: Decimal):
"""Check if payment can be captured."""
if amount <= 0:
@@ -342,7 +334,7 @@ def validate_gateway_response(response: GatewayResponse):
def _gateway_postprocess(transaction, payment):
transaction_kind = transaction.kind
- if transaction_kind in [TransactionKind.CHARGE, TransactionKind.CAPTURE]:
+ if transaction_kind == TransactionKind.CAPTURE:
payment.captured_amount += transaction.amount
# Set payment charge status to fully charged
@@ -383,31 +375,6 @@ def gateway_process_payment(
return transaction
-@require_active_payment
-def gateway_charge(
- payment: Payment, payment_token: str,
- amount: Decimal = None) -> Transaction:
- """Performs authorization and capture in a single run.
-
- For gateways not supporting the authorization it should be a
- dedicated CHARGE transaction.
-
- For gateways not supporting capturing without authorizing,
- it should create two transaction - auth and capture, but only the last one
- is returned.
- """
- if amount is None:
- amount = payment.get_charge_amount()
- clean_charge(payment, amount)
-
- transaction = call_gateway(
- operation_type=OperationType.CHARGE,
- payment=payment, payment_token=payment_token, amount=amount)
-
- _gateway_postprocess(transaction, payment)
- return transaction
-
-
@require_active_payment
def gateway_authorize(payment: Payment, payment_token: str) -> Transaction:
"""Authorizes the payment and creates relevant transaction.
@@ -429,11 +396,14 @@ def gateway_capture(payment: Payment, amount: Decimal = None) -> Transaction:
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
+ if get_can_gateway_authorize(payment):
+ 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
+ else:
+ payment_token = payment.token
transaction = call_gateway(
operation_type=OperationType.CAPTURE,
@@ -480,8 +450,7 @@ def gateway_refund(payment, amount: Decimal = None) -> Transaction:
raise PaymentError('Cannot refund more than captured')
transaction = payment.transactions.filter(
- kind__in=[TransactionKind.CAPTURE, TransactionKind.CHARGE],
- is_success=True).first()
+ kind=TransactionKind.CAPTURE, is_success=True).first()
if transaction is None:
raise PaymentError('Cannot refund uncaptured/uncharged transaction')
payment_token = transaction.token
diff --git a/saleor/settings.py b/saleor/settings.py
index cd4b2f9836..59ca18562e 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -574,7 +574,8 @@ RAZORPAY = 'razorpay'
STRIPE = 'stripe'
CHECKOUT_PAYMENT_GATEWAYS = {
- DUMMY: pgettext_lazy('Payment method name', 'Dummy gateway')}
+ DUMMY: pgettext_lazy('Payment method name', 'Dummy gateway'),
+ RAZORPAY: RAZORPAY} # FIXME: remove me
PAYMENT_GATEWAYS = {
DUMMY: {
diff --git a/tests/gateway/test_dummy.py b/tests/gateway/test_dummy.py
index 1e5f2101c7..d3bd0a3033 100644
--- a/tests/gateway/test_dummy.py
+++ b/tests/gateway/test_dummy.py
@@ -6,11 +6,11 @@ from saleor.payment import (
ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)
from saleor.payment.utils import (
create_payment_information, gateway_authorize, gateway_capture,
- gateway_charge, gateway_process_payment, gateway_refund, gateway_void)
+ gateway_process_payment, gateway_refund, gateway_void)
def test_charge_success(payment_dummy):
- gateway_charge(payment=payment_dummy, payment_token='fake-token')
+ gateway_capture(payment=payment_dummy)
capture_txn = payment_dummy.transactions.last()
assert capture_txn.is_success
@@ -28,10 +28,10 @@ def test_charge_gateway_error(payment_dummy, monkeypatch):
monkeypatch.setattr(
'saleor.payment.gateways.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
- txn = gateway_charge(
+ txn = gateway_capture(
payment=payment_dummy, payment_token='Fake',
amount=payment_dummy.total)
- assert txn.kind == TransactionKind.CHARGE
+ assert txn.kind == TransactionKind.CAPTURE
assert not txn.is_success
assert txn.payment == payment_dummy
@@ -52,7 +52,7 @@ def test_charge_failed(is_active, charge_status, payment_dummy):
payment.charge_status = charge_status
payment.save()
with pytest.raises(PaymentError):
- txn = gateway_charge(payment=payment, payment_token='Fake')
+ txn = gateway_capture(payment=payment)
assert txn is None
@@ -177,7 +177,7 @@ def test_capture_gateway_error(payment_txn_preauth, monkeypatch):
'saleor.payment.gateways.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
txn = gateway_capture(payment=payment_txn_preauth, amount=80)
- assert txn.kind == TransactionKind.CHARGE
+ assert txn.kind == TransactionKind.CAPTURE
assert not txn.is_success
assert txn.payment == payment_txn_preauth
diff --git a/tests/gateway/test_razorpay.py b/tests/gateway/test_razorpay.py
index e02c45dfb7..a09660574a 100644
--- a/tests/gateway/test_razorpay.py
+++ b/tests/gateway/test_razorpay.py
@@ -4,9 +4,9 @@ from unittest.mock import patch
import pytest
from razorpay.errors import BadRequestError, ServerError
-from saleor.payment import ChargeStatus
+from saleor.payment import ChargeStatus, TransactionKind
from saleor.payment.gateways.razorpay import (
- TransactionKind, charge, check_payment_supported, clean_razorpay_response,
+ capture, check_payment_supported, clean_razorpay_response,
create_form, errors, get_amount_for_razorpay, get_client, get_client_token,
logger, refund)
from saleor.payment.gateways.razorpay.forms import (
@@ -48,7 +48,7 @@ def charged_payment(razorpay_payment):
razorpay_payment.transactions.create(
amount=razorpay_payment.total,
- kind=TransactionKind.CHARGE,
+ kind=TransactionKind.CAPTURE,
gateway_response={},
is_success=True)
return razorpay_payment
@@ -145,13 +145,13 @@ def test_charge(
amount=TRANSACTION_AMOUNT)
# Attempt charging
- response = charge(payment_info, gateway_params)
+ response = capture(payment_info, gateway_params)
# Ensure the was no error returned
assert not response.error
assert response.is_success
- assert response.kind == TransactionKind.CHARGE
+ assert response.kind == TransactionKind.CAPTURE
assert response.amount == TRANSACTION_AMOUNT
assert response.currency == razorpay_success_response['currency']
assert response.raw_response == razorpay_success_response
@@ -171,7 +171,7 @@ def test_charge_unsupported_currency(razorpay_payment, gateway_params):
amount=TRANSACTION_AMOUNT)
# Attempt charging
- response = charge(payment_info, gateway_params)
+ response = capture(payment_info, gateway_params)
# Ensure a error was returned
assert response.error == (
@@ -179,7 +179,7 @@ def test_charge_unsupported_currency(razorpay_payment, gateway_params):
assert not response.is_success
# Ensure the response is correctly set
- assert response.kind == TransactionKind.CHARGE
+ assert response.kind == TransactionKind.CAPTURE
assert response.transaction_id == payment_token
@@ -194,7 +194,7 @@ def test_charge_invalid_request(
# Data to be passed
payment_token = '123'
- # Assign the side effect to the gateway's `charge()` method,
+ # Assign the side effect to the gateway's `capture()` method,
# that should trigger the expected error.
mocked_gateway.return_value.payment.capture.side_effect = BadRequestError()
@@ -203,14 +203,14 @@ def test_charge_invalid_request(
amount=TRANSACTION_AMOUNT)
# Attempt charging
- response = charge(payment_info, gateway_params)
+ response = capture(payment_info, gateway_params)
# Ensure an error was returned
assert response.error == errors.INVALID_REQUEST
assert not response.is_success
# Ensure the response is correctly set
- assert response.kind == TransactionKind.CHARGE
+ assert response.kind == TransactionKind.CAPTURE
assert response.transaction_id == payment_token
# Ensure the HTTP error was logged
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 2b86b2a325..b5dcba7f43 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -1,4 +1,3 @@
-import dataclasses
from decimal import Decimal
from unittest.mock import Mock, patch
@@ -15,10 +14,10 @@ from saleor.payment import (
from saleor.payment.interface import GatewayResponse
from saleor.payment.models import Payment
from saleor.payment.utils import (
- ALLOWED_GATEWAY_KINDS, REQUIRED_GATEWAY_KEYS, call_gateway,
- clean_authorize, clean_capture, clean_charge, clean_mark_order_as_paid,
+ ALLOWED_GATEWAY_KINDS, call_gateway,
+ clean_authorize, clean_capture, clean_mark_order_as_paid,
create_payment, create_payment_information, create_transaction,
- gateway_authorize, gateway_capture, gateway_charge,
+ gateway_authorize, gateway_capture,
gateway_get_client_token, gateway_process_payment, gateway_refund,
gateway_void, handle_fully_paid_order, mark_order_as_paid,
require_active_payment, validate_gateway_response)
@@ -127,7 +126,7 @@ def test_handle_fully_paid_order(mock_send_payment_confirmation, order):
def test_require_active_payment():
@require_active_payment
- def test_function(payment, *args, **kwargs):
+ def test_function(_payment, *_args, **_kwargs):
return True
payment = Mock(is_active=True)
@@ -287,6 +286,7 @@ def test_gateway_authorize_errors(payment_dummy):
'Charged transactions cannot be authorized again.')
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
@patch('saleor.payment.utils.handle_fully_paid_order')
@patch('saleor.payment.utils.get_payment_gateway')
def test_gateway_capture(
@@ -304,7 +304,6 @@ def test_gateway_capture(
payment_info = create_payment_information(payment, '', amount)
gateway_capture(payment, amount)
- mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_capture.assert_called_once_with(
payment_information=payment_info, connection_params=gateway_params)
@@ -314,11 +313,12 @@ def test_gateway_capture(
mock_handle_fully_paid_order.assert_called_once_with(payment.order)
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
@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_params, settings, dummy_response):
+ mock_get_payment_gateway, mock_handle_fully_paid_order,
+ payment_txn_preauth, gateway_params, settings, dummy_response):
payment = payment_txn_preauth
amount = payment.total * Decimal('0.5')
txn = payment.transactions.first()
@@ -340,11 +340,12 @@ def test_gateway_capture_partial_capture(
assert not mock_handle_fully_paid_order.called
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
@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_params, dummy_response):
+ mock_get_payment_gateway, mock_handle_fully_paid_order,
+ payment_txn_preauth, gateway_params, dummy_response):
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
@@ -365,6 +366,7 @@ def test_gateway_capture_failed(
assert not mock_handle_fully_paid_order.called
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
def test_gateway_capture_errors(payment_txn_preauth):
with pytest.raises(PaymentError) as exc:
gateway_capture(payment_txn_preauth, Decimal('0'))
@@ -382,26 +384,27 @@ def test_gateway_capture_errors(payment_txn_preauth):
'Unable to capture more than authorized amount.')
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=False)
@patch('saleor.payment.utils.handle_fully_paid_order')
@patch('saleor.payment.utils.get_payment_gateway')
-def test_gateway_charge(
- mock_get_payment_gateway, mock_handle_fully_paid_order, payment_txn_preauth,
- gateway_params, transaction_token, dummy_response):
+def test_gateway_can_capture_when_authorization_unsupported(
+ mock_get_payment_gateway, mock_handle_fully_paid_order,
+ payment_txn_preauth, gateway_params, transaction_token,
+ dummy_response):
payment_token = transaction_token
payment = payment_txn_preauth
assert not payment.captured_amount
amount = payment.total
- dummy_response.kind = TransactionKind.CHARGE
- mock_charge = Mock(return_value=dummy_response)
+ dummy_response.kind = TransactionKind.CAPTURE
+ mock_capture = Mock(return_value=dummy_response)
mock_get_payment_gateway.return_value = (
- Mock(charge=mock_charge), gateway_params)
+ Mock(capture=mock_capture), gateway_params)
payment_info = create_payment_information(payment, payment_token, amount)
- gateway_charge(payment, payment_token, amount)
+ gateway_capture(payment, amount)
- mock_get_payment_gateway.assert_called_once_with(payment.gateway)
- mock_charge.assert_called_once_with(
+ mock_capture.assert_called_once_with(
payment_information=payment_info, connection_params=gateway_params)
payment.refresh_from_db()
@@ -410,12 +413,13 @@ def test_gateway_charge(
mock_handle_fully_paid_order.assert_called_once_with(payment.order)
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
@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_params, transaction_token, settings, dummy_response):
- payment_token = transaction_token
+ mock_get_payment_gateway, mock_handle_fully_paid_order,
+ payment_txn_preauth, gateway_params, transaction_token, settings,
+ dummy_response):
payment = payment_txn_preauth
amount = payment.total * Decimal('0.5')
txn = payment.transactions.first()
@@ -426,9 +430,9 @@ def test_gateway_capture_partial_charge(
dummy_response.amount = amount
mock_charge = Mock(return_value=dummy_response)
mock_get_payment_gateway.return_value = (
- Mock(charge=mock_charge), gateway_params)
+ Mock(capture=mock_charge), gateway_params)
- gateway_charge(payment, payment_token, amount)
+ gateway_capture(payment, amount)
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.PARTIALLY_CHARGED
@@ -437,12 +441,13 @@ def test_gateway_capture_partial_charge(
assert not mock_handle_fully_paid_order.called
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=True)
@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_params, transaction_token, dummy_response):
- payment_token = transaction_token
+ mock_get_payment_gateway, mock_handle_fully_paid_order,
+ payment_txn_preauth, gateway_params, transaction_token,
+ dummy_response):
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
@@ -453,9 +458,9 @@ def test_gateway_charge_failed(
dummy_response.kind = TransactionKind.CAPTURE
mock_charge = Mock(return_value=dummy_response)
mock_get_payment_gateway.return_value = (
- Mock(charge=mock_charge), gateway_params)
+ Mock(capture=mock_charge), gateway_params)
with pytest.raises(PaymentError) as exc:
- gateway_charge(payment, payment_token, amount)
+ gateway_capture(payment, amount)
assert exc.value.message == EXAMPLE_ERROR
payment.refresh_from_db()
assert payment.charge_status == ChargeStatus.NOT_CHARGED
@@ -463,22 +468,22 @@ def test_gateway_charge_failed(
assert not mock_handle_fully_paid_order.called
+@patch('saleor.payment.gateways.dummy.SUPPORTS_AUTHORIZATION', new=False)
def test_gateway_charge_errors(payment_dummy, transaction_token):
payment = payment_dummy
- payment_token = transaction_token
with pytest.raises(PaymentError) as exc:
- gateway_charge(payment, payment_token, 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_charge(payment, payment_token, Decimal('10'))
- assert exc.value.message == 'This payment cannot be charged.'
+ 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_charge(payment, payment_token, Decimal('1000000'))
+ gateway_capture(payment, Decimal('1000000'))
assert exc.value.message == (
'Unable to charge more than un-captured amount.')
@@ -530,7 +535,8 @@ def test_gateway_void_errors(payment_dummy):
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
with pytest.raises(PaymentError) as exc:
gateway_void(payment_dummy)
- exc.value.message == 'Only pre-authorized transactions can be voided.'
+ assert (
+ exc.value.message == 'Only pre-authorized transactions can be voided.')
@patch('saleor.payment.utils.get_payment_gateway')
@@ -598,7 +604,7 @@ def test_gateway_refund_failed(
with pytest.raises(PaymentError) as exc:
gateway_refund(payment, Decimal('10.00'))
- exc.value.message == EXAMPLE_ERROR
+ assert exc.value.message == EXAMPLE_ERROR
payment.refresh_from_db()
assert payment.captured_amount == captured_before
@@ -679,36 +685,6 @@ def test_clean_capture():
clean_capture(payment, amount)
-def test_clean_charge():
- # Amount should be a positive number
- payment = Mock()
- amount = Decimal('0.00')
- with pytest.raises(PaymentError):
- clean_charge(payment, amount)
-
- # Payment cannot be charged
- payment = Mock(can_charge=Mock(return_value=False))
- amount = Decimal('1.00')
- with pytest.raises(PaymentError):
- clean_charge(payment, amount)
-
- # Amount is larger than payment's total
- payment = Mock(
- can_capture=Mock(return_value=True),
- total=Decimal('1.00'),
- captured_amount=Decimal('0.00'))
- amount = Decimal('2.00')
- with pytest.raises(PaymentError):
- clean_charge(payment, amount)
-
- amount = Decimal('2.00')
- payment = Mock(
- can_charge=Mock(return_value=True),
- total=amount,
- captured_amount=Decimal('0.00'))
- clean_charge(payment, amount)
-
-
def test_can_authorize(payment_dummy: Payment):
assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
@@ -745,25 +721,6 @@ def test_can_capture(payment_txn_preauth: Payment):
assert not payment_txn_preauth.can_capture()
-def test_can_charge(payment_dummy: Payment):
- assert payment_dummy.charge_status == ChargeStatus.NOT_CHARGED
-
- payment_dummy.is_active = False
- assert not payment_dummy.can_charge()
-
- payment_dummy.is_active = True
- assert payment_dummy.can_charge()
-
- payment_dummy.charge_status = ChargeStatus.PARTIALLY_CHARGED
- assert payment_dummy.can_charge()
-
- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
- assert not payment_dummy.can_charge()
-
- payment_dummy.captured_amount = payment_dummy.total
- assert not payment_dummy.can_charge()
-
-
def test_can_void(payment_txn_preauth: Payment):
assert payment_txn_preauth.charge_status == ChargeStatus.NOT_CHARGED