Skip to content

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

Improve gateway response validation + add tests

Michał Jelonek73b31d2fa77f
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index e6bdf8c714..538efd4f69 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -1,9 +1,11 @@
+import json
import logging
from decimal import Decimal
from functools import wraps
from typing import Dict, List, Optional
from django.conf import settings
+from django.core.serializers.json import DjangoJSONEncoder
from django.db import transaction
from django.forms.models import model_to_dict
from prices import Money
@@ -19,6 +21,9 @@ from .models import Payment, Transaction
logger = logging.getLogger(__name__)
GENERIC_TRANSACTION_ERROR = 'Transaction was unsuccessful'
+REQUIRED_GATEWAY_KEYS = {
+ 'transaction_id', 'is_success', 'kind', 'error', 'amount', 'currency'}
+ALLOWED_GATEWAY_KINDS = {choices[0] for choices in TransactionKind.CHOICES}
def get_billing_data(order):
@@ -204,15 +209,21 @@ def validate_gateway_response(responses):
if not isinstance(responses, list):
responses = [responses]
- required_fields = {
- 'transaction_id', 'is_success', 'kind', 'error', 'amount', 'currency'}
for response in responses:
- if not required_fields.issubset(response):
+ if not REQUIRED_GATEWAY_KEYS.issubset(response):
raise GatewayError(
'Gateway response needs to contain following keys: {}'.format(
- required_fields - response.keys()))
- #TODO: check if response is json serializable
- #TODO: check if response['kind'] is an allowed choice of TransactionKind
+ sorted(REQUIRED_GATEWAY_KEYS)))
+
+ if response['kind'] not in ALLOWED_GATEWAY_KINDS:
+ raise GatewayError(
+ 'Gateway response kind must be one of {}'.format(
+ sorted(ALLOWED_GATEWAY_KINDS)))
+
+ try:
+ json.dumps(response, cls=DjangoJSONEncoder)
+ except (TypeError, ValueError):
+ raise GatewayError('Gateway response needs to be json serializable')
@validate_payment
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 2ea36d3dc3..5dc93b2ec7 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -10,36 +10,45 @@ from django.forms.models import model_to_dict
from saleor.order import OrderEvents, OrderEventsEmails
from saleor.order.views import PAYMENT_TEMPLATE
from saleor.payment import (
- ChargeStatus, PaymentError, TransactionKind, get_payment_gateway)
+ ChargeStatus, GatewayError, PaymentError, TransactionKind,
+ get_payment_gateway)
from saleor.payment.models import Payment
from saleor.payment.utils import (
clean_authorize, clean_capture, clean_charge, create_payment,
create_transaction, gateway_authorize, gateway_capture, gateway_charge,
gateway_get_client_token, gateway_refund, gateway_void, get_billing_data,
handle_fully_paid_order, validate_payment, create_payment_information,
- gateway_process_payment)
+ gateway_process_payment, validate_gateway_response, ALLOWED_GATEWAY_KINDS,
+ REQUIRED_GATEWAY_KEYS)
NOT_ACTIVE_PAYMENT_ERROR = 'This payment is no longer active.'
EXAMPLE_ERROR = 'Example dummy error'
@pytest.fixture
-def transaction_data(payment_dummy, settings):
+def gateway_response(settings):
+ return {
+ 'is_success': True,
+ 'transaction_id': 'transaction-token',
+ 'amount': Decimal(14.50),
+ 'currency': settings.DEFAULT_CURRENCY,
+ 'kind': TransactionKind.CAPTURE,
+ 'error': None,
+ 'raw_response': {
+ 'credit_card_four': '1234',
+ 'transaction-id': 'transaction-token',
+ }
+ }
+
+
+def transaction_data(payment_dummy, gateway_response):
return {
'payment': payment_dummy,
'payment_information': create_payment_information(
- payment_dummy, 'token'),
+ payment_dummy, 'payment-token'),
'kind': TransactionKind.CAPTURE,
- 'gateway_response': {
- 'is_success': True,
- 'transaction_id': 'token',
- 'errors': None,
- 'amount': Decimal(14.50),
- 'currency': 'USD',
- 'kind': TransactionKind.CAPTURE,
- 'raw_response': {
- 'credit_card': '4321',
- 'transaction': 'token'}}}
+ 'gateway_response': gateway_response}
@pytest.fixture
@@ -786,3 +795,40 @@ def test_payment_get_authorized_amount(payment_txn_preauth):
payment.transactions.all().delete()
assert payment.get_authorized_amount().amount == Decimal(0)
+
+
+def test_validate_gateway_response(gateway_response):
+ validate_gateway_response(gateway_response)
+
+
+def test_validate_gateway_response_missing_required_key(gateway_response):
+ del gateway_response['is_success']
+
+ with pytest.raises(GatewayError) as e:
+ validate_gateway_response(gateway_response)
+
+ assert str(e.value) == (
+ 'Gateway response needs to contain following keys: {}'.format(
+ sorted(REQUIRED_GATEWAY_KEYS)))
+
+
+def test_validate_gateway_response_incorrect_transaction_kind(gateway_response):
+ gateway_response['kind'] = 'incorrect-kind'
+
+ with pytest.raises(GatewayError) as e:
+ validate_gateway_response(gateway_response)
+
+ assert str(e.value) == (
+ 'Gateway response kind must be one of {}'.format(
+ sorted(ALLOWED_GATEWAY_KINDS)))
+
+
+def test_validate_gateway_response_not_json_serializable(gateway_response):
+ class CustomClass(object):
+ pass
+ gateway_response['extra'] = CustomClass()
+
+ with pytest.raises(GatewayError) as e:
+ validate_gateway_response(gateway_response)
+
+ assert str(e.value) == 'Gateway response needs to be json serializable'