CryptAPI's Django Library

View as Markdown

Requirements

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

Installation

shell
pip install django-cryptapi

on pypi or on GitHub

Add to INSTALLED_APPS:

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

Run migrations:

shell
python3 manage.py migrate cryptapi

Collect static files:

shell
python3 manage.py collectstatic

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

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

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 CryptAPI 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 cryptapi 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 cryptapi.models.Request object:
Python
from cryptapi 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 (https://cryptapi.io/cryptocurrencies/). You need to have a Provider set up for that coin.

value is an integer of the value of your order, either in satoshi, litoshi, wei, piconero or IOTA

Getting notified when the user pays

Python
from django.dispatch import receiver
from cryptapi.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 cryptapi.models.Payment object with the payment details, such as TXID, number of confirmations, etc.
value is the value the user paid, either in satoshi, litoshi, wei or IOTA

Important:

Don't forget to import your signals file.

On your App's apps.py file:

Python
class MyAppConfig(AppConfig):
   name = 'MyApp'
   
   def ready(self):
       super(MyAppConfig, self).ready()

       # noinspection PyUnresolvedReferences
       import MyApp.signals

django docs

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 cryptapi_helper %}
{% block content %}
    {% generate_payment_template %}
{% endblock %}