Payments

payments.py
 1from aiogram import Bot
 2from aiogram import types
 3from aiogram.dispatcher import Dispatcher
 4from aiogram.types.message import ContentTypes
 5from aiogram.utils import executor
 6
 7
 8BOT_TOKEN = 'BOT_TOKEN_HERE'
 9PAYMENTS_PROVIDER_TOKEN = '123456789:TEST:1422'
10
11bot = Bot(BOT_TOKEN)
12dp = Dispatcher(bot)
13
14# Setup prices
15prices = [
16    types.LabeledPrice(label='Working Time Machine', amount=5750),
17    types.LabeledPrice(label='Gift wrapping', amount=500),
18]
19
20# Setup shipping options
21shipping_options = [
22    types.ShippingOption(id='instant', title='WorldWide Teleporter').add(types.LabeledPrice('Teleporter', 1000)),
23    types.ShippingOption(id='pickup', title='Local pickup').add(types.LabeledPrice('Pickup', 300)),
24]
25
26
27@dp.message_handler(commands=['start'])
28async def cmd_start(message: types.Message):
29    await bot.send_message(message.chat.id,
30                           "Hello, I'm the demo merchant bot."
31                           " I can sell you a Time Machine."
32                           " Use /buy to order one, /terms for Terms and Conditions")
33
34
35@dp.message_handler(commands=['terms'])
36async def cmd_terms(message: types.Message):
37    await bot.send_message(message.chat.id,
38                           'Thank you for shopping with our demo bot. We hope you like your new time machine!\n'
39                           '1. If your time machine was not delivered on time, please rethink your concept of time'
40                           ' and try again.\n'
41                           '2. If you find that your time machine is not working, kindly contact our future service'
42                           ' workshops on Trappist-1e. They will be accessible anywhere between'
43                           ' May 2075 and November 4000 C.E.\n'
44                           '3. If you would like a refund, kindly apply for one yesterday and we will have sent it'
45                           ' to you immediately.')
46
47
48@dp.message_handler(commands=['buy'])
49async def cmd_buy(message: types.Message):
50    await bot.send_message(message.chat.id,
51                           "Real cards won't work with me, no money will be debited from your account."
52                           " Use this test card number to pay for your Time Machine: `4242 4242 4242 4242`"
53                           "\n\nThis is your demo invoice:", parse_mode='Markdown')
54    await bot.send_invoice(message.chat.id, title='Working Time Machine',
55                           description='Want to visit your great-great-great-grandparents?'
56                                       ' Make a fortune at the races?'
57                                       ' Shake hands with Hammurabi and take a stroll in the Hanging Gardens?'
58                                       ' Order our Working Time Machine today!',
59                           provider_token=PAYMENTS_PROVIDER_TOKEN,
60                           currency='usd',
61                           photo_url='https://telegra.ph/file/d08ff863531f10bf2ea4b.jpg',
62                           photo_height=512,  # !=0/None or picture won't be shown
63                           photo_width=512,
64                           photo_size=512,
65                           is_flexible=True,  # True If you need to set up Shipping Fee
66                           prices=prices,
67                           start_parameter='time-machine-example',
68                           payload='HAPPY FRIDAYS COUPON')
69
70
71@dp.shipping_query_handler(lambda query: True)
72async def shipping(shipping_query: types.ShippingQuery):
73    await bot.answer_shipping_query(shipping_query.id, ok=True, shipping_options=shipping_options,
74                                    error_message='Oh, seems like our Dog couriers are having a lunch right now.'
75                                                  ' Try again later!')
76
77
78@dp.pre_checkout_query_handler(lambda query: True)
79async def checkout(pre_checkout_query: types.PreCheckoutQuery):
80    await bot.answer_pre_checkout_query(pre_checkout_query.id, ok=True,
81                                        error_message="Aliens tried to steal your card's CVV,"
82                                                      " but we successfully protected your credentials,"
83                                                      " try to pay again in a few minutes, we need a small rest.")
84
85
86@dp.message_handler(content_types=ContentTypes.SUCCESSFUL_PAYMENT)
87async def got_payment(message: types.Message):
88    await bot.send_message(message.chat.id,
89                           'Hoooooray! Thanks for payment! We will proceed your order for `{} {}`'
90                           ' as fast as possible! Stay in touch.'
91                           '\n\nUse /buy again to get a Time Machine for your friend!'.format(
92                               message.successful_payment.total_amount / 100, message.successful_payment.currency),
93                           parse_mode='Markdown')
94
95
96if __name__ == '__main__':
97    executor.start_polling(dp, skip_updates=True)