def post(self, request): data = request.data link = Link.objects.filter(code=data['code']).first() if not link: raise exceptions.APIException('Invalid code!') try: order = Order() order.code = link.code order.user_id = link.user.id order.ambassador_email = link.user.email order.first_name = data['first_name'] order.last_name = data['last_name'] order.email = data['email'] order.address = data['address'] order.country = data['country'] order.city = data['city'] order.zip = data['zip'] order.save() line_items = [] for item in data['products']: product = Product.objects.filter(pk=item['product_id']).first() quantity = decimal.Decimal(item['quantity']) order_item = OrderItem() order_item.order = order order_item.product_title = product.title order_item.price = product.price order_item.quantity = quantity order_item.ambassador_revenue = decimal.Decimal(.1) * product.price * quantity order_item.admin_revenue = decimal.Decimal(.9) * product.price * quantity order_item.save() line_items.append({ 'name': product.title, 'description': product.description, 'images': [ product.image ], 'amount': int(100 * product.price), 'currency': 'usd', 'quantity': quantity }) stripe.api_key = 'sk_test_51H0wSsFHUJ5mamKOVQx6M8kihCIxpBk6DzOhrf4RrpEgqh2bfpI7vbsVu2j5BT0KditccHBnepG33QudcrtBUHfv00Bbw1XXjL' source = stripe.checkout.Session.create( success_url='http://localhost:5000/success?source={CHECKOUT_SESSION_ID}', cancel_url='http://localhost:5000/error', payment_method_types=['card'], line_items=line_items ) order.transaction_id = source['id'] order.save() return Response(source) except Exception: transaction.rollback() return Response({ 'message': "Error occurred" })
def add_to_order(request, *args, **kwargs): if request.method == "GET": return render(request, "restaurant/order.php") if request.method == "POST": order = Order() cart = FoodCart.objects.filter(session_key=request.session.session_key, checked_out=False) total = 0 for item in cart: total += item.get_total restaurant = item.restaurant total += restaurant.delivery_charge order.total_price = total order.paid = False first_name = request.POST.get("first_name", "") middle_name = request.POST.get("middle_name", "") last_name = request.POST.get("last_name", "") email = request.POST.get("email", "") contact_number = request.POST.get("contact", "") address_line1 = request.POST.get("address1", "") address_line2 = request.POST.get("address2", "") city = request.POST.get("city", "") state = request.POST.get("state", "") zip = request.POST.get("zip", "") order.first_name = first_name order.last_name = last_name order.middle_name = middle_name order.email = email order.contact_number = contact_number order.address_line1 = address_line1 order.address_line2 = address_line2 order.city = city order.state = state order.zip_code = zip if 'comment' in request.POST: message = request.POST.get('comment', '') order.note = message order.payment = 1 last_order = Order.objects.last() if last_order: order_id = last_order.id order_id = order_id + 1 else: order_id = 1 id_string = randomString() + str(order_id) if Order.objects.filter(id_string=id_string).exists(): id_string = randomString() + str(order_id) order.id_string = id_string else: order.id_string = id_string order.status = 1 order._runsignal = False order._approved = False order._prepared = False order.save() for item in cart: order.cart.add(item) item.checked_out = True item.save() order._runsignal = False order._approved = False order._prepared = False order.save() return HttpResponseRedirect('/')
def post(self, request): data = request.data link = Link.objects.filter(code=data['code']).first() if not link: raise exceptions.APIException('Invalid code!') try: order = Order() # creo ordine # DATI PRESI DAL LINK (il carrello) order.code = link.code order.user_id = link.user.id order.ambassador_email = link.user.email # =========================== # DATI PRESI DALLA RICHIESTA POST (dal front-end) order.first_name = data['first_name'] order.last_name = data['last_name'] order.email = data['email'] order.address = data['address'] order.country = data['country'] order.city = data['city'] order.zip = data['zip'] # salvo su db ma nella trnsazione order.save() # preparo lista di items dell'ordine CON I DATI - lista di oggetti json line_items = [] for item in data['products']: product = Product.objects.filter(pk=item['product_id']).first() quantity = decimal.Decimal(item['quantity']) order_item = OrderItem() # creo item order_item.order = order order_item.product_title = product.title order_item.price = product.price order_item.quantity = quantity order_item.ambassador_revenue = decimal.Decimal(.1) * product.price * quantity order_item.admin_revenue = decimal.Decimal(.9) * product.price * quantity order_item.save() # SALVO l'item dell'ordine line_items.append({ 'name': product.title, 'description': product.description, 'images': [ product.image ], 'amount': int(100 * product.price), #in centesimi di dollaro (per stripe) 'currency': 'usd', 'quantity': quantity }) stripe.verify_ssl_certs = False stripe.api_key = "sk_test_B9N7bDJ1lEt8ey2GZ72qCCor" source = stripe.checkout.Session.create( success_url='http://localhost:5000/success?source={CHECKOUT_SESSION_ID}', #URL DEL FRONTEND cancel_url='http://localhost:5000/error', payment_method_types=['card'], line_items=line_items ) order.transaction_id = source['id'] #id della transazione di stripe order.save() return Response(source) except Exception: transaction.rollback() return Response({ 'message': "Error occurred" })