Skip to content

Commit 579d190

Browse files
author
CryptAPI
committed
* Fixed bug with MySQL database varchar length limitations
* Added build payment URI tag, to help you generate payment URIs to feed into any QR Code generator
1 parent 0e1ebda commit 579d190

File tree

7 files changed

+60
-11
lines changed

7 files changed

+60
-11
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,17 @@ You just need to load ``cryptapi_helper`` on your template and use the following
128128

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

131+
132+
If you want to build a full payment URI for your clients, you can use our `build_payment_uri` tag, like so:
133+
134+
```djangotemplate
135+
{% build_payment_uri btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
136+
137+
# will output: bitcoin:1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3?amount=0.001
138+
```
139+
140+
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
141+
131142
## Help
132143

133144
Need help?

cryptapi/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CallbackForm(BaseCallbackForm):
2626
class AddressCreatedForm(forms.Form):
2727
address_in = forms.CharField(max_length=128)
2828
address_out = forms.CharField(max_length=128)
29-
callback_url = forms.CharField(max_length=16384)
29+
callback_url = forms.CharField(max_length=16383)
3030
status = forms.CharField(max_length=16)
3131

3232
def __init__(self, initials, *args, **kwargs):

cryptapi/meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from distutils.version import StrictVersion
22

33

4-
VERSION = StrictVersion('0.1.4')
4+
VERSION = StrictVersion('0.1.5')

cryptapi/migrations/0001_initial.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Migration(migrations.Migration):
2828
name='PaymentLog',
2929
fields=[
3030
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
31-
('raw_data', models.CharField(max_length=16384)),
31+
('raw_data', models.CharField(max_length=16383)),
3232
('timestamp', models.DateTimeField(auto_now_add=True)),
3333
('payment', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='cryptapi.Payment')),
3434
],
@@ -61,7 +61,7 @@ class Migration(migrations.Migration):
6161
name='RequestLog',
6262
fields=[
6363
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
64-
('raw_data', models.CharField(max_length=16384)),
64+
('raw_data', models.CharField(max_length=16383)),
6565
('timestamp', models.DateTimeField(auto_now_add=True)),
6666
('request', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='cryptapi.Request')),
6767
],

cryptapi/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __str__(self):
4747

4848
class RequestLog(models.Model):
4949
request = models.ForeignKey(Request, on_delete=models.SET_NULL, null=True)
50-
raw_data = models.CharField(max_length=16384)
50+
raw_data = models.CharField(max_length=16383)
5151
timestamp = models.DateTimeField(auto_now_add=True)
5252

5353
def __str__(self):
@@ -56,7 +56,7 @@ def __str__(self):
5656

5757
class PaymentLog(models.Model):
5858
payment = models.ForeignKey(Payment, on_delete=models.SET_NULL, null=True)
59-
raw_data = models.CharField(max_length=16384)
59+
raw_data = models.CharField(max_length=16383)
6060
timestamp = models.DateTimeField(auto_now_add=True)
6161

6262
def __str__(self):

cryptapi/templatetags/cryptapi_helper.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django import template
22
from cryptapi.helpers import get_coin_multiplier
3+
from cryptapi.utils import build_query_string
34

45
register = template.Library()
56

@@ -19,16 +20,48 @@ def convert_value(coin, value):
1920
@register.filter
2021
def coin_protocol(coin):
2122
coins = {
22-
'btc': 'bitcoin',
23-
'bch': 'bitcoincash',
24-
'ltc': 'litecoin',
25-
'eth': 'ethereum',
26-
'iota': 'iota',
23+
'btc': 'bitcoin:',
24+
'bch': 'bitcoincash:',
25+
'ltc': 'litecoin:',
26+
'eth': 'ethereum:',
27+
'xmr': 'monero:',
28+
'iota': 'iota:',
2729
}
2830

2931
return coins.get(coin, '')
3032

3133

34+
@register.simple_tag
35+
def build_payment_uri(coin, address, value):
36+
protocol = coin_protocol(coin)
37+
keys = {
38+
'btc': 'amount',
39+
'bch': 'amount',
40+
'ltc': 'amount',
41+
'eth': 'value',
42+
'xmr': 'tx_amount',
43+
'iota': 'amount'
44+
}
45+
46+
if protocol:
47+
uri = address
48+
49+
if not str(address).startswith('bitcoincash:'):
50+
uri = protocol + address
51+
52+
c_value = value
53+
54+
if coin in ['eth', 'iota']:
55+
multiplier = get_coin_multiplier(coin, default=None)
56+
57+
if multiplier:
58+
c_value = int(value * multiplier)
59+
60+
data = {keys[coin]: c_value}
61+
62+
return "{uri}?{query}".format(uri=uri, query=build_query_string(data))
63+
64+
3265
@register.filter
3366
def coin_name(coin):
3467
coins = {

cryptapi/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import requests
22
from django.shortcuts import reverse
33
from cryptapi.config import CRYPTAPI_URL
4+
from urllib.parse import urlencode
5+
6+
7+
def build_query_string(data):
8+
return urlencode(data)
49

510

611
def build_callback_url(_r, params):

0 commit comments

Comments
 (0)