Skip to content

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

Move errors to a single file, simplify comments, add missing tests

NyanKiyoshi4882207f54da
diff --git a/saleor/payment/gateways/razorpay/__init__.py b/saleor/payment/gateways/razorpay/__init__.py
index d1b4f43e5f..7d5a780d4e 100644
--- a/saleor/payment/gateways/razorpay/__init__.py
+++ b/saleor/payment/gateways/razorpay/__init__.py
@@ -3,34 +3,25 @@ import uuid
from decimal import Decimal
from typing import Dict, Tuple
-from django.utils.translation import pgettext_lazy
-
import razorpay
-from razorpay import errors
+import razorpay.errors
+from . import errors
from ... import TransactionKind
from ...models import Payment, Transaction
from ...utils import create_transaction
from .forms import RazorPaymentForm
from .utils import get_amount_for_razorpay, get_error_response
-# Define the existing error messages as lazy `pgettext`.
-ERROR_MSG_ORDER_NOT_CHARGED = pgettext_lazy(
- 'Razorpay payment error', 'Order was not charged.')
-ERROR_MSG_INVALID_REQUEST = pgettext_lazy(
- 'Razorpay payment error', 'The payment data was invalid.')
-ERROR_MSG_SERVER_ERROR = pgettext_lazy(
- 'Razorpay payment error', 'The order couldn\'t be proceeded.')
-ERROR_UNSUPPORTED_CURRENCY = pgettext_lazy(
- 'Razorpay payment error', 'The %(currency)s currency is not supported.')
-
# The list of currencies supported by razorpay
SUPPORTED_CURRENCIES = 'INR',
# Define what are the razorpay exceptions,
# as the razorpay provider doesn't define a base exception as of now.
RAZORPAY_EXCEPTIONS = (
- errors.BadRequestError, errors.GatewayError, errors.ServerError)
+ razorpay.errors.BadRequestError,
+ razorpay.errors.GatewayError,
+ razorpay.errors.ServerError)
# Get the logger for this file, it will allow us to log
# error responses from razorpay.
@@ -61,7 +52,7 @@ def _generate_transaction(
def check_payment_supported(payment: Payment):
"""Checks that a given payment is supported"""
if payment.currency not in SUPPORTED_CURRENCIES:
- return ERROR_UNSUPPORTED_CURRENCY % {
+ return errors.UNSUPPORTED_CURRENCY % {
'currency': payment.currency}
@@ -69,15 +60,15 @@ def get_error_message_from_razorpay_error(exc: BaseException):
"""Convert a error razorpay error to a user friendly error message
and log the exception to stderr."""
logger.exception(exc)
- if isinstance(exc, errors.BadRequestError):
- return ERROR_MSG_INVALID_REQUEST
+ if isinstance(exc, razorpay.errors.BadRequestError):
+ return errors.INVALID_REQUEST
else:
- return ERROR_MSG_SERVER_ERROR
+ return errors.SERVER_ERROR
def clean_razorpay_response(response: dict):
"""As the Razorpay response payload contains the final amount
- as an integer, we converts it to a decimal object (by dividing by 100)."""
+ in Indian rupees, we convert the amount to paisa (by dividing by 100)."""
response['amount'] = Decimal(response['amount']) / 100
@@ -158,7 +149,7 @@ def refund(payment: Payment, amount: Decimal, **connection_params):
error = get_error_message_from_razorpay_error(exc)
response = get_error_response(amount)
else:
- error = ERROR_MSG_ORDER_NOT_CHARGED
+ error = errors.ORDER_NOT_CHARGED
response = get_error_response(amount)
transaction = _generate_transaction(
diff --git a/saleor/payment/gateways/razorpay/errors.py b/saleor/payment/gateways/razorpay/errors.py
new file mode 100644
index 0000000000..bd1e16f1c2
--- /dev/null
+++ b/saleor/payment/gateways/razorpay/errors.py
@@ -0,0 +1,11 @@
+from django.utils.translation import pgettext_lazy
+
+# Define the existing error messages as lazy `pgettext`.
+ORDER_NOT_CHARGED = pgettext_lazy(
+ 'Razorpay payment error', 'Order was not charged.')
+INVALID_REQUEST = pgettext_lazy(
+ 'Razorpay payment error', 'The payment data was invalid.')
+SERVER_ERROR = pgettext_lazy(
+ 'Razorpay payment error', 'The order couldn\'t be proceeded.')
+UNSUPPORTED_CURRENCY = pgettext_lazy(
+ 'Razorpay payment error', 'The %(currency)s currency is not supported.')
diff --git a/saleor/payment/gateways/razorpay/utils.py b/saleor/payment/gateways/razorpay/utils.py
index 0ad3f53354..7cab2f5635 100644
--- a/saleor/payment/gateways/razorpay/utils.py
+++ b/saleor/payment/gateways/razorpay/utils.py
@@ -8,5 +8,6 @@ def get_error_response(amount: Decimal, **additional_kwargs) -> dict:
def get_amount_for_razorpay(amount: Decimal) -> int:
- """Convert a decimal amount to int, by multiplying the value by 100."""
+ """Convert an amount of Indian rupees to paisa (needed by the razorpay)
+ by multiplying the value by 100."""
return int(amount * 100)
diff --git a/tests/gateway/test_razorpay.py b/tests/gateway/test_razorpay.py
index debb9c6228..be2942250b 100644
--- a/tests/gateway/test_razorpay.py
+++ b/tests/gateway/test_razorpay.py
@@ -6,10 +6,9 @@ import pytest
from razorpay.errors import BadRequestError, ServerError
from saleor.payment import ChargeStatus, TransactionKind
from saleor.payment.gateways.razorpay import (
- ERROR_MSG_INVALID_REQUEST, ERROR_MSG_ORDER_NOT_CHARGED,
- ERROR_MSG_SERVER_ERROR, ERROR_UNSUPPORTED_CURRENCY, charge,
- clean_razorpay_response, get_amount_for_razorpay, get_client,
- get_client_token, get_form_class, logger, refund)
+ charge, clean_razorpay_response, errors, get_amount_for_razorpay,
+ get_client, get_client_token, get_form_class, logger, refund,
+ check_payment_supported)
from saleor.payment.gateways.razorpay.forms import (
RazorPayCheckoutWidget, RazorPaymentForm)
@@ -90,6 +89,17 @@ def test_checkout_form(razorpay_payment, gateway_params):
mocked_charge.assert_called_once_with('123')
+def test_check_payment_supported(razorpay_payment):
+ found_error = check_payment_supported(razorpay_payment)
+ assert not found_error
+
+
+def test_check_payment_supported_non_supported(razorpay_payment):
+ razorpay_payment.currency = 'USD'
+ found_error = check_payment_supported(razorpay_payment)
+ assert found_error
+
+
def test_get_amount_for_razorpay():
assert get_amount_for_razorpay(Decimal('61.33')) == 6133
@@ -160,7 +170,7 @@ def test_charge_unsupported_currency(razorpay_payment, gateway_params):
TRANSACTION_AMOUNT, **gateway_params)
# Ensure a error was returned
- assert error == ERROR_UNSUPPORTED_CURRENCY % {'currency': 'USD'}
+ assert error == errors.UNSUPPORTED_CURRENCY % {'currency': 'USD'}
assert not txn.is_success
# Ensure the transaction is correctly set
@@ -190,7 +200,7 @@ def test_charge_invalid_request(
TRANSACTION_AMOUNT, **gateway_params)
# Ensure an error was returned
- assert error == ERROR_MSG_INVALID_REQUEST
+ assert error == errors.INVALID_REQUEST
assert not txn.is_success
# Ensure the transaction is correctly set
@@ -241,7 +251,7 @@ def test_refund_unsupported_currency(
razorpay_payment, TRANSACTION_AMOUNT, **gateway_params)
# Ensure a error was returned
- assert error == ERROR_UNSUPPORTED_CURRENCY % {'currency': 'USD'}
+ assert error == errors.UNSUPPORTED_CURRENCY % {'currency': 'USD'}
assert not txn.is_success
# Ensure the transaction is correctly set
@@ -266,7 +276,7 @@ def test_refund_not_paid(
razorpay_payment, TRANSACTION_AMOUNT, **gateway_params)
# Ensure a error was returned
- assert error == ERROR_MSG_ORDER_NOT_CHARGED
+ assert error == errors.ORDER_NOT_CHARGED
assert not txn.is_success
# Ensure the transaction is correctly set
@@ -293,7 +303,7 @@ def test_refund_invalid_data(
charged_payment, TRANSACTION_AMOUNT, **gateway_params)
# Ensure a error was returned
- assert error == ERROR_MSG_SERVER_ERROR
+ assert error == errors.SERVER_ERROR
assert not txn.is_success
# Ensure the transaction is correctly set