Skip to main content

Saferpay payment integration for django.

Project description

# django-saferpay

*Saferpay integration for django*

This packages uses the saferpay json api and the saferpay payment page interface.
This is NOT an offical package from SIX Payment Services.

See the official [saferpay json api](http://saferpay.github.io/jsonapi/#ChapterPaymentPage) for more information.
Take also a look at [test.saferpay.com](https://test.saferpay.com/).

## django setup

This django package in used in cunjunction with django-oscar. It can work with other django shop systems,
nothing is tied to django-oscar.

It's used with dango 1.11 but should/could work with django 2.0, 2.1.

first update `settings.py`:

```python
from django.urls import reverse_lazy
from django.utils.translation import ugettext_lazy as _

INSTALLED_APPS = [
...
'saferpay',
]

...
SAFERPAY_APIURL = 'https://test.saferpay.com/api'
SAFERPAY_USERNAME = 'API_XXX'
SAFERPAY_PASSWORD = 'XXX'
SAFERPAY_CUSTOMERID = 'xxxxxx' # a number
SAFERPAY_TERMINALID = 'xxxxxx' # a number
SAFERPAY_SUCCESS_CAPTURE_URL = reverse_lazy('yourshop:payment-capture') # your callback after getting the money
SAFERPAY_SUCCESS_URL = reverse_lazy('yourshop:thank-you') # your callback after a successfull order
SAFERPAY_FAIL_URL = reverse_lazy('yourshop:payment-failed') # your callback after a failed payment
SAFERPAY_ORDER_TEXT_NR = _('Payment for the company XXX, Order nr. %s.')
SAFERPAY_FORCE_LIABILITYSHIFT_ACTIVE = False # default: False
SAFERPAY_DO_NOTIFY = True # default: False
...
```

You need to provide three URLs from your shop app for providing callbacks for the saferpay gateway.

- `SAFERPAY_SUCCESS_CAPTURE_URL` - saferpay callback for a successfull capture of the amount
after PAYMENTPAGE_INIT and PAYMENTPAGE_ASSERT
- `SAFERPAY_SUCCESS_URL` - everything went fine, saferpay callback to shop success url
- `SAFERPAY_FAIL_URL` - something went wrong, saferpay callback to shop failure url

in your project `urls.py`:

```python
...
url(r'', include('saferpay.urls')),
...
```

create the db models

```python
./manage.py migrate
```

### django oscar setup

If you use django oscar got to [django-oscar-saferpay](https://github.com/taywa/django-oscar-saferpay)
to procced with the setup process and see an sample usage implementation.

### sample usage

You can use this code as example. You need to integrate it into
your own views in the shop system.

1. Intizialize the service

```python
from saferpay.gateway import SaferpayService
from saferpay import execptions as sp_execptions


def billing_address_for_saferpay(billing_address):
"""Transform you shop billing address to
a format that saferpay uses.
Your billing_address object can be totaly differnt,
adjust it to your needs.
"""
data = {}
data['FirstName'] = billing_address.first_name
data['LastName'] = billing_address.last_name
data['Street'] = billing_address.line1
if billing_address.line2:
data['Street2'] = billing_address.line2
data['Zip'] = billing_address.postcode
data['City'] = billing_address.city
return data


def payment_init(request, order):
"""
Intizialize a new SaferpayService instance, with your
order data. Adjust the the order calls for your shop.


The init payload is created and after success you get
redirected to the saferpay PayementPage.
"""
saferpay_service = SaferpayService(
order_id=order.number, amount=order.total.incl_tax,
currency=order.total.currency, language_code=language_code
)

payload = saferpay_service.payload_init(billing_address=billing_address_data)

try:
token = saferpay_service.paymentpage_init(payload)
request.session['saferpay_token'] = token
raise RedirectRequired(saferpay_service.paymentpage_redirect().url)
except sp_execptions.GatewayError as e:
# do your rollback in case of payment failure
return redirect(reverse_lazy('yourshop:payment-failed'))
```

2. Assert and Capture the money and place the order

```python
from saferpay.gateway import SaferpayService
from saferpay import execptions as sp_execptions


def payment_capture(request):
token = request.session['saferpay_token']
saferpay_service = SaferpayService.init_from_transaction(token=token)
try:
status = saferpay_service.paymentpage_assert()
except (
sp_execptions.GatewayError, sp_execptions.TransactionDeclined,
sp_execptions.UnableToTakePayment, sp_execptions.PaymentError
) as e:
# do your rollback in case of payment failure
return redirect(reverse_lazy('yourshop:payment-failed'))
if status == 'CAPTURED':
# register the payment in you shop
logger.info("Order #%s: payment successful, placing order", saferpay_service.order_id)

try:
# place the order
except UnableToPlaceOrder as e:
# go back to preview
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-saferpay-0.1.0.tar.gz (9.7 kB view details)

Uploaded Source

File details

Details for the file django-saferpay-0.1.0.tar.gz.

File metadata

  • Download URL: django-saferpay-0.1.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.18.4 setuptools/38.5.2 requests-toolbelt/0.9.1 tqdm/4.31.0 CPython/3.6.4

File hashes

Hashes for django-saferpay-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f394b7027756e02502ee90df15de5792a6c160ba3b5ec9f040c4a70b24fe9f22
MD5 e14ec94f48d9c5eb128ff1dd552c6d6b
BLAKE2b-256 03317bed4c9a65985740c7660bbf149b745d1dfb6e3f7caa756572e5766b8e93

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page