Skip to content

Commit

Permalink
added createInvoiceLink payments api method
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Dec 27, 2023
1 parent 2a06f86 commit 42b715b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
50 changes: 50 additions & 0 deletions include/tgbotxx/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,56 @@ namespace tgbotxx {
bool allowSendingWithoutReply = false,
const Ptr<IReplyMarkup>& replyMarkup = nullptr) const;

/// @brief Use this method to create a link for an invoice. Returns the created invoice link as std::string on success.
/// @param title Product name, 1-32 characters
/// @param description Product description, 1-255 characters
/// @param payload Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, use for your internal processes.
/// @param providerToken Payment provider token, obtained via @BotFather
/// @param currency Three-letter ISO 4217 currency code, [see more on currencies](https://core.telegram.org/bots/payments#supported-currencies)
/// @param prices Array of LabeledPrice, Price breakdown, a JSON-serialized list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.)
/// @param maxTipAmount Optional. The maximum accepted amount for tips in the smallest units of the currency (integer, not float/double).
/// For example, for a maximum tip of US$ 1.45 pass max_tip_amount = 145. See the exp parameter in currencies.json,
/// it shows the number of digits past the decimal point for each currency (2 for the majority of currencies). Defaults to 0
/// @param suggestedTipAmounts Optional. A JSON-serialized array of suggested amounts of tips in the smallest units of the currency (integer, not float/double).
/// At most 4 suggested tip amounts can be specified. The suggested tip amounts must be positive, passed in a strictly increased order and must not exceed maxTipAmount.
/// @param startParameter Optional. Unique deep-linking parameter. If left empty, forwarded copies of the sent message will have a Pay button, allowing multiple users to pay directly from the forwarded message,
/// using the same invoice. If non-empty, forwarded copies of the sent message will have a URL button with a deep link to the bot (instead of a Pay button), with the value used as the start parameter
/// @param providerData Optional. JSON-serialized data about the invoice, which will be shared with the payment provider. A detailed description of required fields should be provided by the payment provider.
/// @param photoUrl Optional. URL of the product photo for the invoice. Can be a photo of the goods or a marketing image for a service. People like it better when they see what they are paying for.
/// @param photoSize Optional. Photo size in bytes
/// @param photoWidth Optional. Photo width
/// @param photoHeight Optional. Photo height
/// @param needName Optional. Pass True if you require the user's full name to complete the order
/// @param needPhoneNumber Optional. Pass True if you require the user's phone number to complete the order
/// @param needEmail Optional. Pass True if you require the user's email address to complete the order
/// @param needShippingAddress Optional. Pass True if you require the user's shipping address to complete the order
/// @param sendPhoneNumberToProvider Optional. Pass True if the user's phone number should be sent to provider
/// @param sendEmailToProvider Optional. Pass True if the user's email address should be sent to provider
/// @param isFlexible Optional. Pass True if the final price depends on the shipping method
/// @returns the created invoice link as std::string on success.
/// @throws Exception on failure
/// @ref https://core.telegram.org/bots/api#createinvoicelink
std::string createInvoiceLink(const std::string& title,
const std::string& description,
const std::string& payload,
const std::string& providerToken,
const std::string& currency,
const std::vector<Ptr<LabeledPrice>>& prices,
std::int32_t maxTipAmount = 0,
const std::vector<std::int32_t>& suggestedTipAmounts = std::vector<std::int32_t>(),
const std::string& providerData = "",
const std::string& photoUrl = "",
std::int32_t photoSize = 0,
std::int32_t photoWidth = 0,
std::int32_t photoHeight = 0,
bool needName = false,
bool needPhoneNumber = false,
bool needEmail = false,
bool needShippingAddress = false,
bool sendPhoneNumberToProvider = false,
bool sendEmailToProvider = false,
bool isFlexible = false) const;


public: /// Updates methods https://core.telegram.org/bots/api#getting-updates
/// @brief Use this method to receive incoming updates using long polling.
Expand Down
65 changes: 65 additions & 0 deletions src/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2177,3 +2177,68 @@ Ptr<Message> Api::sendInvoice(const std::variant<std::int64_t, std::string>& cha
Ptr<Message> message(new Message(sentMessageObj));
return message;
}


