Skip to content

Payment Manager

Abdi Urgessa edited this page Feb 18, 2024 · 4 revisions

The PaymentManager class offers a suite of methods dedicated to managing payment interactions in a Telegram bot, enabling the bot to send invoices, generate invoice links, and respond to shipping and pre-checkout queries directly within the chat interface.

Usage Examples

Below are detailed usage examples for each method provided by the PaymentManager class.

1. Sending an Invoice:

This method is used to send a detailed invoice to a user.

bot.on("message", (ctx) => {
  ctx.sendInvoice({
    chat_id: ctx.chat().id,
    title: 'Product Name',
    description: 'A brief description of the product',
    payload: 'Unique_Payload',
    provider_token: 'PROVIDER_TOKEN',
    start_parameter: 'Start_Parameter',
    currency: 'USD',
    prices: [{ label: 'Product', amount: 10000 }] // Price in the smallest units of the currency (e.g., cents for USD)
  });
});

2. Creating an Invoice Link:

Generates a link to an invoice for payment outside of the direct chat interface.

bot.on("message", (ctx) => {
  const invoiceLink = ctx.createInvoiceLink({
    title: 'Product Name',
    description: 'A brief description of the product',
    payload: 'Unique_Payload',
    provider_token: 'PROVIDER_TOKEN',
    start_parameter: 'Start_Parameter',
    currency: 'USD',
    prices: [{ label: 'Product', amount: 10000 }]
  });
  ctx.reply(`Please pay using this link: ${invoiceLink}`);
});

3. Answering Shipping Queries:

Responds to queries about shipping options for a flexible shipping address.

// Assuming you have a function to handle shipping queries
function handleShippingQuery(ctx) {
  const shippingOptions = [
    {
      id: '1',
      title: 'Next Day Delivery',
      prices: [{ label: 'Delivery', amount: 2000 }]
    },
    {
      id: '2',
      title: 'Standard Delivery',
      prices: [{ label: 'Delivery', amount: 1000 }]
    }
  ];

  ctx.answerShippingQuery({
    ok:true, 
    shipping_options:shippingOptions, 
    shipping_query_id:'<shipping_query_id>'
  });
}

4. Answering Pre-Checkout Queries:

Confirms the final details before processing the payment.

// Assuming you have a function to handle pre-checkout queries
function handlePreCheckoutQuery(ctx) {
  const isEverythingOk = true; // Perform any necessary checks here

  if (isEverythingOk) {
    ctx.answerPreCheckoutQuery({ok:true, pre_checkout_query_id:'<pre_checkout_query_id>'});
  } else {
    ctx.answerPreCheckoutQuery({
     ok:false,
     pre_checkout_query_id:'<pre_checkout_query_id>',
     error_message:"There was a problem with your payment."});
  }
}

Method Overview

  • sendInvoice(objParam): Sends a detailed invoice to a user. Parameters include product title, description, payload, provider token, currency, and pricing details.
  • createInvoiceLink(objParam): Generates a shareable link to an invoice for payment.
  • answerShippingQuery(isSuccessful, shippingOptions): Responds to shipping queries with available shipping options.
  • answerPreCheckoutQuery(isSuccessful, errorMessage): Confirms or denies the completion of a payment transaction.
Clone this wiki locally