Payments

import { Keyboard } from 'degreet-telegram'
import { Payment } from 'degreet-telegram/src/creators'
import { IContext } from 'degreet-telegram/src/types'

bot.command('start', async (ctx: IContext): Promise<any> => {
  try {
    await ctx.answer.send(
      new Payment()
        .setTitle('Title') // required
        .setDescription('Description') // required
        .setToken('Payment token from botFather') // required
        .setCurrency('USD') // required
        .addItem('First item', 20) // required
        .addItem('Second item', 25)
        .addDiscount('Discount', 10)
        .allowTips(5, [1, 3, 5]) // 1 - max tip amount, 2 - suggested tip amounts
        .needUserData(['email']) // email, phone_number, name or shipping_query
        .setPhotoUrl(IMAGE_URL), // you can add photo also, but url only
      new Keyboard('under_the_message') // optional, if you didn't add keyboard, it will be added automatically with  pay button
        .btn('pay', ctx.i18n?.get('pay_btn')!).row()
        .useLayout('go_menu_btn')
    )
  } catch (e: any) {
    console.error(e)
  }
})

bot.on('payment_answer', async (ctx: IContext): Promise<any> => {
  try {
    await ctx.answer.payment(true) // 1 - isSuccess, 2 - error message
  } catch (e: any) {
    console.error(e)
  }
})

bot.on('successful_payment', async (ctx: IContext): Promise<any> => {
  try {
    await ctx.answer.send('Thank you for payment!')
  } catch (e: any) {
    console.error(e)
  }
})

Last updated