Ejemplo n.º 1
0
    def save(self, cart):
        first_name = self.cleaned_data.get('first_name')
        last_name = self.cleaned_data.get('last_name')
        street = self.cleaned_data.get('street')
        house_number = self.cleaned_data.get('house_number')
        flat_number = self.cleaned_data.get('flat_number')
        zip_code = self.cleaned_data.get('zip_code')
        phone_number = self.cleaned_data.get('phone_number')
        city = self.cleaned_data.get('city')

        order = Order()
        order.first_name = first_name
        order.last_name = last_name
        order.street = street
        order.house_number = house_number
        order.flat_number = flat_number
        order.zip_code = zip_code
        order.phone_number = phone_number
        order.city = city
        order.user = self.user
        order.save()

        for item in cart:
            order_item = OrderItem()
            order_item.order = order
            order_item.product = item['product']
            order_item.price = item['price']
            order_item.quantity = item['quantity']
            order_item.save()
Ejemplo n.º 2
0
def checkout(request):
    if request.method == "GET":
        form = CheckoutForm()
        cart = request.session.get('cart')

        if cart is None:
            cart = []

        for c in cart:
            size_str = c.get('size')
            tshirt_id = c.get('tshirt')
            size_obj = SizeVarient.objects.get(size=size_str, tshirt=tshirt_id)
            c['size'] = size_obj
            c['tshirt'] = size_obj.tshirt
        context = {'form': form, 'cart': cart}

        return render(request,
                      template_name="store/checkout.html",
                      context=context)
    else:
        form = CheckoutForm(request.POST)
        user = None
        print("user 11", user)
        if request.user.is_authenticated:
            print("user 12", user)
            user = request.user
        if form.is_valid():
            print("user 13", user)
            # payment
            cart = request.session.get('cart')
            if cart is None:
                cart = []
            for c in cart:
                size_str = c.get('size')
                tshirt_id = c.get('tshirt')
                size_obj = SizeVarient.objects.get(size=size_str,
                                                   tshirt=tshirt_id)
                c['size'] = size_obj
                c['tshirt'] = size_obj.tshirt
            shipping_addres = form.cleaned_data.get('shipping_addres')
            contact = form.cleaned_data.get('contact')
            payment_method = form.cleaned_data.get('payment_method')
            total = cal_total_payable_amount(cart)

            print(shipping_addres, contact, payment_method, total)

            order = Order()
            order.shipping_addres = shipping_addres
            order.contact = contact
            order.payment_method = payment_method
            order.total = total
            order.order_status = "PENDING"
            order.user = user
            order.save()

            # saving order_items
            for c in cart:
                order_item = OrderItems()
                order_item.order = order
                size = c.get('size')
                tshirt = c.get('tshirt')
                order_item.price = floor(size.price -
                                         (size.price *
                                          (tshirt.discount) / 100))
                order_item.quantity = c.get('quantity')
                order_item.size = size
                order_item.tshirt = tshirt
                order_item.save()

            # payment gateway
            response = api.payment_request_create(
                amount=order.total,
                purpose='Payment for tshirt',
                send_email=True,
                buyer_name=f'{user.first_name} {user.last_name}',
                email=user.email,
                redirect_url="http://127.0.0.1:8000/validate_payment")
            # print('response', response)
            print('request_id', response['payment_request']['id'])
            print('request_longurl', response['payment_request']['longurl'])

            payment_id = (response['payment_request']['id'])
            url = response['payment_request']['longurl']

            payment = Payment()
            payment.order = order
            payment.payment_request_id = payment_id
            payment.save()
            return redirect(url)

        else:
            return redirect('/checkout/')
