Exemple #1
0
def process_payment(request):
    """
    Processes the payment depending on the selected payment method. Returns a
    dictionary with the success state, the next url and a optional error
    message.
    """
    payment_method = get_selected_payment_method(request)
    shop = lfs.core.utils.get_default_shop(request)

    if payment_method.module:
        payment_class = lfs.core.utils.import_symbol(payment_method.module)
        instance = payment_class(request)

        create_order_time = instance.get_create_order_time()
        if create_order_time == PM_ORDER_IMMEDIATELY:
            order = lfs.order.utils.add_order(request)
            instance.order = order
            result = instance.process()
        else:
            cart = lfs.cart.utils.get_cart(request)
            instance.cart = cart
            result = instance.process()

        if result.get("order_state"):
            order.state = result.get("order_state")
            order.save()

        order_submitted.send({"order": order, "request": request})

        if result["accepted"]:
            if create_order_time == PM_ORDER_ACCEPTED:
                order = lfs.order.utils.add_order(request)
                if result.get("order_state"):
                    order.state = result.get("order_state")
                    order.save()
                order_submitted.send({"order": order, "request": request})
        return result

    elif payment_method.id == PAYPAL:
        order = lfs.order.utils.add_order(request)
        if order:  # if we have no cart then the order will be None
            order_submitted.send({"order": order, "request": request})
            if settings.LFS_PAYPAL_REDIRECT:
                return {
                    "accepted": True,
                    "next_url": order.get_pay_link(request),
                }
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
    else:
        order = lfs.order.utils.add_order(request)
        order_submitted.send({"order": order, "request": request})
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
Exemple #2
0
def process_payment(request):
    """
    Processes the payment depending on the selected payment method. Returns a
    dictionary with the success state, the next url and a optional error
    message.
    """
    payment_method = get_selected_payment_method(request)

    if payment_method.module:
        payment_class = lfs.core.utils.import_symbol(payment_method.module)
        payment_instance = payment_class(request)

        create_order_time = payment_instance.get_create_order_time()
        if create_order_time == PM_ORDER_IMMEDIATELY:
            order = lfs.order.utils.add_order(request)
            payment_instance.order = order
            result = payment_instance.process()
            if result.get("order_state"):
                order.state = result.get("order_state")
                order.save()
            order_submitted.send({"order": order, "request": request})
        else:
            cart = lfs.cart.utils.get_cart(request)
            payment_instance.cart = cart
            result = payment_instance.process()

        if result["accepted"]:
            if create_order_time == PM_ORDER_ACCEPTED:
                order = lfs.order.utils.add_order(request)
                if result.get("order_state"):
                    order.state = result.get("order_state")
                    order.save()
                order_submitted.send({"order": order, "request": request})
        return result

    elif payment_method.id == PAYPAL:
        order = lfs.order.utils.add_order(request)
        if order:  # if we have no cart then the order will be None
            order_submitted.send({"order": order, "request": request})
            if settings.LFS_PAYPAL_REDIRECT:
                return {
                    "accepted": True,
                    "next_url": order.get_pay_link(request),
                }
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
    else:
        order = lfs.order.utils.add_order(request)
        order_submitted.send({"order": order, "request": request})
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
Exemple #3
0
def process_payment(request):
    """
    Processes the payment depending on the selected payment method. Returns a
    dictionary with the success state, the next url and a optional error
    message.
    """
    from lfs.core.utils import import_symbol
    from lfs.order.utils import add_order
    from lfs.cart.utils import get_cart
    payment_method = get_selected_payment_method(request)

    if payment_method.module:
        payment_class = import_symbol(payment_method.module)
        payment_instance = payment_class(request)

        create_order_time = payment_instance.get_create_order_time()
        if create_order_time == PM_ORDER_IMMEDIATELY:
            order = add_order(request)
            if order is None:
                return {'accepted': True, 'next_url': reverse("lfs_shop_view")}
            payment_instance.order = order
            result = payment_instance.process()
            if result.get("order_state"):
                order.state = result.get("order_state")
                order.save()
            order_submitted.send(sender=order, request=request)
        else:
            cart = get_cart(request)
            payment_instance.cart = cart
            result = payment_instance.process()

        if result["accepted"]:
            if create_order_time == PM_ORDER_ACCEPTED:
                order = add_order(request)
                if result.get("order_state"):
                    order.state = result.get("order_state")
                    order.save()
                order_submitted.send(sender=order, request=request)
        return result
    else:
        order = add_order(request)
        order_submitted.send(sender=order, request=request)
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
Exemple #4
0
def process_payment(request):
    """
    Processes the payment depending on the selected payment method. Returns a
    dictionary with the success state, the next url and a optional error
    message.
    """
    from lfs.core.utils import import_symbol
    from lfs.order.utils import add_order
    from lfs.cart.utils import get_cart
    payment_method = get_selected_payment_method(request)

    if payment_method.module:
        payment_class = import_symbol(payment_method.module)
        payment_instance = payment_class(request)

        create_order_time = payment_instance.get_create_order_time()
        if create_order_time == PM_ORDER_IMMEDIATELY:
            order = add_order(request)
            if order is None:
                return {'accepted': True, 'next_url': reverse("lfs_shop_view")}
            payment_instance.order = order
            result = payment_instance.process()
            if result.get("order_state"):
                order.state = result.get("order_state")
                order.save()
            order_submitted.send(sender=order, request=request)
        else:
            cart = get_cart(request)
            payment_instance.cart = cart
            result = payment_instance.process()

        if result["accepted"]:
            if create_order_time == PM_ORDER_ACCEPTED:
                order = add_order(request)
                if result.get("order_state"):
                    order.state = result.get("order_state")
                    order.save()
                order_submitted.send(sender=order, request=request)
        return result
    else:
        order = add_order(request)
        order_submitted.send(sender=order, request=request)
        return {
            "accepted": True,
            "next_url": reverse("lfs_thank_you"),
        }
