Skip to main content

Django implementation of CryptAPI's payment gateway

Project description

CryptAPI

CryptAPI's Django Library

Django's implementation of CryptAPI's payment gateway

Requirements:

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

Install

pip install django-cryptapi

on pypi or on GitHub

Add to INSTALLED_APPS:

INSTALLED_APPS = (
    'cryptapi',
    ...
)

Run migrations:

python3 manage.py migrate cryptapi

Collect static files:

python3 manage.py collectstatic

Add 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 Admin
  • If you want the cryptapi.models.Request object:
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_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/pricing/). 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 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:

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

       # noinspection PyUnresolvedReferences
       import MyApp.signals

django docs

 

Helpers

This library has a couple of helpers to help you get started

cryptapi.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

cryptapi.get_order_invoices(order_id) returns a list of cryptapi.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)

Template Tags

There's also some template tags which you can import to help you with conversions and the protocols. You just need to load cryptapi_helper on your template and use the following tags / filters:

  • QR code (with cryptapi.models.Request object)

If you want the library to generate and display a clickable QR code for you, just use our generate_qrcode_for_request, like this:

{% generate_qrcode_for_request payment_request %}

You just need to feed it the payment_request object created with invoice.request()

The QR code that can also be clicked on mobile devices to launch the user's wallet.

  • QR code (with address, coin and value)

If you want the library to generate and display a clickable QR code for you, just use our generate_qrcode, like this:

{% generate_qrcode btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}

It takes 3 arguments: the coin, the payment address and the value in the main denomination of the coin, and it will output a neat QR code for your page.

The QR code that can also be clicked on mobile devices to launch the user's wallet.

Example:
{% load cryptapi_helper %}
<body>
    <div class="row">
        <div class="col-sm-12">
            {% generate_qrcode btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
        </div>
    </div>
</body>
  • Payment URI

If you just want to build a full payment URI to plug into your own QR code, you can use our build_payment_uri tag, like so:

{% build_payment_uri btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}

It will output: bitcoin:1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3?amount=0.001

Same arguments as for the QR code

  • Helpers

{% convert_value coin value %} where the coin is the coin ticker and the value is the value in satoshi, litoshi, wei or IOTA, will convert to the main coin unit.

{{ coin|coin_name }} will output the properly formatted cryptocurrency name.

Help

Need help?
Contact us @ https://cryptapi.io/contact/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-cryptapi-0.2.6.tar.gz (383.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_cryptapi-0.2.6-py3-none-any.whl (406.2 kB view details)

Uploaded Python 3

File details

Details for the file django-cryptapi-0.2.6.tar.gz.

File metadata

  • Download URL: django-cryptapi-0.2.6.tar.gz
  • Upload date:
  • Size: 383.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for django-cryptapi-0.2.6.tar.gz
Algorithm Hash digest
SHA256 6b08e561f96eb1083d0f81cb22f4a66b2c2be494883d9e0f3ef810efff3e39e2
MD5 dcd21c58d8a95ff5a7c90acfb4c0fcb5
BLAKE2b-256 bef951ab386e6262574e6bda5c78cccf76630a1870a7d938b24adab6d9188fb8

See more details on using hashes here.

File details

Details for the file django_cryptapi-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: django_cryptapi-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 406.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

File hashes

Hashes for django_cryptapi-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f6f20616ad7d5ec7a904937b03ddd66504c92fbfae98dfb3136d0f510fdc3f7e
MD5 8d9a784305ddaa12ff1e0ccbb2726538
BLAKE2b-256 c48cbe64f74fd15e0e1dda44d911b1079317293310b8ef37da82d593194e17b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page