Skip to content

Instantly share code, notes, and snippets.

@ColinShark
Last active May 18, 2022 05:58
Show Gist options
  • Save ColinShark/30fc5f1263ec7190518935c3f0acc600 to your computer and use it in GitHub Desktop.
Save ColinShark/30fc5f1263ec7190518935c3f0acc600 to your computer and use it in GitHub Desktop.
Simple demonstration of Payments with Pyrogram

Payments with Pyrogram

[pyrogram]
api_id = 123456
api_hash = 123abc456def789ghi
bot_token = 123456:abcdefg

[paybot]
stripe_token = 123456:TEST:somerandomsecret

You can get the required api_id and _hash from Telegram. The bot_token needs to be retrieved from @BotFather.

Set up Payments with Stripe through aforementioned BotFather. /mybots -> Your desired Bot -> Bot Settings -> Payments -> Stripe. Choose Test (if you want to test) or Live (if you got a fleshed-out product). BotFather will guide you through Stripe's bots and set everything up.

Once everything is set up, you should be good to go. Run the script with python PyroBot.py (or whatever you renamed it to).

Update 13. Sep. 2020: This should be updated for Pyrogram v1. Since I edited this Gist via the Webinterface, I have no IDE to correctly typehint me lol.

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()
@ColinShark
Copy link
Author

Please update

I may or may not do so at some point. I have no need for this at the moment. Considering I've last updated this gist in Sep'20 this isn't recent anymore anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment