Merge pull request #96 from MrYadro/payments

Payments API
bot-api-6.1
Syfaro 2017-06-03 02:08:51 -05:00 committed by GitHub
commit c341d9cd91
5 changed files with 246 additions and 35 deletions

36
bot.go
View File

@ -704,3 +704,39 @@ func (bot *BotAPI) GetGameHighScores(config GetGameHighScoresConfig) ([]GameHigh
return highScores, err return highScores, err
} }
// AnswerShippingQuery allows you to reply to Update with shipping_query parameter.
func (bot *BotAPI) AnswerShippingQuery(config ShippingConfig) (APIResponse, error) {
v := url.Values{}
v.Add("shipping_query_id", config.ShippingQueryID)
v.Add("ok", strconv.FormatBool(config.OK))
if config.OK == true {
data, err := json.Marshal(config.ShippingOptions)
if err != nil {
return APIResponse{}, err
}
v.Add("shipping_options", string(data))
} else {
v.Add("error_message", config.ErrorMessage)
}
bot.debugLog("answerShippingQuery", v, nil)
return bot.MakeRequest("answerShippingQuery", v)
}
// AnswerPreCheckoutQuery allows you to reply to Update with pre_checkout_query.
func (bot *BotAPI) AnswerPreCheckoutQuery(config PreCheckoutConfig) (APIResponse, error) {
v := url.Values{}
v.Add("pre_checkout_query_id", config.PreCheckoutQueryID)
v.Add("ok", strconv.FormatBool(config.OK))
if config.OK != true {
v.Add("error", config.ErrorMessage)
}
bot.debugLog("answerPreCheckoutQuery", v, nil)
return bot.MakeRequest("answerPreCheckoutQuery", v)
}

View File

@ -899,3 +899,90 @@ type ChatConfigWithUser struct {
SuperGroupUsername string SuperGroupUsername string
UserID int UserID int
} }
// InvoiceConfig contains information for sendInvoice request.
type InvoiceConfig struct {
BaseChat
Title string // required
Description string // required
Payload string // required
ProviderToken string // required
StartParameter string // required
Currency string // required
Prices *[]LabeledPrice // required
PhotoURL string
PhotoSize int
PhotoWidth int
PhotoHeight int
NeedName bool
NeedPhoneNumber bool
NeedEmail bool
NeedShippingAddress bool
IsFlexible bool
}
func (config InvoiceConfig) values() (url.Values, error) {
v, err := config.BaseChat.values()
if err != nil {
return v, err
}
v.Add("title", config.Title)
v.Add("description", config.Description)
v.Add("payload", config.Payload)
v.Add("provider_token", config.ProviderToken)
v.Add("start_parameter", config.StartParameter)
v.Add("currency", config.Currency)
data, err := json.Marshal(config.Prices)
if err != nil {
return v, err
}
v.Add("prices", string(data))
if config.PhotoURL != "" {
v.Add("photo_url", config.PhotoURL)
}
if config.PhotoSize != 0 {
v.Add("photo_size", strconv.Itoa(config.PhotoSize))
}
if config.PhotoWidth != 0 {
v.Add("photo_width", strconv.Itoa(config.PhotoWidth))
}
if config.PhotoHeight != 0 {
v.Add("photo_height", strconv.Itoa(config.PhotoHeight))
}
if config.NeedName != false {
v.Add("need_name", strconv.FormatBool(config.NeedName))
}
if config.NeedPhoneNumber != false {
v.Add("need_phone_number", strconv.FormatBool(config.NeedPhoneNumber))
}
if config.NeedEmail != false {
v.Add("need_email", strconv.FormatBool(config.NeedEmail))
}
if config.NeedShippingAddress != false {
v.Add("need_shipping_address", strconv.FormatBool(config.NeedShippingAddress))
}
if config.IsFlexible != false {
v.Add("is_flexible", strconv.FormatBool(config.IsFlexible))
}
return v, nil
}
func (config InvoiceConfig) method() string {
return "sendInvoice"
}
// ShippingConfig contains information for answerShippingQuery request.
type ShippingConfig struct {
ShippingQueryID string // required
OK bool // required
ShippingOptions *[]ShippingOption
ErrorMessage string
}
// PreCheckoutConfig conatins information for answerPreCheckoutQuery request.
type PreCheckoutConfig struct {
PreCheckoutQueryID string // required
OK bool // required
ErrorMessage string
}

Binary file not shown.

View File

