|
| 1 | +.. |liqpaylogo| image:: https://www.liqpay.ua/1508940109424071/static/img/images/logo.svg |
| 2 | + |
| 3 | +===== |
| 4 | +|liqpaylogo| SDK-Python |
| 5 | +===== |
| 6 | + |
| 7 | +:Version: 1.1.0 |
| 8 | +:Web: https://www.liqpay.ua/ |
| 9 | +:Download: https://pypi.python.org/pypi/liqpay-python |
| 10 | +:Source: https://github.com/liqpay/sdk-python |
| 11 | +:Documentation: https://www.liqpay.ua/documentation/en/ |
| 12 | +:Keywords: liqpay, privat24, privatbank, python, internet acquiring, P2P payments, two-step payments |
| 13 | + |
| 14 | + |
| 15 | +What python version is supported? |
| 16 | +============ |
| 17 | +- Python 2.7 |
| 18 | +- Python 3.4, 3.5, 3.6 |
| 19 | + |
| 20 | +Get Started |
| 21 | +============ |
| 22 | +1. Sign up in https://www.liqpay.ua/en/authorization. |
| 23 | +2. Create a company. |
| 24 | +3. In company settings, on API tab, get **Public key** and **Private key**. |
| 25 | +4. Done. |
| 26 | + |
| 27 | +Installation |
| 28 | +============ |
| 29 | +From pip |
| 30 | +:: |
| 31 | + $ pip install liqpay-python |
| 32 | + |
| 33 | +From github |
| 34 | +:: |
| 35 | + $ pip install git+https://github.com/liqpay/sdk-python#egg=liqpay |
| 36 | + |
| 37 | +Working with LiqPay Callback locally |
| 38 | +============ |
| 39 | +If you need debugging API Callback on local environment use https://localtunnel.github.io/www/ |
| 40 | + |
| 41 | +How it use? |
| 42 | +============ |
| 43 | + |
| 44 | +Example 1: Basic |
| 45 | +------- |
| 46 | + |
| 47 | +**Backend** |
| 48 | + |
| 49 | +:: |
| 50 | + |
| 51 | + liqpay = LiqPay(public_key, private_key) |
| 52 | + html = liqpay.cnb_form({ |
| 53 | + 'action': 'pay', |
| 54 | + 'amount': '1', |
| 55 | + 'currency': 'USD', |
| 56 | + 'description': 'description text', |
| 57 | + 'order_id': 'order_id_1', |
| 58 | + 'version': '3' |
| 59 | + }) |
| 60 | + |
| 61 | +**Frontend** |
| 62 | + |
| 63 | +Variable ``html`` will contain next html form |
| 64 | + |
| 65 | +:: |
| 66 | + |
| 67 | + <form method="POST" action="https://www.liqpay.ua/api/3/checkout" accept-charset="utf-8"> |
| 68 | + <input type="hidden" name="data" value="eyAidmVyc2lvbiIgOiAzLCAicHVibGljX2tleSIgOiAieW91cl9wdWJsaWNfa2V5IiwgImFjdGlv |
| 69 | + biIgOiAicGF5IiwgImFtb3VudCIgOiAxLCAiY3VycmVuY3kiIDogIlVTRCIsICJkZXNjcmlwdGlv |
| 70 | + biIgOiAiZGVzY3JpcHRpb24gdGV4dCIsICJvcmRlcl9pZCIgOiAib3JkZXJfaWRfMSIgfQ=="/> |
| 71 | + <input type="hidden" name="signature" value="QvJD5u9Fg55PCx/Hdz6lzWtYwcI="/> |
| 72 | + <input type="image" |
| 73 | + src="//static.liqpay.ua/buttons/p1ru.radius.png"/> |
| 74 | + </form> |
| 75 | + |
| 76 | +Example 2: Integrate Payment widget to Django |
| 77 | +------- |
| 78 | +`Payment widget documentation`_ |
| 79 | + |
| 80 | +.. _`Payment widget documentation`: |
| 81 | + https://www.liqpay.ua/documentation/en/api/aquiring/widget/ |
| 82 | + |
| 83 | +**Backend** |
| 84 | + |
| 85 | +views.py |
| 86 | + |
| 87 | +:: |
| 88 | + |
| 89 | + from liqpay import LiqPay |
| 90 | + |
| 91 | + from django.views.generic import TemplateView |
| 92 | + from django.shortcuts import render |
| 93 | + from django.http import HttpResponse |
| 94 | + |
| 95 | + class PayView(TemplateView): |
| 96 | + template_name = 'billing/pay.html' |
| 97 | + |
| 98 | + def get(self, request, *args, **kwargs): |
| 99 | + liqpay = LiqPay(settings.LIQPAY_PUBLIC_KEY, settings.LIQPAY_PRIVATE_KEY) |
| 100 | + params = { |
| 101 | + 'action': 'pay', |
| 102 | + 'amount': '100', |
| 103 | + 'currency': 'USD', |
| 104 | + 'description': 'Payment for clothes', |
| 105 | + 'order_id': 'order_id_1', |
| 106 | + 'version': '3', |
| 107 | + 'sandbox': 0, # sandbox mode, set to 1 to enable it |
| 108 | + 'server_url': 'https://test.com/billing/pay-callback/', # url to callback view |
| 109 | + } |
| 110 | + signature = liqpay.cnb_signature(params) |
| 111 | + data = liqpay.cnb_data(params) |
| 112 | + return render(request, self.template_name, {'signature': signature, 'data': data}) |
| 113 | + |
| 114 | + @method_decorator(csrf_exempt, name='dispatch') |
| 115 | + class PayCallbackView(View): |
| 116 | + def post(self, request, *args, **kwargs): |
| 117 | + liqpay = LiqPay(settings.LIQPAY_PUBLIC_KEY, settings.LIQPAY_PRIVATE_KEY) |
| 118 | + data = request.POST.get('data') |
| 119 | + signature = request.POST.get('signature') |
| 120 | + sign = liqpay.str_to_sign(settings.LIQPAY_PRIVATE_KEY + data + settings.LIQPAY_PRIVATE_KEY) |
| 121 | + if sign == signature: |
| 122 | + print('callback is valid') |
| 123 | + response = liqpay.decode_data_from_str(data) |
| 124 | + print('callback data', response) |
| 125 | + return HttpResponse() |
| 126 | + |
| 127 | +urls.py |
| 128 | + |
| 129 | +:: |
| 130 | + |
| 131 | + from django.conf.urls import url |
| 132 | + |
| 133 | + from billing.views import PayView, PayCallbackView |
| 134 | + |
| 135 | + |
| 136 | + urlpatterns = [ |
| 137 | + url(r'^pay/$', PayView.as_view(), name='pay_view'), |
| 138 | + url(r'^pay-callback/$', PayCallbackView.as_view(), name='pay_callback'), |
| 139 | + ] |
| 140 | + |
| 141 | +**Frontend** |
| 142 | + |
| 143 | +:: |
| 144 | + |
| 145 | + <div id="liqpay_checkout"></div> |
| 146 | + <script> |
| 147 | + window.LiqPayCheckoutCallback = function() { |
| 148 | + LiqPayCheckout.init({ |
| 149 | + data: "{{ data }}", |
| 150 | + signature: "{{ signature }}", |
| 151 | + embedTo: "#liqpay_checkout", |
| 152 | + mode: "embed" // embed || popup, |
| 153 | + }).on("liqpay.callback", function(data){ |
| 154 | + console.log(data.status); |
| 155 | + console.log(data); |
| 156 | + }).on("liqpay.ready", function(data){ |
| 157 | + // ready |
| 158 | + }).on("liqpay.close", function(data){ |
| 159 | + // close |
| 160 | + }); |
| 161 | + }; |
| 162 | + </script> |
| 163 | + <script src="//static.liqpay.ua/libjs/checkout.js" async></script> |
0 commit comments