def finalize_order(request):
    '''Helper function that actually complete the order when the
    payment provider tells us so.
    '''
    order_id = provider.get_order_id(request)
    order = Order.objects.get(pk=order_id)
    #Order is already completed
    if order.payment_done:
        return

    #Simulate the cart for the order.complete, and order_handler that needs it
    try:
        cart_id = provider.get_cart_id(request)
        request.cart = Cart.objects.get(id=cart_id)
    except (NotImplementedError, Cart.DoesNotExist, TypeError):
        pass

    #Recreate an order form for the order handler
    data = checkout.initial_order_data(request)
    data["step"] = checkout.CHECKOUT_STEP_LAST
    order_form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)
    form = order_form_class(request, step=checkout.CHECKOUT_STEP_LAST, data=data)
    form.instance = order
    form.full_clean()
    request.session["order"] = dict(form.cleaned_data)

    order.transaction_id = provider.get_transaction_id(request)
    order.payment_done = True
    order.complete(request)

    order_handler(request, form, order)
    checkout.send_order_email(request, order)
def checkout_steps(request):
    """
    Display the order form and handle processing of each step.
    """
    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)

    # Determine the Form class to use during the checkout process
    form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)

    step = int(request.POST.get("step", checkout.CHECKOUT_STEP_FIRST))
    initial = checkout.initial_order_data(request)
    form = form_class(request, step, initial=initial)
    data = request.POST
    checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                try:
                    billship_handler(request, form)
                except checkout.CheckoutError, e:
                    checkout_errors.append(e)
                form.set_discount()

            # FINAL CHECKOUT STEP has been removed, as payment is now
            # handled by the external provider

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request, step, initial=initial, data=data,
                             errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = form_class(request, step, initial=initial)
Beispiel #3
0
def checkout_steps(request):
    """
    Display the order form and handle processing of each step.
    """

    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)

    # Level C Discount
    if request.user.profile.is_level_C:
        request.session['force_discount'] = 'l3v3lC15' 

    # Determine the Form class to use during the checkout process
    form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)

    initial = checkout.initial_order_data(request, form_class)

    cancelled = request.GET.get('c', None)    
    
    if not cancelled:
        step = int(request.POST.get("step", None)
                   or initial.get("step", None)
                   or checkout.CHECKOUT_STEP_FIRST)
    else:
        step = checkout.CHECKOUT_STEP_FIRST
        
    form = form_class(request, step, initial=initial)
    data = request.POST
    checkout_errors = []
    log.debug('Checkout step %s' % step)

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                try:
                    billship_handler(request, form)
                    tax_handler(request, form)
                except checkout.CheckoutError, e:
                    checkout_errors.append(e)
                    
                form.set_discount()
                
                if form.cleaned_data.get('payment_method') == 'paypal':
                    step += 1
                    try:
                        request.session["order"]["step"] = step
                        request.session.modified = True
                    except KeyError:
                        pass
                    return redirect(Paypal.process(request, form))

            # FINAL CHECKOUT STEP - handle payment and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the initial order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order receipt email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    transaction_id = payment_handler(request, form, order)
                except checkout.CheckoutError, e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    
                    if form.cleaned_data.get('payment_method') == 'paypal':
                        payment = Paypal.find(request)
                        if payment.shipping_info:
                            order.shipping_detail_first_name = payment.shipping_info.first_name
                            order.shipping_detail_last_name = payment.shipping_info.last_name
                            order.shipping_detail_street = payment.shipping_info.address.line1
                            order.shipping_detail_city = payment.shipping_info.address.city
                            order.shipping_detail_state = payment.shipping_info.address.state
                            order.shipping_detail_postcode = payment.shipping_info.address.postal_code
                            order.shipping_detail_country = payment.shipping_info.address.country_code                    
                    
                    order.transaction_id = transaction_id
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = redirect("shop_complete")   
                    if form.cleaned_data.get("remember"):
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response, "remember", remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request, step, initial=initial, data=data,
                              errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = form_class(request, step, initial=initial)
