Skip to content

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

Drop tax off PaymentMethod

Pacu22b5ffde4351f
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index e68ce25379..03a1dc95e6 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -381,21 +381,21 @@ def create_transactions(payment):
# FIXME Add tests for this function
payment.transactions.create(
transaction_type=TransactionType.AUTH,
- is_success=True, amount=payment.total.gross, gateway_response={})
+ is_success=True, amount=payment.total, gateway_response={})
if payment.charge_status == ChargeStatus.NOT_CHARGED:
if random.randint(0, 1):
payment.transactions.create(
transaction_type=TransactionType.VOID,
- is_success=True, amount=payment.total.gross,
+ is_success=True, amount=payment.total,
gateway_response={})
return
payment.transactions.create(
transaction_type=TransactionType.CAPTURE,
- is_success=True, amount=payment.total.gross, gateway_response={})
+ is_success=True, amount=payment.total, gateway_response={})
if payment.charge_status == ChargeStatus.FULLY_REFUNDED:
payment.transactions.create(
transaction_type=TransactionType.REFUND,
- is_success=True, amount=payment.total.gross,
+ is_success=True, amount=payment.total,
gateway_response={})
return payment
@@ -411,18 +411,12 @@ def create_payment(order):
order=order,
charge_status=status,
variant='default',
- total=order.total,
customer_ip_address=fake.ipv4(),
- billing_first_name=order.billing_address.first_name,
- billing_last_name=order.billing_address.last_name,
- billing_address_1=order.billing_address.street_address_1,
- billing_address_2=order.billing_address.street_address_2,
- billing_city=order.billing_address.city,
- billing_postal_code=order.billing_address.postal_code,
- billing_country_code=order.billing_address.country,
- billing_country_area=order.billing_address.country_area)
+ order=order,
+ total=order.total.gross,
+ **get_billing_data(order))
if status == ChargeStatus.CHARGED:
- payment.captured_amount = payment.total.gross
+ payment.captured_amount = payment.total
payment.save()
create_transactions(payment)
return payment
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 44bcdbd3ce..14aa9c04f8 100644
--- a/saleor/dashboard/order/forms.py
+++ b/saleor/dashboard/order/forms.py
@@ -354,7 +354,7 @@ class OrderMarkAsPaidForm(forms.Form):
def save(self):
# FIXME add more fields to the payment method
defaults = {
- 'total': self.order.total,
+ 'total': self.order.total.gross,
'captured_amount': self.order.total.gross,
**get_billing_data(self.order)}
PaymentMethod.objects.get_or_create(
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index ae21ba3873..81c88b9da7 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -290,7 +290,8 @@ class OrderMarkAsPaid(BaseMutation):
return OrderMarkAsPaid(errors=errors)
# FIXME add more fields to the payment method
defaults = {
- 'total': order.total, 'captured_amount': order.total.gross}
+ 'total': order.total.gross, 'captured_amount': order.total.gross,
+ **get_billing_data(order)}
PaymentMethod.objects.get_or_create(
variant=CustomPaymentChoices.MANUAL,
charge_status=ChargeStatus.CHARGED, order=order,
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index 3fb75a8599..9385c93475 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -208,7 +208,7 @@ class Order(CountableDjangoObjectType):
# FIXME adjust to multiple payments in the future
payment = obj.get_last_payment()
if payment:
- return payment.total.gross
+ return payment.total
@staticmethod
@gql_optimizer.resolver_hints(prefetch_related='payments')
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index a14b0f3437..287f14e7ab 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -59,7 +59,7 @@ class CheckoutPaymentMethodCreate(BaseMutation, I18nMixin):
extra_data = cls.get_extra_info(info)
gross = Money(input['amount'], currency=settings.DEFAULT_CURRENCY)
payment_method = create_payment_method(
- total_gross=gross,
+ total=gross,
variant=input['gateway'], billing_email=checkout.email,
extra_data=extra_data, checkout=checkout,
**billing_data)
diff --git a/saleor/order/views.py b/saleor/order/views.py
index 121f149d5d..66f32ce83e 100644
--- a/saleor/order/views.py
+++ b/saleor/order/views.py
@@ -87,9 +87,8 @@ def payment(request, token):
@check_order_status
def start_payment(request, order, variant):
- total = order.total
defaults = {
- 'total': total,
+ 'total': order.total.gross,
'customer_ip_address': get_client_ip(request),
**get_billing_data(order)}
variant_choices = settings.CHECKOUT_PAYMENT_CHOICES
diff --git a/saleor/payment/forms.py b/saleor/payment/forms.py
index 93a1786098..f569b96a42 100644
--- a/saleor/payment/forms.py
+++ b/saleor/payment/forms.py
@@ -11,6 +11,6 @@ class PaymentMethodForm(forms.ModelForm):
def authorize_payment(self):
self.instance.transactions.create(
- amount=self.instance.total.gross,
+ amount=self.instance.total,
transaction_type=TransactionType.AUTH,
gateway_response={}, is_success=True)
diff --git a/saleor/payment/migrations/0001_initial.py b/saleor/payment/migrations/0001_initial.py
index 1fd5e5e263..ea99984d21 100644
--- a/saleor/payment/migrations/0001_initial.py
+++ b/saleor/payment/migrations/0001_initial.py
@@ -39,8 +39,7 @@ class Migration(migrations.Migration):
('customer_ip_address', models.GenericIPAddressField(blank=True, null=True)),
('extra_data', models.TextField(blank=True, default='')),
('token', models.CharField(blank=True, default='', max_length=36)),
- ('total_net', django_prices.models.MoneyField(currency='USD', decimal_places=2, default=saleor.core.zero_money, max_digits=12)),
- ('total_gross', django_prices.models.MoneyField(currency='USD', decimal_places=2, default=saleor.core.zero_money, max_digits=12)),
+ ('total', django_prices.models.MoneyField(currency='USD', decimal_places=2, default=saleor.core.zero_money, max_digits=12)),
('captured_amount', django_prices.models.MoneyField(currency='USD', decimal_places=2, default=saleor.core.zero_money, max_digits=12)),
('checkout', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='payment_methods', to='checkout.Cart')),
('order', models.ForeignKey(null=True, on_delete=django.db.models.deletion.PROTECT, related_name='payment_methods', to='order.Order')),
diff --git a/saleor/payment/models.py b/saleor/payment/models.py
index e2c751b310..13358cbcc5 100644
--- a/saleor/payment/models.py
+++ b/saleor/payment/models.py
@@ -50,15 +50,10 @@ class PaymentMethod(models.Model):
extra_data = models.TextField(blank=True, default='')
token = models.CharField(max_length=36, blank=True, default='')
- total_net = MoneyField(
+ total = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12,
decimal_places=settings.DEFAULT_DECIMAL_PLACES,
default=zero_money)
- total_gross = MoneyField(
- currency=settings.DEFAULT_CURRENCY, max_digits=12,
- decimal_places=settings.DEFAULT_DECIMAL_PLACES,
- default=zero_money)
- total = TaxedMoneyField(net_field='total_net', gross_field='total_gross')
captured_amount = MoneyField(
currency=settings.DEFAULT_CURRENCY, max_digits=12,
decimal_places=settings.DEFAULT_DECIMAL_PLACES,
diff --git a/saleor/payment/providers/braintree/__init__.py b/saleor/payment/providers/braintree/__init__.py
index 8d9091f433..af45f1d0ee 100644
--- a/saleor/payment/providers/braintree/__init__.py
+++ b/saleor/payment/providers/braintree/__init__.py
@@ -103,7 +103,7 @@ def get_client_token(**client_kwargs):
def authorize(payment_method, transaction_token, **client_kwargs):
gateway = get_gateway(**client_kwargs)
result = gateway.transaction.sale({
- 'amount': str(payment_method.total.gross.amount),
+ 'amount': str(payment_method.total.amount),
'payment_method_nonce': transaction_token,
'options': {
'submit_for_settlement': CONFIRM_MANUALLY,
@@ -114,7 +114,7 @@ def authorize(payment_method, transaction_token, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
- amount=payment_method.total.gross.amount,
+ amount=payment_method.total.amount,
gateway_response=gateway_response,
token=getattr(result.transaction, 'id', ''),
is_success=result.is_success)
@@ -128,7 +128,7 @@ def capture(payment_method, amount=None, **client_kwargs):
auth_transaction = payment_method.transactions.filter(
transaction_type=TransactionType.AUTH).first()
if not amount:
- amount = payment_method.total.gross.amount
+ amount = payment_method.total.amount
result = gateway.transaction.submit_for_settlement(
transaction_id=auth_transaction.token, amount=amount)
gateway_response = extract_gateway_response(result)
@@ -156,7 +156,7 @@ def void(payment_method, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
- amount=payment_method.total.gross.amount,
+ amount=payment_method.total.amount,
gateway_response=gateway_response,
token=result.transaction.id,
is_success=result.is_success)
diff --git a/saleor/payment/providers/dummy/__init__.py b/saleor/payment/providers/dummy/__init__.py
index 244d37a976..b2776f58e0 100644
--- a/saleor/payment/providers/dummy/__init__.py
+++ b/saleor/payment/providers/dummy/__init__.py
@@ -20,7 +20,7 @@ def authorize(payment_method, transaction_token, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.AUTH,
- amount=payment_method.total.gross,
+ amount=payment_method.total,
gateway_response={},
token=str(uuid.uuid4()),
is_success=success)
@@ -35,7 +35,7 @@ def void(payment_method, **client_kwargs):
txn = create_transaction(
payment_method=payment_method,
transaction_type=TransactionType.VOID,
- amount=payment_method.total.gross,
+ amount=payment_method.total,
gateway_response={},
token=str(uuid.uuid4()),
is_success=success)
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index a93921b372..b9f8cb4ebe 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -117,8 +117,8 @@ def gateway_capture(
ChargeStatus.CHARGED,
ChargeStatus.NOT_CHARGED}:
raise PaymentError('This payment method cannot be captured.')
- if amount > payment_method.total.gross.amount or amount > (
- payment_method.total.gross.amount -
+ if amount > payment_method.total.amount or amount > (
+ payment_method.total.amount -
payment_method.captured_amount.amount):
raise PaymentError('Unable to capture more than authorized amount.')
@@ -173,11 +173,12 @@ def gateway_refund(
txn, error = provider.refund(payment_method, amount, **provider_params)
if txn.is_success:
changed_fields = ['captured_amount']
- if txn.amount == payment_method.total.gross.amount:
+ if txn.amount == payment_method.total.amount:
payment_method.charge_status = ChargeStatus.FULLY_REFUNDED
payment_method.is_active = False
changed_fields += ['charge_status', 'is_active']
- payment_method.captured_amount -= amount
+ payment_method.captured_amount -= Money(
+ txn.amount, settings.DEFAULT_CURRENCY)
payment_method.save(update_fields=changed_fields)
if not txn.is_success:
raise PaymentError(error)
diff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.py
index e8b50b5072..3bb6e87f54 100644
--- a/tests/api/test_checkout.py
+++ b/tests/api/test_checkout.py
@@ -369,7 +369,7 @@ def test_checkout_complete(
payment_method = payment_method_dummy
payment_method.is_active = True
payment_method.order = None
- payment_method.total = total
+ payment_method.total = total.gross
payment_method.captured_amount = total.gross
payment_method.checkout = checkout
payment_method.save()
diff --git a/tests/api/test_order.py b/tests/api/test_order.py
index 8dce4305fb..640868c2a3 100644
--- a/tests/api/test_order.py
+++ b/tests/api/test_order.py
@@ -911,7 +911,7 @@ def test_order_capture(
}
"""
order_id = graphene.Node.to_global_id('Order', order.id)
- amount = float(payment_method_txn_preauth.total.gross.amount)
+ amount = float(payment_method_txn_preauth.total.amount)
variables = {'id': order_id, 'amount': amount}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders])
@@ -1037,7 +1037,7 @@ def test_order_refund(
}
"""
order_id = graphene.Node.to_global_id('Order', order.id)
- amount = float(payment_method_txn_captured.total.gross.amount)
+ amount = float(payment_method_txn_captured.total.amount)
variables = {'id': order_id, 'amount': amount}
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_orders])
diff --git a/tests/api/test_payment.py b/tests/api/test_payment.py
index 6b02371380..b8a4547f99 100644
--- a/tests/api/test_payment.py
+++ b/tests/api/test_payment.py
@@ -99,7 +99,6 @@ def test_checkout_add_payment_method(
'gateway': 'DUMMY',
'transactionToken': 'sample-token',
'amount': str(cart.get_total().gross.amount),
- 'tax': str(cart.get_total().tax.amount),
'billingAddress': graphql_address_data}}
response = user_api_client.post_graphql(CREATE_QUERY, variables)
content = get_graphql_content(response)
@@ -113,7 +112,7 @@ def test_checkout_add_payment_method(
payment = txn.payment_method
assert payment.checkout == cart
assert payment.is_active
- assert payment.total == cart.get_total()
+ assert payment.total == cart.get_total().gross
assert payment.charge_status == ChargeStatus.NOT_CHARGED
@@ -142,7 +141,7 @@ def test_payment_method_charge_success(
variables = {
'paymentMethodId': payment_method_id,
- 'amount': str(payment_method_dummy.total.gross.amount)}
+ 'amount': str(payment_method_dummy.total.amount)}
response = staff_api_client.post_graphql(
CHARGE_QUERY, variables, permissions=[permission_manage_orders])
content = get_graphql_content(response)
@@ -164,7 +163,7 @@ def test_payment_method_charge_gateway_error(
'PaymentMethod', payment_method_dummy.pk)
variables = {
'paymentMethodId': payment_method_id,
- 'amount': str(payment_method_dummy.total.gross.amount)}
+ 'amount': str(payment_method_dummy.total.amount)}
monkeypatch.setattr(
'saleor.payment.providers.dummy.dummy_success', lambda: False)
response = staff_api_client.post_graphql(
@@ -203,14 +202,14 @@ def test_payment_method_refund_success(
staff_api_client, permission_manage_orders, payment_method_dummy):
payment_method = payment_method_dummy
payment_method.charge_status = ChargeStatus.CHARGED
- payment_method.captured_amount = payment_method.total.gross
+ payment_method.captured_amount = payment_method.total
payment_method.save()
payment_method_id = graphene.Node.to_global_id(
'PaymentMethod', payment_method.pk)
variables = {
'paymentMethodId': payment_method_id,
- 'amount': str(payment_method_dummy.total.gross.amount)}
+ 'amount': str(payment_method_dummy.total.amount)}
response = staff_api_client.post_graphql(
REFUND_QUERY, variables, permissions=[permission_manage_orders])
content = get_graphql_content(response)
@@ -228,13 +227,13 @@ def test_payment_method_refund_error(
monkeypatch):
payment_method = payment_method_dummy
payment_method.charge_status = ChargeStatus.CHARGED
- payment_method.captured_amount = payment_method.total.gross
+ payment_method.captured_amount = payment_method.total
payment_method.save()
payment_method_id = graphene.Node.to_global_id(
'PaymentMethod', payment_method_dummy.pk)
variables = {
'paymentMethodId': payment_method_id,
- 'amount': str(payment_method.total.gross.amount)}
+ 'amount': str(payment_method.total.amount)}
monkeypatch.setattr(
'saleor.payment.providers.dummy.dummy_success', lambda: False)
response = staff_api_client.post_graphql(
diff --git a/tests/conftest.py b/tests/conftest.py
index 8cd179470a..681cc311df 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -475,7 +475,7 @@ def payment_method_txn_preauth(order_with_lines, payment_method_dummy):
payment.save()
payment.transactions.create(
- amount=payment.total.gross,
+ amount=payment.total,
transaction_type=TransactionType.AUTH,
gateway_response={},
is_success=True)
@@ -488,11 +488,11 @@ def payment_method_txn_captured(order_with_lines, payment_method_dummy):
payment = payment_method_dummy
payment.order = order
payment.charge_status = ChargeStatus.CHARGED
- payment.captured_amount = payment.total.gross
+ payment.captured_amount = payment.total
payment.save()
payment.transactions.create(
- amount=payment.total.gross,
+ amount=payment.total,
transaction_type=TransactionType.CAPTURE,
gateway_response={},
is_success=True)
@@ -509,7 +509,7 @@ def payment_method_txn_refunded(order_with_lines, payment_method_dummy):
payment.save()
payment.transactions.create(
- amount=payment.total.gross,
+ amount=payment.total,
transaction_type=TransactionType.REFUND,
gateway_response={},
is_success=True)
@@ -733,7 +733,7 @@ def payment_method_dummy(db, order_with_lines):
variant='dummy',
order=order_with_lines,
is_active=True,
- total=order_with_lines.total,
+ total=order_with_lines.total.gross,
billing_first_name=order_with_lines.billing_address.first_name,
billing_last_name=order_with_lines.billing_address.last_name,
billing_company_name=order_with_lines.billing_address.company_name,
diff --git a/tests/test_order.py b/tests/test_order.py
index 87353e8eb9..8525b98dba 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -368,7 +368,7 @@ def test_order_queryset_to_ship():
for order in orders_to_ship:
order.payment_methods.create(
variant='default', charge_status=ChargeStatus.CHARGED,
- total=order.total, captured_amount=order.total_gross)
+ total=order.total.gross, captured_amount=order.total_gross)
orders_not_to_ship = [
Order.objects.create(status=OrderStatus.DRAFT, total=total),
@@ -456,7 +456,7 @@ def test_order_payment_flow(
data = {
'variant': 'dummy',
'is_active': True,
- 'total': order.total,
+ 'total': order.total.gross,
'charge_status': 'not-charged'}
response = client.post(redirect_url, data)
@@ -467,7 +467,7 @@ def test_order_payment_flow(
# Assert that payment object was created and contains correct data
payment = order.payment_methods.all()[0]
- assert payment.total == order.total
+ assert payment.total == order.total_gross
assert payment.transactions.count() == 1
assert payment.transactions.first().transaction_type == 'auth'