📜 ⬆️ ⬇️

Python Webmoney API

It took me how to implement support for Webmoney API ( Documentation ) in the project. I did not find any libraries on python, so I decided to write my own.


Repository link

So, there are two options for a request to api webmania.
')


I consider only the second option. A certificate is required for the request. How to get it is written here . I managed to get a certificate only from Firefox, Chrome does not support this feature at all, and Explorer (Windows 8) generated an error. After receiving the certificate must be exported to a file. Kakk do it written here

The certificate is exported to the pkcs12 file. It is necessary to get public and private keys from it. This is done by the commands:
openssl pkcs12 -in path.p12 -out crt.pem -clcerts -nokeys openssl pkcs12 -in path.p12 -out key.pem -nocerts -nodes 


Work with API



The package can be installed via pip:

 pip install webmoney-api 


After installation import libraries

 from webmoney_api import ApiInterface, WMLightAuthInterface 


WMLightAuthInterface is a class that describes authentication through Keeper Light.
ApiInterface - class api.

We connect:

 >>> api = ApiInterface(WMLightAuthInterface("/home/stas/wmcerts/crt.pem", "/home/stas/wmcerts/key.pem")) 


When initializing WMLightAuthInterface, we pass to it the paths to our generated public and private key.
After connecting, the following methods are available:

x1 - x10 - correspond to similar webman interfaces. Parameters are passed by name to the called method.
Additionally, you can pass the parameter reqn - request number.

The method makes a request and returns data in the format:
 {"retval": <retval>, "retdesc": <retdesc>, "response": <response} 


Where



Example: search user ID by wallet



 >>> api.x8(purse="R328079907035", reqn=int(time.time()))["response"] OrderedDict([(u'wmid', OrderedDict([(u'@available', u'0'), (u'@themselfcorrstate', u'0'), (u'@newattst', u'110'), ('#text', u'407414370132')])), (u'purse', OrderedDict([(u'@merchant_active_mode', u'-1'), (u'@merchant_allow_cashier', u'-1'), ('#text', u'R328079907035')]))]) >>> api.x8(purse="R328079907035", reqn=int(time.time()))["response"]["wmid"]["#text"] u'407414370132' >>> api.x8(purse="R328079907035", reqn=int(time.time()))["response"]["wmid"]["@available"] u'0' 


Example: getting the history of all invoices for a wallet



 >>> api.x4(purse="R328079907035", datestart="20100101 00:00:00", datefinish="20140501 00:00:00") ValueError: Error while requesting API. retval = -4, retdesc = wrong w3s.request/reqn step=2 Request data: {'cert': ('/home/stas/wmcerts/crt.pem', '/home/stas/wmcerts/key.pem'), 'data': '<w3s.request><reqn></reqn><getoutinvoices><datestart>20100101 00:00:00</datestart><datefinish>20140501 00:00:00</datefinish><purse>R328079907035</purse></getoutinvoices></w3s.request>', 'url': 'https://w3s.wmtransfer.com/asp/XMLOutInvoicesCert.asp', 'verify': False} 


Error because reqn parameter not passed. Let's pass it:
 >>> api.x4(purse="R328079907035", datestart="20100101 00:00:00", datefinish="20140501 00:00:00", reqn=int(time.time())) {'response': OrderedDict([(u'@cnt', u'0'), (u'@cntA', u'0')]), 'retdesc': None, 'retval': u'0'} 


Example: getting a list of invoices for payment



 >>> for order in api.x10(wmid="407414370132", datestart="20100101 00:00:00", datefinish="20140501 00:00:00", reqn=int(time.time()))["response"]["ininvoice"]: >>> print order["orderid"], order["amount"], order["state"] 4640849 122.40 2 24 1.00 2 27 0.40 2 


Links





I will be glad comments and help)

Source: https://habr.com/ru/post/222411/


All Articles