Exemple #1
0
def test_checkout_complete(
    api_client, checkout_with_digital_item, address, payment_dummy
):
    """Ensure it is possible to complete a digital checkout without shipping."""

    order_count = Order.objects.count()
    checkout = checkout_with_digital_item
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    variables = {"checkoutId": checkout_id, "redirectUrl": "https://www.example.com"}

    # Set a billing address
    checkout.billing_address = address
    checkout.save(update_fields=["billing_address"])

    # Create a dummy payment to charge
    total = calculations.checkout_total(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()

    # Send the creation request
    response = api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
    content = get_graphql_content(response)["data"]["checkoutComplete"]
    assert not content["errors"]

    # Ensure the order was actually created
    assert (
        Order.objects.count() == order_count + 1
    ), "The order should have been created"
def test_create_payment_information_for_checkout_payment(
        address, checkout_with_item):
    checkout_with_item.billing_address = address
    checkout_with_item.shipping_address = address
    checkout_with_item.save()
    data = {
        "gateway":
        "Dummy",
        "payment_token":
        "token",
        "total":
        checkout_total(checkout=checkout_with_item,
                       lines=list(checkout_with_item)).gross.amount,
        "currency":
        checkout_with_item.currency,
        "email":
        "*****@*****.**",
        "customer_ip_address":
        "127.0.0.1",
        "checkout":
        checkout_with_item,
    }

    payment = create_payment(**data)
    payment_data = create_payment_information(payment, "token", payment.total)

    billing = payment_data.billing
    shipping = payment_data.shipping
    assert billing
    assert billing.first_name == address.first_name
    assert billing.last_name == address.last_name
    assert billing.street_address_1 == address.street_address_1
    assert billing.city == address.city
    assert shipping == billing
Exemple #3
0
def test_recalculate_checkout_discount_with_sale(
        checkout_with_voucher_percentage, discount_info):
    checkout = checkout_with_voucher_percentage
    recalculate_checkout_discount(checkout, [discount_info])
    assert checkout.discount == Money("1.50", "USD")
    assert calculations.checkout_total(
        checkout, discounts=[discount_info]).gross == Money("13.50", "USD")
def test_create_payment(checkout_with_item, address):
    checkout_with_item.billing_address = address
    checkout_with_item.save()

    data = {
        "gateway":
        "Dummy",
        "payment_token":
        "token",
        "total":
        checkout_total(checkout=checkout_with_item,
                       lines=list(checkout_with_item)).gross.amount,
        "currency":
        checkout_with_item.currency,
        "email":
        "*****@*****.**",
        "customer_ip_address":
        "127.0.0.1",
        "checkout":
        checkout_with_item,
    }
    payment = create_payment(**data)
    assert payment.gateway == "Dummy"

    same_payment = create_payment(**data)
    assert payment == same_payment
Exemple #5
0
def test_use_checkout_billing_address_as_payment_billing(
        user_api_client, checkout_with_item, address):
    checkout = checkout_with_item
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    total = calculations.checkout_total(checkout)
    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway": "Dummy",
            "token": "sample-token",
            "amount": total.gross.amount,
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]

    # check if proper error is returned if address is missing
    assert data["errors"][0]["field"] == "billingAddress"
    assert (data["errors"][0]["message"] ==
            "No billing address associated with this checkout.")

    # assign the address and try again
    address.street_address_1 = "spanish-inqusition"
    address.save()
    checkout.billing_address = address
    checkout.save()
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    get_graphql_content(response)

    checkout.refresh_from_db()
    assert checkout.payments.count() == 1
    payment = checkout.payments.first()
    assert payment.billing_address_1 == address.street_address_1
