Integrate Django-Oscar-Accounts with Django-Oscar

Reading Time : ~ .

Django Oscar Accounts is a package that provides the ability to manage accounts with fully managed account functionality to use with Django Oscar.

* Django Oscar Accounts provides managed accounts using double-entry bookkeeping.

* This package uses double-entry bookkeeping where every transaction is recorded twice (once for the source and once for the destination). This ensures the books always balance and there is full audit trail of all transactional activity.

Features -

  • A user can have multiple accounts
  • An account can be created with - no users assigned, a single primary user assigned or set of users can be assigned
  • An account can have a start and end date to allow its usage in a limited time, can be restricted so that it can only be used to pay for a range of products.
  • An account has a credit limit which defaults to zero.

Installation -

* Install using pip:

    pip install django-oscar-accounts

* Next, add "oscar_accounts" to INSTALLED_APPS in settings.

* After this apply migrations with "manage.py migrate oscar_accounts" command. This will create the appropriate database tables. And create initial core accounts and account-types use "manage.py oscar_accounts_init" command. The names of these accounts can be controlled using settings.

* To access accounts via Django Oscar dashboard, you need to update OSCAR_DASHBOARD_NAVIGATION list

from oscar.defaults import *

OSCAR_DASHBOARD_NAVIGATION.append(
    {
        'label': 'Accounts',
        'icon': 'icon-globe',
        'children': [
            {
                'label': 'Accounts',
                'url_name': 'accounts-list',
            },
            {
                'label': 'Transfers',
                'url_name': 'transfers-list',
            },
            {
                'label': 'Deferred income report',
                'url_name': 'report-deferred-income',
            },
            {
                'label': 'Profit/loss report',
                'url_name': 'report-profit-loss',
            },
        ]
    })

Next, add the url-pattern to your urls.py

from oscar_accounts.dashboard.app import application as accounts_app

urlpatterns = [
    ...
    url(r'^dashboard/accounts/', include(accounts_app.urls)),
]

NOTE: To override templates, add an additional path(account TEMPLATE_DIR) to your TEMPLATE_DIRS

Integrating in Django Oscar Checkout Process

For checkout integration, you have to override PaymentDetailsView in checkout app

Step 1: Display the list of accounts for the user in final step(Payment details Page).

   For this, override the get_context_data method and filter active user accounts

from oscar_accounts.models import Account

accounts = Account.active.filter(user=user, balance__gt=0)

Step 2: Override the "post" method to validate the account and render them again in the preview view (but hidden).

Step 3: Override the handle_payment method of your checkout’s PaymentDetailsView to transfer the amount(order total amount) from the user account to your account(Ex: Sales Account)

* This package provides an API to make transfers in facade module.

* If the transfer is invalid, an exception will be raised. All exceptions are subclasses of oscar_accounts.exceptions.AccountException. So we just need to handle this exception.

from oscar_accounts.models import Account
from oscar_accounts import facade, exceptions

source_account = user_selected_account
destination_account = Account.objects.get(name="Sales")

try:
    transfer = facade.transfer(source_account,
                               destination_account,
                               order_total,
                               user=user
                               merchant_reference=order_number,
                               description="Redeemed to pay for order %s" % order_number)
except exceptions.AccountException, e:
    raise PaymentError("Transfer Failed")
else:
    # Add Payment source and Payment event
    source_type, created = SourceType.objects.get_or_create(name="Accounts")
    source = Source(source_type=source_type,
                    amount_allocated=order_total,
                    amount_debited=transfer.amount,
                    reference=transfer.reference)
    self.add_payment_source(source)

    self.add_payment_event("Transferred",
                           transfer.amount,
                           transfer.reference)

* If the transfer is successful, but something went wrong in placing the order(any exception occurs after post-payment), then you have to roll-back the previous transfer.

from oscar_accounts import facade
try:
    self.place_order()
except Exception, e:
    facade.reverse(transfer, user=user,
                   merchant_reference=order_number,
                   description="Payment Cancellation")

Note: Transfer operation is wrapped in its own database transaction to ensure that only complete transfers are written out.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Creating Django App Siva Chittamuru

Django is a high-level, free and open-source Python Web framework that encourages rapid development. Django follows the model–view–controller (MVC) architectural pattern. Django's primary goal is ...

Continue Reading...
django Payu Payment gateway Integration Shirisha Gaddi

In this blog, we will see how to integrate Django and PayU Payment Gateway. To integrate with PayU, we have package called "django-payu" - a ...

Continue Reading...
Django webpacker - A compression tool to bundles css, js files Nikhila Mergu

django-webpacker is a django compressor tool which bundles css, js files to a single css, js file with webpack and updates your html files with ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties