Skip to content

Commit c19e464

Browse files
committed
v0.4.0
- Support for BlockBee - Fetch all supported cryptocurrencies and cache them - Updated API Helper - Added conversion endpoint - Added support for BlockBee API Key - Added UI for payments - Added store application to provide examples on how to implement - General improvements - Fixed bugs
1 parent 2ff2fe2 commit c19e464

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1638
-454
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# MacOS
2+
.DS_STORE
3+
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]
@@ -21,6 +24,7 @@ var/
2124
*.egg-info/
2225
.installed.cfg
2326
*.egg
27+
django_store/
2428

2529
# PyInstaller
2630
# Usually these files are written by a python script from a template

MANIFEST.in

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 115 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
![CryptAPI](https://i.imgur.com/IfMAa7E.png)
1+
[<img src="https://i.imgur.com/IfMAa7E.png" width="300"/>](image.png)
2+
23

34
# CryptAPI's Django Library
45
Django's implementation of CryptAPI's payment gateway
@@ -99,7 +100,7 @@ from cryptapi import Invoice
99100
def order_creation_view(request):
100101
...
101102
invoice = Invoice(
102-
request=request,
103+
request=request, # This if your view request. It's meant to create the callback URL
103104
order_id=user_order.id,
104105
coin='btc',
105106
value=user_order.value
@@ -117,10 +118,14 @@ def order_creation_view(request):
117118
#### Where:
118119

119120
``request`` is Django's view HttpRequest object
121+
120122
``order_id`` is just your order id
121-
``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.
123+
124+
``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.
125+
122126
``value`` is an integer of the value of your order, either in satoshi, litoshi, wei, piconero or IOTA
123127

128+
&nbsp;
124129

125130
### Getting notified when the user pays
126131

@@ -134,18 +139,15 @@ def payment_received(order_id, payment, value):
134139
...
135140
```
136141

137-
138142
Where:
139143

140144
``order_id`` is the id of the order that you provided earlier, used to fetch your order
141145
``payment`` is an ``cryptapi.models.Payment`` object with the payment details, such as TXID, number of confirmations, etc.
142146
``value`` is the value the user paid, either in satoshi, litoshi, wei or IOTA
143147

144148

145-
&nbsp;
146-
147-
148-
>#### Important:
149+
#### Important:
150+
>
149151
>Don't forget to import your signals file.
150152
>
151153
>On your App's `apps.py` file:
@@ -163,76 +165,142 @@ Where:
163165
>[django docs](https://docs.djangoproject.com/en/3.0/topics/signals/#django.dispatch.receiver)
164166
165167
168+
169+
### Using our provided interface
170+
171+
Create a view in ``views.py``
172+
173+
```python
174+
def payment(_r, request_id):
175+
try:
176+
req = Request.objects.get(id=request_id)
177+
coin = req.provider.coin
178+
179+
qrcode = get_qrcode(coin, req.address_in)
180+
181+
fiat = get_conversion(coin, 'eur', req.value_requested)
182+
183+
print(fiat)
184+
185+
ctx = {
186+
'req': req,
187+
'qrcode': qrcode,
188+
'fiat': fiat['value_coin']
189+
}
190+
191+
return render(_r, 'payment.html', context=ctx)
192+
193+
except Request.DoesNotExist:
194+
pass
195+
196+
return redirect('store:request')
197+
```
198+
In your template HTML
199+
200+
```djangotemplate
201+
{% extends 'base.html' %}
202+
{% load cryptapi_helper %}
203+
{% block content %}
204+
{% generate_payment_template %}
205+
{% endblock %}
206+
```
207+
166208
&nbsp;
167209
168210
169211
### Helpers
170212
171-
This library has a couple of helpers to help you get started
213+
This library has a couple of helpers to help you get started. They are present in the file ``utils.py``.
172214
173215
``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``
174216
175-
``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)
217+
``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 or if he mistakenly didn't send the full payment)
176218
219+
``cryptapi.callback_url(_r, params)`` build your callback URL to provide to ``get_request``. Should be used inside a view since ``_r = request``
177220
178-
### Template Tags
179-
There's also some template tags which you can import to help you with conversions and the protocols.
180-
You just need to load ``cryptapi_helper`` on your template and use the following tags / filters:
221+
&nbsp;
181222
182-
* #### QR code (with `cryptapi.models.Request` object)
183-
If you want the library to generate and display a clickable QR code for you, just use our `generate_qrcode_for_request`, like this:
223+
### CryptAPI Helper
184224
185-
```djangotemplate
186-
{% generate_qrcode_for_request payment_request %}
187-
```
225+
This is the helper responsible for the connections ot the API itself. All these functions are in the ``cryptapi.py`` file.
188226
189-
You just need to feed it the `payment_request` object created with `invoice.request()`
227+
``get_info(coin)`` returns the information of all cryptocurrencies or just if ``coin=''`` or a specific cryptocurrency if ``coin='ltc'`` for example. [docs](https://docs.cryptapi.io/#operation/info)
190228
191-
The QR code that can also be clicked on mobile devices to launch the user's wallet.
229+
``get_supported_coins()`` returns all the support cryptocurrencies. You can consult them in this [list](https://cryptapi.io/fees/).
192230
193-
* #### QR code (with address, coin and value)
194-
If you want the library to generate and display a clickable QR code for you, just use our `generate_qrcode`, like this:
231+
``get_logs(coin, callback_url)`` returns all the callback logs related to a request. ``callback_url`` should be the callback provided to our API. [docs](https://docs.cryptapi.io/#operation/logs)
195232
196-
```djangotemplate
197-
{% generate_qrcode btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
198-
```
233+
``get_qrcode(coin, address, value, size)`` returns a PNG of a QR Code with the address for payment. [docs](https://docs.cryptapi.io/tag/Bitcoin#operation/btcqrcode)
234+
235+
``get_conversion(origin, to, value)`` returns the converted value in the parameter ``value_coin`` to the currency you wish, FIAT or Cryptocurrency.
236+
237+
``get_estimate(coin)`` returns the estimation of the blockchain fees for the cryptocurrency specified in the parameter ``coin``. E.g: ``get_estimate('trc20_usdt')`` [docs](https://docs.cryptapi.io/#operation/estimate)
238+
239+
``get_address(coin, address_out, callback_url, pending, api_key)`` requests a payment address to CryptAPI. If you don't wish to use [BlockBee](https://dash.blockbee.io/), you can leave ``api_key`` empty. [docs](https://docs.cryptapi.io/#operation/create)
199240
200-
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.
241+
&nbsp;
201242
202-
The QR code that can also be clicked on mobile devices to launch the user's wallet.
243+
### How to use the QR code (with address, coin and value)
203244
204-
##### Example:
245+
To generate a QR Code you must use ``get_qrcode`` in your view and feed the parameters to your template. To generate a QR Code image you must place content of the API response after ``data:image/png;base64,{{qr_code}}`` so the browser generates the QR Code.
205246
```djangotemplate
206-
{% load cryptapi_helper %}
207-
<body>
208-
<div class="row">
209-
<div class="col-sm-12">
210-
{% generate_qrcode btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
211-
</div>
212-
</div>
213-
</body>
247+
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
214248
```
215249
216-
* #### Payment URI
217-
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:
250+
You can also make the QR Code clickable.
218251
219252
```djangotemplate
220-
{% build_payment_uri btc 1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3 0.001 %}
253+
<a href='{{ qrcode.payment_uri }}'>
254+
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
255+
</a>
221256
```
222257
223-
It will output: `bitcoin:1PE5U4temq1rFzseHHGE2L8smwHCyRbkx3?amount=0.001`
258+
You can also add a value to the QR Code setting the ``value`` parameter to the value of your order (e.g ``0.2 LTC``). This may not function correctly in some wallets. **Use at your own risk.**
224259
225-
Same arguments as for the QR code
260+
## What is the Store application?
226261
227-
* #### Helpers
262+
We made the ``store`` application to provide you with code examples on how to implement our service. It also has the code for our suggested UI (both CSS and HTML).
228263
229-
``{% 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.
230264
265+
## Help
231266
232-
``{{ coin|coin_name }}`` will output the properly formatted cryptocurrency name.
267+
Need help?
268+
Contact us @ https://cryptapi.io/contacts/
233269
234270
235-
## Help
271+
### Changelog
236272
237-
Need help?
238-
Contact us @ https://cryptapi.io/contact/
273+
#### 0.1.2
274+
* Version bump
275+
276+
#### 0.1.3
277+
* Pending transactions (through the `payment_pending` signal)
278+
* Monero
279+
280+
#### 0.1.5
281+
* Fixed bug with MySQL database varchar length limitations
282+
* Added build payment URI tag, to help you generate payment URIs to feed into any QR Code generator
283+
284+
#### 0.2.0
285+
* Added ERC-20 token support
286+
* Added payment QR code
287+
* Improved documentation
288+
* Fixed bugs
289+
290+
#### 0.2.7
291+
* Added coin info helper
292+
293+
#### 0.3.1
294+
* New API URL
295+
* New general info API
296+
297+
#### 0.4.0
298+
* Support for BlockBee
299+
* Fetch all supported cryptocurrencies and cache them
300+
* Updated API Helper
301+
* Added conversion endpoint
302+
* Added support for BlockBee API Key
303+
* Added UI for payments
304+
* Added store application to provide examples on how to implement
305+
* General improvements
306+
* Fixed bugs

cryptapi/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from cryptapi.meta import VERSION
22
from cryptapi.dispatchers import RequestDispatcher as Invoice # noqa
3-
from cryptapi.utils import get_active_providers as valid_providers, get_order_request as get_order_invoices, info as coin_info # noqa
4-
from cryptapi.helpers import get_coin_multiplier, round_sig
3+
from cryptapi.utils import get_active_providers as valid_providers, get_order_request as get_order_invoices, build_callback_url as callback_url
54

65
__version__ = str(VERSION)
7-

cryptapi/admin.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from django.contrib import admin
2-
from cryptapi.models import Provider, Request, Payment, RequestLog, PaymentLog
2+
from .models import Provider, Request, Payment, RequestLog, PaymentLog, Metadata
3+
from .forms import CreateProviderForm
34

45

56
class ReadOnlyAdmin(admin.ModelAdmin):
@@ -18,41 +19,17 @@ def has_delete_permission(self, request, obj=None):
1819

1920

2021
class ProviderAdmin(admin.ModelAdmin):
21-
add_fieldsets = (
22-
('Coin', {
23-
'fields': ('coin',),
24-
'description': 'Select provider coin'
25-
}),
26-
('Cold Wallet', {
27-
'fields': ('cold_wallet',),
28-
'description': "Insert your cold wallet's address"
29-
}),
30-
('Active', {
31-
'fields': ('active',),
32-
'description': "Enable this provider"
33-
}),
34-
)
35-
36-
fieldsets = (
37-
('Coin', {
38-
'fields': ('coin', ),
39-
'description': 'Select provider coin'
40-
}),
41-
('Cold Wallet', {
42-
'fields': ('cold_wallet', ),
43-
'description': "Insert your cold wallet's address"
44-
}),
45-
('Active', {
46-
'fields': (('active',), ('last_updated', ), ),
47-
'description': "Enable this provider"
48-
}),
49-
)
50-
51-
readonly_fields = ('last_updated', )
52-
53-
54-
admin.site.register(Provider)
22+
readonly_fields = ['last_updated']
23+
form = CreateProviderForm
24+
25+
26+
class MetadataAdmin(ReadOnlyAdmin):
27+
list_display = ('__str__', 'last_updated',)
28+
29+
30+
admin.site.register(Provider, ProviderAdmin)
5531
admin.site.register(Request, ReadOnlyAdmin)
5632
admin.site.register(RequestLog, ReadOnlyAdmin)
5733
admin.site.register(Payment, ReadOnlyAdmin)
5834
admin.site.register(PaymentLog, ReadOnlyAdmin)
35+
admin.site.register(Metadata, MetadataAdmin)

cryptapi/choices.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,9 @@
11
from django.utils.translation import gettext_lazy as _
22

3-
TOKENS = [
4-
('erc20_usdt', 'ERC-20 USDT', '0xdAC17F958D2ee523a2206206994597C13D831ec7', 6, 4),
5-
('erc20_usdc', 'ERC-20 USDC', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 6, 4),
6-
('erc20_busd', 'ERC-20 BUSD', '0x4Fabb145d64652a948d72533023f6E7A623C7C53', 18, 16),
7-
('erc20_pax', 'ERC-20 PAX', '0x8E870D67F660D95d5be530380D0eC0bd388289E1', 18, 16),
8-
('erc20_tusd', 'ERC-20 TUSD', '0x0000000000085d4780B73119b644AE5ecd22b376', 18, 16),
9-
('erc20_bnb', 'ERC-20 BNB', '0xB8c77482e45F1F44dE1745F52C74426C631bDD52', 18, 0),
10-
('erc20_link', 'ERC-20 ChainLink', '0x514910771AF9Ca656af840dff83E8264EcF986CA', 18, 0),
11-
('erc20_cro', 'ERC-20 Crypto.com Coin', '0xA0b73E1Ff0B80914AB6fe0444E65848C4C34450b', 8, 0),
12-
('erc20_mkr', 'ERC-20 Maker', '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2', 18, 0),
13-
('erc20_nexo', 'ERC-20 NEXO', '0xB62132e35a6c13ee1EE0f84dC5d40bad8d815206', 18, 0),
14-
('erc20_bcz', 'ERC-20 BECAZ', '0x08399ab5eBBE96870B289754A7bD21E7EC8c6FCb', 18, 0),
15-
]
16-
17-
18-
TOKEN_DICT = {t[0]: t for t in TOKENS}
19-
20-
21-
COINS = [
22-
('btc', 'Bitcoin'),
23-
('eth', 'Ethereum'),
24-
('bch', 'Bitcoin Cash'),
25-
('ltc', 'Litecoin'),
26-
('iota', 'IOTA'),
27-
('xmr', 'Monero'),
28-
] + [(t[0], t[1]) for t in TOKENS] # Tokens
29-
30-
313
STATUS = (
324
('created', _('Created')),
335
('pending', _('Pending')),
346
('insufficient', _('Payment Insufficient')),
357
('received', _('Received')),
368
('done', _('Done')),
37-
)
38-
39-
40-
COIN_MULTIPLIERS = {
41-
'btc': 10**8,
42-
'bch': 10**8,
43-
'ltc': 10**8,
44-
'eth': 10**18,
45-
'iota': 10**6,
46-
'xmr': 10**12,
47-
48-
# Tokens
49-
**{t[0]: 10**t[3] for t in TOKENS}
50-
}
9+
)

cryptapi/config.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)