Beispiel #4
0
def checkout(request):
    """
    Display the order form and handle processing of each step.
    """
    
    # Do the authentication check here rather than using standard login_required
    # decorator. This means we can check for a custom LOGIN_URL and fall back
    # to our own login view.
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and \
        not request.user.is_authenticated():
        return HttpResponseRedirect("%s?next=%s" % (settings.SHOP_LOGIN_URL, 
                                                    reverse("shop_checkout")))
    
    step = int(request.POST.get("step", CHECKOUT_STEP_FIRST))
    initial = initial_order_data(request)
    form = OrderForm(request, step, initial=initial)
    data = request.POST

    if request.POST.get("back") is not None:
        step -= 1
        form = OrderForm(request, step, initial=initial)
    elif request.method == "POST":
        form = OrderForm(request, step, initial=initial, data=data)
        if form.is_valid():
            checkout_errors = []
            request.session["order"] = dict(form.cleaned_data)
            for field in ("card_number", "card_expiry_month", 
                "card_expiry_year", "card_ccv"):
                del request.session["order"][field]

            # Handle shipping and discount code on first step.
            if step == CHECKOUT_STEP_FIRST:
                try:
                    billing_shipping(request, form)
                except CheckoutError, e:
                    checkout_errors.append(e)
                if hasattr(form, "discount"):
                    cart = Cart.objects.from_request(request)
                    discount_total = discount.calculate(cart.total_price())
                    request.session["free_shipping"] = discount.free_shipping
                    request.session["discount_total"] = discount_total

            # Process order on final step.
            if step == CHECKOUT_STEP_LAST and not checkout_errors:
                try:
                    payment(request, form)
                except CheckoutError, e:
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:    
                    order = form.save(commit=False)
                    order.process(request)
                    send_order_email(request, order)
                    response = HttpResponseRedirect(reverse("shop_complete"))
                    if form.cleaned_data.get("remember") is not None:
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response, "remember", remembered, 
                            secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # Assign checkout errors to new form if any and re-run is_valid 
            # if valid set form to next step.
            form = OrderForm(request, step, initial=initial, data=data, 
                checkout_errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = OrderForm(request, step, initial=initial)
Beispiel #5
0
def checkout_steps(request, form_class=OrderForm, extra_context=None):
    """
    Display the order form and handle processing of each step.
    """

    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)

    try:
        settings.SHOP_CHECKOUT_FORM_CLASS
    except AttributeError:
        pass
    else:
        from warnings import warn
        warn("The SHOP_CHECKOUT_FORM_CLASS setting is deprecated - please "
             "define your own urlpattern for the checkout_steps view, "
             "passing in your own form_class argument.")
        form_class = import_dotted_path(settings.SHOP_CHECKOUT_FORM_CLASS)

    initial = checkout.initial_order_data(request, form_class)
    step = int(
        request.POST.get("step", None) or initial.get("step", None)
        or checkout.CHECKOUT_STEP_FIRST)
    form = form_class(request, step, initial=initial)
    data = request.POST
    checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle discount code. This needs to
            # be set before shipping, to allow for free shipping to be
            # first set by a discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                form.set_discount()

            # ALL STEPS - run billing/tax handlers. These are run on
            # all steps, since all fields (such as address fields) are
            # posted on each step, even as hidden inputs when not
            # visible in the current step.
            try:
                billship_handler(request, form)
                tax_handler(request, form)
            except checkout.CheckoutError as e:
                checkout_errors.append(e)

            # FINAL CHECKOUT STEP - run payment handler and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the initial order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order receipt email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    transaction_id = payment_handler(request, form, order)
                except checkout.CheckoutError as e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    order.transaction_id = transaction_id
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = redirect("shop_complete")
                    if form.cleaned_data.get("remember"):
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response,
                                   "remember",
                                   remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request,
                              step,
                              initial=initial,
                              data=data,
                              errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = form_class(request, step, initial=initial)

    # Update the step so that we don't rely on POST data to take us back to
    # the same point in the checkout process.
    try:
        request.session["order"]["step"] = step
        request.session.modified = True
    except KeyError:
        pass

    step_vars = checkout.CHECKOUT_STEPS[step - 1]
    template = "shop/%s.html" % step_vars["template"]
    context = {
        "CHECKOUT_STEP_FIRST":
        step == checkout.CHECKOUT_STEP_FIRST,
        "CHECKOUT_STEP_LAST":
        step == checkout.CHECKOUT_STEP_LAST,
        "CHECKOUT_STEP_PAYMENT": (settings.SHOP_PAYMENT_STEP_ENABLED
                                  and step == checkout.CHECKOUT_STEP_PAYMENT),
        "step_title":
        step_vars["title"],
        "step_url":
        step_vars["url"],
        "steps":
        checkout.CHECKOUT_STEPS,
        "step":
        step,
        "form":
        form
    }
    context.update(extra_context or {})
    return TemplateResponse(request, template, context)
