Demo Application
Learn how to build a Django application using the AvaTax API.
We'll cover the following...
This lesson walks us through a Django e-commerce application integrated with AvaTax API. We have used the following endpoints in our application:
{{BASE_URL}}/companies/{companyId}/items/
{{BASE_URL}}/companies/{companyId}/transactions/
{{BASE_URL}}/transactions/
Running the application
You can run the demo application by pressing the "Run" button in the widget below. When the server has started, click the URL given against "Your app can be found at" to view the application better.
Note: The following widget only shows the necessary files required to show the integration of the AvaTax API.
from decimal import Decimal from django.shortcuts import redirect, render, get_object_or_404 from estore.models import * from .api_call_methods import * def mod_home(request): categories = Category.objects.all() items = Item.objects.all() context = { 'categories': categories, 'items': items, 'account_type': "moderator" } return render(request, 'estore/index.html', context) def mod_items(request, url_slug): category = get_object_or_404(Category, url_slug=url_slug) items = Item.objects.filter(category=category) categories = Category.objects.all() context = { 'category': category, 'cat_url_slug': url_slug, 'items': items, 'categories': categories, 'account_type': "moderator" } return render(request, 'estore/mod_editor.html', context) def order_total(transaction_code): total_amount = 0.0 total_tax = 0.0 try: transaction_obj = get_transaction(transaction_code) if transaction_obj: total_amount = transaction_obj["totalAmount"] total_tax = transaction_obj["totalTax"] except: pass return total_amount, total_tax def mod_orders(request): orders = Order.objects.all() context_orders = [] for order in orders: transaction_code = order.transaction_code order_items = Order_Item.objects.filter(order_id=str(order.id)) order_amount, order_tax = order_total(transaction_code) context_orders.append({ 'order': order, 'order_items': order_items, 'order_total': order_amount, 'order_tax': order_tax, 'transaction': transaction_code }) context = { 'context_orders': context_orders } return render(request, 'estore/mod_orders.html', context) def edit_item(request, url_slug, item_id, taxcode_status=0): category = get_object_or_404(Category, url_slug=url_slug) item = Item.objects.get(pk=item_id) context = { 'cat_url_slug': url_slug, 'type': "edit", 'category': category, 'new_item': item, 'taxcode_status': taxcode_status } return render(request, 'estore/mod_item_editor.html', context) def create_new_item(request, url_slug, taxcode_status=0): context = { 'cat_url_slug': url_slug, 'type': "create", 'taxcode_status': taxcode_status } return render(request, 'estore/mod_item_editor.html', context) def item_creation(request): item_name, item_price, item_tax_code, item_img_url, cat_slug, fetched_item_id, type = "", Decimal( 0), "P0000000", "", "", None, "" # Fetching any URL parameters if provided try: item_name = request.GET['item_name'] except: pass try: item_price = request.GET['item_price'] except: pass try: item_tax_code = request.GET['item_tax_code'] except: pass try: item_img_url = request.GET['item_img_url'] except: pass try: cat_slug = request.GET['cat_slug'] except: pass try: fetched_item_id = request.GET['item'] except: pass try: type = request.GET['type'] except: pass # Creating/Updating item try: category = Category.objects.get(url_slug=cat_slug) if type == 'create': item_id, item_code, taxcode_status = create_item( item_tax_code, item_name, cat_slug) if taxcode_status: request return redirect('estore:create-item-redirect', url_slug=cat_slug, taxcode_status=1) else: item = Item(title=item_name, price=item_price, tax_code=item_tax_code, item_code=item_code, img_url=item_img_url, item_id=item_id, category=category) item.save() if type == 'edit': item = Item.objects.filter(pk=fetched_item_id) item_id, item_code, taxcode_status = update_item( item_tax_code, item_name, cat_slug, Item.objects.get(pk=fetched_item_id).item_id) if taxcode_status: return redirect('estore:edit-item-redirect', url_slug=cat_slug, item_id=fetched_item_id, taxcode_status=1) else: item.update(title=item_name, price=item_price, tax_code=item_tax_code, item_code=item_code, img_url=item_img_url, item_id=item_id, category=category) except Exception as e: print("Error:", e) if cat_slug == "": return redirect('estore:moderator') else: return redirect('estore:mod-items', url_slug=cat_slug) def item_deletion(request, url_slug, item_id): try: del_item_id = Item.objects.get(pk=item_id).item_id delete_item(del_item_id) Item.objects.get(pk=item_id).delete() return redirect('estore:mod-items', url_slug=url_slug) except: return redirect('estore:mod-items', url_slug=url_slug) def complete_order(request, order_id): Order_Item.objects.filter(order_id=str(order_id)).delete() Order.objects.get(pk=order_id).delete() return redirect('estore:mod-orders') def cancel_order(request, order_id): try: order = Order.objects.get(pk=order_id) refund_transaction(order.transaction_code) order.delete() except: pass return redirect('estore:mod-orders')
To open the moderator interface, navigate to the following URL after running the demo application:
{{EDUCATIVE_LIVE_VM_URL}}/moderator/
Code explanation
Let's dive into the code and see how we have integrated the AvaTax API into our application.
We can see the contents of the view_user.py
and ...