Exemple #5
0
def process_payment(request):
    """Processes the payment depending on the selected payment method. Returns
    a dictionary with the success state, the next url and a optional error
    message.
    """
    payment_method = get_selected_payment_method(request)
    shop = lfs.core.utils.get_default_shop(request)

    if payment_method.module:
        module = lfs.core.utils.import_module(payment_method.module + ".views")
        result = module.process(request)
        if result["accepted"] == True:
            order = lfs.order.utils.add_order(request)
            # TODO: this has to be returned from the module
            order.state = PAID
            order.save()
            order_submitted.send({"order": order, "request": request})
        return result

    elif payment_method.id == PAYPAL:
        order = lfs.order.utils.add_order(request)
        if order:  # if we have no cart then the order will be None
            order_submitted.send({"order": order, "request": request})
            if settings.LFS_PAYPAL_REDIRECT:
                return {
                    "accepted": True,
                    "next-url": order.get_pay_link(),
                }
        return {
            "accepted": True,
            "next-url": reverse("lfs_thank_you"),
        }
    else:
        order = lfs.order.utils.add_order(request)
        order_submitted.send({"order": order, "request": request})
        return {
            "accepted": True,
            "next-url": reverse("lfs_thank_you"),
        }
Exemple #6
0
def process_payment(request):
    """Processes the payment depending on the selected payment method. Returns
    a dictionary with the success state, the next url and a optional error
    message.
    """
    payment_method = get_selected_payment_method(request)
    shop = lfs.core.utils.get_default_shop(request)

    if payment_method.module:
        module = lfs.core.utils.import_module(payment_method.module + ".views")
        result = module.process(request)
        if result["accepted"] == True:
            order = lfs.order.utils.add_order(request)
            # TODO: this has to be returned from the module
            order.state = PAID
            order.save()
            order_submitted.send({"order": order, "request": request})
        return result

    elif payment_method.id == PAYPAL:
        order = lfs.order.utils.add_order(request)
        if order:  # if we have no cart then the order will be None
            order_submitted.send({"order": order, "request": request})
            if settings.LFS_PAYPAL_REDIRECT:
                return {
                    "accepted": True,
                    "next-url": order.get_pay_link(),
                }
        return {
            "accepted": True,
            "next-url": reverse("lfs_thank_you"),
        }
    else:
        order = lfs.order.utils.add_order(request)
        order_submitted.send({"order": order, "request": request})
        return {
            "accepted": True,
            "next-url": reverse("lfs_thank_you"),
        }
