TelegramBots/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/api/methods/send/SendInvoice.java

193 lines
9.1 KiB
Java

package org.telegram.telegrambots.meta.api.methods.send;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.payments.LabeledPrice;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.io.IOException;
import java.util.List;
/**
* @author Ruben Bermudez
* @version 1.0
* Use this method to send an invoice. On success, the sent Message is returned.
*/
@EqualsAndHashCode(callSuper = false)
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class SendInvoice extends BotApiMethod<Message> {
public static final String PATH = "sendinvoice";
private static final String CHATID_FIELD = "chat_id";
private static final String TITLE_FIELD = "title";
private static final String DESCRIPTION_FIELD = "description";
private static final String PAYLOAD_FIELD = "payload";
private static final String PROVIDER_TOKEN_FIELD = "provider_token";
private static final String START_PARAMETER_FIELD = "start_parameter";
private static final String CURRENCY_FIELD = "currency";
private static final String PRICES_FIELD = "prices";
private static final String PHOTO_URL_FIELD = "photo_url";
private static final String PHOTO_SIZE_FIELD = "photo_size";
private static final String PHOTO_WIDTH_FIELD = "photo_width";
private static final String PHOTO_HEIGHT_FIELD = "photo_height";
private static final String NEED_NAME_FIELD = "need_name";
private static final String NEED_PHONE_NUMBER_FIELD = "need_phone_number";
private static final String NEED_EMAIL_FIELD = "need_email";
private static final String NEED_SHIPPING_ADDRESS_FIELD = "need_shipping_address";
private static final String SEND_PHONE_NUMBER_TO_PROVIDER_FIELD = "send_phone_number_to_provider";
private static final String SEND_EMAIL_TO_PROVIDER_FIELD = "send_email_to_provider";
private static final String IS_FLEXIBLE_FIELD = "is_flexible";
private static final String DISABLE_NOTIFICATION_FIELD = "disable_notification";
private static final String REPLY_TO_MESSAGE_ID_FIELD = "reply_to_message_id";
private static final String REPLY_MARKUP_FIELD = "reply_markup";
private static final String PROVIDER_DATA_FIELD = "provider_data";
private static final String ALLOWSENDINGWITHOUTREPLY_FIELD = "allow_sending_without_reply";
@JsonProperty(CHATID_FIELD)
@NonNull
private Integer chatId; ///< Unique identifier for the target private chat
@JsonProperty(TITLE_FIELD)
@NonNull
private String title; ///< Product name
@JsonProperty(DESCRIPTION_FIELD)
@NonNull
private String description; ///< Product description
@JsonProperty(PAYLOAD_FIELD)
@NonNull
private String payload; ///< Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, use for your internal processes.
@JsonProperty(PROVIDER_TOKEN_FIELD)
@NonNull
private String providerToken; ///< Payments provider token, obtained via Botfather
@JsonProperty(START_PARAMETER_FIELD)
@NonNull
private String startParameter; ///< Unique deep-linking parameter that can be used to generate this invoice when used as a start parameter.
@JsonProperty(CURRENCY_FIELD)
@NonNull
private String currency; ///< 3-letter ISO 4217 currency code
@JsonProperty(PRICES_FIELD)
@NonNull
private List<LabeledPrice> prices; ///< Price breakdown, a list of components (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.)
/**
* 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
*/
@JsonProperty(PHOTO_URL_FIELD)
private String photoUrl;
@JsonProperty(PHOTO_SIZE_FIELD)
private Integer photoSize; ///< Optional. Photo size
@JsonProperty(PHOTO_WIDTH_FIELD)
private Integer photoWidth; ///< Optional. Photo width
@JsonProperty(PHOTO_HEIGHT_FIELD)
private Integer photoHeight; ///< Optional. Photo height
@JsonProperty(NEED_NAME_FIELD)
private Boolean needName; ///< Optional. Pass True, if you require the user's full name to complete the order
@JsonProperty(NEED_PHONE_NUMBER_FIELD)
private Boolean needPhoneNumber; ///< Optional. Pass True, if you require the user's phone number to complete the order
@JsonProperty(NEED_EMAIL_FIELD)
private Boolean needEmail; ///< Optional. Pass True, if you require the user's email to complete the order
@JsonProperty(NEED_SHIPPING_ADDRESS_FIELD)
private Boolean needShippingAddress; ///< Optional. Pass True, if you require the user's shipping address to complete the order
@JsonProperty(IS_FLEXIBLE_FIELD)
private Boolean isFlexible; ///< Optional. Pass True, if the final price depends on the shipping method
@JsonProperty(DISABLE_NOTIFICATION_FIELD)
private Boolean disableNotification; ///< Optional. Sends the message silently. Users will receive a notification with no sound.
@JsonProperty(REPLY_TO_MESSAGE_ID_FIELD)
private Integer replyToMessageId; ///< Optional. If the message is a reply, ID of the original message
@JsonProperty(SEND_PHONE_NUMBER_TO_PROVIDER_FIELD)
private Boolean sendPhoneNumberToProvider; ///< Optional. Pass True, if user's phone number should be sent to provider
@JsonProperty(SEND_EMAIL_TO_PROVIDER_FIELD)
private Boolean sendEmailToProvider; ///< Optional. Pass True, if user's email address should be sent to provider
/**
* Optional. A JSON-serialized object for an inline keyboard.
*
* @apiNote If empty, one 'Buy title' button will be shown. If not empty, the first button must be a Pay button.
*/
@JsonProperty(REPLY_MARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup;
/**
* Optional JSON-encoded data about the invoice, which will be shared with the payment provider.
*
* @apiNote A detailed description of required fields should be provided by the payment provider.
*/
@JsonProperty(PROVIDER_DATA_FIELD)
private String providerData;
@JsonProperty(ALLOWSENDINGWITHOUTREPLY_FIELD)
private Boolean allowSendingWithoutReply; ///< Optional Pass True, if the message should be sent even if the specified replied-to message is not found
@Override
public String getMethod() {
return PATH;
}
@Override
public Message deserializeResponse(String answer) throws TelegramApiRequestException {
try {
ApiResponse<Message> result = OBJECT_MAPPER.readValue(answer,
new TypeReference<ApiResponse<Message>>(){});
if (result.getOk()) {
return result.getResult();
} else {
throw new TelegramApiRequestException("Error sending invoice", result);
}
} catch (IOException e) {
throw new TelegramApiRequestException("Unable to deserialize response", e);
}
}
@Override
public void validate() throws TelegramApiValidationException {
if (chatId == null) {
throw new TelegramApiValidationException("ChatId parameter can't be empty", this);
}
if (title == null || title.isEmpty()) {
throw new TelegramApiValidationException("Title parameter can't be empty", this);
}
if (description == null || description.isEmpty()) {
throw new TelegramApiValidationException("Description parameter can't be empty", this);
}
if (payload == null || payload.isEmpty()) {
throw new TelegramApiValidationException("Payload parameter can't be empty", this);
}
if (providerToken == null || providerToken.isEmpty()) {
throw new TelegramApiValidationException("ProviderToken parameter can't be empty", this);
}
if (startParameter == null || startParameter.isEmpty()) {
throw new TelegramApiValidationException("StartParameter parameter can't be empty", this);
}
if (currency == null || currency.isEmpty()) {
throw new TelegramApiValidationException("Currency parameter can't be empty", this);
}
if (prices == null || prices.isEmpty()) {
throw new TelegramApiValidationException("Prices parameter can't be empty", this);
} else {
for (LabeledPrice price : prices) {
price.validate();
}
}
if (replyMarkup != null) {
replyMarkup.validate();
}
}
}