Exemple #6
0
def test_create_order_with_gift_card_partial_use(
    checkout_with_item, gift_card_used, customer_user, shipping_method
):
    checkout = checkout_with_item
    checkout.user = customer_user
    checkout.billing_address = customer_user.default_billing_address
    checkout.shipping_address = customer_user.default_billing_address
    checkout.shipping_method = shipping_method
    checkout.save()

    price_without_gift_card = calculations.checkout_total(checkout)
    gift_card_balance_before_order = gift_card_used.current_balance_amount

    checkout.gift_cards.add(gift_card_used)
    checkout.save()

    order = create_order(
        checkout=checkout,
        order_data=prepare_order_data(
            checkout=checkout, tracking_code="tracking_code", discounts=None
        ),
        user=customer_user,
        redirect_url="https://www.example.com",
    )

    gift_card_used.refresh_from_db()

    expected_old_balance = (
        price_without_gift_card.gross.amount + gift_card_used.current_balance_amount
    )

    assert order.gift_cards.count() > 0
    assert order.total == zero_taxed_money()
    assert gift_card_balance_before_order == expected_old_balance
Exemple #7
0
def test_checkout_add_payment_bad_amount(
    user_api_client, checkout_without_shipping_required, address
):
    checkout = checkout_without_shipping_required
    checkout.billing_address = address
    checkout.save()
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)

    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway": "DUMMY",
            "token": "sample-token",
            "amount": str(
                calculations.checkout_total(
                    checkout=checkout, lines=list(checkout)
                ).gross.amount
                + Decimal(1)
            ),
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]
    assert (
        data["paymentErrors"][0]["code"]
        == PaymentErrorCode.PARTIAL_PAYMENT_NOT_ALLOWED.name
    )
Exemple #8
0
def test_checkout_add_payment_without_shipping_method_and_not_shipping_required(
    user_api_client, checkout_without_shipping_required, address
):
    checkout = checkout_without_shipping_required
    checkout.billing_address = address
    checkout.save()

    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway": "Dummy",
            "token": "sample-token",
            "amount": total.gross.amount,
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]
    assert not data["paymentErrors"]
    transactions = data["payment"]["transactions"]
    assert not transactions
    payment = Payment.objects.get()
    assert payment.checkout == checkout
    assert payment.is_active
    assert payment.token == "sample-token"
    assert payment.total == total.gross.amount
    assert payment.currency == total.gross.currency
    assert payment.charge_status == ChargeStatus.NOT_CHARGED
    assert payment.billing_address_1 == checkout.billing_address.street_address_1
    assert payment.billing_first_name == checkout.billing_address.first_name
    assert payment.billing_last_name == checkout.billing_address.last_name
Exemple #9
0
def checkout_with_charged_payment(checkout_with_billing_address):
    checkout = checkout_with_billing_address

    taxed_total = calculations.checkout_total(checkout=checkout,
                                              lines=list(checkout))
    payment = Payment.objects.create(
        gateway="mirumee.payments.dummy",
        is_active=True,
        total=taxed_total.gross.amount,
        currency="USD",
    )

    payment.charge_status = ChargeStatus.FULLY_CHARGED
    payment.captured_amount = payment.total
    payment.checkout = checkout_with_billing_address
    payment.save()

    payment.transactions.create(
        amount=payment.total,
        kind=TransactionKind.CAPTURE,
        gateway_response={},
        is_success=True,
    )

    return checkout
Exemple #10
0
def test_checkout_add_payment(user_api_client, checkout_with_item,
                              graphql_address_data):
    checkout = checkout_with_item
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    total = calculations.checkout_total(checkout)
    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway": "Dummy",
            "token": "sample-token",
            "amount": total.gross.amount,
            "billingAddress": graphql_address_data,
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]
    assert not data["errors"]
    transactions = data["payment"]["transactions"]
    assert not transactions
    payment = Payment.objects.get()
    assert payment.checkout == checkout
    assert payment.is_active
    assert payment.token == "sample-token"
    assert payment.total == total.gross.amount
    assert payment.currency == total.gross.currency
    assert payment.charge_status == ChargeStatus.NOT_CHARGED
