Requirements
Python >= 3.0Django >= 2.0Requests >= 2.20
Installation
pip install django-blockbeeFull Source Code Available: You can find the complete source code, examples, and contribute to this library on our official GitHub repository: github.com/blockbee-io/django-blockbee
Add to INSTALLED_APPS:
INSTALLED_APPS = (
'blockbee',
...
)Run migrations:
python3 manage.py migrate blockbeeCollect static files:
python3 manage.py collectstaticAdd BlockBee's URLs to your project's urls.py file:
urlpatterns = [
path('blockbee/', include('blockbee.urls')),
...
]Add your BlockBee API Key (obtained from our Dashboard).
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:
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 AdminIf you want the blockbee.models.Request object:
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 AdminWhere:
requestis Django's view HttpRequest objectorder_idis just your order idcoinis the ticker of the coin you wish to use, any of our supported coins. You need to have aProviderset up for that coin.valueis an integer of the value of your orderapikeyis the API Key that you got from your BlockBee Dashboard
Getting notified when the user pays
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_idis the id of the order that you provided earlier, used to fetch your orderpaymentis anblockbee.models.Paymentobject with the payment details, such as TXID, number of confirmations, etc.valueis the value the user paid
Important: Don't forget to import your signals file.
On your App's apps.py file:
class MyAppConfig(AppConfig):
name = 'MyApp'
def ready(self):
super(MyAppConfig, self).ready()
# noinspection PyUnresolvedReferences
import MyApp.signalsUsing our provided interface
Create a view in views.py
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
{% extends 'base.html' %}
{% load blockbee_helper %}
{% block content %}
{% generate_payment_template %}
{% endblock %}