Requirements
Python >= 3.0
Django >= 2.0
Requests >= 2.20Installation
pip install django-cryptapiAdd to INSTALLED_APPS:
INSTALLED_APPS = (
'cryptapi',
...
)Run migrations:
python3 manage.py migrate cryptapiCollect static files:
python3 manage.py collectstaticAdd CryptAPI's URLs to your project's urls.py file:
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:
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 AdminIf you want the
cryptapi.models.Requestobject:
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 AdminWhere:
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
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 orderpayment 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.pyfile:Pythonclass MyAppConfig(AppConfig): name = 'MyApp' def ready(self): super(MyAppConfig, self).ready() # noinspection PyUnresolvedReferences import MyApp.signals
Using 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 cryptapi_helper %}
{% block content %}
{% generate_payment_template %}
{% endblock %}