示例#1
0
def create_order_with_token(request):
    '''
    Create an order using an existing transaction ID.
    This is useful for capturing the payment outside of
    longclaw - e.g. using paypals' express checkout or
    similar
    '''
    # Get the request data
    try:
        address = request.data['address']
        postage = float(request.data['shipping_rate'])
        email = request.data['email']
        transaction_id = request.data['transaction_id']
    except KeyError:
        return Response(
            data={"message": "Missing parameters from request data"},
            status=status.HTTP_400_BAD_REQUEST)

    # Get the contents of the basket
    items, _ = get_basket_items(request)
    # Create the order
    ip_address = request.data.get('ip', '0.0.0.0')
    order = create_order(items, address, email, postage, ip_address)

    order.payment_date = timezone.now()
    order.transaction_id = transaction_id
    order.save()
    # Once the order has been successfully taken, we can empty the basket
    destroy_basket(request)

    return Response(data={"order_id": order.id},
                    status=status.HTTP_201_CREATED)
示例#2
0
    def create(self, request):
        """
        Add an item to the basket
        """
        variant_id = request.data.get("variant_id", None)

        if variant_id is not None:
            variant = ProductVariant.objects.get(id=variant_id)

            quantity = int(request.data.get("quantity", 1))
            items, bid = utils.get_basket_items(request)

            # Check if the variant is already in the basket
            in_basket = False
            for item in items:
                if item.variant.id == variant.id:
                    item.increase_quantity(quantity)
                    in_basket = True
                    break
            if not in_basket:
                item = BasketItem(variant=variant, quantity=quantity, basket_id=bid)
                item.save()

            serializer = BasketItemSerializer(self.get_queryset(request), many=True)
            response = Response(data=serializer.data,
                                status=status.HTTP_201_CREATED)

        else:
            response = Response(
                {"message": "Missing 'variant_id'"},
                status=status.HTTP_400_BAD_REQUEST)

        return response
示例#3
0
def basket_total_items(request):
    '''
    Get total number of items in the basket
    '''
    items, _ = utils.get_basket_items(request)
    n_total = 0
    for item in items:
        n_total += item.quantity

    return Response(data={"quantity": n_total}, status=status.HTTP_200_OK)
示例#4
0
def add_to_basket(request):
    '''
    Add an item to the basket
    '''
    variant = ProductVariant.objects.get(id=request.data["variant_id"])
    quantity = request.data.get("quantity", 1)

    items, bid = utils.get_basket_items(request)
    # Check if the variant is already in the basket
    in_basket = False
    for item in items:
        if item.variant.id == variant.id:
            item.increase_quantity(quantity)
            in_basket = True
            break
    if not in_basket:
        item = BasketItem(variant=variant, quantity=quantity, basket_id=bid)
        item.save()

    items, _ = utils.get_basket_items(request)
    serializer = BasketItemSerializer(items, many=True)
    return Response(data=serializer.data, status=status.HTTP_201_CREATED)
示例#5
0
文件: views.py 项目: xtelaur/longclaw
 def get_context_data(self, **kwargs):
     context = super(CheckoutView, self).get_context_data(**kwargs)
     items, _ = get_basket_items(self.request)
     total_price = sum(item.total() for item in items)
     site = getattr(self.request, 'site', None)
     context['checkout_form'] = self.checkout_form(self.request.POST
                                                   or None)
     context['shipping_form'] = self.shipping_address_form(
         self.request.POST or None, prefix='shipping', site=site)
     context['billing_form'] = self.billing_address_form(self.request.POST
                                                         or None,
                                                         prefix='billing',
                                                         site=site)
     context['basket'] = items
     context['total_price'] = total_price
     return context
示例#6
0
def remove_from_basket(request):
    '''
    Remove an item from the basket
    '''
    print(request.data["variant_id"])
    variant = ProductVariant.objects.get(id=request.data["variant_id"])
    quantity = request.data.get("quantity", 1)
    try:
        item = BasketItem.objects.get(basket_id=utils.basket_id(request),
                                      variant=variant)
    except BasketItem.DoesNotExist:
        return Response(data={"message": "Item does not exist in cart"},
                        status=status.HTTP_400_BAD_REQUEST)

    if quantity >= item.quantity:
        item.delete()
    else:
        item.decrease_quantity(quantity)

    items, _ = utils.get_basket_items(request)
    serializer = BasketItemSerializer(items, many=True)
    return Response(data=serializer.data, status=status.HTTP_201_CREATED)
示例#7
0
 def get_queryset(self, request=None):
     items, _ = utils.get_basket_items(request or self.request)
     return items
示例#8
0
 def get_context_data(self, **kwargs):
     items, _ = utils.get_basket_items(self.request)
     total_price = sum(item.total() for item in items)
     return {"basket": items, "total_price": total_price}