Exemple #11
0
def test_checkout_payment_charge(api_client, checkout_with_billing_address,
                                 count_queries):
    query = """
        mutation createPayment($input: PaymentInput!, $checkoutId: ID!) {
          checkoutPaymentCreate(input: $input, checkoutId: $checkoutId) {
            errors {
              field
              message
            }
          }
        }
    """

    variables = {
        "checkoutId":
        Node.to_global_id("Checkout", checkout_with_billing_address.pk),
        "input": {
            "amount":
            calculations.checkout_total(
                checkout=checkout_with_billing_address,
                lines=list(checkout_with_billing_address),
            ).gross.amount,
            "gateway":
            "Dummy",
            "token":
            "charged",
        },
    }
    response = get_graphql_content(api_client.post_graphql(query, variables))
    assert not response["data"]["checkoutPaymentCreate"]["errors"]
Exemple #12
0
def test_calculations_checkout_total_with_vatlayer(vatlayer, settings,
                                                   checkout_with_item):
    settings.PLUGINS = ["saleor.plugins.vatlayer.plugin.VatlayerPlugin"]
    checkout_subtotal = calculations.checkout_total(
        checkout=checkout_with_item, lines=list(checkout_with_item))
    assert checkout_subtotal == TaxedMoney(net=Money("30", "USD"),
                                           gross=Money("30", "USD"))
def test_checkout_totals_use_discounts(api_client, checkout_with_item, sale):
    checkout = checkout_with_item
    # make sure that we're testing a variant that is actually on sale
    product = checkout.lines.first().variant.product
    sale.products.add(product)

    query = """
    query getCheckout($token: UUID) {
        checkout(token: $token) {
            lines {
                totalPrice {
                    gross {
                        amount
                    }
                }
            }
            totalPrice {
                gross {
                    amount
                }
            }
            subtotalPrice {
                gross {
                    amount
                }
            }
        }
    }
    """

    variables = {"token": str(checkout.token)}
    response = api_client.post_graphql(query, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkout"]

    discounts = [
        DiscountInfo(
            sale=sale,
            product_ids={product.id},
            category_ids=set(),
            collection_ids=set(),
        )
    ]
    taxed_total = calculations.checkout_total(checkout=checkout,
                                              lines=list(checkout),
                                              discounts=discounts)
    assert data["totalPrice"]["gross"]["amount"] == taxed_total.gross.amount
    assert data["subtotalPrice"]["gross"]["amount"] == taxed_total.gross.amount

    line = checkout.lines.first()
    line_total = calculations.checkout_line_total(line=line,
                                                  discounts=discounts)
    assert data["lines"][0]["totalPrice"]["gross"][
        "amount"] == line_total.gross.amount
def test_checkout_get_total_with_gift_card(api_client, checkout_with_item, gift_card):
    taxed_total = calculations.checkout_total(checkout_with_item)
    total_with_gift_card = taxed_total.gross.amount - gift_card.current_balance_amount

    checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_item.pk)
    variables = {"checkoutId": checkout_id, "promoCode": gift_card.code}
    data = _mutate_checkout_add_promo_code(api_client, variables)

    assert not data["errors"]
    assert data["checkout"]["id"] == checkout_id
    assert not data["checkout"]["giftCards"] == []
    assert data["checkout"]["totalPrice"]["gross"]["amount"] == total_with_gift_card
Exemple #15
0
def test_create_order_with_many_gift_cards(
    checkout_with_item,
    gift_card_created_by_staff,
    gift_card,
    customer_user,
    shipping_method,
):
    checkout = checkout_with_item
    checkout.user = customer_user
    checkout.billing_address = customer_user.default_billing_address
    checkout.shipping_address = customer_user.default_billing_address
    checkout.shipping_method = shipping_method
    checkout.save()

    price_without_gift_card = calculations.checkout_total(
        checkout=checkout, lines=list(checkout)
    )
    gift_cards_balance_before_order = (
        gift_card_created_by_staff.current_balance.amount
        + gift_card.current_balance.amount
    )

    checkout.gift_cards.add(gift_card_created_by_staff)
    checkout.gift_cards.add(gift_card)
    checkout.save()

    order = create_order(
        checkout=checkout,
        order_data=prepare_order_data(
            checkout=checkout,
            lines=list(checkout),
            tracking_code="tracking_code",
            discounts=None,
        ),
        user=customer_user,
        redirect_url="https://www.example.com",
    )

    gift_card_created_by_staff.refresh_from_db()
    gift_card.refresh_from_db()
    zero_price = zero_money()
    assert order.gift_cards.count() > 0
    assert gift_card_created_by_staff.current_balance == zero_price
    assert gift_card.current_balance == zero_price
    assert price_without_gift_card.gross.amount == (
        gift_cards_balance_before_order + order.total.gross.amount
    )
