def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) amount = int(order.total_price() * 100) token = request.data.get('stripeToken') billing_address_id = request.data.get('selectedBillingAddress') shipping_address_id = request.data.get('selectedShippingAddress') billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) try: charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token ) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = charge['amount'] payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment order.ref_code = create_ref_code() order.billing_address = billing_address order.shipping_address = shipping_address order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: return Response({"message": f"{err.get('messages')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: return Response({"message": "Rate limit error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: return Response({"message": "Invalid request error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: print(e) return Response({"message": "Authentication error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: return Response({"message": "Api connection error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: return Response({"message": "Stripe error"}, status=HTTP_400_BAD_REQUEST) except Exception as e: # send a email to ourselves return Response({"message": "Something wrong.Please try again.."}, status=HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) userprofile = UserProfile.objects.get(user=self.request.user) token = request.data.get('stripeToken') # save = form.cleaned_data.get('save') # use_default = form.cleaned_data.get('use_default') save = False use_default = False if save: if userprofile.stripe_customer_id != '' and userprofile.stripe_customer_id is not None: customer = stripe.Customer.retrieve( userprofile.stripe_customer_id) customer.sources.create(source=token) else: customer = stripe.Customer.create( email=self.request.user.email, ) customer.sources.create(source=token) userprofile.stripe_customer_id = customer['id'] userprofile.one_click_purchasing = True userprofile.save() amount = int(order.get_total() * 100) try: if use_default or save: # charge the customer because we cannot charge the token more than once charge = stripe.Charge.create( amount=amount, # cents currency="usd", customer=userprofile.stripe_customer_id ) else: # charge once off on the token charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token ) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment # order.ref_code = create_ref_code() order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly messages.warning(self.request, "Rate limit error") return Response({"message": "Rate limit error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return Response({"message": "Invalid parameters"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response({"message": "Not authenticated"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response({"message": "Network error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response({"message": "Something went wrong. You were not charged. Please try again."}, status=HTTP_400_BAD_REQUEST) except Exception as e: # send an email to ourselves print(e) return Response({"message": "A serious error occurred. We have been notifed."}, status=HTTP_400_BAD_REQUEST) return Response({"message": "Invalid data received"}, status=HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) userprofile = UserProfile.objects.get(user=self.request.user) token = request.data.get("stripeToken") billing_address = "" shipping_address = "" if "selectedShippingAddress" in request.data.keys(): shipping_address_id = request.data.get("selectedShippingAddress") shipping_address = Address.objects.get(id=shipping_address_id) print(shipping_address, shipping_address_id) if "selectedBillingAddress" in request.data.keys(): billing_address_id = request.data.get("selectedBillingAddress") billing_address = Address.objects.get(id=billing_address_id) print(billing_address, billing_address_id) if (userprofile.stripe_customer_id != "" and userprofile.stripe_customer_id is not None): customer = stripe.Customer.retrieve(userprofile.stripe_customer_id) customer.sources.create(source=token) else: customer = stripe.Customer.create(email=self.request.user.email) customer.sources.create(source=token) userprofile.stripe_customer_id = customer["id"] userprofile.one_click_purchasing = True userprofile.save() amount = int(order.get_total() * 100) try: # charge the customer because we cannot charge the token more than once charge = stripe.Charge.create( amount=amount, # cents currency="eur", customer=userprofile.stripe_customer_id, ) # charge once off on the token # charge = stripe.Charge.create( # amount=amount, # cents # currency="usd", # source=token # ) # create the payment payment = Payment() payment.stripe_charge_id = charge["id"] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment if len(billing_address) > 0: order.billing_address = billing_address if len(shipping_address) > 0: order.shipping_address = shipping_address # order.ref_code = create_ref_code() order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: body = e.json_body err = body.get("error", {}) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly messages.warning(self.request, "Rate limit error") return Response({"message": "Rate limit error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: print(e) # Invalid parameters were supplied to Stripe's API return Response({"message": "Invalid parameters"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response({"message": "Not authenticated"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response({"message": "Network error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response( { "message": "Something went wrong. You were not charged. Please try again." }, status=HTTP_400_BAD_REQUEST, ) except Exception as e: # send an email to ourselves return Response( { "message": "A serious error occurred. We have been notifed." + str(e) }, status=HTTP_400_BAD_REQUEST, ) return Response({"message": "Invalid data received"}, status=HTTP_400_BAD_REQUEST)
def post(self, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) token = self.request.POST.get('stripeToken') amount = int(order.get_total() * 100) # customer.sources.create(source=token) # userprofile.stripe_customer_id = customer['id'] # userprofile.one_click_purchasing = True # userprofile.save() try: # if use_default or save: # # charge the customer because we cannot charge the token more than once # charge = stripe.Charge.create( # amount=amount, # cents # currency="usd", # customer=userprofile.stripe_customer_id # ) # else: # charge once off on the token charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order # order_items = order.items.all() # order_items.update(ordered=True) # for item in order_items: # item.save() order.ordered = True order.payment = payment # order.ref_code = create_ref_code() order.save() messages.success(self.request, "Your order was successful!") return redirect("/") except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) messages.warning(self.request, f"{err.get('message')}") return redirect("/") except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly messages.warning(self.request, "Rate limit error") return redirect("/") except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API print(e) messages.warning(self.request, "Invalid parameters") return redirect("/") except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) messages.warning(self.request, "Not authenticated") return redirect("/") except stripe.error.APIConnectionError as e: # Network communication with Stripe failed messages.warning(self.request, "Network error") return redirect("/") except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email messages.warning( self.request, "Something went wrong. You were not charged. Please try again." ) return redirect("/") except Exception as e: # send an email to ourselves messages.warning( self.request, "A serious error occurred. We have been notifed.") return redirect("/") messages.warning(self.request, "Invalid data received") return redirect("/payment/stripe/")
def post(self, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) token = self.request.POST.get('stripeToken') amount = int(order.get_total() * 100) try: charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order.ordered = True order.payment = payment # TODO : assign ref code order.ref_code = create_ref_code() order.save() messages.success(self.request, "Order was successful") return redirect("/") except stripe.error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught body = e.json_body err = body.get('error', {}) messages.error(self.request, f"{err.get('message')}") return redirect("/") except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly messages.error(self.request, "RateLimitError") return redirect("/") except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API messages.error(self.request, "Invalid parameters") return redirect("/") except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) messages.error(self.request, "Not Authentication") return redirect("/") except stripe.error.APIConnectionError as e: # Network communication with Stripe failed messages.error(self.request, "Network Error") return redirect("/") except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email messages.error(self.request, "Something went wrong") return redirect("/") except Exception as e: # send an email to ourselves messages.error(self.request, "Serious Error occured") return redirect("/")
def post(self, request, *args, **kwargs): order_qs = Order.objects.filter(user=self.request.user, ordered=False) userprofile = UserProfile.objects.get(user=self.request.user) token = request.data.get('stripeToken') print("token", token) billing_address_id = request.data.get('selectedBillingAddress') shipping_address_id = request.data.get('selectedShippingAddress') addresses = list( Address.objects.filter(user=request.user).values_list('id', flat=True)) # change tis to accomadate empty id's if not is_valid(billing_address_id) or not is_valid( shipping_address_id) or int( billing_address_id) not in addresses or int( shipping_address_id) not in addresses: return Response( {"message": "Please fill in your appropiate addresses."}, status=HTTP_400_BAD_REQUEST) billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) if not order_qs.exists(): return Response( { "message": "Order not found, you've probably checked out already." }, status=HTTP_400_BAD_REQUEST) order = order_qs[0] # if is_valid(userprofile.stripe_customer_id): # customer = stripe.Customer.retrieve( # userprofile.stripe_customer_id) # # customer.sources.create(source=token) # else: # customer = stripe.Customer.create( # email=self.request.user.email, # ) # # customer.sources.create(source=token) # userprofile.stripe_customer_id = customer['id'] # userprofile.one_click_purchasing = True # userprofile.save() amount = int(order.get_total() * 100) try: # if is_valid(userprofile.stripe_customer_id): # # charge the customer because we cannot charge the token more than once # charge = stripe.Charge.create( # amount=amount, # cents # currency="usd", # customer=userprofile.stripe_customer_id # ) # else: # charge once off on the token print(amount) charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment order.billing_address = billing_address order.shipping_address = shipping_address order.ref_code = create_ref_code() order.save() return Response( status=HTTP_200_OK, data={"message": "Payment Successful and captured."}) except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) print(e) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly print(e) messages.warning(self.request, "Rate limit error") return Response({"message": "Rate limit error."}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: print(e) # Invalid parameters were supplied to Stripe's API return Response({"message": "Invalid parameters."}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) print(e) return Response({"message": "Not authenticated."}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed print(e) return Response({"message": "Network error."}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email print(e) return Response( { "message": "Something went wrong. You were not charged. Please try again." }, status=HTTP_400_BAD_REQUEST) except Exception as e: # send an email to ourselves print(e) return Response( {"message": "A serious error occurred. We have been notifed."}, status=HTTP_400_BAD_REQUEST) return Response({"message": "Invalid data received."}, status=HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): print(self.request.user) order = get_object_or_404(Order, user=self.request.user, ordered=False) user_profile = get_object_or_404(UserProfile, user=self.request.user) # doesn't work on test visa card with the number 4242 4242 4242 4242 token = self.request.data.get('stripeToken') # https://stripe.com/docs/testing#cards # token = 'tok_visa' billing_address_id = self.request.data.get('selectedBillingAddress') shipping_address_id = self.request.data.get('selectedShippingAddress') billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) if user_profile.stripe_customer_id != '' and user_profile.stripe_customer_id is not None: customer = stripe.Customer.retrieve( user_profile.stripe_customer_id) customer.sources.create(source=token) else: customer = stripe.Customer.create(email=self.request.user.email, ) customer.sources.create(source=token) user_profile.stripe_customer_id = customer['id'] user_profile.one_click_purchasing = True user_profile.save() amount = int(order.get_total() * 100) try: # create a charge (https://stripe.com/docs/api/charges/create?lang=python) # charge the customer because we cannot charge the token more than once charge = stripe.Charge.create( amount=amount, # cents currency="usd", customer=user_profile.stripe_customer_id) # charge once off on the token # charge = stripe.Charge.create( # amount=amount, # cents # currency="usd", # source=token # obtained with Stripe.js # ) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment order.billing_address = billing_address order.shipping_address = shipping_address # order.ref_code = generate_ref_code() order.save() return Response({'message': 'Your order was successful!'}, status=status.HTTP_200_OK) # stripe error handling (https://stripe.com/docs/api/errors/handling?lang=python) except stripe.error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught body = e.json_body err = body.get('error', {}) return Response({'message': f"{err.get('message')}"}, status=status.HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly return Response( {'message': 'Too many requests made to the API too quickly'}, status=status.HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return Response( { 'message': "Invalid parameters were supplied to Stripe's API" }, status=status.HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response( {'message': "Authentication with Stripe's API failed"}, status=status.HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response( {'message': 'Network communication with Stripe failed'}, status=status.HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response( { 'message': 'Something went wrong. You were not charged. Please try again' }, status=status.HTTP_400_BAD_REQUEST) except Exception as e: # send an email to myself because there's something wrong with my code return Response( {'message': 'Serious error occurred. We have been notified'}, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) userprofile = UserProfile.objects.get(user=self.request.user) billing_address_id = request.data.get('selectedBillingAddress') shipping_address_id = request.data.get('selectedShippingAddress') print(billing_address_id, shipping_address_id) billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) token = request.data.get('stripeToken') if userprofile.stripe_customer_id != '' and userprofile.stripe_customer_id is not None: customer = stripe.Customer.retrieve(userprofile.stripe_customer_id) else: customer = stripe.Customer.create(email=self.request.user.email, source=token) userprofile.stripe_customer_id = customer['id'] userprofile.one_click_purchasing = True userprofile.save() amount = int(order.get_total() * 100) try: try: # charge once off on the token for first time purchases charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token) except Exception as e: print("CANT CHARGE TOKEN TWICE ERROR: " + e) else: # charge the customer because we cannot charge the token more than once charge = stripe.Charge.create( amount=amount, # cents currency="usd", customer=userprofile.stripe_customer_id) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment order.billing_address = billing_address order.shipping_address = shipping_address # order.ref_code = create_ref_code() order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly return Response({"message": "RATE_LIMIT_ERROR"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: # Invalid parameters were supplied to Stripe's API return Response({"message": "INVALID_PARAMETERS"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response({"message": "NOT_AUTHENTICATED"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response({"message": "NETWORK_ERROR"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response({"message": "STRIPE_ERROR"}, status=HTTP_400_BAD_REQUEST) except Exception as e: # send an email to ourselves print(e) return Response( { "message": "SERIOUS_PYTHON_ERORR_OCCURRED, DEV_TEAM_NOTIFIED" }, status=HTTP_400_BAD_REQUEST) return Response({"message": "INVALID_DATA"}, status=HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) token = request.data.get('stripeToken') billing_address_id = request.data.get('selectedBillingAddress') shipping_address_id = request.data.get('selectedShippingAddress') amount = int(order.get_total() * 100) billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) try: # charge once off on the token charge = stripe.Charge.create( amount=amount, # cents currency="usd", source=token ) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() order_items.update(ordered=True) for item in order_items: item.save() order.ordered = True order.payment = payment order.billing_address = billing_address order.shipping_address = shipping_address order.ref_code = str(order.id) + create_ref_code() order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly return Response({"message": "Rate limit error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: print(e) # Invalid parameters were supplied to Stripe's API return Response({"message": "Invalid parameters"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response({"message": "Not authenticated"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response({"message": "Network error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response({"message": "Something went wrong. You were not charged. Please try again."}, status=HTTP_400_BAD_REQUEST) except Exception as e: # send an email to ourselves return Response({"message": "A serious error occurred. We have been notifed."}, status=HTTP_400_BAD_REQUEST) return Response({"message": "Invalid data received"}, status=HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): order = Order.objects.get(user=self.request.user, ordered=False) userprofile = UserProfile.objects.get(user=self.request.user) token = request.data.get('stripeToken') billing_address_id = request.data.get('selectedBillingAddress') shipping_address_id = request.data.get('selectedShippingAddress') billing_address = Address.objects.get(id=billing_address_id) shipping_address = Address.objects.get(id=shipping_address_id) if userprofile.stripe_customer_id != '' and userprofile.stripe_customer_id is not None: customer = stripe.Customer.retrieve(userprofile.stripe_customer_id) customer.sources.create(source=token) else: customer = stripe.Customer.create(email=self.request.user.email, ) customer.sources.create(source=token) userprofile.stripe_customer_id = customer['id'] userprofile.one_click_purchasing = True userprofile.save() amount = int(order.get_total() * 100) try: # charge the customer because we cannot charge the token more than once charge = stripe.Charge.create( amount=amount, # cents currency="usd", customer=userprofile.stripe_customer_id) # charge once off on the token # charge = stripe.Charge.create( # amount=amount, # cents # currency="usd", # source=token # ) # create the payment payment = Payment() payment.stripe_charge_id = charge['id'] payment.user = self.request.user payment.amount = order.get_total() payment.save() # assign the payment to the order order_items = order.items.all() for item in order_items: if item.quantity > item.item.stock_quantity: print("this happend") return Response( {"message": "This product is now out of stock"}, status=HTTP_400_BAD_REQUEST) else: pass subtract_item_quantity_from_stock(item) order_items.update(ordered=True) order.ordered = True order.payment = payment order.billing_address = billing_address order.shipping_address = shipping_address order.ordered_date = datetime.datetime.now() # order.ref_code = create_ref_code() try: for i in order_items: old_item = ItemOrdered( price=i.item.price, title=i.item.title, sku=i.item.sku, upc=i.item.upc, discount_price=i.item.discount_price, category=i.item.category, label=i.item.label, slug=i.item.slug, description=i.item.description, image=i.item.image) old_item.save() i.ordered_item = old_item i.save() except Exception as e: print(e) return Response("Dhappa") # for item in order_items: order.save() return Response(status=HTTP_200_OK) except stripe.error.CardError as e: body = e.json_body err = body.get('error', {}) return Response({"message": f"{err.get('message')}"}, status=HTTP_400_BAD_REQUEST) except stripe.error.RateLimitError as e: # Too many requests made to the API too quickly messages.warning(self.request, "Rate limit error") return Response({"message": "Rate limit error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.InvalidRequestError as e: print(e) # Invalid parameters were supplied to Stripe's API return Response({"message": "Invalid parameters"}, status=HTTP_400_BAD_REQUEST) except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) return Response({"message": "Not authenticated"}, status=HTTP_400_BAD_REQUEST) except stripe.error.APIConnectionError as e: # Network communication with Stripe failed return Response({"message": "Network error"}, status=HTTP_400_BAD_REQUEST) except stripe.error.StripeError as e: # Display a very generic error to the user, and maybe send # yourself an email return Response( { "message": "Something went wrong. You were not charged. Please try again." }, status=HTTP_400_BAD_REQUEST) except Exception as e: # send an email to ourselves return Response( {"message": "A serious error occurred. We have been notifed."}, status=HTTP_400_BAD_REQUEST) return Response({"message": "Invalid data received"}, status=HTTP_400_BAD_REQUEST)