def add_to_cart(req, product_id, quantity=1): try: props = json.loads(req.GET['props']) except: props = [] if not props: props = [] product = Product.objects.get(id=product_id) order_item = OrderItem() order_item.product = product order_item.save() for prop in props: pps = list( ProductProp.objects.filter(title=prop['name'], product_id=product.id, value=prop['value'])) if len(pps): pp = pps[0] order_item.props.add(pp) cart = Cart(req) cart.add(order_item, product.price, quantity) return JsonResponse({'success': True, 'count': cart.count()})
def setUp(self): """ """ self.tax = TaxClass(name="20%", rate=Decimal('20.0')) self.tax.save() self.p1 = Product.objects.create(name="Product 1", slug="product-1") self.p2 = Product.objects.create(name="Product 2", slug="product-2") self.p1.save() self.p2.save() price1 = ProductPrice(product=self.p1, _unit_price=Decimal('10.0'), currency='CZK', tax_class=self.tax, tax_included=self.tax_included) price2 = ProductPrice(product=self.p2, _unit_price=Decimal('100.0'), currency='CZK', tax_class=self.tax, tax_included=self.tax_included) price1.save() price2.save() self.cart = Order() self.cart.save() item1 = OrderItem(order=self.cart, product=self.p1, quantity=1) item2 = OrderItem(order=self.cart, product=self.p2, quantity=1) item1.save() item2.save() self.cart.recalculate_totals() self.cart.save()
def create_order_items(cart, order): """ Create & save OrderItems from items in session cart """ for item in cart['items']: variation = ProdVariation.objects.get(sku=item['sku']) order_item = OrderItem( order=order, price=variation.price, product=variation.product, quantity=item['quantity'], size=variation.size, sku=variation.sku, width=variation.width ) order_item.save()
def create_order(request): data = copy.deepcopy(request.data) request_serializer = CreateNewOrderSerializer(data=data) request_serializer.is_valid(raise_exception=True) user = request.user person = Person.objects.get(user=user) try: # Create the initial order items = [] for order_item in data['order_items']: items.append({ "type": 'sku', "parent": order_item['parent'], "quantity": order_item['quantity'] }) stripe.api_key = settings.STRIPE_PRIVATE_KEY stripe_order = stripe.Order.create( currency='usd', items=items, shipping={ "name": '%s %s' % (user.first_name, user.last_name), "address": { "line1": person.address_line_1, "city": person.city, "state": person.state, "country": 'US', "postal_code": person.zipcode }, }, email=user.email) # Store the order data in our database order = Order(order_id=stripe_order.id, user=user, amount=stripe_order.amount, email=stripe_order.email, status=stripe_order.status, created=datetime.fromtimestamp(stripe_order.created), updated=datetime.fromtimestamp(stripe_order.updated)) order.currency = stripe_order.currency order.save() order_status = OrderStatusTransition(order=order, status=stripe_order.status, created=datetime.fromtimestamp( stripe_order.updated)) order_status.save() for item in stripe_order['items']: order_item = OrderItem(order=order, amount=item['amount'], description=item['description'], parent=item['parent'], quantity=item['quantity'], item_type=item['type']) order_item.currency = item.currency, order_item.save() updated_order = Order.objects.get(order_id=order.order_id) response_serializer = OrderSerializer(updated_order) return Response(response_serializer.data, status=status.HTTP_201_CREATED) except Exception as e: return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def checkout(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, '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=True, 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/')