Beispiel #6
0
def checkout_steps(request):
    """
    Display the order form and handle processing of each step.
    """

    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)

    # Determine the Form class to use during the checkout process
    form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)

    initial = checkout.initial_order_data(request, form_class)
    step = int(request.POST.get("step", None)
               or initial.get("step", None)
               or checkout.CHECKOUT_STEP_FIRST)
    form = form_class(request, step, initial=initial)
    data = request.POST
    checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                # Discount should be set before shipping, to allow
                # for free shipping to be first set by a discount
                # code.
                form.set_discount()
                try:
                    billship_handler(request, form)
                    tax_handler(request, form)
                except checkout.CheckoutError as e:
                    checkout_errors.append(e)

            # FINAL CHECKOUT STEP - handle payment and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the initial order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order receipt email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    transaction_id = payment_handler(request, form, order)
                except checkout.CheckoutError as e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    order.transaction_id = transaction_id
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = redirect("shop_complete")
                    if form.cleaned_data.get("remember"):
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response, "remember", remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request, step, initial=initial, data=data,
                              errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = form_class(request, step, initial=initial)

    # Update the step so that we don't rely on POST data to take us back to
    # the same point in the checkout process.
    try:
        request.session["order"]["step"] = step
        request.session.modified = True
    except KeyError:
        pass

    step_vars = checkout.CHECKOUT_STEPS[step - 1]
    template = "shop/%s.html" % step_vars["template"]
    context = {"CHECKOUT_STEP_FIRST": step == checkout.CHECKOUT_STEP_FIRST,
               "CHECKOUT_STEP_LAST": step == checkout.CHECKOUT_STEP_LAST,
               "step_title": step_vars["title"], "step_url": step_vars["url"],
               "steps": checkout.CHECKOUT_STEPS, "step": step, "form": form}
    return render(request, template, context)
Beispiel #7
0
def checkout_steps(request):
    """
    Display the order form and handle processing of each step.
    """

    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)

    # Determine the Form class to use during the checkout process
    form_class = get_callable(settings.SHOP_CHECKOUT_FORM_CLASS)

    initial = checkout.initial_order_data(request, form_class)
    step = int(
        request.POST.get("step", None) or initial.get("step", None)
        or checkout.CHECKOUT_STEP_FIRST)
    form = form_class(request, step, initial=initial)
    data = request.POST
    checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                try:
                    billship_handler(request, form)
                    tax_handler(request, form)
                except checkout.CheckoutError, e:
                    checkout_errors.append(e)
                form.set_discount()

            # FINAL CHECKOUT STEP - handle payment and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the initial order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order receipt email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    transaction_id = payment_handler(request, form, order)
                except checkout.CheckoutError, e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    order.transaction_id = transaction_id
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = redirect("shop_complete")
                    if form.cleaned_data.get("remember"):
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response,
                                   "remember",
                                   remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request,
                              step,
                              initial=initial,
                              data=data,
                              errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = form_class(request, step, initial=initial)
Beispiel #8
0
def checkout_steps(request):
    """
    Display the order form and handle processing of each step.
    """

    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return HttpResponseRedirect(url)

    step = int(request.POST.get("step", checkout.CHECKOUT_STEP_FIRST))
    initial = checkout.initial_order_data(request)
    form = OrderForm(request, step, initial=initial)
    data = request.POST
    checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = OrderForm(request, step, initial=initial)
    elif request.method == "POST":
        form = OrderForm(request, step, initial=initial, data=data)
        if form.is_valid():
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                try:
                    billship_handler(request, form)
                except checkout.CheckoutError, e:
                    checkout_errors.append(e)
                form.set_discount()

            # FINAL CHECKOUT STEP - handle payment and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the inital order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order reciept email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    payment_handler(request, form, order)
                except checkout.CheckoutError, e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = HttpResponseRedirect(reverse("shop_complete"))
                    if form.cleaned_data.get("remember") is not None:
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response, "remember", remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = OrderForm(request, step, initial=initial, data=data,
                             errors=checkout_errors)
            if form.is_valid():
                step += 1
                form = OrderForm(request, step, initial=initial)
