Datacash payment module for django-oscar
It also supports batch fraud screening using The3rdMan.
When following the below instructions, it may be helpful to browse the sandbox folder above as this is an example Oscar install which has been integrated with Datacash.
pip install django-oscar-datacash
or from Github:
pip install git+git://github.com/tangentlabs/django-oscar-datacash.git#egg=django-oscar-datacash
Add 'datacash' to INSTALLED_APPS and run:
./manage.py migrate datacash
to create the appropriate database tables.
Edit your settings.py to set the following settings:
DATACASH_HOST = 'testserver.datacash.com' DATACASH_CLIENT = '...' DATACASH_PASSWORD = '...' DATACASH_CURRENCY = 'GBP'
There are other settings available (see below). Obviously, you’ll need to specify different settings in your test environment as opposed to your production environment.
You’ll need to use a subclass of oscar.apps.checkout.views.PaymentDetailsView within your own checkout views. See Oscar’s documentation on how to create a local version of the checkout app.
Override the handle_payment method (which does nothing by default) and add your integration code. See the sandbox site for an example of integrating Datacash payments into an Oscar site.
The gateway modules uses the named logger datacash.
The3rdMan callbacks use the named logger datacash.the3rdman. It is recommended that you use django.utils.log.AdminMailHandler with this logger to ensure error emails are sent out for 500 responses.
Many Datacash features require your merchant account to be configured correctly. For instance, the default Datacash set-up won’t include:
When investigating problems, make sure your Datacash account is set-up correctly.
Using realtime fraud services requires submitting a dict of relevant data as part of the initial transaction. A helper method is provided that will extract all it needs from Oscar’s models:
from datacash.the3rdman import build_data_dict fraud_data = build_data_dict( request=request, order_number='1234', basket=request.basket, email=email shipping_address=shipping_address, billing_addres=billing_address)
then pass this data as a named argument when creating the transaction:
ref = Facade().pre_authorise(..., the3rdman_data=fraud_data)
To receive the callback, include the following in your urls.py:
urlpatterns = patterns('', ... (r'^datacash/', include('datacash.urls')), ... )
When a fraud response is received, a custom signal is raised which your client code should listen for. Example:
from django.dispatch import receiver from datacash.the3rdman import signals @receiver(signals.response_received) def handle_fraud_response(sender, response, **kwargs): # Do something with response
There are two key components:
The class datacash.gateway.Gateway provides fine-grained access to the various DataCash APIs, which involve constructing XML requests and decoding XML responses. All calls return a datacash.gateway.Response instance which provides dictionary-like access to the attributes of the response.
from decimal import Decimal as D from datacash.gateway import Gateway gateway = Gateway() # Single stage processing response = gateway.auth(amount=D('100.00'), currency='GBP', merchant_reference='AA_1234', card_number='4500203021916406', expiry_date='10/14', ccv='345') response = gateway.refund(amount=D('100.00'), currency='GBP', merchant_reference='AA_1234', card_number='4500203021916406', expiry_date='10/14', ccv='345') # Two-stage processing (using pre-registered card) response = gateway.pre(amount=D('50.00'), currency='GBP', previous_txn_reference='3000000088888888') response = gateway.fulfill(amount=D('50.00'), currency='GBP', txn_reference=response['datacash_reference'])
The gateway object know nothing of Oscar’s classes and can be used in a stand-alone manner.
The class datacash.facade.Facade wraps the above gateway object and provides a less granular API, as well as saving instances of datacash.models.OrderTransaction to provide an audit trail for Datacash activity.
To work on django-oscar-datacash, clone the repo, set up a virtualenv and install in develop mode:
The test suite can then be run using:
There is a sandbox Oscar site that can be used for development. Create it with:
and browse it with:
python sandbox/manage.py runserver
Magic card numbers are available on the Datacash site: https://testserver.datacash.com/software/download.cgi?show=magicnumbers
Here’s an example: