Skip to content

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

Fix naming between payment_token, transaction_token and transaction_id

Pacu243d0c5a0f081
diff --git a/docs/guides/payments.rst b/docs/guides/payments.rst
index b19d7abddc..16362fdfce 100644
--- a/docs/guides/payments.rst
+++ b/docs/guides/payments.rst
@@ -18,13 +18,13 @@ Your changes should live under the
available from the API level. You will also need to integrate it into your
Frontend's workflow.
-get_transaction_token(**connection_params)
+get_client_token(**connection_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-A transaction token is a signed data blob that includes configuration and
+A client token is a signed data blob that includes configuration and
authorization information required by the payment gateway.
-These should not be reused; a new transaction token should be generated for
+These should not be reused; a new client token should be generated for
each payment request.
Example
@@ -32,12 +32,12 @@ Example
.. code-block:: python
- def get_transaction_token(**connection_params: Dict) -> str:
+ def get_client_token(**connection_params: Dict) -> str:
gateway = get_payment_gateway(**connection_params)
- transaction_token = gateway.transaction_token.generate()
- return transaction_token
+ client_token = gateway.client_token.generate()
+ return client_token
-authorize(payment, transaction_token, **connection_params)
+authorize(payment, payment_token, **connection_params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
A process of reserving the amount of money against the customer's funding
@@ -50,11 +50,11 @@ Example
def authorize(
payment: Payment,
- transaction_token: str,
+ payment_token: str,
**connection_params: Dict) -> Tuple[Transaction, str]:
# Handle connecting to the gateway and sending the auth request here
- response = gateway.auth(token=transaction_token)
+ response = gateway.authorize(token=payment_token)
txn = Transaction.objects.create(
payment=payment,
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 4e24dbca15..f6895c42f6 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -2,6 +2,7 @@ import itertools
import os
import random
import unicodedata
+import uuid
from datetime import date
from decimal import Decimal
from unittest.mock import patch
@@ -28,7 +29,7 @@ from ...menu.models import Menu
from ...order.models import Fulfillment, Order
from ...order.utils import update_order_status
from ...page.models import Page
-from ...payment import ChargeStatus, Transactions, get_payment_gateway
+from ...payment import ChargeStatus, Transactions
from ...payment.models import Payment
from ...payment.utils import get_billing_data
from ...product.models import (
@@ -379,7 +380,7 @@ def create_fake_user():
return user
# We don't want to spam the console with payment confirmations sent to
-# non-existing fake customers.
+# fake customers.
@patch('saleor.order.emails.send_payment_confirmation.delay')
def create_payment(mock_email_confirmation, order):
payment = Payment.objects.create(
@@ -387,27 +388,26 @@ def create_payment(mock_email_confirmation, order):
customer_ip_address=fake.ipv4(),
is_active=True,
order=order,
+ token=str(uuid.uuid4()),
total=order.total.gross.amount,
currency=order.total.gross.currency,
**get_billing_data(order))
- gateway, gateway_params = get_payment_gateway(payment.gateway)
- transaction_token = gateway.get_transaction_token(**gateway_params)
# Create authorization transaction
- gateway_authorize(payment, transaction_token, **gateway_params)
+ payment.authorize(payment.token)
# 20% chance to void the transaction at this stage
if random.choice([0, 0, 0, 0, 1]):
- gateway_void(payment, **gateway_params)
+ payment.void()
return payment
# 25% to end the payment at the authorization stage
if not random.choice([1, 1, 1, 0]):
return payment
# Create capture transaction
- gateway_capture(payment, payment.total, **gateway_params)
+ payment.capture()
# 25% to refund the payment
if random.choice([0, 0, 0, 1]):
- gateway_refund(payment, payment.total, **gateway_params)
+ payment.refund()
return payment
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index cc4a35df00..ab1d961eff 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -45,7 +45,7 @@ from .page.types import Page
from .product.schema import ProductMutations, ProductQueries
from .payment.types import Payment, PaymentGatewayEnum
from .payment.resolvers import (
- resolve_payments, resolve_payment_transaction_token)
+ resolve_payments, resolve_payment_client_token)
from .payment.mutations import (
CheckoutPaymentCreate, PaymentCapture, PaymentRefund,
PaymentVoid)
@@ -124,7 +124,7 @@ class Query(ProductQueries):
description=DESCRIPTIONS['page']),
description='List of the shop\'s pages.')
payment = graphene.Field(Payment, id=graphene.Argument(graphene.ID))
- payment_transaction_token = graphene.Field(
+ payment_client_token = graphene.Field(
graphene.String, args={'gateway': PaymentGatewayEnum()})
payments = PrefetchingConnectionField(
Payment,
@@ -218,8 +218,8 @@ class Query(ProductQueries):
def resolve_payment(self, info, id):
return graphene.Node.get_node_from_global_id(info, id, Payment)
- def resolve_payment_transaction_token(self, info, gateway=None):
- return resolve_payment_transaction_token(gateway)
+ def resolve_payment_client_token(self, info, gateway=None):
+ return resolve_payment_client_token(gateway)
@permission_required('order.manage_orders')
def resolve_payments(self, info, query=None, **kwargs):
diff --git a/saleor/graphql/payment/resolvers.py b/saleor/graphql/payment/resolvers.py
index 8fb400b2e7..9b233eca9a 100644
--- a/saleor/graphql/payment/resolvers.py
+++ b/saleor/graphql/payment/resolvers.py
@@ -1,5 +1,5 @@
from ...payment import models
-from ...payment.utils import gateway_get_transaction_token
+from ...payment.utils import gateway_get_client_token
from ..utils import filter_by_query_param
PAYMENT_SEARCH_FIELDS = ['id']
@@ -12,6 +12,6 @@ def resolve_payments(info, query):
return queryset
-def resolve_payment_transaction_token(gateway=None):
+def resolve_payment_client_token(gateway=None):
# FIXME Add tests
- return gateway_get_transaction_token(gateway)
+ return gateway_get_client_token(gateway)
diff --git a/saleor/graphql/payment/types.py b/saleor/graphql/payment/types.py
index ea2450d21b..fae97d7c22 100644
--- a/saleor/graphql/payment/types.py
+++ b/saleor/graphql/payment/types.py
@@ -67,6 +67,10 @@ class Payment(CountableDjangoObjectType):
transactions = graphene.List(
Transaction,
description='List of all transactions within this payment.')
+ available_capture_amount = graphene.Field(
+ Money, description='Maximum amount of money that can be captured.')
+ available_refund_amount = graphene.Field(
+ Money, description='Maximum amount of money that can be refunded.')
class Meta:
description = 'Represents a payment of a given type.'
@@ -110,3 +114,15 @@ class Payment(CountableDjangoObjectType):
def resolve_transactions(self, info):
return self.transactions.all()
+
+ def resolve_available_refund_amount(self, info):
+ # FIXME TESTME
+ if not self.can_refund():
+ return None
+ return self.get_captured_amount()
+
+ def resolve_available_capture_amount(self, info):
+ # FIXME TESTME
+ if not self.can_capture():
+ return None
+ return self.get_total() - self.get_captured_amount()
diff --git a/saleor/order/forms.py b/saleor/order/forms.py
index ffdaf8f509..695eb37514 100644
--- a/saleor/order/forms.py
+++ b/saleor/order/forms.py
@@ -3,7 +3,6 @@ from django.conf import settings
from django.utils.translation import pgettext_lazy
from ..account.forms import SignupForm
-from ..payment import can_be_voided
from ..payment.models import Payment
from .models import Order
@@ -34,7 +33,7 @@ class PaymentDeleteForm(forms.Form):
self._errors['number'] = self.error_class([
pgettext_lazy(
'Payment delete form error', 'Payment does not exist')])
- elif not can_be_voided(payment):
+ elif not payment.can_void():
self._errors['number'] = self.error_class([
pgettext_lazy(
'Payment delete form error',
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 341a5aa29c..b44880305b 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -101,8 +101,7 @@ def start_payment(request, order, gateway):
or payment.charge_status == ChargeStatus.FULLY_REFUNDED):
return redirect(order.get_absolute_url())
payment_gateway, gateway_params = get_payment_gateway(payment.gateway)
- transaction_token = payment_gateway.get_transaction_token(
- **gateway_params)
+ client_token = payment_gateway.get_client_token(**gateway_params)
form = get_form_for_payment(payment)
form = form(data=request.POST or None, instance=payment)
if form.is_valid():
@@ -115,7 +114,7 @@ def start_payment(request, order, gateway):
template = 'order/payment/%s.html' % gateway
ctx = {
'form': form, 'payment': payment,
- 'transaction_token': transaction_token, 'order': order}
+ 'client_token': client_token, 'order': order}
return TemplateResponse(
request, [template, 'order/payment/default.html'], ctx)
diff --git a/saleor/payment/__init__.py b/saleor/payment/__init__.py
index 8ca045dc21..f6a9a41f63 100644
--- a/saleor/payment/__init__.py
+++ b/saleor/payment/__init__.py
@@ -97,7 +97,3 @@ def get_payment_gateway(gateway_name):
gateway_params = settings.PAYMENT_GATEWAYS[gateway_name][
'connection_params']
return gateway_module, gateway_params
-
-
-def can_be_voided(payment):
- return payment.charge_status == ChargeStatus.NOT_CHARGED
diff --git a/saleor/payment/forms.py b/saleor/payment/forms.py
index 06b9827af9..34a38a1428 100644
--- a/saleor/payment/forms.py
+++ b/saleor/payment/forms.py
@@ -29,9 +29,10 @@ class DummyPaymentForm(forms.Form):
def process_payment(self):
# FIXME add tests
gateway, gateway_params = get_payment_gateway(self.instance.gateway)
- transaction_token = gateway.get_transaction_token(**gateway_params)
+ # Dummy provider requires no token
+ fake_token = gateway.get_client_token(**gateway_params)
+ self.instance.authorize(fake_token)
charge_status = self.cleaned_data['charge_status']
- self.instance.authorize(transaction_token)
if charge_status == ChargeStatus.NOT_CHARGED:
return
self.instance.capture()
@@ -44,9 +45,9 @@ class BraintreePaymentForm(forms.Form):
amount = forms.DecimalField()
# Unique transaction identifier returned by Braintree
- # for testing in the sandbox please refer to
+ # for testing in the sandbox mode please refer to
# https://developers.braintreepayments.com/reference/general/testing/python#nonces-representing-cards
- # as it's values should be hardcoded to simulate each payment-gateway
+ # as it's values should be hardcoded to simulate each payment gateway
# response
payment_method_nonce = forms.CharField()
@@ -69,12 +70,16 @@ class BraintreePaymentForm(forms.Form):
'payment error',
'Unable to process transaction. Please try again in a moment')
raise ValidationError(msg)
- cleaned_data['payment_method_nonce'] = 'fake-valid-nonce'
+ cleaned_data['payment_method_nonce'] = 'fake-valid-nonce' # FIXME remove after testing
return cleaned_data
def process_payment(self):
# FIXME add tests
- self.instance.authorize(self.cleaned_data['payment_method_nonce'])
+ payment_token = self.cleaned_data['payment_method_nonce']
+ self.instance.token = payment_token
+ self.instance.save(updated_fields=['token'])
+
+ self.instance.authorize(payment_token)
try:
self.instance.capture(self.instance.total)
except PaymentError as exc:
diff --git a/saleor/payment/gateways/braintree/__init__.py b/saleor/payment/gateways/braintree/__init__.py
index c658a75161..3188d50f71 100644
--- a/saleor/payment/gateways/braintree/__init__.py
+++ b/saleor/payment/gateways/braintree/__init__.py
@@ -129,21 +129,21 @@ def get_braintree_gateway(sandbox_mode, merchant_id, public_key, private_key):
return gateway
-def get_transaction_token(**connection_params: Dict) -> str:
+def get_client_token(**connection_params: Dict) -> str:
gateway = get_braintree_gateway(**connection_params)
- transaction_token = gateway.client_token.generate()
- return transaction_token
+ client_token = gateway.client_token.generate()
+ return client_token
def authorize(
payment: Payment,
- transaction_token: str,
+ payment_token: str,
**connection_params: Dict) -> Tuple[Transaction, str]:
gateway = get_braintree_gateway(**connection_params)
try:
result = gateway.transaction.sale({
'amount': str(payment.total),
- 'payment_method_nonce': transaction_token,
+ 'payment_method_nonce': payment_token,
'options': {
'submit_for_settlement': CONFIRM_MANUALLY,
'three_d_secure': {
@@ -151,7 +151,7 @@ def authorize(
**get_customer_data(payment)})
except braintree_sdk.exceptions.NotFoundError:
return transaction_and_incorrect_token_error(
- payment, type=Transactions.AUTH, token=transaction_token)
+ payment, type=Transactions.AUTH, token=payment_token)
gateway_response = extract_gateway_response(result)
error = get_error_for_client(gateway_response['errors'])
credit_card_data = gateway_response.pop('credit_cart')
diff --git a/saleor/payment/gateways/dummy/__init__.py b/saleor/payment/gateways/dummy/__init__.py
index 5aa49fc427..0cc7d57f09 100644
--- a/saleor/payment/gateways/dummy/__init__.py
+++ b/saleor/payment/gateways/dummy/__init__.py
@@ -14,11 +14,11 @@ def dummy_success():
return True
-def get_transaction_token(**connection_params):
+def get_client_token(**connection_params):
return str(uuid.uuid4())
-def authorize(payment: Payment, transaction_token: str, **connection_params):
+def authorize(payment: Payment, payment_token: str, **connection_params):
success = dummy_success()
error = None
if not success:
@@ -29,7 +29,7 @@ def authorize(payment: Payment, transaction_token: str, **connection_params):
amount=payment.total,
currency=payment.currency,
gateway_response={},
- token=str(uuid.uuid4()),
+ token=payment_token,
is_success=success)
return txn, error
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index ecfca43629..9f1bc07259 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -89,10 +89,10 @@ class Payment(models.Model):
return Money(
self.captured_amount, self.currency or settings.DEFAULT_CURRENCY)
- def authorize(self, transaction_token):
+ def authorize(self, payment_token):
from . import utils
return utils.gateway_authorize(
- payment=self, transaction_token=transaction_token)
+ payment=self, payment_token=payment_token)
def void(self):
from . import utils
@@ -112,10 +112,18 @@ class Payment(models.Model):
amount = self.captured_amount
return utils.gateway_refund(payment=self, amount=amount)
- def can_capture(self):
+ def can_authorize(self):
return (
self.is_active and self.charge_status == ChargeStatus.NOT_CHARGED)
+ def can_capture(self):
+ # FIXME should have auth transaction
+ not_charged = self.charge_status == ChargeStatus.NOT_CHARGED
+ not_fully_charged = (
+ self.charge_status == ChargeStatus.CHARGED
+ and self.get_total() > self.get_captured_amount())
+ return self.is_active and not_charged or not_fully_charged
+
def can_void(self):
return (
self.is_active and self.charge_status == ChargeStatus.NOT_CHARGED)
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index bcae0f79fa..e5c97423b6 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -7,7 +7,7 @@ from django.conf import settings
from django.db import transaction
from prices import Money
-from . import ChargeStatus, PaymentError, can_be_voided, get_payment_gateway
+from . import ChargeStatus, PaymentError, get_payment_gateway
from ..core import analytics
from ..order import OrderEvents, OrderEventsEmails
from ..order.emails import send_payment_confirmation
@@ -88,21 +88,26 @@ def create_transaction(
return txn
-def gateway_get_transaction_token(gateway_name: str):
+def gateway_get_client_token(gateway_name: str):
# FIXME Add tests
gateway, gateway_params = get_payment_gateway(gateway_name)
- return gateway.get_transaction_token(**gateway_params)
+ return gateway.get_client_token(**gateway_params)
@validate_payment
-def gateway_authorize(payment: Payment, transaction_token: str) -> Transaction:
- if not payment.charge_status == ChargeStatus.NOT_CHARGED:
+def gateway_authorize(payment: Payment, payment_token: str) -> Transaction:
+ """Authorizes the payment and creates relevant transaction.
+
+ Args:
+ - payment_token: One-time-use reference to payment information.
+ """
+ if not payment.can_authorize():
raise PaymentError('Charged transactions cannot be authorized again.')
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
txn, error = gateway.authorize(
- payment, transaction_token, **gateway_params)
+ payment, payment_token, **gateway_params)
# FIXME Create an order event ?
if not txn.is_success:
raise PaymentError(error)
@@ -113,8 +118,7 @@ def gateway_authorize(payment: Payment, transaction_token: str) -> Transaction:
def gateway_capture(payment: Payment, amount: Decimal) -> Transaction:
if amount <= 0:
raise PaymentError('Amount should be a positive number.')
- if payment.charge_status not in {ChargeStatus.CHARGED,
- ChargeStatus.NOT_CHARGED}:
+ if not payment.can_capture():
raise PaymentError('This payment cannot be captured.')
if amount > payment.total or amount > (
payment.total - payment.captured_amount):
@@ -139,7 +143,7 @@ def gateway_capture(payment: Payment, amount: Decimal) -> Transaction:
@validate_payment
def gateway_void(payment) -> Transaction:
- if not can_be_voided(payment):
+ if not payment.can_void():
raise PaymentError('Only pre-authorized transactions can be voided.')
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
@@ -159,9 +163,8 @@ def gateway_refund(payment, amount: Decimal) -> Transaction:
raise PaymentError('Amount should be a positive number.')
if amount > payment.captured_amount:
raise PaymentError('Cannot refund more than captured')
- if not payment.charge_status == ChargeStatus.CHARGED:
- raise PaymentError(
- 'Refund is possible only when transaction is captured.')
+ if not payment.can_refund():
+ raise PaymentError('This payment cannot be refunded.')
gateway, gateway_params = get_payment_gateway(payment.gateway)
with transaction.atomic():
diff --git a/templates/order/payment/braintree.html b/templates/order/payment/braintree.html
index 301f107678..0eef053d6e 100644
--- a/templates/order/payment/braintree.html
+++ b/templates/order/payment/braintree.html
@@ -10,6 +10,9 @@
{% for non_field_error in form.non_field_errors %}
{{ non_field_error }}
{% endfor %}
+ {% for error in form.errors %}
+ {{ error }}
+ {% endfor %}
</blockquote>
{% endif %}
<div class="form-group">
@@ -31,7 +34,7 @@
<script src="https://js.braintreegateway.com/web/dropin/1.13.0/js/dropin.min.js"></script>
<script>
var form = document.querySelector('#payment-form');
- var client_token = '{{ transaction_token }}';
+ var client_token = '{{ client_token }}';
braintree.dropin.create({
authorization: client_token,
container: '#bt-dropin',
diff --git a/tests/gateway/test_braintree.py b/tests/gateway/test_braintree.py
index a2c75a0732..af2eeab7ab 100644
--- a/tests/gateway/test_braintree.py
+++ b/tests/gateway/test_braintree.py
@@ -13,7 +13,7 @@ from prices import Money
from saleor.payment import Transactions
from saleor.payment.gateways.braintree import (
CONFIRM_MANUALLY, THREE_D_SECURE_REQUIRED, authorize, capture,
- extract_gateway_response, get_transaction_token, get_customer_data,
+ extract_gateway_response, get_client_token, get_customer_data,
get_error_for_client, get_braintree_gateway, refund,
transaction_and_incorrect_token_error, void)
@@ -188,14 +188,14 @@ def test_get_braintree_gateway_inproperly_configured(gateway_config):
@patch('saleor.payment.gateways.braintree.get_braintree_gateway')
-def test_get_transaction_token(mock_gateway, gateway_config):
- transaction_token = 'transaction-token'
- mock_generate = Mock(return_value='transaction-token')
+def test_get_client_token(mock_gateway, gateway_config):
+ client_token = 'client-token'
+ mock_generate = Mock(return_value='client-token')
mock_gateway.return_value = Mock(client_token=Mock(generate=mock_generate))
- result = get_transaction_token(**gateway_config)
+ result = get_client_token(**gateway_config)
mock_gateway.assert_called_once_with(**gateway_config)
mock_generate.assert_called_once_with()
- assert result == transaction_token
+ assert result == client_token
@pytest.mark.integration
@@ -203,10 +203,10 @@ def test_get_transaction_token(mock_gateway, gateway_config):
def test_authorize_error_response(
mock_gateway, payment_dummy, braintree_error_response):
payment = payment_dummy
- token = 'example-token'
+ 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, token)
+ txn, error = authorize(payment, payment_token)
assert txn.gateway_response == extract_gateway_response(
braintree_error_response)
@@ -224,12 +224,12 @@ def test_authorize_incorrect_token(
mock_gateway, mock_transaction, payment_dummy,
braintree_not_found_error):
payment = payment_dummy
- token = 'example-token'
+ 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, token)
+ result = authorize(payment, payment_token)
assert result == expected_result
mock_transaction.assert_called_once_with(
payment, type=Transactions.AUTH, token=token)
@@ -242,7 +242,8 @@ def test_authorize(
payment = payment_dummy
mock_response = Mock(return_value=braintree_success_response)
mock_gateway.return_value = Mock(transaction=Mock(sale=mock_response))
- txn, error = authorize(payment, 'transaction-token')
+
+ txn, error = authorize(payment, 'authentication-token')
assert not error
assert txn.payment == payment
@@ -256,7 +257,7 @@ def test_authorize(
mock_response.assert_called_once_with({
'amount': str(payment.total),
- 'payment_method_nonce': 'transaction-token',
+ 'payment_method_nonce': 'authentication-token',
'options': {
'submit_for_settlement': CONFIRM_MANUALLY,
'three_d_secure': {
diff --git a/tests/gateway/test_dummy.py b/tests/gateway/test_dummy.py
index 177adde67c..2f78a463ae 100644
--- a/tests/gateway/test_dummy.py
+++ b/tests/gateway/test_dummy.py
@@ -6,7 +6,7 @@ from saleor.payment import ChargeStatus, PaymentError, Transactions
def test_authorize(payment_dummy):
- txn = payment_dummy.authorize(transaction_token='Fake')
+ txn = payment_dummy.authorize(payment_token='Fake')
assert txn.is_success
assert txn.kind == Transactions.AUTH
assert txn.payment == payment_dummy
@@ -18,7 +18,7 @@ def test_authorize_gateway_error(payment_dummy, monkeypatch):
monkeypatch.setattr(
'saleor.payment.gateways.dummy.dummy_success', lambda: False)
with pytest.raises(PaymentError):
- txn = payment_dummy.authorize(transaction_token='Fake')
+ txn = payment_dummy.authorize(payment_token='Fake')
assert txn.kind == Transactions.AUTH
assert not txn.is_success
assert txn.payment == payment_dummy
diff --git a/tests/test_payment.py b/tests/test_payment.py
index 56600cbf6b..1b0000963e 100644
--- a/tests/test_payment.py
+++ b/tests/test_payment.py
@@ -10,7 +10,7 @@ from saleor.payment import (
ChargeStatus, PaymentError, Transactions, get_payment_gateway)
from saleor.payment.utils import (
create_payment, create_transaction, gateway_authorize, gateway_capture,
- gateway_get_transaction_token, gateway_refund, gateway_void,
+ gateway_get_client_token, gateway_refund, gateway_void,
get_billing_data, handle_fully_paid_order, validate_payment)
NOT_ACTIVE_PAYMENT_ERROR = 'This payment is no longer active.'
@@ -150,27 +150,27 @@ def test_create_transaction_no_gateway_response(transaction_data):
assert txn.gateway_response == {}
-def test_gateway_get_transaction_token(settings):
+def test_gateway_get_client_token(settings):
gateway_name = list(settings.PAYMENT_GATEWAYS.keys())[0]
gateway = settings.PAYMENT_GATEWAYS[gateway_name]
module = gateway['module']
- with patch('%s.get_transaction_token' % module) as transaction_token_mock:
- gateway_get_transaction_token(gateway_name)
+ with patch('%s.get_client_token' % module) as transaction_token_mock:
+ gateway_get_client_token(gateway_name)
transaction_token_mock.assert_called_once_with()
-def test_gateway_get_transaction_token_not_allowed_gateway(settings):
+def test_gateway_get_client_token_not_allowed_gateway(settings):
gateway = 'example-gateway'
settings.CHECKOUT_PAYMENT_GATEWAYS = {}
with pytest.raises(ValueError):
- gateway_get_transaction_token(gateway)
+ gateway_get_client_token(gateway)
-def test_gateway_get_transaction_token_not_existing_gateway(settings):
+def test_gateway_get_client_token_not_existing_gateway(settings):
gateway = 'example-gateway'
settings.CHECKOUT_PAYMENT_GATEWAYS = {gateway: 'Example gateway'}
with pytest.raises(ImproperlyConfigured):
- gateway_get_transaction_token(gateway)
+ gateway_get_client_token(gateway)
@pytest.mark.parametrize(
@@ -178,14 +178,14 @@ def test_gateway_get_transaction_token_not_existing_gateway(settings):
def test_payment_needs_to_be_active_for_any_action(func, payment_dummy):
payment_dummy.is_active = False
with pytest.raises(PaymentError) as exc:
- func(payment_dummy, 'transaction-token')
+ func(payment_dummy, 'token')
assert exc.value.message == NOT_ACTIVE_PAYMENT_ERROR
def test_gateway_authorize_errors(payment_dummy):
payment_dummy.charge_status = ChargeStatus.CHARGED
with pytest.raises(PaymentError) as exc:
- gateway_authorize(payment_dummy, 'transaction-token')
+ gateway_authorize(payment_dummy, 'payment-token')
assert exc.value.message == (
'Charged transactions cannot be authorized again.')
@@ -194,6 +194,7 @@ def test_gateway_authorize_errors(payment_dummy):
def test_gateway_authorize(
mock_get_payment_gateway, payment_txn_preauth, gateway_params,
transaction_token):
+ payment_token = transaction_token
txn = payment_txn_preauth.transactions.first()
payment = payment_txn_preauth
@@ -201,16 +202,17 @@ def test_gateway_authorize(
mock_get_payment_gateway.return_value = (
Mock(authorize=mock_authorize), gateway_params)
- gateway_authorize(payment, transaction_token)
+ gateway_authorize(payment, payment_token)
mock_get_payment_gateway.assert_called_once_with(payment.gateway)
mock_authorize.assert_called_once_with(
- payment, transaction_token, **gateway_params)
+ payment, payment_token, **gateway_params)
@patch('saleor.payment.utils.get_payment_gateway')
def test_gateway_authorize_failed(
mock_get_payment_gateway, payment_txn_preauth, gateway_params,
transaction_token):
+ payment_token = transaction_token
txn = payment_txn_preauth.transactions.first()
txn.is_success = False
payment = payment_txn_preauth
@@ -219,7 +221,7 @@ def test_gateway_authorize_failed(
mock_get_payment_gateway.return_value = (
Mock(authorize=mock_authorize), gateway_params)
with pytest.raises(PaymentError) as exc:
- gateway_authorize(payment, transaction_token)
+ gateway_authorize(payment, payment_token)
assert exc.value.message == EXAMPLE_ERROR