Beispiel #9
0
def checkout_steps(request, form_class=OrderForm):
    """
    Display the order form and handle processing of each step.
    """
    #cart(request)
    # Do the authentication check here rather than using standard
    # login_required decorator. This means we can check for a custom
    # LOGIN_URL and fall back to our own login view.
    authenticated = request.user.is_authenticated()
    onekey = request.GET.get('keycode', None)
    checkout_errors = []
    #add get instead of request.session['onekeycode']
    #when homepage change to network+ style, weixin onekey order error that no onekeycode in session
    #but pc is OK, guess due to cookie forbidden within weixin browser, not diginto yet, just workaround here
    if onekey and onekey != request.session.get('onekeycode', None):
        #raise Exception('code got right!')
        #checkout_errors.append('您可能存在恶意下单,请联系客服,非常抱歉给您带来不便!')
        pass
    if settings.SHOP_CHECKOUT_ACCOUNT_REQUIRED and not authenticated and not onekey:
        url = "%s?next=%s" % (settings.LOGIN_URL, reverse("shop_checkout"))
        return redirect(url)
    #wni: add here for weixin pay
    #weixincode = request.GET.get('code',None)
    if settings.WITH_WEIXIN_PAY:
        weixinopenid = request.session.get('openid', None)
        if 'micromessenger' in request.META.get('HTTP_USER_AGENT').lower(
        ) and not weixinopenid:  #and not weixincode:
            api_pub = JsApi_pub()
            #url = api_pub.createOauthUrlForCode(reverse('weixin:usercode',args=(tmp_no,),current_app=resolve(request.path).namespace))
            #url = api_pub.createOauthUrlForCode('http://eason.happydiaosi.com/weixin/getusercode')
            url = api_pub.createOauthUrlForCode('http://%s' %
                                                settings.SITE_DOMAIN +
                                                reverse('weixin:usercode'))
            return redirect(url)

    try:
        settings.SHOP_CHECKOUT_FORM_CLASS
    except AttributeError:
        pass
    else:
        from warnings import warn
        warn("The SHOP_CHECKOUT_FORM_CLASS setting is deprecated - please "
             "define your own urlpattern for the checkout_steps view, "
             "passing in your own form_class argument.")
        form_class = import_dotted_path(settings.SHOP_CHECKOUT_FORM_CLASS)

    initial = checkout.initial_order_data(request, form_class)
    step = int(
        request.POST.get("step", None) or initial.get("step", None)
        or checkout.CHECKOUT_STEP_FIRST)
    form = form_class(request, step, initial=initial)
    data = request.POST
    #checkout_errors = []

    if request.POST.get("back") is not None:
        # Back button in the form was pressed - load the order form
        # for the previous step and maintain the field values entered.
        step -= 1
        form = form_class(request, step, initial=initial)
    elif request.method == "POST" and request.cart.has_items():
        form = form_class(request, step, initial=initial, data=data)
        if form.is_valid():
            #human = True
            # Copy the current form fields to the session so that
            # they're maintained if the customer leaves the checkout
            # process, but remove sensitive fields from the session
            # such as the credit card fields so that they're never
            # stored anywhere.
            request.session["order"] = dict(form.cleaned_data)
            sensitive_card_fields = ("card_number", "card_expiry_month",
                                     "card_expiry_year", "card_ccv")
            for field in sensitive_card_fields:
                if field in request.session["order"]:
                    del request.session["order"][field]

            # FIRST CHECKOUT STEP - handle shipping and discount code.
            if step == checkout.CHECKOUT_STEP_FIRST:
                # Discount should be set before shipping, to allow
                # for free shipping to be first set by a discount
                # code.
                form.set_discount()
                try:
                    billship_handler(request, form)
                    tax_handler(request, form)
                except checkout.CheckoutError as e:
                    checkout_errors.append(e)
                #wni: added for wx pay, for get order total on confirmation page
                #tmp_order = form.save(commit=False)
                #request.session['wni_wxpay_total'] = tmp_order.get_tmp_total(request)

            # FINAL CHECKOUT STEP - handle payment and process order.
            if step == checkout.CHECKOUT_STEP_LAST and not checkout_errors:
                # Create and save the initial order object so that
                # the payment handler has access to all of the order
                # fields. If there is a payment error then delete the
                # order, otherwise remove the cart items from stock
                # and send the order receipt email.
                order = form.save(commit=False)
                order.setup(request)
                # Try payment.
                try:
                    transaction_id = payment_handler(request, form, order)
                except checkout.CheckoutError as e:
                    # Error in payment handler.
                    order.delete()
                    checkout_errors.append(e)
                    if settings.SHOP_CHECKOUT_STEPS_CONFIRMATION:
                        step -= 1
                else:
                    # Finalize order - ``order.complete()`` performs
                    # final cleanup of session and cart.
                    # ``order_handler()`` can be defined by the
                    # developer to implement custom order processing.
                    # Then send the order email to the customer.
                    order.transaction_id = transaction_id
                    order.complete(request)
                    order_handler(request, form, order)
                    checkout.send_order_email(request, order)
                    # Set the cookie for remembering address details
                    # if the "remember" checkbox was checked.
                    response = redirect("shop_complete")
                    if form.cleaned_data.get("remember"):
                        remembered = "%s:%s" % (sign(order.key), order.key)
                        set_cookie(response,
                                   "remember",
                                   remembered,
                                   secure=request.is_secure())
                    else:
                        response.delete_cookie("remember")
                    return response

            # If any checkout errors, assign them to a new form and
            # re-run is_valid. If valid, then set form to the next step.
            form = form_class(request,
                              step,
                              initial=initial,
                              data=data,
                              errors=checkout_errors)
            #print type(form.errors),form.errors
            #form.errors.clear()
            if form.is_valid():
                step += 1
                #human = True
                form = form_class(request, step, initial=initial)

    # Update the step so that we don't rely on POST data to take us back to
    # the same point in the checkout process.
    try:
        request.session["order"]["step"] = step
        request.session.modified = True
    except KeyError:
        pass

    step_vars = checkout.CHECKOUT_STEPS[step - 1]
    template = "shop/%s.html" % step_vars["template"]
    context = {
        "CHECKOUT_STEP_FIRST": step == checkout.CHECKOUT_STEP_FIRST,
        "CHECKOUT_STEP_LAST": step == checkout.CHECKOUT_STEP_LAST,
        "step_title": step_vars["title"],
        "step_url": step_vars["url"],
        "steps": checkout.CHECKOUT_STEPS,
        "step": step,
        "form": form
    }

    return render(request, template, context)
