How to accept payments in Telegram | Yoomoney Python API

In this post, we will learn how to accept payments in the Telegram bot using the Yoomoney API.





Introduction

To begin with, I recently wanted to create an electronic goods store on Telegram. And I ran into the problem that at the time of work there were no ready-made solutions. I wanted to accept payments without individual entrepreneurs and all this movement. Therefore, my choice was between Qiwi and Yoomoney (formerly Yandex Money). I myself am from Belarus ... Therefore, it is easier to get an "Identified" account from Yoomoney.





As a result, I created the yoomoney library for Python.





If this post helped you, please star on GitHub . I will be very pleased!





Description

  • We get the token





  • Checking the token





  • How to issue an invoice for payment





  • Payment verification





We get the token

In order to use the Yoomoney API, you need to get a special token. First of all, we register the application:





1.  Go to the YuMoney wallet. If there is no wallet,  create one .





2.  Go to the Application Registration page  .





3.  Specify application parameters:





4.  Click the Confirm button  .





  , , (client_id) , , (client_secret).





!





client_id redirect_uri, .





: . .





pip install yoomoney





from yoomoney import Authorize

Authorize(
      client_id="YOUR_CLIENT_ID",
      redirect_uri="YOUR_REDIRECT_URI",
      scope=["account-info",
             "operation-history",
             "operation-details",
             "incoming-transfers",
             "payment-p2p",
             "payment-shop",
             ]
      )
      
      



! !





YOUR_TOKEN :





from yoomoney import Client
token = "YOUR_TOKEN"
client = Client(token)
user = client.account_info()
print("Account number:", user.account)
print("Account balance:", user.balance)
print("Account currency code in ISO 4217 format:", user.currency)
print("Account status:", user.account_status)
print("Account type:", user.account_type)
print("Extended balance information:")
for pair in vars(user.balance_details):
    print("\t-->", pair, ":", vars(user.balance_details).get(pair))
print("Information about linked bank cards:")
cards = user.cards_linked
if len(cards) != 0:
    for card in cards:
        print(card.pan_fragment, " - ", card.type)
else:
    print("No card is linked to the account")
      
      



:





Account number: 410019014512803
Account balance: 999999999999.99
Account currency code in ISO 4217 format: 643
Account status: identified
Account type: personal
Extended balance information:
   --> total : 999999999999.99
   --> available : 999999999999.99
   --> deposition_pending : None
   --> blocked : None
   --> debt : None
   --> hold : None
Information about linked bank cards:
No card is linked to the account
      
      



! .





Quickpay.





from yoomoney import Quickpay
quickpay = Quickpay(
            receiver="410019014512803",
            quickpay_form="shop",
            targets="Sponsor this project",
            paymentType="SB",
            sum=150,
            )
print(quickpay.base_url)
print(quickpay.redirected_url)
      
      



:





https://yoomoney.ru/quickpay/confirm.xml?receiver=410019014512803&quickpay-form=shop&targets=Sponsor%20this%20project&paymentType=SB&sum=150
https://yoomoney.ru/transfer/quickpay?requestId=343532353937313933395f66326561316639656131626539326632616434376662373665613831373636393537613336383639
      
      



. . .





Payment form

, .





: , ?

label - , . ,   .





:





from yoomoney import Quickpay
quickpay = Quickpay(
            receiver="410019014512803",
            quickpay_form="shop",
            targets="Sponsor this project",
            paymentType="SB",
            sum=150,
            lebel="a1b2c3d4e5"
            )
print(quickpay.base_url)
print(quickpay.redirected_url)
      
      



.





Client.





Knowing the transaction label , we can filter the wallet's transaction history. Just put a label in client.operation_history ():





from yoomoney import Client
token = "YOUR_TOKEN"
client = Client(token)
history = client.operation_history(label="a1b2c3d4e5")
print("List of operations:")
print("Next page starts with: ", history.next_record)
for operation in history.operations:
    print()
    print("Operation:",operation.operation_id)
    print("\tStatus     -->", operation.status)
    print("\tDatetime   -->", operation.datetime)
    print("\tTitle      -->", operation.title)
    print("\tPattern id -->", operation.pattern_id)
    print("\tDirection  -->", operation.direction)
    print("\tAmount     -->", operation.amount)
    print("\tLabel      -->", operation.label)
    print("\tType       -->", operation.type)
      
      



As a result, we get a list of all operations for our filter:





List of operations:
Next page starts with:  None
Operation: 670278348725002105
  Status     --> success
  Datetime   --> 2021-10-10 10:10:10
  Title      -->    ****4487
  Pattern id --> None
  Direction  --> in
  Amount     --> 150.0
  Label      --> a1b2c3d4e5
  Type       --> deposition
      
      



Now we know if the payment went through.





Everything! Nothing else is needed to receive payments.





Conclusion

If this post helped you, please star on GitHub . I will be very pleased!








All Articles