Skip to content

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

Improve error processing in payments

Artur Smęt39d593dd0760
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 0bdd12bf72..827ae18e61 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -94,7 +94,6 @@ def start_payment(request, order, variant):
'total': total.gross.amount,
'tax': total.tax.amount,
'currency': total.currency,
- 'delivery': order.shipping_price.net.amount,
'billing_first_name': billing.first_name,
'billing_last_name': billing.last_name,
'billing_address_1': billing.street_address_1,
@@ -103,9 +102,6 @@ def start_payment(request, order, variant):
'billing_postcode': billing.postal_code,
'billing_country_code': billing.country.code,
'billing_email': order.user_email,
- 'description': pgettext_lazy(
- 'Payment description', 'Order %(order_number)s') % {
- 'order_number': order},
'billing_country_area': billing.country_area,
'customer_ip_address': get_client_ip(request)}
variant_choices = settings.CHECKOUT_PAYMENT_CHOICES
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ff127e72b6..ae99a31662 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -62,11 +62,16 @@ class PaymentMethod(models.Model):
def get_tax_money(self):
return self._get_money(self.tax)
- def authorize(self):
+ def get_auth_transaction(self):
+ txn = self.transactions.get(
+ transaction_type=TransactionType.AUTH, is_success=True)
+ return txn
+
+ def authorize(self, client_token):
# FIXME
from . import utils
-
- return utils.gateway_authorize(payment_method=self)
+ return utils.gateway_authorize(
+ payment_method=self, transaction_token=client_token)
def void(self):
# FIXME
diff --git a/saleor/payment/providers/braintree/__init__.py b/saleor/payment/providers/braintree/__init__.py
index 8bceb9e649..173f0942c0 100644
--- a/saleor/payment/providers/braintree/__init__.py
+++ b/saleor/payment/providers/braintree/__init__.py
@@ -3,6 +3,7 @@ from typing import Dict
import braintree as braintree_sdk
from django.db import transaction
+from django.utils.translation import pgettext_lazy
from ... import PaymentMethodChargeStatus, TransactionType
from ...models import Transaction
@@ -13,6 +14,23 @@ from ...utils import create_transaction
AUTO_CHARGE = False
THREE_D_SECURE_REQUIRED = False
+def get_error_for_client(errors):
+ """
+ Filters all error messages and decides which one is visible for the
+ client side
+ """
+ if not errors:
+ return ''
+
+ default_msg = pgettext_lazy(
+ 'payment error', 'Unable to process transaction. Please try again in a moment')
+ error_codes_whitelist = [] #FIXME: Provide list of visible errors and messages translations
+ for error in errors:
+ if error['code'] in error_codes_whitelist:
+ return error['message']
+ return default_msg
+
+
def extract_gateway_response(braintree_result) -> Dict:
"""
Extract data from Braintree response that will be stored locally
@@ -67,6 +85,7 @@ def authorize(payment_method, transaction_token, **client_kwargs):
'required': THREE_D_SECURE_REQUIRED
}}})
gateway_response = extract_gateway_response(result)
+ error = get_error_for_client(gateway_response['errors'])
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
@@ -74,7 +93,7 @@ def authorize(payment_method, transaction_token, **client_kwargs):
gateway_response=gateway_response,
token=result.transaction.id,
is_success=result.is_success)
- return txn
+ return txn, error
def charge(payment_method, amount=None, **client_kwargs):
gateway = get_gateway(**client_kwargs)
@@ -86,6 +105,7 @@ def charge(payment_method, amount=None, **client_kwargs):
transaction_id=auth_transaction.token,
amount=amount)
gateway_response = extract_gateway_response(result)
+ error = get_error_for_client(gateway_response['errors'])
txn = create_transaction(
payment_method=payment_method,
@@ -94,7 +114,7 @@ def charge(payment_method, amount=None, **client_kwargs):
token=result.transaction.id,
is_success=result.is_success,
gateway_response=gateway_response)
- return txn
+ return txn, error
def void(payment_method, **client_kwargs):
gateway = get_gateway(**client_kwargs)
@@ -103,7 +123,7 @@ def void(payment_method, **client_kwargs):
result = gateway.transaction.void(
transaction_id=auth_transaction.token)
gateway_response = extract_gateway_response(result)
-
+ error = get_error_for_client(gateway_response['errors'])
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
@@ -111,7 +131,7 @@ def void(payment_method, **client_kwargs):
gateway_response=gateway_response,
token=result.transaction.id,
is_success=result.is_success)
- return txn
+ return txn, error
def refund(payment_method, amount=None, **client_kwargs):
gateway = get_gateway(**client_kwargs)
@@ -120,6 +140,7 @@ def refund(payment_method, amount=None, **client_kwargs):
result = gateway.transaction.refund(
transaction_id=auth_transaction.token, amount=amount)
gateway_response = extract_gateway_response(result)
+ error = get_error_for_client(gateway_response['errors'])
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.REFUND,
@@ -127,4 +148,4 @@ def refund(payment_method, amount=None, **client_kwargs):
token=result.transaction.id,
is_success=result.is_success,
gateway_response=gateway_response)
- return txn
+ return txn, error
diff --git a/saleor/payment/providers/dummy/__init__.py b/saleor/payment/providers/dummy/__init__.py
index cefa9deb99..e592899fc9 100644
--- a/saleor/payment/providers/dummy/__init__.py
+++ b/saleor/payment/providers/dummy/__init__.py
@@ -1,9 +1,12 @@
import uuid
+
from django.db import transaction
+from django.utils.translation import pgettext_lazy
-from ... import TransactionType, PaymentMethodChargeStatus
+from ... import PaymentMethodChargeStatus, TransactionType
from ...utils import create_transaction
+
def dummy_success():
return True
@@ -12,42 +15,58 @@ def get_client_token(**client_kwargs):
def authorize(payment_method, transaction_token, **client_kwargs):
+ success = dummy_success()
+ error = None
+ if not success:
+ error = 'Unable to authorize transaction'
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
amount=payment_method.total,
gateway_response={},
token=str(uuid.uuid4()),
- is_success=dummy_success())
- return txn
+ is_success=success)
+ return txn, error
def void(payment_method, **client_kwargs):
+ error = None
+ success = dummy_success()
+ if not success:
+ error = 'Unable to void transaction'
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
amount=payment_method.total,
gateway_response={},
token=str(uuid.uuid4()),
- is_success=dummy_success())
- return txn
+ is_success=success)
+ return txn, error
def charge(payment_method, amount=None):
+ error = None
+ success = dummy_success()
+ if not success:
+ error = 'Unable to process charge'
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.CHARGE,
amount=amount,
token=str(uuid.uuid4()),
- is_success=dummy_success())
- return txn
+ is_success=success)
+ return txn, error
def refund(payment_method, amount=None):
+ error = None
+ success = dummy_success()
+ if not success:
+ error = 'Unable to process refund'
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.REFUND,
amount=amount,
token=str(uuid.uuid4()),
- is_success=dummy_success())
- return txn
+ is_success=success)
+ return txn, error
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 9f02d06086..f24e40bb2a 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -43,13 +43,14 @@ def gateway_authorize(payment_method, transaction_token) -> Transaction:
provider, provider_params = get_provider(payment_method.variant)
with transaction.atomic():
- txn = provider.authorize(payment_method, transaction_token, **provider_params)
+ txn, error = provider.authorize(
+ payment_method, transaction_token, **provider_params)
if txn.is_success:
payment_method.charge_status = PaymentMethodChargeStatus.NOT_CHARGED
payment_method.save(update_fields=['charge_status'])
if not txn.is_success:
# TODO: Handle gateway response here somehow
- raise PaymentError('Unable to authorize transaction')
+ raise PaymentError(error)
return txn
@@ -65,7 +66,7 @@ def gateway_charge(payment_method, amount) -> Transaction:
raise PaymentError('Unable to charge more than authozied amount')
provider, provider_params = get_provider(payment_method.variant)
with transaction.atomic():
- txn = provider.charge(payment_method, amount=amount, **provider_params)
+ txn, error = provider.charge(payment_method, amount=amount, **provider_params)
if txn.is_success:
payment_method.charge_status = PaymentMethodChargeStatus.CHARGED
payment_method.captured_amount += txn.amount
@@ -74,7 +75,7 @@ def gateway_charge(payment_method, amount) -> Transaction:
if not txn.is_success:
# TODO: Handle gateway response here somehow
- raise PaymentError('Unable to process transaction')
+ raise PaymentError(error)
return txn
@@ -85,12 +86,12 @@ def gateway_void(payment_method) -> Transaction:
raise PaymentError('Only pre-authorized transactions can be void')
provider, provider_params = get_provider(payment_method.variant)
with transaction.atomic():
- txn = provider.void(payment_method, **provider_params)
+ txn, error = provider.void(payment_method, **provider_params)
if txn.is_success:
payment_method.is_active = False
payment_method.save(update_fields=['is_active'])
if not txn.is_success:
- raise PaymentError('Unable to void transaction')
+ raise PaymentError(error)
return txn
@@ -105,7 +106,7 @@ def gateway_refund(payment_method, amount) -> Transaction:
provider, provider_params = get_provider(payment_method.variant)
with transaction.atomic():
- txn = provider.refund(payment_method, amount, **provider_params)
+ txn, error = provider.refund(payment_method, amount, **provider_params)
if txn.is_success:
changed_fields = ['captured_amount']
if txn.amount == payment_method.total:
@@ -115,5 +116,5 @@ def gateway_refund(payment_method, amount) -> Transaction:
payment_method.captured_amount -= amount
payment_method.save(update_fields=changed_fields)
if not txn.is_success:
- raise PaymentError('Unable to process refund')
+ raise PaymentError(error)
return txn
diff --git a/tests/test_payments.py b/tests/test_payments.py
index 0cf97993b5..71aa7646d1 100644
--- a/tests/test_payments.py
+++ b/tests/test_payments.py
@@ -5,7 +5,7 @@ from saleor.payment import (
def test_authorize(payment_method_dummy):
- txn = payment_method_dummy.authorize()
+ txn = payment_method_dummy.authorize(client_token='Fake')
assert txn.is_success
assert txn.transaction_type == TransactionType.AUTH
assert txn.payment_method == payment_method_dummy
@@ -17,7 +17,7 @@ def test_authorize_gateway_error(payment_method_dummy, monkeypatch):
monkeypatch.setattr(
'saleor.payment.providers.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
- txn = payment_method_dummy.authorize()
+ txn = payment_method_dummy.authorize(client_token='Fake')
assert txn.transaction_type == TransactionType.AUTH
assert not txn.is_success
assert txn.payment_method == payment_method_dummy
@@ -55,7 +55,7 @@ def test_void_gateway_error(payment_method_dummy, monkeypatch):
monkeypatch.setattr(
'saleor.payment.providers.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
- txn = payment_method_dummy.authorize()
+ txn = payment_method_dummy.void()
assert txn.transaction_type == TransactionType.VOID
assert not txn.is_success
assert txn.payment_method == payment_method_dummy