Beispiel #10
0
def quickpay_checkout(request: HttpRequest) -> HttpResponse:
    """Checkout using Quickpay payment form.

    Use the normal cartridge.views.checkout_steps for GET and for the rest other payments steps,
    use this special version for POSTing paument form for Quickpay.

    Settings:

    QUICKPAY_ORDER_FORM = dotted path to order form to use
    QUICKPAY_FRAMED_MODE = <whether to use framed Quickpay>

    QUICKPAY_SHOP_BASE_URL: str required = URL of the shop for success, cancel and callback URLs
    QUICKPAY_ACQUIRER: str|list required = The acquirer(s) to use, e.g. 'clearhaus'
    QUICKPAY_AUTO_CAPTURE: bool default False = Whether to auto-capture payment

    urls.py setup:

    from cartridge_quickpay.views import checkout_quickpay, order_form_class
    ...

    url("^shop/checkout/", checkout_steps, {'form_class': order_form_class}),
    url("^shop/checkout_quickpay/", checkout_quickpay, name="checkout_quickpay"),
    url("^shop/", include("cartridge.shop.urls")),
    ...

    ** FOR FRAMED MODE: **

    Change checkout.html
    - <form ... onsubmit="return false">
    - Change submit button to:
    - <button class="btn btn-lg btn-primary pull-right" onclick="checkout_quickpay();">Go to payment</button>
    - add payment modal

    <div class="modal db-modal fade" id="payment_window" tabindex="-1" role="dialog" aria-labelledby="payment_window_label">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-body">
              <iframe id="payment_iframe" style="width: 100%; border: none; height: 90vh;"></iframe>
          </div>
        </div>
      </div>
    </div>

    - and add JS at the bottom:

    <script>
    function checkout_quickpay() {
      $.post("{% url 'quickpay_checkout' %}", $('.checkout-form').serialize(), function(data) {
        if (data.success) {
          $('#payment_iframe').attr('src', data.payment_link);
          $('#payment_window').modal('show');
        } else {
          alert("failed");
        }
      });
    }
    </script>
    """
    framed: bool = getattr(settings, 'QUICKPAY_FRAMED_MODE', False)
    acquirer = request.POST.get('acquirer', None)
    logging.debug("quickpay_checkout: using acquirer {}".format(acquirer
                                                                or '<any>'))
    in_popup = acquirer_requires_popup(acquirer)
    step = checkout.CHECKOUT_STEP_FIRST  # Was: _LAST
    checkout_errors = []

    initial = checkout.initial_order_data(request, order_form_class)
    logging.debug("quickpay_checkout: initial order data = {}".format(initial))
    form = order_form_class(request, step, initial=initial, data=request.POST)
    if form.is_valid():
        logging.debug("quickpay_checkout() - Form valid")
        request.session["order"] = dict(form.cleaned_data)
        try:
            billship_handler(request, form)
            tax_handler(request, form)
        except checkout.CheckoutError as e:
            logging.warn(
                "quickpay_checkout() - billship or tax handler failed")
            checkout_errors.append(e)

        # Create order and Quickpay payment, redirect to Quickpay/Mobilepay form
        order = form.save(commit=False)
        order.setup(request)  # Order is saved here so it gets an ID

        # Handle subscription or one-time order
        if (hasattr(order, 'has_subscription') and order.has_subscription()
                and acquirer_supports_subscriptions(acquirer)):
            quickpay_subs_id, quickpay_link = start_subscription(
                order,
                order.items.all().order_by('id')[0])
            logging.debug(
                "quickpay_checkout() - starting subscription {}, payment link {}"
                .format(quickpay_subs_id, quickpay_link))
        else:
            # One-time order OR subscription with acquirer that doesn't support subscriptions
            quickpay_link: str = get_quickpay_link(order, acquirer)['url']
            logging.debug(
                "quickpay_checkout() - product purchase (or subscription w/o auto-renewal), payment link {}"
                .format(quickpay_link))

        # Redirect to Quickpay
        if framed:
            logging.debug("quickpay_checkout() - JSON response {}".format(
                str({
                    'success': True,
                    'payment_link': quickpay_link
                })))
            return JsonResponse({
                'success': True,
                'payment_link': quickpay_link
            })
            # Medsende om url skal åbnes i nyt vindue, åben i JS, håndtere at returside havner i iframe igen
        elif in_popup:
            logging.debug("quickpay_checkout() - Opening popup window")
            return render(request, "cartridge_quickpay/payment_toplevel.html",
                          {'quickpay_link': quickpay_link})
        else:
            logging.debug("quickpay_checkout() - Redirect response")
            return HttpResponseRedirect(redirect_to=quickpay_link)

    # Form invalid, go back to checkout step
    step_vars = checkout.CHECKOUT_STEPS[step - 1]
    template = "shop/%s.html" % step_vars["template"]
    context = {
        "CHECKOUT_STEP_FIRST":
        step == checkout.CHECKOUT_STEP_FIRST,
        "CHECKOUT_STEP_LAST":
        step == checkout.CHECKOUT_STEP_LAST,
        "CHECKOUT_STEP_PAYMENT": (settings.SHOP_PAYMENT_STEP_ENABLED
                                  and step == checkout.CHECKOUT_STEP_PAYMENT),
        "step_title":
        step_vars["title"],
        "step_url":
        step_vars["url"],
        "steps":
        checkout.CHECKOUT_STEPS,
        "step":
        step,
        "form":
        form,
        "payment_url":
        "https://payment.quickpay.net/d7ad25ea15154ef4bdffb5bf78f623fc"
    }

    page = loader.get_template(template).render(context=context,
                                                request=request)
    if framed:
        logging.debug("quickpay_checkout() - Form not OK, JSON response")
        return JsonResponse({'success': False, 'page': page})
    else:
        logging.debug("quickpay_checkout() - Form not OK, page response")
        return HttpResponse(page)