Django's implementation of BlockBee's payment gateway
Python >= 3.0
Django >= 2.0
Requests >= 2.20
pip install django-blockbee
Add to INSTALLED_APPS:
INSTALLED_APPS = (
'blockbee',
...
)
Run migrations:
python3 manage.py migrate blockbee
Collect static files:
python3 manage.py collectstatic
Add 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'
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.
In your order creation view, assuming user_order
is your order object:
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
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
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 (https://blockbee.io/cryptocurrencies/). 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
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
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.signals
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 %}
This library has a couple of helpers to help you get started. They are present in the file utils.py
.
blockbee.valid_providers()
is a method that returns a list of tuples of the active providers that you can just feed into the choices of a form.ChoiceField
blockbee.get_order_invoices(order_id)
returns a list of blockbee.models.Request
objects of your order (you can have multiple objects for the same order if the user mistakenly initiated the payment with another coin or if he mistakenly didn't send the full payment)
blockbee.callback_url(_r, params)
build your callback URL to provide to get_request
. Should be used inside a view since _r = request
This is the helper responsible for the connections ot the API itself. All these functions are in the blockbee.py
file.
get_info(coin)
returns the information of all cryptocurrencies or just if coin=''
or a specific cryptocurrency if coin='ltc'
for example. docs
get_supported_coins()
returns all the support cryptocurrencies. You can consult them in this list.
get_logs(coin, callback_url)
returns all the callback logs related to a request. callback_url
should be the callback provided to our API. docs
get_qrcode(coin, address, value, size)
returns a PNG of a QR Code with the address for payment. docs
get_conversion(origin, to, value)
returns the converted value in the parameter value_coin
to the currency you wish, FIAT or Cryptocurrency.
get_estimate(coin)
returns the estimation of the blockchain fees for the cryptocurrency specified in the parameter coin
. E.g: get_estimate('trc20_usdt')
docs
get_address(coin, params)
requests a payment address to BlockBee. Params include all the parameters that can be found here. The parameter apikey
is mandatory.
To generate a QR Code you must use get_qrcode
in your view and feed the parameters to your template. To generate a QR Code image you must place content of the API response after data:image/png;base64,{{qr_code}}
so the browser generates the QR Code.
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
You can also make the QR Code clickable.
<a href='{{ qrcode.payment_uri }}'>
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
</a>
You can also add a value to the QR Code setting the value
parameter to the value of your order (e.g 0.2 LTC
). This may not function correctly in some wallets. Use at your own risk.
We made the store
application to provide you with code examples on how to implement our service. It also has the code for our suggested UI (both CSS and HTML).
Need help?
Contact us @ https://blockbee.io/contacts/
- Initial Release
- Minor fixes
- UI Improvements
- Minor fixes
- Minor fixes
- Signals now use the value_coin
- Removed deprecated fields from the payment model
- Minor fixes
- Minor fixes
- Minor fixes
- Minor fixes