std::string Api::createInvoiceLink(const std::string& title,
const std::string& description,
const std::string& payload,
const std::string& providerToken,
const std::string& currency,
const std::vector<Ptr<LabeledPrice>>& prices,
std::int32_t maxTipAmount,
const std::vector<std::int32_t>& suggestedTipAmounts,
const std::string& providerData,
const std::string& photoUrl,
std::int32_t photoSize,
std::int32_t photoWidth,
std::int32_t photoHeight,
bool needName,
bool needPhoneNumber,
bool needEmail,
bool needShippingAddress,
bool sendPhoneNumberToProvider,
bool sendEmailToProvider,
bool isFlexible) const {

cpr::Multipart data{};
data.parts.reserve(20);
data.parts.emplace_back("title", title);
data.parts.emplace_back("description", description);
data.parts.emplace_back("payload", payload);
data.parts.emplace_back("provider_token", providerToken);
data.parts.emplace_back("currency", currency);
nl::json pricesJson = nl::json::array();
for (const Ptr<LabeledPrice>& price: prices)
pricesJson.push_back(price->toJson());
data.parts.emplace_back("prices", pricesJson.dump());
if (maxTipAmount)
data.parts.emplace_back("max_tip_amount", maxTipAmount);
if (not suggestedTipAmounts.empty())
data.parts.emplace_back("suggested_tip_amounts", nl::json(suggestedTipAmounts).dump());
if (not providerData.empty())
data.parts.emplace_back("provider_data", providerData);
if (not photoUrl.empty())
data.parts.emplace_back("photo_url", photoUrl);
if (photoSize)
data.parts.emplace_back("photo_size", photoSize);
if (photoWidth)
data.parts.emplace_back("photo_width", photoWidth);
if (photoHeight)
data.parts.emplace_back("photo_height", photoHeight);
if (needName)
data.parts.emplace_back("need_name", needName);
if (needPhoneNumber)
data.parts.emplace_back("need_phone_number", needPhoneNumber);
if (needEmail)
data.parts.emplace_back("need_email", needEmail);
if (needShippingAddress)
data.parts.emplace_back("need_shipping_address", needShippingAddress);
if (sendPhoneNumberToProvider)
data.parts.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider);
if (sendEmailToProvider)
data.parts.emplace_back("send_email_to_provider", sendEmailToProvider);
if (isFlexible)
data.parts.emplace_back("is_flexible", isFlexible);

return sendRequest("createInvoiceLink", data);
}
20 changes: 19 additions & 1 deletion tests/manual_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ class MyBot : public Bot {
Ptr<BotCommand> sendInvoice(new BotCommand());
sendInvoice->command = "/send_invoice";
sendInvoice->description = "You will receive a test invoice";
Ptr<BotCommand> createInvoiceLink(new BotCommand());
createInvoiceLink->command = "/create_invoice_link";
createInvoiceLink->description = "You will receive a test invoice link";
getApi()->setMyCommands({greet, stop, photo, buttons, audio, document, animation, voice, mediaGroup,
location, userProfilePhotos, ban, poll, quiz, webhookInfo, botName,
menuButtonWebApp, menuButtonDefault, showAdministratorRights, editMessageText,
deleteMessage, sendInvoice}); // The above commands will be shown in the bot chat menu (bottom left)
deleteMessage, sendInvoice, createInvoiceLink}); // The above commands will be shown in the bot chat menu (bottom left)

std::cout << __func__ << ": " << api()->getMyName()->name << " bot started!" << std::endl;
}
Expand Down Expand Up @@ -300,6 +303,21 @@ class MyBot : public Bot {
discountPrice->amount = -50; // -0.5$
prices.push_back(discountPrice);
api()->sendInvoice(message->chat->id, "Product name", "Product description", "payload", providerToken, "USD", prices);
} else if (message->text == "/create_invoice_link") {
// Create invoice link
const std::string providerToken = getPaymentProviderToken();
std::vector<Ptr<LabeledPrice>> prices;
Ptr<LabeledPrice> originalPrice = std::make_shared<LabeledPrice>();
originalPrice->label = "Original price";
originalPrice->amount = 150; // 1.50$
prices.push_back(originalPrice);
Ptr<LabeledPrice> discountPrice = std::make_shared<LabeledPrice>();
discountPrice->label = "Discount";
discountPrice->amount = -50; // -0.5$
prices.push_back(discountPrice);
std::string link = api()->createInvoiceLink("Product name", "Product description", "payload", providerToken, "USD", prices);
// Send link to user
api()->sendMessage(message->chat->id, link);
}
}

Expand Down

0 comments on commit 42b715b

Please sign in to comment.