We are writing a payment for a telegram bot in python using the telebot library part 3

For those who have not read the previous parts:





Why do I need to pay?



In the life of every telegram bot developer, there comes a time when you need to implement payment in your bot. And here you have two ways. The first is to register a TIN, IP / Legal entity and connect official payment from a telegram or just use a third-party service like Free-Kassa, QIWI, etc. I think the choice is obvious, in this article I will use QIWI, because I want it so.



To begin, let's think about how the bot will check what has come, from whom and how much. The captain of evidence tells me that it will be necessary to get the entire history of transfers to an account, which we will use as our account to accept payments. Let's do it:



import requests import json QIWI_TOKEN = '' QIWI_ACCOUNT = '' s = requests.Session() s.headers['authorization'] = 'Bearer ' + QIWI_TOKEN parameters = {'rows': '50'} h = s.get('https://edge.qiwi.com/payment-history/v1/persons/'+ QIWI_ACCOUNT +'/payments', params = parameters) req = json.loads(h.text)
      
      





In the QIWI_TOKEN constant , we write the api key for working with QIWI, you can get it here . And in QIWI_ACCOUNT we record the phone number of your main account. If you did everything correctly, there will be no errors. The data is in the req dictionary, which includes lists of data. Or rather (do not read) the req variable that includes the data dictionary, which contains a list of numbered dictionaries that include lists.







Here we should get a phone number, a comment (later you will understand why) and the transaction amount. In order to continue writing code, you will need to be able to use databases, if you donโ€™t know anything or even donโ€™t know what it is - here is a link to an article that describes working with Sqlite3 quite clearly. Now let's think over the algorithm that will be used in our bot.



  1. We generate a random number from 100000 to 999999.
  2. Temporarily enter data into the table (user id, phone number, amount generated earlier random number)
  3. We check the comment, account and amount in the req dictionary.
  4. If the amount, phone and comment agree, we count the payment.
  5. Add your functionality after payment ...


Now you are probably thinking, why do you need to check this comment and generate a random number? And all ingenious is simple. The fact is that if we just check the amount and phone, then the bot will be able to find the previously sent transaction and count it. Simply put, this is done for security and minimizing bugs. Let's continue to write the code:



Create a table:



 import sqlite3 c.execute("CREATE TABLE IF NOT EXISTS payment_query(user_id INTEGER, phone TEXT, sum INTEGER, code INTEGER)")
      
      





We enter the data in the table as soon as the user wants to pay something in your bot.



 from random import randint #   ,    phone = '+79999999999' sum = 100 random_code = randint(100000, 999999) c.execute(f"INSERT INTO payment_query VALUES({message.from_user.id}, {phone}, {sum}, {random_code})") conn.commit()
      
      





Next, you need to do some kind of payment verification, in my case it will be an inline keyboard:



Here is an example from my recent bot.





As soon as the user clicks on the payment button, the bot will receive a translation history with QIWI. Now we need to do the most important thing - verification of payment.



 result = c.execute(f"SELECT * FROM payment_query WHERE user_id = {call.message.chat.id}").fetchone() #     #    ,      () phone = result[1] random_code = result[3] sum = result[2] #     for i in range(len(req['data'])): if req['data'][i]['account'] == phone: if req['data'][i]['comment'] == random_code: if req['data'][i]['sum']['amount'] == sum: c.execute(f"DELETE FROM payment_query WHERE user_id = {call.message.chat.id}") #      # ,  ,    
      
      





That's all, I hope I helped you solve another problem! Customers, where are you? Write me in telegrams: dimagorovtsov , waiting for everyone!



All Articles