1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #pragma once
8 
9 #include "td/telegram/DialogId.h"
10 #include "td/telegram/files/FileId.h"
11 #include "td/telegram/FullMessageId.h"
12 #include "td/telegram/MessageId.h"
13 #include "td/telegram/Photo.h"
14 #include "td/telegram/td_api.h"
15 #include "td/telegram/telegram_api.h"
16 
17 #include "td/actor/PromiseFuture.h"
18 
19 #include "td/utils/common.h"
20 #include "td/utils/Slice.h"
21 #include "td/utils/Status.h"
22 #include "td/utils/StringBuilder.h"
23 
24 namespace td {
25 
26 class Td;
27 
28 struct LabeledPricePart {
29   string label;
30   int64 amount = 0;
31 
32   LabeledPricePart() = default;
LabeledPricePartLabeledPricePart33   LabeledPricePart(string &&label, int64 amount) : label(std::move(label)), amount(amount) {
34   }
35 };
36 
37 struct Invoice {
38   string currency;
39   vector<LabeledPricePart> price_parts;
40   int64 max_tip_amount = 0;
41   vector<int64> suggested_tip_amounts;
42   bool is_test = false;
43   bool need_name = false;
44   bool need_phone_number = false;
45   bool need_email_address = false;
46   bool need_shipping_address = false;
47   bool send_phone_number_to_provider = false;
48   bool send_email_address_to_provider = false;
49   bool is_flexible = false;
50 
51   Invoice() = default;
InvoiceInvoice52   Invoice(string &&currency, bool is_test, bool need_shipping_address)
53       : currency(std::move(currency)), is_test(is_test), need_shipping_address(need_shipping_address) {
54   }
55 };
56 
57 struct InputInvoice {
58   string title;
59   string description;
60   Photo photo;
61   string start_parameter;
62   Invoice invoice;
63   string payload;
64   string provider_token;
65   string provider_data;
66 
67   int64 total_amount = 0;
68   MessageId receipt_message_id;
69 };
70 
71 struct Address {
72   string country_code;
73   string state;
74   string city;
75   string street_line1;
76   string street_line2;
77   string postal_code;
78 
79   Address() = default;
AddressAddress80   Address(string &&country_code, string &&state, string &&city, string &&street_line1, string &&street_line2,
81           string &&postal_code)
82       : country_code(std::move(country_code))
83       , state(std::move(state))
84       , city(std::move(city))
85       , street_line1(std::move(street_line1))
86       , street_line2(std::move(street_line2))
87       , postal_code(std::move(postal_code)) {
88   }
89 };
90 
91 struct OrderInfo {
92   string name;
93   string phone_number;
94   string email_address;
95   unique_ptr<Address> shipping_address;
96 
97   OrderInfo() = default;
OrderInfoOrderInfo98   OrderInfo(string &&name, string &&phone_number, string &&email_address, unique_ptr<Address> &&shipping_address)
99       : name(std::move(name))
100       , phone_number(std::move(phone_number))
101       , email_address(std::move(email_address))
102       , shipping_address(std::move(shipping_address)) {
103   }
104 };
105 
106 struct ShippingOption {
107   string id;
108   string title;
109   vector<LabeledPricePart> price_parts;
110 };
111 
112 bool operator==(const LabeledPricePart &lhs, const LabeledPricePart &rhs);
113 bool operator!=(const LabeledPricePart &lhs, const LabeledPricePart &rhs);
114 
115 StringBuilder &operator<<(StringBuilder &string_builder, const LabeledPricePart &labeled_price_part);
116 
117 bool operator==(const Invoice &lhs, const Invoice &rhs);
118 bool operator!=(const Invoice &lhs, const Invoice &rhs);
119 
120 StringBuilder &operator<<(StringBuilder &string_builder, const Invoice &invoice);
121 
122 bool operator==(const InputInvoice &lhs, const InputInvoice &rhs);
123 bool operator!=(const InputInvoice &lhs, const InputInvoice &rhs);
124 
125 InputInvoice get_input_invoice(tl_object_ptr<telegram_api::messageMediaInvoice> &&message_invoice, Td *td,
126                                DialogId owner_dialog_id);
127 
128 InputInvoice get_input_invoice(tl_object_ptr<telegram_api::botInlineMessageMediaInvoice> &&message_invoice, Td *td,
129                                DialogId owner_dialog_id);
130 
131 Result<InputInvoice> process_input_message_invoice(
132     td_api::object_ptr<td_api::InputMessageContent> &&input_message_content, Td *td);
133 
134 tl_object_ptr<td_api::messageInvoice> get_message_invoice_object(const InputInvoice &input_invoice, Td *td);
135 
136 tl_object_ptr<telegram_api::inputMediaInvoice> get_input_media_invoice(const InputInvoice &input_invoice, Td *td);
137 
138 tl_object_ptr<telegram_api::inputBotInlineMessageMediaInvoice> get_input_bot_inline_message_media_invoice(
139     const InputInvoice &input_invoice, tl_object_ptr<telegram_api::ReplyMarkup> &&reply_markup, Td *td);
140 
141 vector<FileId> get_input_invoice_file_ids(const InputInvoice &input_invoice);
142 
143 bool operator==(const Address &lhs, const Address &rhs);
144 bool operator!=(const Address &lhs, const Address &rhs);
145 
146 StringBuilder &operator<<(StringBuilder &string_builder, const Address &address);
147 
148 unique_ptr<Address> get_address(tl_object_ptr<telegram_api::postAddress> &&address);
149 
150 Result<Address> get_address(td_api::object_ptr<td_api::address> &&address);
151 
152 tl_object_ptr<td_api::address> get_address_object(const unique_ptr<Address> &address);
153 
154 tl_object_ptr<td_api::address> get_address_object(const Address &address);
155 
156 string address_to_json(const Address &address);
157 
158 Result<Address> address_from_json(Slice json);
159 
160 Status check_country_code(string &country_code);
161 
162 bool operator==(const OrderInfo &lhs, const OrderInfo &rhs);
163 bool operator!=(const OrderInfo &lhs, const OrderInfo &rhs);
164 
165 StringBuilder &operator<<(StringBuilder &string_builder, const OrderInfo &order_info);
166 
167 unique_ptr<OrderInfo> get_order_info(tl_object_ptr<telegram_api::paymentRequestedInfo> order_info);
168 
169 tl_object_ptr<td_api::orderInfo> get_order_info_object(const unique_ptr<OrderInfo> &order_info);
170 
171 bool operator==(const ShippingOption &lhs, const ShippingOption &rhs);
172 bool operator!=(const ShippingOption &lhs, const ShippingOption &rhs);
173 
174 StringBuilder &operator<<(StringBuilder &string_builder, const ShippingOption &shipping_option);
175 
176 void answer_shipping_query(Td *td, int64 shipping_query_id,
177                            vector<tl_object_ptr<td_api::shippingOption>> &&shipping_options,
178                            const string &error_message, Promise<Unit> &&promise);
179 
180 void answer_pre_checkout_query(Td *td, int64 pre_checkout_query_id, const string &error_message,
181                                Promise<Unit> &&promise);
182 
183 void get_payment_form(Td *td, FullMessageId full_message_id, const td_api::object_ptr<td_api::paymentFormTheme> &theme,
184                       Promise<tl_object_ptr<td_api::paymentForm>> &&promise);
185 
186 void validate_order_info(Td *td, FullMessageId full_message_id, tl_object_ptr<td_api::orderInfo> order_info,
187                          bool allow_save, Promise<tl_object_ptr<td_api::validatedOrderInfo>> &&promise);
188 
189 void send_payment_form(Td *td, FullMessageId full_message_id, int64 payment_form_id, const string &order_info_id,
190                        const string &shipping_option_id, const tl_object_ptr<td_api::InputCredentials> &credentials,
191                        int64 tip_amount, Promise<tl_object_ptr<td_api::paymentResult>> &&promise);
192 
193 void get_payment_receipt(Td *td, FullMessageId full_message_id,
194                          Promise<tl_object_ptr<td_api::paymentReceipt>> &&promise);
195 
196 void get_saved_order_info(Td *td, Promise<tl_object_ptr<td_api::orderInfo>> &&promise);
197 
198 void delete_saved_order_info(Td *td, Promise<Unit> &&promise);
199 
200 void delete_saved_credentials(Td *td, Promise<Unit> &&promise);
201 
202 void get_bank_card_info(Td *td, const string &bank_card_number,
203                         Promise<td_api::object_ptr<td_api::bankCardInfo>> &&promise);
204 
205 }  // namespace td
206