Skip to content

Payments

Chris Watson edited this page Sep 7, 2019 · 1 revision

Intro

A fairly recent addition to Telegram is payment handling. To get started with payments be sure to follow the instructions here, then continue on with setting it up in your bot.

Invoices

The Payments API gives you the ability to send invoices to people through Telegram, and then accept payment for the invoice via Stripe, Rave, Yandex, and several other payment processors. After you have everything set up, sending an invoice is easy:

@[Command("buy")]
def buy_command(message, params)
  send_invoice(
    chat_id: message.chat.id,
    title: "Sample Invoice",
    description: "This is a test...",
    payload: "123344232323",
    provider_token: "YOUR_PROVIDER_TOKEN",
    start_parameter: "test1",
    currency: "USD",
    prices: labeled_prices(
      [
        {label: "Sample", amount: 299},
        {label: "Another", amount: 369}
      ]
    )
  )
end

Answering Shipping Queries

If you sent an invoice requesting a shipping address and the parameter is_flexible was specified, the Bot API will send an Update with a shipping_query field to the bot. You can listen for these by using the :shipping_query event listener.

@[On(:shipping_query)]
def on_shipping_query(update)
  if query = update.shipping_query
    # create shipping options
 
    answer_shipping_query(query.id, shipping_options)
  end
end

Answering Pre-checkout Query

Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field pre_checkout_query. Use the answer_pre_checkout_query to respond.

Note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.

@[On(:pre_checkout_query)]
def on_pre_checkout_query(update)
  if query = update.pre_checkout_query
    # check for errors in the order
    # if everything is ok:
 
    answer_pre_checkout_query(query.id, true)
    # otherwise
    answer_pre_checkout_query(query.id, false, "Error message")
  end
end