|
from configparser import ConfigParser |
|
|
|
from pyrogram import Client, filters |
|
from pyrogram.types import Message, Update |
|
from pyrogram.raw.functions.messages import SendMedia, SetBotPrecheckoutResults |
|
from pyrogram.raw.types import ( |
|
DataJSON, |
|
InputMediaInvoice, |
|
Invoice, |
|
LabeledPrice, |
|
MessageActionPaymentSentMe, |
|
UpdateBotPrecheckoutQuery, |
|
UpdateNewMessage, |
|
) |
|
|
|
app = Client(session_name="paybot", config_file="paybot.ini") |
|
|
|
# See PyroPay.md for information. |
|
cfg = ConfigParser() |
|
cfg.read("paybot.ini") |
|
STRIPE_TOKEN = cfg.get("paybot", "stripe_token") |
|
|
|
START = ( |
|
"__**Welcome to Payments with @Pyrogram!**__\n\n" |
|
"To test payments just send /pay (or tap the command).\n\n" |
|
"Please note that this bot issues invoices in test mode only.\n" |
|
"**NO MONEY WILL BE CHARGED!**\n\n" |
|
"For the credit card, just use `4242 4242 4242 4242` with any " |
|
"future expiration date and any 3 digits for CVC." |
|
) |
|
|
|
|
|
@app.on_message(filters.command("start") & filters.private) |
|
async def start(app: Client, message: Message): |
|
await message.reply_text(START) |
|
|
|
|
|
@app.on_message(filters.command("pay") & filters.private) |
|
async def payment(app: Client, message: Message): |
|
peer = await app.resolve_peer(message.from_user.id) |
|
await app.send( |
|
SendMedia( |
|
peer=peer, |
|
media=InputMediaInvoice( |
|
title="Test Invoice", |
|
description="Description of a Payment", |
|
invoice=Invoice( |
|
currency="EUR", |
|
# prices needs to be a list, even for a single item |
|
prices=[LabeledPrice(label="Test Item", amount=1337)], |
|
test=True, |
|
), |
|
payload=b"payment", |
|
provider=STRIPE_TOKEN, |
|
provider_data=DataJSON(data=r"{}"), |
|
start_param="pay", |
|
), |
|
message="", |
|
random_id=app.rnd_id(), |
|
) |
|
) |
|
|
|
|
|
@app.on_raw_update() |
|
async def raw_update(app: Client, update: Update, users: dict, chats: dict): |
|
if isinstance(update, UpdateBotPrecheckoutQuery): |
|
# This is to tell Telegram that everything is okay with this order. |
|
await app.send(SetBotPrecheckoutResults(query_id=update.query_id, success=True)) |
|
|
|
if ( |
|
isinstance(update, UpdateNewMessage) |
|
and hasattr(update.message, "action") |
|
and isinstance(update.message.action, MessageActionPaymentSentMe) |
|
): |
|
# Sending a message confirming the order (additional to TGs service message) |
|
user = users[update.message.from_id] |
|
await app.send_message(user.id, 'Order "confirmed".') |
|
print(user.id, user.first_name, "order confirmed") |
|
|
|
|
|
app.run() |
update.message.from_iddoesn't seem to be sent anymore. Please update toupdate.message.peer_id.user_id