|
| 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 | + |
| 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 | + |
| 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 | + |
| 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 | + |
| 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 |
0 commit comments