Exemple #7
0
def add_order(request):
    """Adds an order based on current cart for the current customer. 
    
    It assumes that the customer is prepared with all needed information. This 
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if customer.selected_shipping_address:
        shipping_address = customer.selected_shipping_address
    else:
        shipping_address = customer.selected_invoice_address
    
    cart = cart_utils.get_cart(request)
    if cart is None:
        return order
    cart_costs = cart_utils.get_cart_costs(request, cart, total=False)
    
    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request, shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a 
    # django user account, so we set the name of the invoice address to the 
    # customer name.
    
    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = invoice_address.email
        
    # Calculate the totals    
    price = cart_costs["price"] + shipping_costs["price"] + payment_costs["price"]
    tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]
    
    order = Order.objects.create(
        user = user,
        session = request.session.session_key,
        price = price,
        tax = tax,

        customer_firstname = invoice_address.firstname,
        customer_lastname = invoice_address.lastname,
        customer_email = customer_email,
                
        shipping_method = shipping_method,
        shipping_price = shipping_costs["price"],
        shipping_tax = shipping_costs["tax"],
        payment_method = payment_method,
        payment_price = payment_costs["price"],
        payment_tax = payment_costs["tax"],

        invoice_firstname = invoice_address.firstname,
        invoice_lastname = invoice_address.lastname,
        invoice_company_name = invoice_address.company_name,
        invoice_street = invoice_address.street,
        invoice_zip_code = invoice_address.zip_code,
        invoice_city = invoice_address.city,
        invoice_country = invoice_address.country,
        invoice_phone = invoice_address.phone,

        shipping_firstname = shipping_address.firstname,
        shipping_lastname = shipping_address.lastname,
        shipping_company_name = shipping_address.company_name,
        shipping_street = shipping_address.street,
        shipping_zip_code = shipping_address.zip_code,
        shipping_city = shipping_address.city,
        shipping_country = shipping_address.country,
        shipping_phone = shipping_address.phone,

        message = request.POST.get("message", ""),
    )
        
    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor
    
    order.save()
    
    # Copy cart items
    for cart_item in cart.cartitem_set.all():        
        OrderItem.objects.create(
            order=order,
            
            price_net = cart_item.get_price_net(),
            price_gross = cart_item.get_price_gross(),
            tax = cart_item.get_tax(),

            product = cart_item.product,
            product_sku = cart_item.product.sku,
            product_name = cart_item.product.get_name(),
            product_amount=cart_item.amount,                        
            product_price_net = cart_item.product.get_price_net(),
            product_price_gross = cart_item.product.get_price_gross(),
            product_tax = cart_item.product.get_tax(),
        )
        
        cart_item.product.decrease_stock_amount(cart_item.amount)
        
    cart.delete()
    order_submitted.send(order)
    
    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order
    
    return order

                               
Exemple #8
0
def add_order(request):
    """Adds an order based on current cart for the current customer. 
    
    It assumes that the customer is prepared with all needed information. This 
    is within the responsibility of the checkout form.
    """
    customer = customer_utils.get_customer(request)
    order = None

    invoice_address = customer.selected_invoice_address
    if customer.selected_shipping_address:
        shipping_address = customer.selected_shipping_address
    else:
        shipping_address = customer.selected_invoice_address

    cart = cart_utils.get_cart(request)
    if cart is None:
        return order
    cart_costs = cart_utils.get_cart_costs(request, cart, total=False)

    shipping_method = shipping_utils.get_selected_shipping_method(request)
    shipping_costs = shipping_utils.get_shipping_costs(request,
                                                       shipping_method)

    payment_method = payment_utils.get_selected_payment_method(request)
    payment_costs = payment_utils.get_payment_costs(request, payment_method)

    # Set email dependend on login state. An anonymous customer doesn't  have a
    # django user account, so we set the name of the invoice address to the
    # customer name.

    # Note: After this has been processed the order's customer email has an
    # email in any case. That means you can use it to send emails to the
    # customer.
    if request.user.is_authenticated():
        user = request.user
        customer_email = user.email
    else:
        user = None
        customer_email = invoice_address.email

    # Calculate the totals
    price = cart_costs["price"] + shipping_costs["price"] + payment_costs[
        "price"]
    tax = cart_costs["tax"] + shipping_costs["tax"] + payment_costs["tax"]

    order = Order.objects.create(
        user=user,
        session=request.session.session_key,
        price=price,
        tax=tax,
        customer_firstname=invoice_address.firstname,
        customer_lastname=invoice_address.lastname,
        customer_email=customer_email,
        shipping_method=shipping_method,
        shipping_price=shipping_costs["price"],
        shipping_tax=shipping_costs["tax"],
        payment_method=payment_method,
        payment_price=payment_costs["price"],
        payment_tax=payment_costs["tax"],
        invoice_firstname=invoice_address.firstname,
        invoice_lastname=invoice_address.lastname,
        invoice_company_name=invoice_address.company_name,
        invoice_street=invoice_address.street,
        invoice_zip_code=invoice_address.zip_code,
        invoice_city=invoice_address.city,
        invoice_country=invoice_address.country,
        invoice_phone=invoice_address.phone,
        shipping_firstname=shipping_address.firstname,
        shipping_lastname=shipping_address.lastname,
        shipping_company_name=shipping_address.company_name,
        shipping_street=shipping_address.street,
        shipping_zip_code=shipping_address.zip_code,
        shipping_city=shipping_address.city,
        shipping_country=shipping_address.country,
        shipping_phone=shipping_address.phone,
        message=request.POST.get("message", ""),
    )

    # Copy bank account if one exists
    if customer.selected_bank_account:
        bank_account = customer.selected_bank_account
        order.account_number = bank_account.account_number
        order.bank_identification_code = bank_account.bank_identification_code
        order.bank_name = bank_account.bank_name
        order.depositor = bank_account.depositor

    order.save()

    # Copy cart items
    for cart_item in cart.cartitem_set.all():
        OrderItem.objects.create(
            order=order,
            price_net=cart_item.get_price_net(),
            price_gross=cart_item.get_price_gross(),
            tax=cart_item.get_tax(),
            product=cart_item.product,
            product_sku=cart_item.product.sku,
            product_name=cart_item.product.get_name(),
            product_amount=cart_item.amount,
            product_price_net=cart_item.product.get_price_net(),
            product_price_gross=cart_item.product.get_price_gross(),
            product_tax=cart_item.product.get_tax(),
        )

        cart_item.product.decrease_stock_amount(cart_item.amount)

    cart.delete()
    order_submitted.send(order)

    # Note: Save order for later use in thank you page. The order will be
    # removed from the session if the thank you page has been called.
    request.session["order"] = order

    return order