Ejemplo n.º 3
0
def checkout(request):
    # get Request
    if request.method == 'GET':
        form = CheckForm()
        cart = request.session.get('cart')
        if cart is None:
            cart = []

        for c in cart:
            size_str = c.get('size')
            tshirt_id = c.get('tshirt')
            size_obj = SizeVariant.objects.get(size=size_str, tshirt=tshirt_id)
            c['size'] = size_obj
            c['tshirt'] = size_obj.tshirt

        print(cart)

        return render(request, 'store/checkout.html', {
            "form": form,
            'cart': cart
        })
    else:
        # post request
        form = CheckForm(request.POST)
        user = None
        if request.user.is_authenticated:
            user = request.user
        if form.is_valid():
            # payment
            cart = request.session.get('cart')
            if cart is None:
                cart = []
            for c in cart:
                size_str = c.get('size')
                tshirt_id = c.get('tshirt')
                size_obj = SizeVariant.objects.get(size=size_str,
                                                   tshirt=tshirt_id)
                c['size'] = size_obj
                c['tshirt'] = size_obj.tshirt
            shipping_address = form.cleaned_data.get('shipping_address')
            phone = form.cleaned_data.get('phone')
            payment_method = form.cleaned_data.get('payment_method')
            total = cal_total_payable_amount(cart)
            print(shipping_address, phone, payment_method, total)

            order = Order()
            order.shipping_address = shipping_address
            order.phone = phone
            order.payment_method = payment_method
            order.total = total
            order.order_status = "PENDING"
            order.user = user
            order.save()

            # saving order items
            for c in cart:
                order_item = OrderItem()
                order_item.order = order
                size = c.get('size')
                tshirt = c.get('tshirt')
                order_item.price = floor(size.price -
                                         (size.price *
                                          (tshirt.discount / 100)))
                order_item.quantity = c.get('quantity')
                order_item.size = size
                order_item.tshirt = tshirt
                order_item.save()

            buyer_name = f'{user.first_name} {user.last_name}'
            print(buyer_name)
            # crating payment
            response = API.payment_request_create(
                amount=order.total,
                purpose="Payment For Tshirts",
                send_email=False,
                buyer_name=f'{user.first_name} {user.last_name}',
                email=user.email,
                redirect_url="http://localhost:8000/validate_payment")

            payment_request_id = response['payment_request']['id']
            url = response['payment_request']['longurl']

            payment = Payment()
            payment.order = order
            payment.payment_request_id = payment_request_id
            payment.save()
            return redirect(url)
        else:
            return redirect('/checkout/')
def checkout(request):
    if request.method == 'GET':     #for get Request
        form = CheckForm()
        cart=request.session.get('cart')
        if cart is None:
            cart=[]
        
        for c in cart:
            size_str = c.get('size')
            tshirt_id = c.get('tshirt')
            size_obj=SizeVariant.objects.get(size=size_str , tshirt=tshirt_id)   #now in cart list ,size is replaced by its object
            c['size']=size_obj
            c['tshirt']=size_obj.tshirt   #replacing in cart list , tshirt is id ,inplace of id tshirt object is stored

        return render(request,'store/checkout.html',{"form" : form, 'cart':cart})
    else:
        #this is for POST request
        form = CheckForm(request.POST)
        user = None
        if request.user.is_authenticated :
            user = request.user
        if form.is_valid():
            #payment should be done here
            cart=request.session.get('cart')
            if cart is None:
                cart = []
            for c in cart:
                size_str = c.get('size')
                tshirt_id = c.get('tshirt')
                size_obj=SizeVariant.objects.get(size=size_str , tshirt=tshirt_id)   #now in cart list ,size is replaced by its object
                c['size']=size_obj
                c['tshirt']=size_obj.tshirt
            shipping_address=form.cleaned_data.get('shipping_address')   #the shipping address entered will be stored in shipping_address variable
            phone=form.cleaned_data.get('phone')
            payment_method = form.cleaned_data.get('payment_method')
            total = cal_total_payable_amount(cart)
            print(shipping_address,phone,payment_method , total)
            order= Order()               #created Order Model object
            order.shipping_address = shipping_address   #entering data in ORDER table
            order.phone = phone
            order.payment_method= payment_method
            order.total = total
            order.order_status = "PENDING"
            order.user = user
            order.save()    
            #print(order.id)        # we will get order id 

            #saving to OrderItems Table
            for c in cart :
                order_item = OrderItem()   #creating new object for OrderItem Table
                order_item.order = order      #order id will be stored in order
                size=c.get('size')   #to access dictionary we use get('key')
                tshirt=c.get('tshirt')
                order_item.price = floor(size.price- (size.price*(tshirt.discount/100)))
                order_item.quantity = c.get('quantity')
                order_item.size = size
                order_item.tshirt = tshirt
                order_item.save()

            
            # CREATING PAYMENT
            # Create a new Payment Request
            response = API.payment_request_create(
                amount=order.total,
                purpose='Payment For Tshirts',
                buyer_name=f'{user.first_name} {user.last_name}',
                send_email=True,
                email=user.email,
                redirect_url="http://localhost:8000/validate_payment"
                )
            #print(response['payment_request'])
            payment_request_id = response['payment_request']['id']
            #the long URL of the payment request
            url =response['payment_request']['longurl']
            print(url)
            
            payment= Payment()
            payment.order= order
            payment.payment_request_id= payment_request_id
            payment.save()
            return redirect(url)
        else:
            return redirect('/checkout/')