示例#9
0
 def get_context_data(self, **kwargs):
     items, _ = utils.get_basket_items(self.request)
     return {"basket": items}
示例#10
0
def create_order(email,
                 request,
                 addresses=None,
                 shipping_address=None,
                 billing_address=None,
                 shipping_option=None,
                 capture_payment=False):
    '''
    Create an order from a basket and customer infomation
    '''
    basket_items, _ = get_basket_items(request)
    if addresses:
        # Longclaw < 0.2 used 'shipping_name', longclaw > 0.2 uses a consistent
        # prefix (shipping_address_xxxx)
        try:
            shipping_name = addresses['shipping_name']
        except KeyError:
            shipping_name = addresses['shipping_address_name']

        shipping_country = addresses['shipping_address_country']
        if not shipping_country:
            shipping_country = None
        shipping_address, _ = Address.objects.get_or_create(name=shipping_name,
                                                            line_1=addresses[
                                                                'shipping_address_line1'],
                                                            city=addresses[
                                                                'shipping_address_city'],
                                                            postcode=addresses[
                                                                'shipping_address_zip'],
                                                            country=shipping_country)
        shipping_address.save()
        try:
            billing_name = addresses['billing_name']
        except KeyError:
            billing_name = addresses['billing_address_name']
        billing_country = addresses['shipping_address_country']
        if not billing_country:
            billing_country = None
        billing_address, _ = Address.objects.get_or_create(name=billing_name,
                                                           line_1=addresses[
                                                               'billing_address_line1'],
                                                           city=addresses[
                                                               'billing_address_city'],
                                                           postcode=addresses[
                                                               'billing_address_zip'],
                                                           country=billing_country)
        billing_address.save()
    else:
        shipping_country = shipping_address.country

    ip_address = get_real_ip(request)
    if shipping_country and shipping_option:
        site_settings = LongclawSettings.for_site(request.site)
        shipping_rate = get_shipping_cost(
            shipping_address.country.pk,
            shipping_option,
            site_settings)
    else:
        shipping_rate = 0
    order = Order(
        email=email,
        ip_address=ip_address,
        shipping_address=shipping_address,
        billing_address=billing_address,
        shipping_rate=shipping_rate
    )
    order.save()

    # Create the order items & compute total
    total = 0
    for item in basket_items:
        total += item.total()
        order_item = OrderItem(
            product=item.variant,
            quantity=item.quantity,
            order=order
        )
        order_item.save()

    if capture_payment:
        desc = 'Payment from {} for order id #{}'.format(email, order.id)
        transaction_id = GATEWAY.create_payment(request,
                                                float(total) + shipping_rate,
                                                description=desc)
        order.payment_date = timezone.now()
        order.transaction_id = transaction_id
        # Once the order has been successfully taken, we can empty the basket
        destroy_basket(request)
    return order
def basket(context):
    """
    Return the BasketItems in the current basket
    """
    items, _ = get_basket_items(context["request"])
    return items
示例#12
0
def capture_payment(request):
    '''
    Capture the payment for a basket and create an order

    request.data should contain:

    'address': Dict with the following fields:
        shipping_name
        shipping_address_line1
        shipping_address_city
        shipping_address_zip
        shipping_address_country
        billing_name
        billing_address_line1
        billing_address_city
        billing_address_zip
        billing_address_country

    'email': Email address of the customer
    'ip': IP address of the customer
    'shipping': The shipping rate (in the sites' currency)
    '''

    # Get the contents of the basket
    items, _ = get_basket_items(request)

    # Compute basket total
    total = 0
    for item in items:
        total += item.total()

    # Create the order
    address = request.data['address']
    postage = float(request.data['shipping_rate'])
    email = request.data['email']
    ip_address = request.data.get('ip', '0.0.0.0')
    order = create_order(items, address, email, postage, ip_address)

    # Capture the payment
    try:
        desc = 'Payment from {} for order id #{}'.format(
            request.data['email'], order.id)
        transaction_id = gateway.create_payment(request,
                                                float(total) + postage,
                                                description=desc)
        order.payment_date = timezone.now()
        order.transaction_id = transaction_id
        # Once the order has been successfully taken, we can empty the basket
        destroy_basket(request)
        response = Response(data={"order_id": order.id},
                            status=status.HTTP_201_CREATED)
    except PaymentError as err:
        order.status = Order.CANCELLED
        order.note = "Payment failed"
        response = Response(data={
            "message": err.message,
            "order_id": order.id
        },
                            status=status.HTTP_400_BAD_REQUEST)
    order.save()
    return response
示例#13
0
def get_basket(request):
    ''' Get all basket items
    '''
    items, _ = utils.get_basket_items(request)
    serializer = BasketItemSerializer(items, many=True)
    return Response(data=serializer.data, status=status.HTTP_200_OK)