def post(self, request): is_valid = True # Validation on the submitted request if len(request.DATA['order_items']) == 0: # Failed due to empty order is_valid = False response_data = { 'status': 'failed', 'reason': 'order_items cannot be empty' } if is_valid: order = Order(**request.DATA['order_data']) order.save() # Create OrderItem instances from the submitted order order_items = request.DATA['order_items'] for item in order_items: new_item = OrderItem() new_item.beer = Beer.objects.get(pk=item['beer']['id']) new_item.quantity = item['quantity'] new_item.order = order new_item.save() # Send relevant emails order.notify_ert() order.send_confirmation() # Send API response response_data = { 'status': 'success', 'order_id': order.id } response = Response(response_data) else: response = Response(response_data, status=status.HTTP_400_BAD_REQUEST) return response
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()
def post(self, request): is_valid = True # Validation on the submitted request if len(request.DATA['order_items']) == 0: # Failed due to empty order is_valid = False response_data = { 'status': 'failed', 'reason': 'order_items cannot be empty' } if is_valid: order = Order(**request.DATA['order_data']) order.save() # Create OrderItem instances from the submitted order order_items = request.DATA['order_items'] for item in order_items: new_item = OrderItem() new_item.beer = Beer.objects.get(pk=item['beer']['id']) new_item.quantity = item['quantity'] new_item.order = order new_item.save() # Send relevant emails order.notify_ert() order.send_confirmation() # Send API response response_data = {'status': 'success', 'order_id': order.id} response = Response(response_data) else: response = Response(response_data, status=status.HTTP_400_BAD_REQUEST) return response
def test_orderItem(self): order = OrderItem() order.quantity = 2 #Quantity of item order.product = Product() order.product.name = "A solution book" order.product.price = 200 #Price of the product order.product.digital = True order.product.save() order.save() record = OrderItem.objects.get(pk=1) self.assertEqual(record.get_total, 600) #Check if expected total equal to returned total
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/')