Integrate Django-Oscar-Accounts with Django-Oscar
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.
- 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.
* 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': 'Deferred income report',
'label': 'Profit/loss report',
Next, add the url-pattern to your urls.py
from oscar_accounts.dashboard.app import application as accounts_appurlpatterns = [
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 Accountaccounts = 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, exceptionssource_account = user_selected_account
destination_account = Account.objects.get(name="Sales")try:
transfer = facade.transfer(source_account,
description="Redeemed to pay for order %s" % order_number)
except exceptions.AccountException, e:
raise PaymentError("Transfer Failed")
# Add Payment source and Payment event
source_type, created = SourceType.objects.get_or_create(name="Accounts")
source = Source(source_type=source_type,
* 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
except Exception, e:
Note: Transfer operation is wrapped in its own database transaction to ensure that only complete transfers are written out.