Exemple #16
0
def test_create_payment_from_checkout_requires_billing_address(
    checkout_with_item, settings
):
    checkout_with_item.billing_address = None
    checkout_with_item.save()

    data = {
        "gateway": "Dummy",
        "payment_token": "token",
        "total": checkout_total(checkout_with_item),
        "currency": checkout_with_item.currency,
        "email": "*****@*****.**",
        "checkout": checkout_with_item,
    }
    with pytest.raises(PaymentError) as e:
        create_payment(**data)
    assert e.value.code == PaymentErrorCode.BILLING_ADDRESS_NOT_SET.value
Exemple #17
0
def test_recalculate_checkout_discount_free_shipping_subtotal_bigger_than_shipping(
    checkout_with_voucher_percentage_and_shipping,
    voucher_free_shipping,
    shipping_method,
):
    checkout = checkout_with_voucher_percentage_and_shipping

    shipping_method.price = calculations.checkout_subtotal(
        checkout).gross - Money("1.00", "USD")
    shipping_method.save()

    recalculate_checkout_discount(checkout, None)

    assert checkout.discount == shipping_method.price
    assert checkout.discount_name == "Free shipping"
    assert calculations.checkout_total(
        checkout) == calculations.checkout_subtotal(checkout)
def test_checkout_get_total_with_many_gift_card(
    api_client, checkout_with_gift_card, gift_card_created_by_staff
):
    taxed_total = calculations.checkout_total(checkout_with_gift_card)
    taxed_total.gross -= checkout_with_gift_card.get_total_gift_cards_balance()
    taxed_total.net -= checkout_with_gift_card.get_total_gift_cards_balance()
    total_with_gift_card = (
        taxed_total.gross.amount - gift_card_created_by_staff.current_balance_amount
    )

    assert checkout_with_gift_card.gift_cards.count() > 0
    checkout_id = graphene.Node.to_global_id("Checkout", checkout_with_gift_card.pk)
    variables = {
        "checkoutId": checkout_id,
        "promoCode": gift_card_created_by_staff.code,
    }
    data = _mutate_checkout_add_promo_code(api_client, variables)

    assert not data["errors"]
    assert data["checkout"]["id"] == checkout_id
    assert data["checkout"]["totalPrice"]["gross"]["amount"] == total_with_gift_card
Exemple #19
0
def test_checkout_add_payment_without_shipping_method_with_shipping_required(
    user_api_client, checkout_with_shipping_required, address
):
    checkout = checkout_with_shipping_required

    checkout.billing_address = address
    checkout.save()

    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
    total = calculations.checkout_total(checkout=checkout, lines=list(checkout))
    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway": "Dummy",
            "token": "sample-token",
            "amount": total.gross.amount,
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]

    assert data["paymentErrors"][0]["code"] == "SHIPPING_METHOD_NOT_SET"
    assert data["paymentErrors"][0]["field"] == "shippingMethod"
Exemple #20
0
def test_checkout_add_payment_bad_amount(user_api_client, checkout_with_item,
                                         graphql_address_data):
    checkout = checkout_with_item
    checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)

    variables = {
        "checkoutId": checkout_id,
        "input": {
            "gateway":
            "DUMMY",
            "token":
            "sample-token",
            "amount":
            str(
                calculations.checkout_total(checkout).gross.amount +
                Decimal(1)),
            "billingAddress":
            graphql_address_data,
        },
    }
    response = user_api_client.post_graphql(CREATE_QUERY, variables)
    content = get_graphql_content(response)
    data = content["data"]["checkoutPaymentCreate"]
    assert data["errors"]