Skip to content

Commit

Permalink
* Fixed bug with MySQL database varchar length limitations
Browse files Browse the repository at this point in the history
* Added build payment URI tag, to help you generate payment URIs to feed into any QR Code generator
  • Loading branch information
CryptAPI committed Oct 24, 2019
1 parent 0e1ebda commit 579d190
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ You just need to load ``cryptapi_helper`` on your template and use the following

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


If you want to build a full payment URI for your clients, you can use our `build_payment_uri` tag, like so:

```djangotemplate
{% build_payment_uri btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
# will output: bitcoin:1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3?amount=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 payment URI ready for you to feed into a QR Code generator

## Help

Need help?
Expand Down
2 changes: 1 addition & 1 deletion cryptapi/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CallbackForm(BaseCallbackForm):
class AddressCreatedForm(forms.Form):
address_in = forms.CharField(max_length=128)
address_out = forms.CharField(max_length=128)
callback_url = forms.CharField(max_length=16384)
callback_url = forms.CharField(max_length=16383)
status = forms.CharField(max_length=16)

def __init__(self, initials, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion cryptapi/meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from distutils.version import StrictVersion


VERSION = StrictVersion('0.1.4')
VERSION = StrictVersion('0.1.5')
4 changes: 2 additions & 2 deletions cryptapi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Migration(migrations.Migration):
name='PaymentLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('raw_data', models.CharField(max_length=16384)),
('raw_data', models.CharField(max_length=16383)),
('timestamp', models.DateTimeField(auto_now_add=True)),
('payment', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='cryptapi.Payment')),
],
Expand Down Expand Up @@ -61,7 +61,7 @@ class Migration(migrations.Migration):
name='RequestLog',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('raw_data', models.CharField(max_length=16384)),
('raw_data', models.CharField(max_length=16383)),
('timestamp', models.DateTimeField(auto_now_add=True)),
('request', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='cryptapi.Request')),
],
Expand Down
4 changes: 2 additions & 2 deletions cryptapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __str__(self):

class RequestLog(models.Model):
request = models.ForeignKey(Request, on_delete=models.SET_NULL, null=True)
raw_data = models.CharField(max_length=16384)
raw_data = models.CharField(max_length=16383)
timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):
Expand All @@ -56,7 +56,7 @@ def __str__(self):

class PaymentLog(models.Model):
payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, null=True)
raw_data = models.CharField(max_length=16384)
raw_data = models.CharField(max_length=16383)
timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):
Expand Down
43 changes: 38 additions & 5 deletions cryptapi/templatetags/cryptapi_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import template
from cryptapi.helpers import get_coin_multiplier
from cryptapi.utils import build_query_string

register = template.Library()

Expand All @@ -19,16 +20,48 @@ def convert_value(coin, value):
@register.filter
def coin_protocol(coin):
coins = {
'btc': 'bitcoin',
'bch': 'bitcoincash',
'ltc': 'litecoin',
'eth': 'ethereum',
'iota': 'iota',
'btc': 'bitcoin:',
'bch': 'bitcoincash:',
'ltc': 'litecoin:',
'eth': 'ethereum:',
'xmr': 'monero:',
'iota': 'iota:',
}

return coins.get(coin, '')


@register.simple_tag
def build_payment_uri(coin, address, value):
protocol = coin_protocol(coin)
keys = {
'btc': 'amount',
'bch': 'amount',
'ltc': 'amount',
'eth': 'value',
'xmr': 'tx_amount',
'iota': 'amount'
}

if protocol:
uri = address

if not str(address).startswith('bitcoincash:'):
uri = protocol + address

c_value = value

if coin in ['eth', 'iota']:
multiplier = get_coin_multiplier(coin, default=None)

if multiplier:
c_value = int(value * multiplier)

data = {keys[coin]: c_value}

return "{uri}?{query}".format(uri=uri, query=build_query_string(data))


@register.filter
def coin_name(coin):
coins = {
Expand Down
5 changes: 5 additions & 0 deletions cryptapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import requests
from django.shortcuts import reverse
from cryptapi.config import CRYPTAPI_URL
from urllib.parse import urlencode


def build_query_string(data):
return urlencode(data)


def build_callback_url(_r, params):
Expand Down

0 comments on commit 579d190

Please sign in to comment.