Beispiel #1
0
    def post(self, request, *args, **kwargs):
        client_data = cookie_parser(request.COOKIES.get('client_data'))
        order = Order()
        order.shipping_address = client_data.get('shipping_address')
        order.phone = client_data.get('phone')
        order.client_name = client_data.get('client_name')
        if request.user.is_authenticated:
            order.customer = request.user
        order.save()
        cart_items = self.get_cart_items(self.request)
        total_price = 0

        json_data = []
        for cart_item in cart_items:
            cart_item.order = order
            total_price += cart_item.total_price
            cart_item.save()

            title = "{} {}".format(cart_item.variation.product.name,
                                   cart_item.variation.name)
            if cart_item.variation.color:
                title = "{} {}".format(title, cart_item.variation.color.name)
            json_data.append({
                'title': title,
                'price': float(cart_item.variation.price),
                'count': cart_item.count,
                'total_price': float(cart_item.total_price)
            })
        order.products = json_data
        order.total_price = Decimal(total_price)
        order.order_unique_id = uuid.uuid4()
        order.save()

        order_detail_url = reverse_lazy(
            'orders:order_detail', args=[order.phone, order.order_unique_id])
        data = {
            'message': _('Order successful placed'),
            'status': 'success',
            'redirect_url': order_detail_url
        }
        return JsonResponse(data=data, safe=False)
Beispiel #2
0
    def post(self, request, *args, **kwargs):
        if request.POST.get('is_submitted'):
            cart_items = get_cart_items(self.request)
            total_weight = get_total_weight(cart_items=cart_items)
            client_data_cookie = self.request.COOKIES.get('client_data')
            client_data = cookie_parser(client_data_cookie)
            address = client_data.get('address')

            order = Order()
            order.client_name = address.get('client_name')
            order.payment = PaymentMethod.objects.get(
                method__exact=client_data.get('payment_method'))
            order.phone = address.get('phone')
            order.shipping_address = address.get('shipping_address')
            order.total_weight = total_weight
            order.delivery_price = get_delivery_price(cart_items)
            try:
                order.need_porter = int(client_data.get('need_porter'))
            except ValueError:
                pass

            if request.user.is_authenticated():
                order.customer = request.user

            if order.need_porter:
                order.porter_work_price = get_delivery_price(cart_items)
            order.shipping_time = datetime.fromtimestamp(
                client_data.get('time'), tz=pytz.timezone(settings.TIME_ZONE))
            order.save()

            total_price = 0

            json_data = []
            for cart_item in cart_items:
                cart_item.order = order
                total_price += cart_item.total_price
                cart_item.save()

                title = "{} {}".format(cart_item.variation.product.name,
                                       cart_item.variation.name)
                if cart_item.variation.color:
                    title = "{} {}".format(title,
                                           cart_item.variation.color.name)
                json_data.append({
                    'title': title,
                    'price': float(cart_item.variation.price),
                    'count': cart_item.count,
                    'total_price': float(cart_item.total_price)
                })
            order.products = json_data
            order.products_price = Decimal(total_price)
            order.order_unique_id = uuid.uuid4()

            order.total_price = order.products_price + get_delivery_price(
                cart_items)
            order.save()
            if order.payment.method == PaymentMethod.CASH_ON_DELIVERY:
                return redirect(
                    reverse_lazy('orders:order_detail',
                                 args=[order.phone, order.order_unique_id]))
            elif order.payment.method == PaymentMethod.PAYME:
                # https://checkout.paycom.uz/base64(m=587f72c72cac0d162c722ae2;ac.order_id=197;a=500)
                merchant_id = settings.PAYME_MERCHANT_ID
                ac_order_id = order.id
                ac_customer_id = order.phone
                amount = int(order.total_price.real) * 100
                redirect_url_after_operation = reverse_lazy(' basic:home')
                ct = 15
                cr = 860
                data = "m={};ac.order_id={};ac.customer_id={};a={};c={}".format(
                    merchant_id, ac_order_id, ac_customer_id, amount,
                    redirect_url_after_operation)
                base64_data = base64.b64encode(
                    data.encode('utf-8')).decode('utf-8')
                base64_data_url = "https://checkout.paycom.uz/{}".format(
                    base64_data)
                return redirect(to=base64_data_url)
            elif order.payment.method == PaymentMethod.UZCARD:
                return redirect(
                    reverse('orders:payment_uzcard',
                            args=[order.phone, order.order_unique_id]))
            else:
                return redirect(reverse(' basic:home'))
        return redirect(reverse(' basic:home'))