Skip to content

Commit cad3b3c

Browse files
committed
v0.1.0
0 parents  commit cad3b3c

Some content is hidden

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

43 files changed

+2450
-0
lines changed

.gitignore

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# MacOS
2+
.DS_STORE
3+
4+
# Byte-compiled / optimized / DLL files
5+
__pycache__/
6+
*.py[cod]
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
env/
14+
build/
15+
develop-eggs/
16+
dist/
17+
downloads/
18+
eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
django_store/
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.coverage
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
47+
# Translations
48+
*.mo
49+
*.pot
50+
51+
# Django stuff:
52+
*.log
53+
54+
# Sphinx documentation
55+
docs/_build/
56+
57+
# PyBuilder
58+
target/
59+
README.rst
60+
venv
61+
62+
63+
# IDE stuff
64+
65+
.idea
66+
67+
# Test DB
68+
69+
*.sqlite3

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 BlockBee
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
[<img src="https://blockbee.io/static/assets/images/blockbee_logo_nospaces.png" width="300"/>](image.png)
2+
3+
4+
# BlockBee's Django Library
5+
Django's implementation of BlockBee's payment gateway
6+
7+
## Requirements:
8+
9+
```
10+
Python >= 3.0
11+
Django >= 2.0
12+
Requests >= 2.20
13+
```
14+
15+
16+
17+
## Install
18+
19+
20+
```shell script
21+
pip install django-blockbee
22+
```
23+
24+
25+
[on pypi](https://pypi.python.org/pypi/django-cryptapi)
26+
or
27+
[on GitHub](https://github.com/blockbee-io/django-blockbee)
28+
29+
Add to INSTALLED_APPS:
30+
31+
```python
32+
INSTALLED_APPS = (
33+
'blockbee',
34+
...
35+
)
36+
```
37+
38+
39+
Run migrations:
40+
41+
```shell script
42+
python3 manage.py migrate blockbee
43+
```
44+
45+
Collect static files:
46+
47+
```shell script
48+
python3 manage.py collectstatic
49+
```
50+
51+
Add CryptAPI's URLs to your project's urls.py file:
52+
53+
```python
54+
urlpatterns = [
55+
path('blockbee/', include('blockbee.urls')),
56+
...
57+
]
58+
```
59+
60+
## Configuration
61+
62+
After the installation you need to set up Providers for each coin you wish to accept.
63+
64+
You need to go into your Django Admin and create a new BlockBee ``Provider`` for each coin with your cold wallet address where the funds will be forwarded to.
65+
66+
## Usage
67+
68+
### Creating an Invoice
69+
70+
In your order creation view, assuming ``user_order`` is your order object:
71+
72+
* ##### If you want the address generated:
73+
74+
```python
75+
from blockbee import Invoice
76+
77+
...
78+
79+
80+
def order_creation_view(request):
81+
...
82+
invoice = Invoice(
83+
request=request,
84+
order_id=user_order.id,
85+
coin='btc',
86+
value=user_order.value
87+
)
88+
89+
payment_address = invoice.address()
90+
91+
if payment_address is not None:
92+
# Show the payment address to the user
93+
...
94+
else:
95+
# Handle request error, check RequestLogs on Admin
96+
```
97+
98+
* ##### If you want the `blockbee.models.Request` object:
99+
100+
```python
101+
from blockbee import Invoice
102+
103+
...
104+
105+
106+
def order_creation_view(request):
107+
...
108+
invoice = Invoice(
109+
request=request, # This if your view request. It's meant to create the callback URL
110+
order_id=user_order.id,
111+
coin='btc',
112+
value=user_order.value
113+
)
114+
115+
payment_request = invoice.request()
116+
117+
if payment_request is not None:
118+
# Show the payment address to the user
119+
...
120+
else:
121+
# Handle request error, check RequestLogs on Admin
122+
```
123+
124+
#### Where:
125+
126+
``request`` is Django's view HttpRequest object
127+
128+
``order_id`` is just your order id
129+
130+
``coin`` is the ticker of the coin you wish to use, any of our supported coins (https://blockbee.io/cryptocurrencies/). You need to have a ``Provider`` set up for that coin.
131+
132+
``value`` is an integer of the value of your order
133+
134+
``apikey`` is the API Key that you got from your BlockBee Dashboard
135+
136+
&nbsp;
137+
138+
### Getting notified when the user pays
139+
140+
```python
141+
from django.dispatch import receiver
142+
from blockbee.signals import payment_complete
143+
144+
145+
@receiver(payment_complete)
146+
def payment_received(order_id, payment, value):
147+
# Implement your logic to mark the order as paid and release the goods to the user
148+
...
149+
```
150+
151+
Where:
152+
153+
``order_id`` is the id of the order that you provided earlier, used to fetch your order
154+
``payment`` is an ``blockbee.models.Payment`` object with the payment details, such as TXID, number of confirmations, etc.
155+
``value`` is the value the user paid
156+
157+
158+
#### Important:
159+
>
160+
>Don't forget to import your signals file.
161+
>
162+
>On your App's `apps.py` file:
163+
>
164+
>```python
165+
>class MyAppConfig(AppConfig):
166+
> name = 'MyApp'
167+
>
168+
> def ready(self):
169+
> super(MyAppConfig, self).ready()
170+
>
171+
> # noinspection PyUnresolvedReferences
172+
> import MyApp.signals
173+
>```
174+
>[django docs](https://docs.djangoproject.com/en/3.0/topics/signals/#django.dispatch.receiver)
175+
176+
177+
178+
### Using our provided interface
179+
180+
Create a view in ``views.py``
181+
182+
```python
183+
def payment(_r, request_id):
184+
try:
185+
req = Request.objects.get(id=request_id)
186+
coin = req.provider.coin
187+
188+
qrcode = get_qrcode(coin, req.address_in)
189+
190+
fiat = get_conversion(coin, 'eur', req.value_requested)
191+
192+
print(fiat)
193+
194+
ctx = {
195+
'req': req,
196+
'qrcode': qrcode,
197+
'fiat': fiat['value_coin']
198+
}
199+
200+
return render(_r, 'payment.html', context=ctx)
201+
202+
except Request.DoesNotExist:
203+
pass
204+
205+
return redirect('store:request')
206+
```
207+
In your template HTML
208+
209+
```djangotemplate
210+
{% extends 'base.html' %}
211+
{% load cryptapi_helper %}
212+
{% block content %}
213+
{% generate_payment_template %}
214+
{% endblock %}
215+
```
216+
217+
&nbsp;
218+
219+
220+
### Helpers
221+
222+
This library has a couple of helpers to help you get started. They are present in the file ``utils.py``.
223+
224+
``blockbee.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``
225+
226+
``blockbee.get_order_invoices(order_id)`` returns a list of ``blockbee.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)
227+
228+
``blockbee.callback_url(_r, params)`` build your callback URL to provide to ``get_request``. Should be used inside a view since ``_r = request``
229+
230+
&nbsp;
231+
232+
### BlockBee Helper
233+
234+
This is the helper responsible for the connections ot the API itself. All these functions are in the ``cryptapi.py`` file.
235+
236+
``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.blockbee.io/#operation/info)
237+
238+
``get_supported_coins()`` returns all the support cryptocurrencies. You can consult them in this [list](https://blockbee.io/fees/).
239+
240+
``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.blockbee.io/#operation/logs)
241+
242+
``get_qrcode(coin, address, value, size)`` returns a PNG of a QR Code with the address for payment. [docs](https://docs.blockbee.io/tag/Bitcoin#operation/btcqrcode)
243+
244+
``get_conversion(origin, to, value)`` returns the converted value in the parameter ``value_coin`` to the currency you wish, FIAT or Cryptocurrency.
245+
246+
``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.blockbee.io/#operation/estimate)
247+
248+
``get_address(coin, params)`` requests a payment address to BlockBee. Params include all the parameters that can be found [here](https://docs.blockbee.io/#operation/create). The parameter ``apikey`` is mandatory.
249+
250+
&nbsp;
251+
252+
### How to use the QR code (with address, coin and value)
253+
254+
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.
255+
```djangotemplate
256+
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
257+
```
258+
259+
You can also make the QR Code clickable.
260+
261+
```djangotemplate
262+
<a href='{{ qrcode.payment_uri }}'>
263+
<img src="data:image/png;base64,{{ qrcode.qr_code }}" alt="Payment QR Code"/>
264+
</a>
265+
```
266+
267+
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.**
268+
269+
## What is the Store application?
270+
271+
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).
272+
273+
274+
## Help
275+
276+
Need help?
277+
Contact us @ https://blockbee.io/contacts/
278+
279+
280+
### Changelog
281+
282+
#### 0.1.0
283+
* Initial Release

blockbee/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from blockbee.meta import VERSION
2+
from blockbee.dispatchers import RequestDispatcher as Invoice # noqa
3+
from blockbee.utils import get_active_providers as valid_providers, get_order_request as get_order_invoices, build_callback_url as callback_url
4+
5+
6+
__version__ = str(VERSION)

0 commit comments

Comments
 (0)