kopia lustrzana https://github.com/longclawshop/longclaw
				
				
				
			Adds an api view for creating an order from a pre-captured payment
							rodzic
							
								
									b33e03dd36
								
							
						
					
					
						commit
						6927eb6c78
					
				| 
						 | 
				
			
			@ -10,7 +10,7 @@ from rest_framework.response import Response
 | 
			
		|||
from longclaw.longclawbasket.utils import get_basket_items, destroy_basket
 | 
			
		||||
from longclaw.longclaworders.models import Order, OrderItem
 | 
			
		||||
from longclaw.longclawshipping.models import Address
 | 
			
		||||
from longclaw.longclawcheckout.utils import PaymentError
 | 
			
		||||
from longclaw.longclawcheckout.utils import PaymentError, create_order
 | 
			
		||||
from longclaw import settings
 | 
			
		||||
 | 
			
		||||
gateway = import_string(settings.PAYMENT_GATEWAY)()
 | 
			
		||||
| 
						 | 
				
			
			@ -26,6 +26,27 @@ def create_token(request):
 | 
			
		|||
    token = gateway.get_token(request)
 | 
			
		||||
    return Response({'token': token}, status=status.HTTP_200_OK)
 | 
			
		||||
 | 
			
		||||
@transaction.atomic
 | 
			
		||||
@api_view(['POST'])
 | 
			
		||||
@permission_classes([permissions.AllowAny])
 | 
			
		||||
def create_order_with_token(request):
 | 
			
		||||
    # Get the contents of the basket
 | 
			
		||||
    items, _ = get_basket_items(request)
 | 
			
		||||
    # 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
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    order.payment_date = timezone.now()
 | 
			
		||||
    order.transaction_id = request.data['transaction_id']
 | 
			
		||||
 | 
			
		||||
@transaction.atomic
 | 
			
		||||
@api_view(['POST'])
 | 
			
		||||
