Ejemplo n.º 1
0
def create_order(request):
	order = Order()
	checkout_form = CheckoutForm(request.POST, instance=order)
	order = checkout_form.save(commit=False)
	order.user = None
	if request.user.is_authenticated():
		order.user = request.user
	order.status = Order.SUBMITTED
	print (order.number)
	order.save()
	print (order.number)
	order.number = int(str(order.id) + datetime.today().strftime('%m%d%Y'))
	order.save()
	print (order.number)
	# create order item for each cart item
	if order.pk:
		cart_items = carts.get_cart_items(request)
		for ci in cart_items: 
			oi = OrderItem()
			oi.order = order
			oi.quantity = ci.quantity
			oi.price = ci.product.price
			oi.product = ci.product
			oi.save()
			carts.empty_cart(request)
	return order
def _build_xml_shopping_cart(request):
    """
    constructs the XML representation of the current customer's shopping cart items to POST to
    the Google Checkout API
    """
    doc = Document()
    root = doc.createElement('checkout-shopping-cart')
    root.setAttribute('xmlns', 'http://checkout.google.com/schema/2')
    doc.appendChild(root)
    shopping_cart = doc.createElement('shopping-cart')
    root.appendChild(shopping_cart)
    items = doc.createElement('items')
    shopping_cart.appendChild(items)
    cart_items = carts.get_cart_items(request)

    for cart_item in cart_items:
        item = doc.createElement('item')
        items.appendChild(item)

        item_name = doc.createElement('item-name')
        item_name_text = doc.createTextNode(str(cart_item.name))
        item_name.appendChild(item_name_text)
        item.appendChild(item_name)

        item_description = doc.createElement('item-description')
        item_description_text = doc.createTextNode(str(cart_item.name))
        item_description.appendChild(item_description_text)
        item.appendChild(item_description)

        unit_price = doc.createElement('unit-price')
        unit_price.setAttribute('currency', 'USD')
        unit_price_text = doc.createTextNode(str(cart_item.price))
        unit_price.appendChild(unit_price_text)
        item.appendChild(unit_price)

        quantity = doc.createElement('quantity')
        quantity_text = doc.createTextNode(str(cart_item.quantity))
        quantity.appendChild(quantity_text)
        item.appendChild(quantity)

    checkout_flow = doc.createElement('checkout-flow-support')
    root.appendChild(checkout_flow)
    merchant_flow = doc.createElement('merchant-checkout-flow-support')
    checkout_flow.appendChild(merchant_flow)

    shipping_methods = doc.createElement('shipping-methods')
    merchant_flow.appendChild(shipping_methods)

    flat_rate_shipping = doc.createElement('flat-rate-shipping')
    flat_rate_shipping.setAttribute('name', 'FedEx Ground')
    shipping_methods.appendChild(flat_rate_shipping)

    shipping_price = doc.createElement('price')
    shipping_price.setAttribute('currency', 'USD')
    flat_rate_shipping.appendChild(shipping_price)

    shipping_price_text = doc.createTextNode('9.99')
    shipping_price.appendChild(shipping_price_text)

    return doc.toxml(encoding='utf-8')
Ejemplo n.º 3
0
def show_cart(request, template_name="cart/cart_new.html"):
    if request.method == 'POST':
        post_data = request.POST.copy()
        if post_data['submit'] == 'Remove':
            carts.remove_from_cart(request)
        if post_data['submit'] == 'Update':
            carts.update_cart(request)
        if post_data['submit'] == 'Checkout':
            checkout_url = checkouts.get_checkout_url(request)
            return HttpResponseRedirect(checkout_url)
    cart_items = carts.get_cart_items(request)
    page_title = 'Shopping Cart'
    cart_subtotal = carts.cart_subtotal(request)

    return render(request, template_name, locals())
Ejemplo n.º 4
0
def show_cart(request):
    context = custom_processor(request)
    if request.method == 'POST':
        postdata = request.POST.copy()

        if postdata['submit'] == 'Remove':
            carts.remove_from_cart(request)
        if postdata['submit'] == 'Update':
            carts.update_cart(request)
        if postdata['submit'] == 'Checkout':
            checkout_url = checkout.get_checkout_url(request)
            return HttpResponseRedirect(checkout_url)
    cart_items = carts.get_cart_items(request)
    page_title = 'Shopping Cart'
    cart_subtotal = carts.cart_subtotal(request)
    context.update({'cart_items': cart_items, 'page_title': page_title, 'cart_subtotal': cart_subtotal})
    return render(request, "cart/cart.html", context)
Ejemplo n.º 5
0
def create_order(request):
	order = Order()
	checkout_form = CheckoutForm(request.POST, instance=order)
	order = checkout_form.save(commit=False)
	order.user = None
	if request.user.is_authenticated():
		order.user = request.user
	order.status = Order.SUBMITTED
	order.save()
	order.number = int(str(order.id) + datetime.today().strftime('%m%d%Y'))
	order.save()
	# create order item for each cart item
	if order.pk:
		cart_items = carts.get_cart_items(request)
		for ci in cart_items: 
			oi = OrderItem()
			oi.order = order
			oi.quantity = ci.quantity

			if ci.product.quantity >= ci.quantity:
				ci.product.quantity = ci.product.quantity - ci.quantity
			else:
				ci.product.quantity = 0	
			ci.product.save()

			oi.price = ci.product.price
			oi.product = ci.product
			oi.save()
			carts.empty_cart(request)
	# send_mail('Subject here', 'Here is the message.', '*****@*****.**',
	# ['*****@*****.**'], fail_silently=False)
	items = OrderItem.objects.filter(order = order)
	plaintext = get_template('email.txt')
	htmly = get_template('email.html')
	d = Context({ 'order': order, 'items': items})
	subject, from_email, to = 'Ваш заказ', '*****@*****.**', [order.email]
	text_content = plaintext.render(d)
	html_content = htmly.render(d)
	msg = EmailMultiAlternatives(subject, text_content, from_email, to)
	msg.attach_alternative(html_content, "text/html")
	msg.send()
	print('sent!')
	return order
Ejemplo n.º 6
0
def create_order(request, transaction_id):
    """
    if the POST to the payment gateway successfully billed the customer, create a new order
    containing each CartItem instance, save the order with the transaction ID from the gateway,
    and empty the shopping cart

    """
    order = Order()
    checkout_form = CheckoutForm(request.POST, instance=order)
    order = checkout_form.save(commit=False)

    order.transaction_id = transaction_id
    order.ip_address = request.META.get('REMOTE_ADDR')
    order.user = None
    if request.user.is_authenticated():
        order.user = request.user
    order.status = Order.SUBMITTED
    order.save()

    if order.pk:
        """ if the order save succeeded """
        cart_items = carts.get_cart_items(request)
        for ci in cart_items:
            """ create order item for each cart item """
            oi = OrderItem()
            oi.order = order
            oi.quantity = ci.quantity
            oi.price = ci.price  # now using @property
            oi.product = ci.product
            oi.save()
        # all set, clear the cart
        carts.empty_cart(request)

        # save profile info for future orders
        if request.user.is_authenticated():
            from accounts import profile
            profile.set(request)

    return order