Skip to content

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

Braintree adjustments and fixes

Michał Jelonek00070b4bcf58
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index 06dca1e899..e4dbc74a40 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -9,6 +9,7 @@ from django.utils.translation import pgettext_lazy
from prices import Money
from .forms import BraintreePaymentForm
+from .errors import BraintreeException
logger = logging.getLogger(__name__)
@@ -30,23 +31,23 @@ ERROR_CODES_WHITELIST = {
}
-def get_customer_data(payment: Dict) -> Dict:
+def get_customer_data(payment_information: Dict) -> Dict:
+ billing = payment_information['billing']
return {
- 'order_id': payment['order'],
+ 'order_id': payment_information['order_id'],
'billing': {
- 'first_name': payment['billing_first_name'],
- 'last_name': payment['billing_last_name'],
- 'company': payment['billing_company_name'],
- 'postal_code': payment['billing_postal_code'],
- 'street_address': payment['billing_address_1'],
- 'extended_address': payment['billing_address_2'],
- "locality": payment['billing_city'],
- 'region': payment['billing_country_area'],
- 'country_code_alpha2': payment['billing_country_code']},
+ 'first_name': billing['first_name'],
+ 'last_name': billing['last_name'],
+ 'company': billing['company_name'],
+ 'postal_code': billing['postal_code'],
+ 'street_address': billing['street_address_1'],
+ 'extended_address': billing['street_address_2'],
+ 'locality': billing['city'],
+ 'region': billing['country_area'],
+ 'country_code_alpha2': billing['country']},
'risk_data': {
- 'customer_ip': payment['customer_ip_address'] or ''},
- 'customer': {
- 'email': payment['billing_email']}}
+ 'customer_ip': payment_information['customer_ip_address'] or ''},
+ 'customer': {'email': payment_information['customer_email']}}
def get_error_for_client(errors: List) -> str:
@@ -71,21 +72,17 @@ def extract_gateway_response(braintree_result) -> Dict:
errors = [
{'code': error.code, 'message': error.message}
for error in braintree_result.errors.deep_errors]
+
bt_transaction = braintree_result.transaction
if not bt_transaction:
return {'errors': errors}
- if bt_transaction.currency_iso_code != settings.DEFAULT_CURRENCY:
- logger.error(
- 'Braintree\'s currency is different than shop\'s currency')
- # FIXME we should have a predefined list of fields that will be supported
- # in the API
- gateway_response = {
+
+ return {
'transaction_id': getattr(bt_transaction, 'id', ''),
'currency': bt_transaction.currency_iso_code,
'amount': bt_transaction.amount, # Decimal type
'credit_card': bt_transaction.credit_card,
'errors': errors}
- return gateway_response
def get_template():
@@ -119,110 +116,107 @@ def get_client_token(**connection_params: Dict) -> str:
def process_payment(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params: Dict) -> Dict:
- auth_resp = authorize(payment, payment_token, **connection_params)
- transaction_token = auth_resp['transaction_id']
+ payment_information: Dict, **connection_params: Dict) -> Dict:
+ auth_resp = authorize(payment_information, **connection_params)
+ payment_information['token'] = auth_resp['transaction_id']
try:
- return capture(transaction_token, amount, **connection_params)
+ return [auth_resp, capture(payment_information, **connection_params)]
except Exception as e:
- void(transaction_token, **connection_params)
- raise
+ return [auth_resp, void(payment_information, **connection_params)]
-def authorize(
- payment: Dict, payment_token: str, **connection_params: Dict) -> Dict:
+def authorize(payment_information: Dict, **connection_params: Dict) -> Dict:
gateway = get_braintree_gateway(**connection_params)
try:
result = gateway.transaction.sale({
- 'amount': str(payment['total']),
- 'payment_method_nonce': payment_token,
+ 'amount': str(payment_information['amount']),
+ 'payment_method_nonce': payment_information['token'],
'options': {
'submit_for_settlement': CONFIRM_MANUALLY,
'three_d_secure': {
'required': THREE_D_SECURE_REQUIRED}},
- **get_customer_data(payment)})
+ **get_customer_data(payment_information)})
except braintree_sdk.exceptions.NotFoundError:
- # FIXME: make this a payment generic exception
- raise Exception('Unable to process the transaction. '
- 'Transaction\'s token is incorrect or expired.')
+ raise BraintreeException
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response['errors'])
return {
'is_success': result.is_success,
- 'transaction_id': gateway_response['transaction_id'],
- 'raw_response': gateway_response,
- 'errors': error,
- }
+ 'kind': 'auth',
+ 'amount': gateway_response.get('amount'),
+ 'currency': gateway_response.get('currency'),
+ 'transaction_id': gateway_response.get('transaction_id'),
+ 'error': error,
+ 'raw_response': gateway_response}
-def capture(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params: Dict) -> Dict:
+def capture(payment_information: Dict, **connection_params: Dict) -> Dict:
gateway = get_braintree_gateway(**connection_params)
try:
result = gateway.transaction.submit_for_settlement(
- transaction_id=payment_token, amount=str(amount))
+ transaction_id=payment_information['token'],
+ amount=str(payment_information['amount']))
except braintree_sdk.exceptions.NotFoundError:
- raise Exception('Unable to process the transaction. '
- 'Transaction\'s token is incorrect or expired.')
+ raise BraintreeException
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response['errors'])
return {
'is_success': result.is_success,
- 'transaction_id': gateway_response['transaction_id'],
- 'raw_response': gateway_response,
- 'errors': error,
- }
+ 'kind': 'capture',
+ 'amount': gateway_response.get('amount'),
+ 'currency': gateway_response.get('currency'),
+ 'transaction_id': gateway_response.get('transaction_id'),
+ 'error': error,
+ 'raw_response': gateway_response}
-def void(
- payment: Dict, payment_token: str, **connection_params: Dict) -> Dict:
+def void(payment_information: Dict, **connection_params: Dict) -> Dict:
gateway = get_braintree_gateway(**connection_params)
try:
- result = gateway.transaction.void(transaction_id=payment_token)
+ result = gateway.transaction.void(
+ transaction_id=payment_information['token'])
except braintree_sdk.exceptions.NotFoundError:
- raise Exception('Unable to process the transaction. '
- 'Transaction\'s token is incorrect or expired.')
+ raise BraintreeException
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response['errors'])
return {
'is_success': result.is_success,
- 'transaction_id': gateway_response['transaction_id'],
- 'raw_response': gateway_response,
- 'errors': error,
- }
+ 'kind': 'void',
+ 'amount': gateway_response.get('amount'),
+ 'currency': gateway_response.get('currency'),
+ 'transaction_id': gateway_response.get('transaction_id'),
+ 'error': error,
+ 'raw_response': gateway_response}
-def refund(
- payment: Dict, payment_token: str, amount: Decimal,
- **connection_params: Dict) -> Dict:
+def refund(payment_information: Dict, **connection_params: Dict) -> Dict:
gateway = get_braintree_gateway(**connection_params)
try:
result = gateway.transaction.refund(
- transaction_id=payment_token,
- amount_or_options=str(amount))
+ transaction_id=payment_information['token'],
+ amount_or_options=str(payment_information['amount']))
except braintree_sdk.exceptions.NotFoundError:
- raise Exception('Unable to process the transaction. '
- 'Transaction\'s token is incorrect or expired.')
+ raise BraintreeException
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response['errors'])
return {
'is_success': result.is_success,
- 'transaction_id': gateway_response['transaction_id'],
- 'raw_response': gateway_response,
- 'errors': error,
- }
+ 'kind': 'refund',
+ 'amount': gateway_response.get('amount'),
+ 'currency': gateway_response.get('currency'),
+ 'transaction_id': gateway_response.get('transaction_id'),
+ 'error': error,
+ 'raw_response': gateway_response}
diff --git a/saleor/payment/gateways/braintree/errors.py b/saleor/payment/gateways/braintree/errors.py
index cda3193781..32e9932c56 100644
--- a/saleor/payment/gateways/braintree/errors.py
+++ b/saleor/payment/gateways/braintree/errors.py
@@ -110,3 +110,17 @@ def get_error_type(error_code):
if error_code in error_codes:
return error_type
return DEFAULT_ERROR_TYPE
+
+
+DEFAULT_ERROR_MESSAGE = (
+ 'Unable to process the transaction. '
+ 'Transaction\'s token is incorrect or expired.')
+
+
+class BraintreeException(Exception):
+
+ def __init__(self, *args, **kwargs):
+ if args or kwargs:
+ super().__init__(*args, **kwargs)
+ else:
+ super().__init__(DEFAULT_ERROR_MESSAGE)
diff --git a/saleor/payment/gateways/braintree/forms.py b/saleor/payment/gateways/braintree/forms.py
index 530480b7ed..8c58bfb023 100644
--- a/saleor/payment/gateways/braintree/forms.py
+++ b/saleor/payment/gateways/braintree/forms.py
@@ -13,10 +13,10 @@ class BraintreePaymentForm(forms.Form):
# response
payment_method_nonce = forms.CharField()
- def __init__(self, amount, currency, gateway_params, *args, **kwargs):
+ def __init__(self, payment_information, gateway_params, *args, **kwargs):
super().__init__(*args, **kwargs)
- self._amount = amount
- self.fields['amount'].initial = amount
+ self.payment_information = payment_information
+ self.fields['amount'].initial = payment_information['amount']
# FIXME IMPROVEMENT:
# if environment is Sandbox, we could provide couple of predefined
@@ -29,7 +29,7 @@ class BraintreePaymentForm(forms.Form):
# when manually adjusting the template value as we do not allow
# partial-payments at this moment, error is returned instead.
amount = cleaned_data.get('amount')
- if amount and amount != self._amount:
+ if amount and amount != self.payment_information['amount']:
msg = pgettext_lazy(
'payment error',
'Unable to process transaction. Please try again in a moment')
diff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.py
index 6ff1b07f23..67452031f6 100644
--- a/tests/gateway/test_braintree.py
+++ b/tests/gateway/test_braintree.py
@@ -17,6 +17,10 @@ from saleor.payment.gateways.braintree import (
extract_gateway_response, get_braintree_gateway, get_client_token,
get_customer_data, get_error_for_client, refund, void)
from saleor.payment.gateways.braintree.forms import BraintreePaymentForm
+from saleor.payment.gateways.braintree.errors import (
+ BraintreeException, DEFAULT_ERROR_MESSAGE)
+from saleor.payment.utils import create_payment_information
+
INCORRECT_TOKEN_ERROR = (
'Unable to process the transaction. Transaction\'s token is incorrect '
@@ -89,7 +93,8 @@ def success_gateway_response(gateway_response):
def test_get_customer_data(payment_dummy):
payment = payment_dummy
- result = get_customer_data(payment)
+ payment_info = create_payment_information(payment)
+ result = get_customer_data(payment_info)
expected_result = {
'order_id': payment.order_id,
'billing': {
@@ -99,7 +104,7 @@ def test_get_customer_data(payment_dummy):
'postal_code': payment.billing_postal_code,
'street_address': payment.billing_address_1[:255],
'extended_address': payment.billing_address_2[:255],
- "locality": payment.billing_city,
+ 'locality': payment.billing_city,
'region': payment.billing_country_area,
'country_code_alpha2': payment.billing_country_code},
'risk_data': {
@@ -138,7 +143,8 @@ def test_extract_gateway_response(braintree_success_response):
'currency': t.currency_iso_code,
'amount': t.amount,
'credit_card': t.credit_card,
- 'errors': []}
+ 'errors': [],
+ 'transaction_id': t.id}
assert result == expected_result
@@ -194,32 +200,28 @@ def test_authorize_error_response(
payment_token = 'payment-token'
mock_response = Mock(return_value=braintree_error_response)
mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response))
- txn, error = authorize(payment, payment_token)
- assert txn.gateway_response == extract_gateway_response(
- braintree_error_response)
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
- assert error == DEFAULT_ERROR
+ payment_info = create_payment_information(payment, payment_token)
+ response = authorize(payment_info)
+
+ assert response['raw_response'] == extract_gateway_response(braintree_error_response)
+ assert not response['is_success']
+ assert response['error'] == DEFAULT_ERROR
@pytest.mark.integration
-@patch('saleor.payment.gateways.braintree.transaction_and_incorrect_token_error')
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
def test_authorize_incorrect_token(
- mock_gateway, mock_transaction, payment_dummy,
- braintree_not_found_error):
+ mock_gateway, payment_dummy, braintree_not_found_error):
payment = payment_dummy
payment_token = 'payment-token'
mock_response = Mock(side_effect=braintree_not_found_error)
mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response))
- expected_result = ('txn', 'error')
- mock_transaction.return_value = expected_result
- result = authorize(payment, payment_token)
- assert result == expected_result
- mock_transaction.assert_called_once_with(
- payment, kind=TransactionKind.AUTH, token=payment_token)
+
+ payment_info = create_payment_information(payment, payment_token)
+ with pytest.raises(BraintreeException) as e:
+ authorize(payment_info)
+ assert str(e.value) == DEFAULT_ERROR_MESSAGE
@pytest.mark.integration
@@ -230,26 +232,24 @@ def test_authorize(
mock_response = Mock(return_value=braintree_success_response)
mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response))
- txn, error = authorize(payment, 'authentication-token')
- assert not error
+ payment_info = create_payment_information(payment, 'auth-token')
+ response = authorize(payment_info)
+ assert not response['error']
- assert txn.payment == payment
- assert txn.kind == TransactionKind.AUTH
- assert txn.amount == braintree_success_response.transaction.amount
- assert txn.currency == braintree_success_response.transaction.currency_iso_code
- assert txn.gateway_response == success_gateway_response(
- braintree_success_response)
- assert txn.token == braintree_success_response.transaction.id
- assert txn.is_success == braintree_success_response.is_success
+ assert response['kind'] == TransactionKind.AUTH
+ assert response['amount'] == braintree_success_response.transaction.amount
+ assert response['currency'] == braintree_success_response.transaction.currency_iso_code
+ assert response['transaction_id'] == braintree_success_response.transaction.id
+ assert response['is_success'] == braintree_success_response.is_success
mock_response.assert_called_once_with({
'amount': str(payment.total),
- 'payment_method_nonce': 'authentication-token',
+ 'payment_method_nonce': 'auth-token',
'options': {
'submit_for_settlement': CONFIRM_MANUALLY,
'three_d_secure': {
'required': THREE_D_SECURE_REQUIRED}},
- **get_customer_data(payment)})
+ **get_customer_data(payment_info)})
@pytest.mark.integration
@@ -261,43 +261,34 @@ def test_refund(
amount = Decimal('10.00')
mock_response = Mock(return_value=braintree_success_response)
mock_gateway.return_value = Mock(transaction=Mock(refund=mock_response))
- txn, error = refund(payment, amount)
- assert not error
-
- assert txn.payment == payment
- assert txn.kind == TransactionKind.REFUND
- assert txn.amount == braintree_success_response.transaction.amount
- assert txn.currency == braintree_success_response.transaction.currency_iso_code
- assert txn.gateway_response == success_gateway_response(
- braintree_success_response)
- assert txn.token == braintree_success_response.transaction.id
- assert txn.is_success == braintree_success_response.is_success
-
- capture_txn = payment.transactions.filter(
- kind=TransactionKind.CAPTURE).first()
+
+ payment_info = create_payment_information(payment, 'token', amount)
+ response = refund(payment_info)
+ assert not response['error']
+
+ assert response['kind'] == TransactionKind.REFUND
+ assert response['amount'] == braintree_success_response.transaction.amount
+ assert response['currency'] == braintree_success_response.transaction.currency_iso_code
+ assert response['transaction_id'] == braintree_success_response.transaction.id
+ assert response['is_success'] == braintree_success_response.is_success
mock_response.assert_called_once_with(
- amount_or_options=str(amount), transaction_id=capture_txn.token)
+ amount_or_options=str(amount), transaction_id=payment_info['token'])
@pytest.mark.integration
-@patch('saleor.payment.gateways.braintree.transaction_and_incorrect_token_error')
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
def test_refund_incorrect_token(
- mock_gateway, mock_transaction, payment_txn_captured,
- braintree_not_found_error):
+ mock_gateway, payment_txn_captured, braintree_not_found_error):
payment = payment_txn_captured
amount = Decimal('10.00')
+
mock_response = Mock(side_effect=braintree_not_found_error)
mock_gateway.return_value = Mock(transaction=Mock(refund=mock_response))
- expected_result = ('txn', 'error')
- mock_transaction.return_value = expected_result
- result = refund(payment, amount)
- assert result == expected_result
- capture_txn = payment.transactions.filter(
- kind=TransactionKind.CAPTURE).first()
- mock_transaction.assert_called_once_with(
- payment, kind=TransactionKind.REFUND, token=capture_txn.token,
- amount=amount)
+
+ payment_info = create_payment_information(payment, 'token', amount)
+ with pytest.raises(BraintreeException) as e:
+ refund(payment_info)
+ assert str(e.value) == DEFAULT_ERROR_MESSAGE
@pytest.mark.integration
@@ -308,14 +299,14 @@ def test_refund_error_response(
amount = Decimal('10.00')
mock_response = Mock(return_value=braintree_error_response)
mock_gateway.return_value = Mock(transaction=Mock(refund=mock_response))
- txn, error = refund(payment, amount)
- assert txn.gateway_response == extract_gateway_response(
+ payment_info = create_payment_information(payment, 'token', amount)
+ response = refund(payment_info)
+
+ assert response['raw_response'] == extract_gateway_response(
braintree_error_response)
- assert not txn.is_success
- assert txn.amount == amount
- assert txn.currency == payment.currency
- assert error == DEFAULT_ERROR
+ assert not response['is_success']
+ assert response['error'] == DEFAULT_ERROR
@pytest.mark.integration
@@ -327,47 +318,36 @@ def test_capture(
amount = Decimal('10.00')
mock_response = Mock(return_value=braintree_success_response)
mock_gateway.return_value = Mock(
- transaction=Mock(
- submit_for_settlement=mock_response))
- txn, error = capture(payment, amount)
- assert not error
-
- assert txn.payment == payment
- assert txn.kind == TransactionKind.CAPTURE
- assert txn.amount == braintree_success_response.transaction.amount
- assert txn.currency == braintree_success_response.transaction.currency_iso_code
- assert txn.gateway_response == success_gateway_response(
- braintree_success_response)
- assert txn.token == braintree_success_response.transaction.id
- assert txn.is_success == braintree_success_response.is_success
-
- auth_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH).first()
+ transaction=Mock(submit_for_settlement=mock_response))
+
+ payment_info = create_payment_information(payment, 'token', amount)
+ response = capture(payment_info)
+ assert not response['error']
+
+ assert response['kind'] == TransactionKind.CAPTURE
+ assert response['amount'] == braintree_success_response.transaction.amount
+ assert response['currency'] == braintree_success_response.transaction.currency_iso_code
+ assert response['transaction_id'] == braintree_success_response.transaction.id
+ assert response['is_success'] == braintree_success_response.is_success
+
mock_response.assert_called_once_with(
- amount=str(amount), transaction_id=auth_txn.token)
+ amount=str(amount), transaction_id=payment_info['token'])
@pytest.mark.integration
-@patch('saleor.payment.gateways.braintree.transaction_and_incorrect_token_error')
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
def test_capture_incorrect_token(
- mock_gateway, mock_transaction, payment_txn_preauth,
- braintree_not_found_error):
+ mock_gateway, payment_txn_preauth, braintree_not_found_error):
payment = payment_txn_preauth
amount = Decimal('10.00')
mock_response = Mock(side_effect=braintree_not_found_error)
mock_gateway.return_value = Mock(
- transaction=Mock(
- submit_for_settlement=mock_response))
- expected_result = ('txn', 'error')
- mock_transaction.return_value = expected_result
- result = capture(payment, amount)
- assert result == expected_result
- auth_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH).first()
- mock_transaction.assert_called_once_with(
- payment, kind=TransactionKind.CAPTURE, token=auth_txn.token,
- amount=amount)
+ transaction=Mock(submit_for_settlement=mock_response))
+
+ payment_info = create_payment_information(payment)
+ with pytest.raises(BraintreeException) as e:
+ capture(payment_info)
+ assert str(e.value) == DEFAULT_ERROR_MESSAGE
@pytest.mark.integration
@@ -379,14 +359,14 @@ def test_capture_error_response(
mock_response = Mock(return_value=braintree_error_response)
mock_gateway.return_value = Mock(
transaction=Mock(submit_for_settlement=mock_response))
- txn, error = capture(payment, amount)
- assert txn.gateway_response == extract_gateway_response(
+ payment_info = create_payment_information(payment, 'token')
+ response = capture(payment_info)
+
+ assert response['raw_response'] == extract_gateway_response(
braintree_error_response)
- assert not txn.is_success
- assert txn.amount == amount
- assert txn.currency == payment.currency
- assert error == DEFAULT_ERROR
+ assert not response['is_success']
+ assert response['error'] == DEFAULT_ERROR
@pytest.mark.integration
@@ -396,41 +376,32 @@ def test_void(
payment = payment_txn_preauth
mock_response = Mock(return_value=braintree_success_response)
mock_gateway.return_value = Mock(transaction=Mock(void=mock_response))
- txn, error = void(payment)
- assert not error
- assert txn.payment == payment
- assert txn.kind == TransactionKind.VOID
- assert txn.amount == braintree_success_response.transaction.amount
- assert txn.currency == braintree_success_response.transaction.currency_iso_code
- assert txn.gateway_response == success_gateway_response(
- braintree_success_response)
- assert txn.token == braintree_success_response.transaction.id
- assert txn.is_success == braintree_success_response.is_success
+ payment_info = create_payment_information(payment, 'token')
+ response = void(payment_info)
+ assert not response['error']
- auth_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH).first()
- mock_response.assert_called_once_with(transaction_id=auth_txn.token)
+ assert response['kind'] == TransactionKind.VOID
+ assert response['amount'] == braintree_success_response.transaction.amount
+ assert response['currency'] == braintree_success_response.transaction.currency_iso_code
+ assert response['transaction_id'] == braintree_success_response.transaction.id
+ assert response['is_success'] == braintree_success_response.is_success
+ mock_response.assert_called_once_with(transaction_id=payment_info['token'])
@pytest.mark.integration
-@patch('saleor.payment.gateways.braintree.transaction_and_incorrect_token_error')
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
def test_void_incorrect_token(
- mock_gateway, mock_transaction, payment_txn_preauth,
- braintree_not_found_error):
+ mock_gateway, payment_txn_preauth, braintree_not_found_error):
payment = payment_txn_preauth
+
mock_response = Mock(side_effect=braintree_not_found_error)
mock_gateway.return_value = Mock(transaction=Mock(void=mock_response))
- expected_result = ('txn', 'error')
- mock_transaction.return_value = expected_result
- result = void(payment)
- assert result == expected_result
- auth_txn = payment.transactions.filter(
- kind=TransactionKind.AUTH).first()
- mock_transaction.assert_called_once_with(
- payment, kind=TransactionKind.VOID, token=auth_txn.token)
+ payment_info = create_payment_information(payment)
+ with pytest.raises(BraintreeException) as e:
+ void(payment_info)
+ assert str(e.value) == DEFAULT_ERROR_MESSAGE
@pytest.mark.integration
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
@@ -439,25 +410,26 @@ def test_void_error_response(
payment = payment_txn_preauth
mock_response = Mock(return_value=braintree_error_response)
mock_gateway.return_value = Mock(transaction=Mock(void=mock_response))
- txn, error = void(payment)
- assert txn.gateway_response == extract_gateway_response(
+ payment_info = create_payment_information(payment)
+ response = void(payment_info)
+
+ assert response['raw_response'] == extract_gateway_response(
braintree_error_response)
- assert not txn.is_success
- assert txn.amount == payment.total
- assert txn.currency == payment.currency
- assert error == DEFAULT_ERROR
+ assert not response['is_success']
+ assert response['error'] == DEFAULT_ERROR
def test_braintree_payment_form_incorrect_amount(payment_dummy):
amount = Decimal('0.01')
data = {'amount': amount, 'payment_method_nonce': 'fake-nonce'}
assert amount != payment_dummy.total
+ payment_info = create_payment_information(payment_dummy)
- payment_gateway, gateway_params = get_payment_gateway(
- payment_dummy.gateway)
form = BraintreePaymentForm(
- data=data, amount=payment_dummy.total, gateway=payment_gateway)
+ data=data, payment_information=payment_info,
+ gateway_params={'secret': '123'})
+
assert not form.is_valid()
assert form.non_field_errors
@@ -465,10 +437,14 @@ def test_braintree_payment_form_incorrect_amount(payment_dummy):
def test_braintree_payment_form(settings, payment_dummy):
payment = payment_dummy
payment.gateway = settings.BRAINTREE
+
data = {'amount': payment.total, 'payment_method_nonce': 'fake-nonce'}
+ payment_info = create_payment_information(payment)
form = BraintreePaymentForm(
- data=data, amount=payment.total, currency=payment.currency)
+ data=data, payment_information=payment_info,
+ gateway_params={'secret': '123'})
+
assert form.is_valid()