| 
						 | 
				
			
			@ -61,48 +82,20 @@ def capture_payment(request):
 | 
			
		|||
    for item in items:
 | 
			
		||||
        total += item.total()
 | 
			
		||||
 | 
			
		||||
    # Create the address for the order
 | 
			
		||||
    address = request.data['address']
 | 
			
		||||
    shipping_address, _ = Address.objects.get_or_create(name=address['shipping_name'],
 | 
			
		||||
                                                        line_1=address[
 | 
			
		||||
                                                            'shipping_address_line1'],
 | 
			
		||||
                                                        city=address[
 | 
			
		||||
                                                            'shipping_address_city'],
 | 
			
		||||
                                                        postcode=address[
 | 
			
		||||
                                                            'shipping_address_zip'],
 | 
			
		||||
                                                        country=address['shipping_address_country'])
 | 
			
		||||
    shipping_address.save()
 | 
			
		||||
 | 
			
		||||
    address = request.data['address']
 | 
			
		||||
    billing_address, _ = Address.objects.get_or_create(name=address['billing_name'],
 | 
			
		||||
                                                       line_1=address[
 | 
			
		||||
                                                           'billing_address_line1'],
 | 
			
		||||
                                                       city=address[
 | 
			
		||||
                                                           'billing_address_city'],
 | 
			
		||||
                                                       postcode=address[
 | 
			
		||||
                                                           'billing_address_zip'],
 | 
			
		||||
                                                       country=address['billing_address_country'])
 | 
			
		||||
    billing_address.save()
 | 
			
		||||
    postage = float(request.data['shipping_rate'])
 | 
			
		||||
    # Create the order
 | 
			
		||||
    order = Order(
 | 
			
		||||
        email=request.data['email'],
 | 
			
		||||
        ip_address=request.data.get('ip', '0.0.0.0'),
 | 
			
		||||
        shipping_address=shipping_address,
 | 
			
		||||
        billing_address=billing_address,
 | 
			
		||||
        shipping_rate=postage
 | 
			
		||||
    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
 | 
			
		||||
    )
 | 
			
		||||
    order.save()
 | 
			
		||||
 | 
			
		||||
    # Create the order items
 | 
			
		||||
    for item in items:
 | 
			
		||||
        order_item = OrderItem(
 | 
			
		||||
            product=item.variant,
 | 
			
		||||
            quantity=item.quantity,
 | 
			
		||||
            order=order
 | 
			
		||||
        )
 | 
			
		||||
        order_item.save()
 | 
			
		||||
 | 
			
		||||
    # Capture the payment
 | 
			
		||||
    try:
 | 
			
		||||
        desc = 'Payment from {} for order id #{}'.format(request.data['email'], order.id)
 | 
			
		||||
        transaction_id = gateway.create_payment(request,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,4 +1,56 @@
 | 
			
		|||
from longclaw.longclaworders.models import Order, OrderItem
 | 
			
		||||
from longclaw.longclawshipping.models import Address
 | 
			
		||||
 | 
			
		||||
class PaymentError(Exception):    
 | 
			
		||||
    def __init__(self, message):
 | 
			
		||||
        self.message = str(message)
 | 
			
		||||
 | 
			
		||||
def create_order(basket_items,
 | 
			
		||||
                 addresses,
 | 
			
		||||
                 email,
 | 
			
		||||
                 shipping_rate,
 | 
			
		||||
                 ip_address='0.0.0.0'):
 | 
			
		||||
    '''
 | 
			
		||||
    Create an order from a basket and customer infomation
 | 
			
		||||
    '''
 | 
			
		||||
    if isinstance(addresses, dict):
 | 
			
		||||
        shipping_address, _ = Address.objects.get_or_create(name=addresses['shipping_name'],
 | 
			
		||||
                                                            line_1=addresses[
 | 
			
		||||
                                                                'shipping_address_line1'],
 | 
			
		||||
                                                            city=addresses[
 | 
			
		||||
                                                                'shipping_address_city'],
 | 
			
		||||
                                                            postcode=addresses[
 | 
			
		||||
                                                                'shipping_address_zip'],
 | 
			
		||||
                                                            country=addresses[
 | 
			
		||||
                                                                'shipping_address_country'])
 | 
			
		||||
        shipping_address.save()
 | 
			
		||||
        billing_address, _ = Address.objects.get_or_create(name=addresses['billing_name'],
 | 
			
		||||
                                                           line_1=addresses[
 | 
			
		||||
                                                               'billing_address_line1'],
 | 
			
		||||
                                                           city=addresses[
 | 
			
		||||
                                                               'billing_address_city'],
 | 
			
		||||
                                                           postcode=addresses[
 | 
			
		||||
                                                               'billing_address_zip'],
 | 
			
		||||
                                                           country=addresses[
 | 
			
		||||
                                                               'billing_address_country'])
 | 
			
		||||
        billing_address.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
    for item in basket_items:
 | 
			
		||||
        order_item = OrderItem(
 | 
			
		||||
            product=item.variant,
 | 
			
		||||
            quantity=item.quantity,
 | 
			
		||||
            order=order
 | 
			
		||||
        )
 | 
			
		||||
        order_item.save()
 | 
			
		||||
 | 
			
		||||
    return order
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ class Order(models.Model):
 | 
			
		|||
    ORDER_STATUSES = ((SUBMITTED, 'Submitted'),
 | 
			
		||||
                      (FULFILLED, 'Fulfilled'),
 | 
			
		||||
                      (CANCELLED, 'Cancelled'))
 | 
			
		||||
    payment_date = models.DateTimeField()
 | 
			
		||||
    payment_date = models.DateTimeField(blank=True, null=True)
 | 
			
		||||
    created_date = models.DateTimeField(auto_now_add=True)
 | 
			
		||||
    status = models.IntegerField(choices=ORDER_STATUSES, default=SUBMITTED)
 | 
			
		||||
    status_note = models.CharField(max_length=128, blank=True, null=True)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,7 +17,10 @@ def get_shipping_cost(country_code, option, settings):
 | 
			
		|||
    try:
 | 
			
		||||
        qrs = models.ShippingRate.objects.filter(countries__contains=country_code)
 | 
			
		||||
        try:
 | 
			
		||||
            shipping_rate = qrs.filter(name=option)[0]
 | 
			
		||||
            if qrs.count() > 1:
 | 
			
		||||
                shipping_rate = qrs.filter(name=option)[0]
 | 
			
		||||
            else:
 | 
			
		||||
                shipping_rate = qrs[0]
 | 
			
		||||
            return {
 | 
			
		||||
                "rate": shipping_rate.rate,
 | 
			
		||||
                "description": shipping_rate.description,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -29,7 +29,7 @@ class ShippingRate(models.Model):
 | 
			
		|||
    An individual shipping rate. This can be applied to
 | 
			
		||||
    multiple countries.
 | 
			
		||||
    '''
 | 
			
		||||
    name = models.CharField(max_length=32)
 | 
			
		||||
    name = models.CharField(max_length=32, unique=True)
 | 
			
		||||
    rate = models.DecimalField(max_digits=12, decimal_places=2)
 | 
			
		||||
    carrier = models.CharField(max_length=64)
 | 
			
		||||
    description = models.CharField(max_length=128)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue