2017-06-10 21:43:38 +02:00
|
|
|
#!/usr/bin/env python
|
2021-03-13 16:21:03 +01:00
|
|
|
# pylint: disable=C0116
|
2019-02-13 13:38:07 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2019-02-13 13:38:07 +01:00
|
|
|
"""
|
|
|
|
Basic example for a bot that can receive payment from user.
|
2017-10-20 20:24:00 +02:00
|
|
|
"""
|
2017-06-10 21:43:38 +02:00
|
|
|
|
2018-09-21 08:57:01 +02:00
|
|
|
import logging
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
from telegram import LabeledPrice, ShippingOption, Update
|
2020-10-09 17:22:07 +02:00
|
|
|
from telegram.ext import (
|
|
|
|
Updater,
|
|
|
|
CommandHandler,
|
|
|
|
MessageHandler,
|
|
|
|
Filters,
|
|
|
|
PreCheckoutQueryHandler,
|
|
|
|
ShippingQueryHandler,
|
2020-10-31 16:33:34 +01:00
|
|
|
CallbackContext,
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def start_callback(update: Update, _: CallbackContext) -> None:
|
2021-03-28 18:53:44 +02:00
|
|
|
msg = (
|
|
|
|
"Use /shipping to get an invoice for shipping-payment, or /noshipping for an "
|
|
|
|
"invoice without shipping."
|
|
|
|
)
|
|
|
|
|
2017-06-10 21:43:38 +02:00
|
|
|
update.message.reply_text(msg)
|
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def start_with_shipping_callback(update: Update, context: CallbackContext) -> None:
|
2017-06-10 21:43:38 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
title = "Payment Example"
|
|
|
|
description = "Payment Example using python-telegram-bot"
|
|
|
|
# select a payload just for you to recognize its the donation from your bot
|
|
|
|
payload = "Custom-Payload"
|
2017-10-20 20:24:00 +02:00
|
|
|
# In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
|
2017-06-10 21:43:38 +02:00
|
|
|
provider_token = "PROVIDER_TOKEN"
|
|
|
|
start_parameter = "test-payment"
|
|
|
|
currency = "USD"
|
|
|
|
# price in dollars
|
|
|
|
price = 1
|
2019-10-27 00:15:09 +02:00
|
|
|
# price * 100 so as to include 2 decimal points
|
2017-06-10 22:30:21 +02:00
|
|
|
# check https://core.telegram.org/bots/payments#supported-currencies for more details
|
2017-06-10 21:43:38 +02:00
|
|
|
prices = [LabeledPrice("Test", price * 100)]
|
|
|
|
|
|
|
|
# optionally pass need_name=True, need_phone_number=True,
|
|
|
|
# need_email=True, need_shipping_address=True, is_flexible=True
|
2020-10-09 17:22:07 +02:00
|
|
|
context.bot.send_invoice(
|
|
|
|
chat_id,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
payload,
|
|
|
|
provider_token,
|
|
|
|
start_parameter,
|
|
|
|
currency,
|
|
|
|
prices,
|
|
|
|
need_name=True,
|
|
|
|
need_phone_number=True,
|
|
|
|
need_email=True,
|
|
|
|
need_shipping_address=True,
|
|
|
|
is_flexible=True,
|
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
def start_without_shipping_callback(update: Update, context: CallbackContext) -> None:
|
2017-06-10 21:43:38 +02:00
|
|
|
chat_id = update.message.chat_id
|
|
|
|
title = "Payment Example"
|
|
|
|
description = "Payment Example using python-telegram-bot"
|
|
|
|
# select a payload just for you to recognize its the donation from your bot
|
|
|
|
payload = "Custom-Payload"
|
2017-10-20 20:24:00 +02:00
|
|
|
# In order to get a provider_token see https://core.telegram.org/bots/payments#getting-a-token
|
2017-06-10 21:43:38 +02:00
|
|
|
provider_token = "PROVIDER_TOKEN"
|
|
|
|
start_parameter = "test-payment"
|
|
|
|
currency = "USD"
|
|
|
|
# price in dollars
|
|
|
|
price = 1
|
2019-10-27 00:15:09 +02:00
|
|
|
# price * 100 so as to include 2 decimal points
|
2017-06-10 21:43:38 +02:00
|
|
|
prices = [LabeledPrice("Test", price * 100)]
|
|
|
|
|
|
|
|
# optionally pass need_name=True, need_phone_number=True,
|
|
|
|
# need_email=True, need_shipping_address=True, is_flexible=True
|
2020-10-09 17:22:07 +02:00
|
|
|
context.bot.send_invoice(
|
|
|
|
chat_id, title, description, payload, provider_token, start_parameter, currency, prices
|
|
|
|
)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def shipping_callback(update: Update, _: CallbackContext) -> None:
|
2017-06-10 21:43:38 +02:00
|
|
|
query = update.shipping_query
|
|
|
|
# check the payload, is this from your bot?
|
|
|
|
if query.invoice_payload != 'Custom-Payload':
|
|
|
|
# answer False pre_checkout_query
|
2018-09-21 08:57:01 +02:00
|
|
|
query.answer(ok=False, error_message="Something went wrong...")
|
2017-06-10 21:43:38 +02:00
|
|
|
return
|
2020-10-31 16:33:34 +01:00
|
|
|
|
|
|
|
options = list()
|
|
|
|
# a single LabeledPrice
|
|
|
|
options.append(ShippingOption('1', 'Shipping Option A', [LabeledPrice('A', 100)]))
|
|
|
|
# an array of LabeledPrice objects
|
|
|
|
price_list = [LabeledPrice('B1', 150), LabeledPrice('B2', 200)]
|
|
|
|
options.append(ShippingOption('2', 'Shipping Option B', price_list))
|
|
|
|
query.answer(ok=True, shipping_options=options)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
# after (optional) shipping, it's the pre-checkout
|
2021-03-13 16:21:03 +01:00
|
|
|
def precheckout_callback(update: Update, _: CallbackContext) -> None:
|
2017-06-10 21:43:38 +02:00
|
|
|
query = update.pre_checkout_query
|
|
|
|
# check the payload, is this from your bot?
|
|
|
|
if query.invoice_payload != 'Custom-Payload':
|
|
|
|
# answer False pre_checkout_query
|
2018-09-21 08:57:01 +02:00
|
|
|
query.answer(ok=False, error_message="Something went wrong...")
|
2017-06-10 21:43:38 +02:00
|
|
|
else:
|
2018-09-21 08:57:01 +02:00
|
|
|
query.answer(ok=True)
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# finally, after contacting the payment provider...
|
2021-03-13 16:21:03 +01:00
|
|
|
def successful_payment_callback(update: Update, _: CallbackContext) -> None:
|
2019-10-11 20:10:21 +02:00
|
|
|
# do something after successfully receiving payment?
|
2017-06-10 21:43:38 +02:00
|
|
|
update.message.reply_text("Thank you for your payment!")
|
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2018-09-21 08:57:01 +02:00
|
|
|
# Create the Updater and pass it your bot's token.
|
2021-02-01 17:59:39 +01:00
|
|
|
updater = Updater("TOKEN")
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Get the dispatcher to register handlers
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher = updater.dispatcher
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# simple start function
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(CommandHandler("start", start_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Add command handler to start the payment invoice
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(CommandHandler("shipping", start_with_shipping_callback))
|
|
|
|
dispatcher.add_handler(CommandHandler("noshipping", start_without_shipping_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Optional handler if your product requires shipping
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(ShippingQueryHandler(shipping_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Pre-checkout handler to final check
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Success! Notify your user!
|
2020-10-31 16:33:34 +01:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.successful_payment, successful_payment_callback))
|
2017-06-10 21:43:38 +02:00
|
|
|
|
|
|
|
# Start the Bot
|
|
|
|
updater.start_polling()
|
|
|
|
|
|
|
|
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
|
|
|
# SIGTERM or SIGABRT. This should be used most of the time, since
|
|
|
|
# start_polling() is non-blocking and will stop the bot gracefully.
|
|
|
|
updater.idle()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|