@ -640,3 +640,16 @@ func NewCallbackWithAlert(id, text string) CallbackConfig {
ShowAlert: true, ShowAlert: true,
} }
} }
// NewInvoice created a new Invoice request to the user.
func NewInvoice(chatID int64, title, description, payload, providerToken, startParameter, currency string, prices *[]LabeledPrice) InvoiceConfig {
return InvoiceConfig{
BaseChat: BaseChat{ChatID: chatID},
Title: title,
Description: description,
Payload: payload,
ProviderToken: providerToken,
StartParameter: startParameter,
Currency: currency,
Prices: prices}
}

View File

@ -35,6 +35,8 @@ type Update struct {
InlineQuery *InlineQuery `json:"inline_query"` InlineQuery *InlineQuery `json:"inline_query"`
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result"` ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result"`
CallbackQuery *CallbackQuery `json:"callback_query"` CallbackQuery *CallbackQuery `json:"callback_query"`
ShippingQuery *ShippingQuery `json:"shipping_query"`
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"`
} }
// UpdatesChannel is the channel for getting updates. // UpdatesChannel is the channel for getting updates.
@ -153,6 +155,8 @@ type Message struct {
MigrateToChatID int64 `json:"migrate_to_chat_id"` // optional MigrateToChatID int64 `json:"migrate_to_chat_id"` // optional
MigrateFromChatID int64 `json:"migrate_from_chat_id"` // optional MigrateFromChatID int64 `json:"migrate_from_chat_id"` // optional
PinnedMessage *Message `json:"pinned_message"` // optional PinnedMessage *Message `json:"pinned_message"` // optional
Invoice *Invoice `json:"invoice"` // optional
SuccessfulPayment *SuccessfulPayment `json:"successful_payment"` // optional
} }
// Time converts the message timestamp into a Time. // Time converts the message timestamp into a Time.
@ -372,6 +376,7 @@ type InlineKeyboardButton struct {
SwitchInlineQuery *string `json:"switch_inline_query,omitempty"` // optional SwitchInlineQuery *string `json:"switch_inline_query,omitempty"` // optional
SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"` // optional SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"` // optional
CallbackGame *CallbackGame `json:"callback_game,omitempty"` // optional CallbackGame *CallbackGame `json:"callback_game,omitempty"` // optional
Pay bool `json:"pay"`
} }
// CallbackQuery is data sent when a keyboard button with callback data // CallbackQuery is data sent when a keyboard button with callback data
@ -646,3 +651,73 @@ type InputContactMessageContent struct {
FirstName string `json:"first_name"` FirstName string `json:"first_name"`
LastName string `json:"last_name"` LastName string `json:"last_name"`
} }
// Invoice contains basic information about an invoice.
type Invoice struct {
Title string `json:"title"`
Description string `json:"description"`
StartParameter string `json:"start_parameter"`
Currency string `json:"currency"`
TotalAmount int `json:"total_amount"`
}
// LabeledPrice represents a portion of the price for goods or services.
type LabeledPrice struct {
Label string `json:"label"`
Amount int `json:"amount"`
}
// ShippingAddress represents a shipping address.
type ShippingAddress struct {
CountryCode string `json:"country_code"`
State string `json:"state"`
City string `json:"city"`
StreetLine1 string `json:"street_line1"`
StreetLine2 string `json:"street_line2"`
PostCode string `json:"post_code"`
}
// OrderInfo represents information about an order.
type OrderInfo struct {
Name string `json:"name,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
Email string `json:"email,omitempty"`
ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
}
// ShippingOption represents one shipping option.
type ShippingOption struct {
ID string `json:"id"`
Title string `json:"title"`
Prices *[]LabeledPrice `json:"prices"`
}
// SuccessfulPayment contains basic information about a successful payment.
type SuccessfulPayment struct {
Currency string `json:"currency"`
TotalAmount int `json:"total_amount"`
InvoicePayload string `json:"invoice_payload"`
ShippingOptionID string `json:"shipping_option_id,omitempty"`
OrderInfo *OrderInfo `json:"order_info,omitempty"`
TelegramPaymentChargeID string `json:"telegram_payment_charge_id"`
ProviderPaymentChargeID string `json:"provider_payment_charge_id"`
}
// ShippingQuery contains information about an incoming shipping query.
type ShippingQuery struct {
ID string `json:"id"`
From *User `json:"from"`
InvoicePayload string `json:"invoice_payload"`
ShippingAddress *ShippingAddress `json:"shipping_address"`
}
// PreCheckoutQuery contains information about an incoming pre-checkout query.
type PreCheckoutQuery struct {
ID string `json:"id"`
From *User `json:"from"`
Currency string `json:"currency"`
TotalAmount int `json:"total_amount"`
InvoicePayload string `json:"invoice_payload"`
ShippingOptionID string `json:"shipping_option_id,omitempty"`
OrderInfo *OrderInfo `json:"order_info,omitempty"`
}