Payment API minimal implementation.

pull/192/head
Ian Byrd 5 years ago
parent 49593279d6
commit 7eeea8f32a
No known key found for this signature in database
GPG Key ID: 598F598CA3B8055F

@ -99,6 +99,8 @@ type Update struct {
Query *Query `json:"inline_query,omitempty"`
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,omitempty"`
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"`
}
// ChosenInlineResult represents a result of an inline query that was chosen
@ -113,6 +115,14 @@ type ChosenInlineResult struct {
Location *Location `json:"location,omitempty"`
}
type PreCheckoutQuery struct {
ID string `json:"id"`
Sender *User `json:"from"`
Currency string `json:"currency"`
Total int `json:"total_amount"`
Payload string `json:"invoice_payload"`
}
// Handle lets you set the handler for some command name or
// one of the supported endpoints.
//
@ -379,6 +389,24 @@ func (b *Bot) incomingUpdate(upd *Update) {
}
return
}
if upd.PreCheckoutQuery != nil {
if handler, ok := b.handlers[OnCheckout]; ok {
if handler, ok := handler.(func(*PreCheckoutQuery)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*PreCheckoutQuery),
r *PreCheckoutQuery) {
defer b.deferDebug()
handler(r)
}(b, handler, upd.PreCheckoutQuery)
} else {
panic("telebot: checkout handler is bad")
}
}
return
}
}
func (b *Bot) handle(end string, m *Message) bool {
@ -747,6 +775,27 @@ func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
return extractOkResponse(respJSON)
}
// Accept finalizes the deal.
func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
params := map[string]string{
"pre_checkout_query_id": query.ID,
}
if len(errorMessage) == 0 {
params["ok"] = "True"
} else {
params["ok"] = "False"
params["error_message"] = errorMessage[0]
}
respJSON, err := b.Raw("answerPreCheckoutQuery", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// Answer sends a response for a given inline query. A query can only
// be responded to once, subsequent attempts to respond to the same query
// will result in an error.

@ -1,21 +1,59 @@
package telebot
import "encoding/json"
import (
"encoding/json"
"math"
)
type Invoice struct {
// Product name, 1-32 characters.
Title string `json:"title"`
// Product description, 1-255 characters.
Description string `json:"description"`
// Custom payload, required, 1-128 bytes.
Payload string `json:"payload"`
// Unique deep-linking parameter that can be used to
// generate this invoice when used as a start parameter.
Start string `json:"start_parameter"`
// Provider token to use.
Token string `json:"provider_token"`
Currency string `json:"currency"`
Prices []Price `json:"prices"`
}
type Price struct {
Label string `json:"label"`
Amount int `json:"amount"`
}
type Currency struct {
Code string `json:"code"`
Title string `json:"title"`
Symbol string `json:"symbol"`
Native string `json:"native"`
ThousandsSep string `json:"thousands_sep"`
DecimalSep string `json:"decimal_sep"`
SymbolLeft bool `json:"symbol_left"`
SpaceBetween bool `json:"space_between"`
Exp int `json:"exp"`
Code string `json:"code"`
Title string `json:"title"`
Symbol string `json:"symbol"`
Native string `json:"native"`
ThousandsSep string `json:"thousands_sep"`
DecimalSep string `json:"decimal_sep"`
SymbolLeft bool `json:"symbol_left"`
SpaceBetween bool `json:"space_between"`
Exp int `json:"exp"`
MinAmount interface{} `json:"min_amount"`
MaxAmount interface{} `json:"max_amount"`
}
func (c Currency) FromTotal(total int) float64 {
return float64(total) / math.Pow(10, float64(c.Exp))
}
func (c Currency) ToTotal(total float64) int {
return int(total) * int(math.Pow(10, float64(c.Exp)))
}
var SupportedCurrencies = map[string]Currency{}
func init() {

@ -1,6 +1,7 @@
package telebot
import (
"encoding/json"
"fmt"
"strconv"
)
@ -239,3 +240,27 @@ func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
return extractMsgResponse(respJSON)
}
// Send delivers media 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,
"description": i.Description,
"start_parameter": i.Start,
"payload": i.Payload,
"provider_token": i.Token,
"currency": i.Currency,
"prices": string(prices),
}
embedSendOptions(params, opt)
respJSON, err := b.Raw("sendInvoice", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}

@ -83,6 +83,11 @@ const (
//
// Handler: func(*ChosenInlineResult)
OnChosenInlineResult = "\achosen_inline_result"
// Will fire on PreCheckoutQuery.
//
// Handler: func(*PreCheckoutQuery)
OnCheckout = "\apre_checkout_query"
)
// ChatAction is a client-side status indicating bot activity.

Loading…
Cancel
Save