Skip to content

Commit

Permalink
payments: SuccessfulPayment and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed May 21, 2020
1 parent 1a55898 commit 7a1d59c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
10 changes: 10 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ func (b *Bot) ProcessUpdate(upd Update) {
return
}

if m.Invoice != nil {
b.handle(OnInvoice, m)
return
}

if m.Payment != nil {
b.handle(OnPayment, m)
return
}

wasAdded := (m.UserJoined != nil && m.UserJoined.ID == b.Me.ID) ||
(m.UsersJoined != nil && isUserInList(b.Me, m.UsersJoined))
if m.GroupCreated || m.SuperGroupCreated || wasAdded {
Expand Down
6 changes: 6 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ type Message struct {
// if it is itself a reply.
PinnedMessage *Message `json:"pinned_message"`

// Message is an invoice for a payment.
Invoice *Invoice `json:"invoice"`

// Message is a service message about a successful payment.
Payment *Payment `json:"successful_payment"`

// The domain name of the website on which the user has logged in.
ConnectedWebsite string `json:"connected_website,omitempty"`

Expand Down
31 changes: 21 additions & 10 deletions payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ type ShippingOption struct {
Prices []Price `json:"prices"`
}

// Payment contains basic information about a successful payment.
type Payment struct {
Currency Currency `json:"currency"`
Total int `json:"total_amount"`
Payload string `json:"invoice_payload"`
OptionID string `json:"shipping_option_id"`
Order Order `json:"order_info"`
TelegramChargeID string `json:"telegram_payment_charge_id"`
ProviderChargeID string `json:"provider_payment_charge_id"`
}

// PreCheckoutQuery contains information about an incoming pre-checkout query.
type PreCheckoutQuery struct {
Sender *User `json:"from"`
Expand All @@ -51,22 +62,22 @@ type Order struct {

// Invoice contains basic information about an invoice.
type Invoice struct {
Title string `json:"title"`
Description string `json:"description"`
Payload string `json:"payload"`
Token string `json:"provider_token"`
Currency string `json:"currency"`
Prices []Price `json:"prices"`
ProviderData string `json:"provider_data"`
Title string `json:"title"`
Description string `json:"description"`
Payload string `json:"payload"`
Currency string `json:"currency"`
Prices []Price `json:"prices"`
Token string `json:"provider_token"`
Data string `json:"provider_data"`

Photo *Photo `json:"photo"`
PhotoSize int `json:"photo_size"`

// Start is a unique deep-linking parameter that can be used to
// Unique deep-linking parameter that can be used to
// generate this invoice when used as a start parameter.
Start string `json:"start_parameter"`

// Total shows the total price in the smallest units of the currency.
// Shows the total price in the smallest units of the currency.
// For example, for a price of US$ 1.45 pass amount = 145.
Total int `json:"total_amount"`

Expand All @@ -76,7 +87,7 @@ type Invoice struct {
NeedShippingAddress bool `json:"need_shipping_address"`
SendPhoneNumber bool `json:"send_phone_number_to_provider"`
SendEmail bool `json:"send_email_to_provider"`
IsFlexible bool `json:"is_flexible"`
Flexible bool `json:"is_flexible"`
}

// Price represents a portion of the price for goods or services.
Expand Down
9 changes: 5 additions & 4 deletions sendable.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {

// Send delivers invoice through bot b to recipient.
func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
prices, _ := json.Marshal(i.Prices)

params := map[string]string{
"chat_id": to.Recipient(),
"title": i.Title,
Expand All @@ -305,14 +303,13 @@ func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error)
"payload": i.Payload,
"provider_token": i.Token,
"currency": i.Currency,
"prices": string(prices),
"need_name": strconv.FormatBool(i.NeedName),
"need_phone_number": strconv.FormatBool(i.NeedPhoneNumber),
"need_email": strconv.FormatBool(i.NeedEmail),
"need_shipping_address": strconv.FormatBool(i.NeedShippingAddress),
"send_phone_number_to_provider": strconv.FormatBool(i.SendPhoneNumber),
"send_email_to_provider": strconv.FormatBool(i.SendEmail),
"is_flexible": strconv.FormatBool(i.IsFlexible),
"is_flexible": strconv.FormatBool(i.Flexible),
}
if i.Photo != nil {
if i.Photo.FileURL != "" {
Expand All @@ -328,6 +325,10 @@ func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error)
params["photo_height"] = strconv.Itoa(i.Photo.Height)
}
}
if len(i.Prices) > 0 {
data, _ := json.Marshal(i.Prices)
params["prices"] = string(data)
}
embedSendOptions(params, opt)

data, err := b.Raw("sendInvoice", params)
Expand Down
2 changes: 2 additions & 0 deletions telebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ const (
OnChannelPost = "\achan_post"
OnEditedChannelPost = "\achan_edited_post"
OnDice = "\adice"
OnInvoice = "\ainvoice"
OnPayment = "\apayment"

// Will fire when bot is added to a group.
OnAddedToGroup = "\aadded_to_group"
Expand Down

0 comments on commit 7a1d59c

Please sign in to comment.