Ejemplo n.º 5
0
    def post(self, request):
        user = None
        form = CheckoutForm(request.POST)
        if request.user.is_authenticated:
            user = request.user

        if form.is_valid():
            buy = request.session.get('buy')
            cart = request.session.get('cart')

            #creating order
            shipping_address = form.cleaned_data.get('shipping_address')
            phone = form.cleaned_data.get('phone')
            payment_method = form.cleaned_data.get('payment_method')
            order = Order()
            order.shipping_address = shipping_address
            order.phone = phone
            order.payment_method = payment_method

            #logic for buing item directly through pay button
            if buy:
                price = 0
                for b in buy:
                    size_str = b.get('size')
                    cloth_id = b.get('cloth')
                    cloth_obj = Size_Variant.objects.get(size=size_str,
                                                         cloth=cloth_id)
                    cloth = Cloth.objects.get(id=cloth_id)
                    b['size'] = cloth_obj
                    b['cloth'] = cloth_obj.cloth
                    price = cloth_obj.price
                    price = floor(price - (price *
                                           (cloth.cloth_discount / 100)))
                    b['total'] = price

                order.total = price

                order.order_status = "PENDING"
                order.user = user
                order.save()
                # preventing duplicated value in buy
                preventing_duplicate(buy)

                #creating order item
                for b in buy:
                    order_item = Order_Item()
                    order_item.order = order
                    size = b.get('size')
                    cloth = b.get('cloth')
                    order_item.price = floor(size.price -
                                             (size.price *
                                              (cloth.cloth_discount / 100)))
                    order_item.quantity = 1
                    order_item.size = size
                    order_item.cloth = cloth
                    order_item.save()

        #logic for buying item through cart
            else:
                if cart is None:
                    cart = []
                for c in cart:
                    size_str = c.get('size')
                    cloth_id = c.get('cloth')
                    cloth_obj = Size_Variant.objects.get(size=size_str,
                                                         cloth=cloth_id)
                    c['size'] = cloth_obj
                    c['cloth'] = cloth_obj.cloth
                total = total_payable_amount(cart)
                order.total = total
                order.order_status = "PENDING"
                order.user = user
                order.save()

                # saving order items
                for c in cart:
                    order_item = Order_Item()
                    order_item.order = order
                    size = c.get('size')
                    cloth = c.get('cloth')
                    order_item.price = floor(size.price -
                                             (size.price *
                                              (cloth.cloth_discount / 100)))
                    order_item.quantity = c.get('quantity')
                    order_item.size = size
                    order_item.cloth = cloth
                    order_item.save()

            if payment_method == "ONLINE":
                # creating payment
                try:
                    client = razorpay.Client(auth=(KEY_ID, KEY_SECRET))
                    payment_id = client.order.create({
                        "amount": order.total * 100,
                        'currency': 'INR',
                        'payment_capture': '1',
                    })
                except:
                    messages.error(
                        request,
                        'Payment gateway is not Responding , Check your connection !'
                    )
                    return redirect('Checkout')

                payment = Payment()
                payment.order = order
                payment.payment_request_id = payment_id['id']
                payment.save()
                form = CheckoutForm()
                context = {
                    'form': form,
                    'cart': cart,
                    'buy': buy,
                    'payment': payment_id
                }
                return render(request, 'store/checkout.html', context=context)
            else:
                order.order_status = "PLACED"
                order.save()
                try:
                    context = {
                        "username": user.first_name,
                        "order": order,
                    }

                    html_message = loader.render_to_string(
                        "store/product_email.html", context=context)

                    # mail to coustomer
                    send_mail(
                        'Product Purchased',
                        'Payment for ordered product has successfull worth of money !',
                        EMAIL_HOST_USER,
                        [user.email],
                        fail_silently=False,
                        html_message=html_message,
                    )
                    # mail to user
                    send_mail(
                        'Product Purchased',
                        f"You have recieved a COD delivery order of {order.total} Rupee for order id = {order.id}, check it out and procced for delivery",
                        EMAIL_HOST_USER,
                        [EMAIL_HOST_USER],
                        fail_silently=False,
                    )
                except:
                    return redirect('Orders')

                return redirect('Orders')
        else:
            return redirect('/checkout/')