BlockBee's Django Library

View as Markdown

Requirements

  • Python >= 3.0
  • Django >= 2.0
  • Requests >= 2.20

Installation

shell
pip install django-blockbee

Add to INSTALLED_APPS:

Python
INSTALLED_APPS = (
    'blockbee',
    ...
)

Run migrations:

shell
python3 manage.py migrate blockbee

Collect static files:

shell
python3 manage.py collectstatic

Add BlockBee's URLs to your project's urls.py file:

Python
urlpatterns = [
    path('blockbee/', include('blockbee.urls')),
    ...
]

Add your BlockBee API Key (obtained from our Dashboard).

Python
BLOCKBEE_API_KEY = 'your_api_key'

Configuration

After the installation you need to set up Providers for each coin you wish to accept.

You need to go into your Django Admin and create a new BlockBee Provider for each coin with your cold wallet address where the funds will be forwarded to.

Usage

Creating an Invoice

In your order creation view, assuming user_order is your order object:

If you want the address generated:
Python
from blockbee import Invoice

def order_creation_view(request):
    ...
    invoice = Invoice(
        request=request,
        order_id=user_order.id,
        coin='btc',
        value=user_order.value
    )

    payment_address = invoice.address()

    if payment_address is not None:
        # Show the payment address to the user
        ...
    else:
        # Handle request error, check RequestLogs on Admin
If you want the blockbee.models.Request object:
Python
from blockbee import Invoice

def order_creation_view(request):
    ...
    invoice = Invoice(
        request=request,  # This if your view request. It's meant to create the callback URL
        order_id=user_order.id,
        coin='btc',
        value=user_order.value
    )

    payment_request = invoice.request()

    if payment_request is not None:
        # Show the payment address to the user
        ...
    else:
        # Handle request error, check RequestLogs on Admin

Where:

  • request is Django's view HttpRequest object
  • order_id is just your order id
  • coin is the ticker of the coin you wish to use, any of our supported coins. You need to have a Provider set up for that coin.
  • value is an integer of the value of your order
  • apikey is the API Key that you got from your BlockBee Dashboard

Getting notified when the user pays

Python
from django.dispatch import receiver
from blockbee.signals import payment_complete

@receiver(payment_complete)
def payment_received(order_id, payment, value):
    # Implement your logic to mark the order as paid and release the goods to the user
    ...

Where:

  • order_id is the id of the order that you provided earlier, used to fetch your order
  • payment is an blockbee.models.Payment object with the payment details, such as TXID, number of confirmations, etc.
  • value is the value the user paid

Using our provided interface

Create a view in views.py

Python
def payment(_r, request_id):
    try:
        req = Request.objects.get(id=request_id)
        coin = req.provider.coin

        qrcode = get_qrcode(coin, req.address_in)

        fiat = get_conversion(coin, 'eur', req.value_requested)

        print(fiat)

        ctx = {
            'req': req,
            'qrcode': qrcode,
            'fiat': fiat['value_coin']
        }

        return render(_r, 'payment.html', context=ctx)

    except Request.DoesNotExist:
        pass

    return redirect('store:request')

In your template HTML

Django Template
{% extends 'base.html' %}
{% load blockbee_helper %}
{% block content %}
    {% generate_payment_template %}
{% endblock %}