Skip to content

Commit

Permalink
Allow to finish chekcout when price amount is 0 (saleor#6064)
Browse files Browse the repository at this point in the history
  • Loading branch information
IKarbowiak authored Aug 28, 2020
1 parent c34ded6 commit 1424f17
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
10 changes: 5 additions & 5 deletions saleor/checkout/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ def create_line_for_order(checkout_line: "CheckoutLine", discounts) -> OrderLine
unit_price = quantize_price(
total_line_price / checkout_line.quantity, total_line_price.currency
)
tax_rate = (
unit_price.tax / unit_price.net
if not isinstance(unit_price, Decimal)
else Decimal("0.0")
)
tax_rate = Decimal("0.0")
# The condition will return False when unit_price.gross is 0.0
if not isinstance(unit_price, Decimal) and unit_price.gross:
tax_rate = unit_price.tax / unit_price.net

line = OrderLine(
product_name=product_name,
variant_name=variant_name,
Expand Down
74 changes: 74 additions & 0 deletions saleor/graphql/checkout/tests/test_checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1754,6 +1754,80 @@ def test_checkout_complete(
).exists(), "Checkout should have been deleted"


@pytest.mark.integration
def test_checkout_complete_0_total_value(
user_api_client,
checkout_with_item,
gift_card,
payment_dummy,
address,
shipping_method,
):

assert not gift_card.last_used_on

checkout = checkout_with_item
checkout.billing_address = address
checkout.store_value_in_metadata(items={"accepted": "true"})
checkout.store_value_in_private_metadata(items={"accepted": "false"})
checkout.save()

checkout_line = checkout.lines.first()
checkout_line_quantity = checkout_line.quantity
checkout_line_variant = checkout_line.variant

product_type = checkout_line_variant.product.product_type
product_type.is_shipping_required = False
product_type.save(update_fields=["is_shipping_required"])

checkout_line_variant.cost_price_amount = Decimal(0)
checkout_line_variant.price_amount = Decimal(0)
checkout_line_variant.save()

checkout.refresh_from_db()

total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
payment = payment_dummy
payment.is_active = True
payment.order = None
payment.total = total.gross.amount
payment.currency = total.gross.currency
payment.checkout = checkout
payment.save()
assert not payment.transactions.exists()

orders_count = Order.objects.count()
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}
response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)

content = get_graphql_content(response)
data = content["data"]["checkoutComplete"]
assert not data["checkoutErrors"]

order_token = data["order"]["token"]
assert Order.objects.count() == orders_count + 1
order = Order.objects.first()
assert order.token == order_token
assert order.total.gross == total.gross
assert order.metadata == checkout.metadata
assert order.private_metadata == checkout.private_metadata

order_line = order.lines.first()
assert checkout_line_quantity == order_line.quantity
assert checkout_line_variant == order_line.variant
assert order.shipping_address is None
assert order.shipping_method is None
assert order.payments.exists()
order_payment = order.payments.first()
assert order_payment == payment
assert payment.transactions.count() == 1

assert not Checkout.objects.filter(
pk=checkout.pk
).exists(), "Checkout should have been deleted"


@pytest.mark.integration
def test_checkout_with_voucher_complete(
user_api_client,
Expand Down

0 comments on commit 1424f17

Please sign in to comment.