- Python 2.7 or Python 3.x
- pip
- Install it with
pip install yandex-money-sdk
- Paste
from yandex_money.api import Wallet, ExternalPayment
to your source code
Using Yandex.Money API requires following steps
Obtain token URL and redirect user's browser to Yandex.Money service. Note:
client_id
,redirect_uri
,client_secret
are constants that you get, when register app in Yandex.Money API.scope = ['account-info', 'operation-history'] # etc.. auth_url = Wallet.build_obtain_token_url(client_id, redirect_uri, scope)
After that, user fills Yandex.Money HTML form and user is redirected back to
REDIRECT_URI?code=CODE
.You should immediately exchange
CODE
withACCESS_TOKEN
.access_token = Wallet.get_access_token(client_id, code, redirect_uri, client_secret=None)
Now you can use Yandex.Money API.
wallet = Wallet(access_token=access_token) account_info = wallet.account_info() balance = account_info['balance'] # and so on request_options = { "pattern_id": "p2p", "to": "410011161616877", "amount_due": "0.02", "comment": "test payment comment from yandex-money-python", "message": "test payment message from yandex-money-python", "label": "testPayment", "test_payment": true, "test_result": "success" } request_result = wallet.request_payment(request_options) # check status process_payment = wallet.process_payment({ "request_id": request_result['request_id'], }) # check result if process_payment['status'] == "success": # show success page else: # something went wrong
Fetch instantce-id(ussually only once for every client. You can store result in DB).
response = ExternalPayment.get_instance_id(client_id) if reponse.status == "success": instance_id = response.instance_id; else: # throw exception with reponse->error message
Make request payment
# make instance external_payment = ExternalPayment(instance_id); payment_options = { # pattern_id, etc.. } response = external_payment.request(payment_options) if response.status == "success": request_id = response.request_id else: # throw exception with response->message
Process the request with process-payment.
process_options = { "request_id": request_id # other params.. } result = external_payment.process(process_options) # process result according to docs
- Clone this repo.
- Create
tests/constants.python
file withACCESS_TOKEN
andCLIENT_ID
constants. - Install
tox
- Run
tox
in repo root directory